KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > action > evaluator > MatchTextEvaluator


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.action.evaluator;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.alfresco.model.ContentModel;
23 import org.alfresco.repo.action.ParameterDefinitionImpl;
24 import org.alfresco.service.cmr.action.ActionCondition;
25 import org.alfresco.service.cmr.action.ParameterDefinition;
26 import org.alfresco.service.cmr.dictionary.DictionaryService;
27 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
28 import org.alfresco.service.cmr.repository.NodeRef;
29 import org.alfresco.service.cmr.repository.NodeService;
30 import org.alfresco.service.namespace.QName;
31
32 /**
33  * Contains text evaluator
34  *
35  * @author Roy Wetherall
36  */

37 public class MatchTextEvaluator extends ActionConditionEvaluatorAbstractBase
38 {
39     /**
40      * Evaluator constants
41      */

42     public final static String JavaDoc NAME = "match-text";
43     public final static String JavaDoc PARAM_TEXT = "text";
44     public final static String JavaDoc PARAM_OPERATION = "operation";
45     
46     /**
47      * Operations enum
48      */

49     public enum Operation {CONTAINS, BEGINS, ENDS, EXACT};
50     
51     /**
52      * The node service
53      */

54     private NodeService nodeService;
55     
56     /**
57      * The dictionary service
58      */

59     private DictionaryService dictionaryService;
60     
61     /**
62      * Special star string
63      */

64     private static final String JavaDoc STAR = "*";
65     
66     /**
67      * Set node service
68      *
69      * @param nodeService the node service
70      */

71     public void setNodeService(NodeService nodeService)
72     {
73         this.nodeService = nodeService;
74     }
75     
76     /**
77      * Set dictionary service
78      *
79      * @param dictionaryService the dictionary service
80      */

81     public void setDictionaryService(DictionaryService dictionaryService)
82     {
83         this.dictionaryService = dictionaryService;
84     }
85     
86     
87     /**
88      * Add paremeter defintions
89      */

90     @Override JavaDoc
91     protected void addParameterDefintions(List JavaDoc<ParameterDefinition> paramList)
92     {
93         paramList.add(new ParameterDefinitionImpl(PARAM_TEXT, DataTypeDefinition.TEXT, true, getParamDisplayLabel(PARAM_TEXT)));
94         paramList.add(new ParameterDefinitionImpl(PARAM_OPERATION, DataTypeDefinition.TEXT, false, getParamDisplayLabel(PARAM_OPERATION)));
95     }
96
97     /**
98      * @see ActionConditionEvaluatorAbstractBase#evaluateImpl(ActionCondition, NodeRef)
99      */

100     public boolean evaluateImpl(
101             ActionCondition ruleCondition,
102             NodeRef actionedUponNodeRef)
103     {
104         boolean result = false;
105         
106         if (this.nodeService.exists(actionedUponNodeRef) == true)
107         {
108             // TODO: Move this type check into its own Class Evaluator
109
QName nodeType = nodeService.getType(actionedUponNodeRef);
110             if (dictionaryService.isSubClass(nodeType, ContentModel.TYPE_CONTENT))
111             {
112                 // Get the text to match against
113
String JavaDoc matchText = (String JavaDoc)ruleCondition.getParameterValue(PARAM_TEXT);
114                 
115                 // Get the operation to be performed
116
Operation operation = null;
117                 String JavaDoc stringOperation = (String JavaDoc)ruleCondition.getParameterValue(PARAM_OPERATION);
118                 if (stringOperation != null)
119                 {
120                     operation = Operation.valueOf(stringOperation);
121                 }
122                 else
123                 {
124                     // Check for a trailing or leading star since it implies special behaviour when no default operation is specified
125
if (matchText.startsWith(STAR) == true)
126                     {
127                         // Remove the star and set the operation to endsWith
128
operation = Operation.ENDS;
129                         matchText = matchText.substring(1);
130                     }
131                     else if (matchText.endsWith(STAR) == true)
132                     {
133                         // Remove the star and set the operation to startsWith
134
operation = Operation.BEGINS;
135                         matchText = matchText.substring(0, (matchText.length()-2));
136                     }
137                     else
138                     {
139                         operation = Operation.CONTAINS;
140                     }
141                 }
142                 
143                 // Build the reg ex
144
String JavaDoc regEx = buildRegEx(matchText, operation);
145                 
146                 // Get the name value of the node
147
String JavaDoc name = (String JavaDoc)this.nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_NAME);
148                 
149                 // Do the match
150
if (name != null)
151                 {
152                     result = name.matches(regEx);
153                 }
154             }
155         }
156         
157         return result;
158     }
159
160     /**
161      * Builds the regular expressin that it used to make the match
162      *
163      * @param matchText the raw text to be matched
164      * @param operation the operation
165      * @return the regular expression string
166      */

167     private String JavaDoc buildRegEx(String JavaDoc matchText, Operation operation)
168     {
169         // TODO the result of this could be cached to speed things up ...
170

171         String JavaDoc result = escapeText(matchText);
172         switch (operation)
173         {
174             case CONTAINS:
175                 result = "^.*" + result + ".*$";
176                 break;
177             case BEGINS:
178                 result = "^" + result + ".*$";
179                 break;
180             case ENDS:
181                 result = "^.*" + result + "$";
182                 break;
183             default:
184                 break;
185         }
186         return result;
187     }
188
189     /**
190      * Escapes the text before it is turned into a regualr expression
191      *
192      * @param matchText the raw text
193      * @return the escaped text
194      */

195     private String JavaDoc escapeText(String JavaDoc matchText)
196     {
197         StringBuilder JavaDoc builder = new StringBuilder JavaDoc(matchText.length());
198         for (char charValue : matchText.toCharArray())
199         {
200             if (getEscapeCharList().contains(charValue) == true)
201             {
202                 builder.append("\\");
203             }
204             builder.append(charValue);
205         }
206         
207         return builder.toString();
208     }
209
210     /**
211      * List of escape characters
212      */

213     private static List JavaDoc<Character JavaDoc> ESCAPE_CHAR_LIST = null;
214     
215     /**
216      * Get the list of escape chars
217      *
218      * @return list of excape chars
219      */

220     private List JavaDoc<Character JavaDoc> getEscapeCharList()
221     {
222         if (ESCAPE_CHAR_LIST == null)
223         {
224             ESCAPE_CHAR_LIST = new ArrayList JavaDoc<Character JavaDoc>(4);
225             ESCAPE_CHAR_LIST.add('.');
226             ESCAPE_CHAR_LIST.add('^');
227             ESCAPE_CHAR_LIST.add('*');
228             ESCAPE_CHAR_LIST.add('$');
229         }
230         return ESCAPE_CHAR_LIST;
231     }
232 }
233
Popular Tags