KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > action > evaluator > compare > TextPropertyValueComparator


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.compare;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.alfresco.repo.action.evaluator.ComparePropertyValueEvaluator;
24 import org.alfresco.service.cmr.action.ActionServiceException;
25 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
26
27 /**
28  * Test property value comparator
29  *
30  * @author Roy Wetherall
31  */

32 public class TextPropertyValueComparator implements PropertyValueComparator
33 {
34     /**
35      * I18N message ids
36      */

37     private static final String JavaDoc MSGID_INVALID_OPERATION = "text_property_value_comparator.invalid_operation";
38     
39     /**
40      * Special star string
41      */

42     private static final String JavaDoc STAR = "*";
43     
44     /**
45      * @see org.alfresco.repo.action.evaluator.compare.PropertyValueComparator#compare(java.io.Serializable, java.io.Serializable, org.alfresco.repo.action.evaluator.compare.ComparePropertyValueOperation)
46      */

47     public boolean compare(
48             Serializable JavaDoc propertyValue,
49             Serializable JavaDoc compareValue,
50             ComparePropertyValueOperation operation)
51     {
52         String JavaDoc compareText = (String JavaDoc)compareValue;
53         
54         boolean result = false;
55         if (operation == null)
56         {
57             // Check for a trailing or leading star since it implies special behaviour when no default operation is specified
58
if (compareText.startsWith(STAR) == true)
59             {
60                 // Remove the star and set the operation to endsWith
61
operation = ComparePropertyValueOperation.ENDS;
62                 compareText = compareText.substring(1);
63             }
64             else if (compareText.endsWith(STAR) == true)
65             {
66                 // Remove the star and set the operation to startsWith
67
operation = ComparePropertyValueOperation.BEGINS;
68                 compareText = compareText.substring(0, (compareText.length()-2));
69             }
70             else
71             {
72                 operation = ComparePropertyValueOperation.CONTAINS;
73             }
74         }
75             
76         // Build the reg ex
77
String JavaDoc regEx = buildRegEx(compareText, operation);
78         
79         // Do the match
80
if (propertyValue != null)
81         {
82             result = ((String JavaDoc)propertyValue).toLowerCase().matches(regEx);
83         }
84         
85         return result;
86     }
87     
88     /**
89      * Builds the regular expressin that it used to make the match
90      *
91      * @param matchText the raw text to be matched
92      * @param operation the operation
93      * @return the regular expression string
94      */

95     private String JavaDoc buildRegEx(String JavaDoc matchText, ComparePropertyValueOperation operation)
96     {
97         String JavaDoc result = escapeText(matchText.toLowerCase());
98         switch (operation)
99         {
100             case CONTAINS:
101                 result = "^.*" + result + ".*$";
102                 break;
103             case BEGINS:
104                 result = "^" + result + ".*$";
105                 break;
106             case ENDS:
107                 result = "^.*" + result + "$";
108                 break;
109             case EQUALS:
110                 break;
111             default:
112                 // Raise an invalid operation exception
113
throw new ActionServiceException(
114                         MSGID_INVALID_OPERATION,
115                         new Object JavaDoc[]{operation.toString()});
116         }
117         return result;
118     }
119
120     /**
121      * Escapes the text before it is turned into a regualr expression
122      *
123      * @param matchText the raw text
124      * @return the escaped text
125      */

126     private String JavaDoc escapeText(String JavaDoc matchText)
127     {
128         StringBuilder JavaDoc builder = new StringBuilder JavaDoc(matchText.length());
129         for (char charValue : matchText.toCharArray())
130         {
131             if (charValue == '*')
132             {
133                 builder.append(".");
134             }
135             else if (getEscapeCharList().contains(charValue) == true)
136             {
137                 builder.append("\\");
138             }
139             builder.append(charValue);
140         }
141         
142         return builder.toString();
143     }
144
145     /**
146      * List of escape characters
147      */

148     private static List JavaDoc<Character JavaDoc> ESCAPE_CHAR_LIST = null;
149     
150     /**
151      * Get the list of escape chars
152      *
153      * @return list of excape chars
154      */

155     private List JavaDoc<Character JavaDoc> getEscapeCharList()
156     {
157         if (ESCAPE_CHAR_LIST == null)
158         {
159             //([{\^$|)?*+.
160
ESCAPE_CHAR_LIST = new ArrayList JavaDoc<Character JavaDoc>(4);
161             ESCAPE_CHAR_LIST.add('.');
162             ESCAPE_CHAR_LIST.add('^');
163             ESCAPE_CHAR_LIST.add('$');
164             ESCAPE_CHAR_LIST.add('(');
165             ESCAPE_CHAR_LIST.add('[');
166             ESCAPE_CHAR_LIST.add('{');
167             ESCAPE_CHAR_LIST.add('\\');
168             ESCAPE_CHAR_LIST.add('|');
169             ESCAPE_CHAR_LIST.add(')');
170             ESCAPE_CHAR_LIST.add('?');
171             ESCAPE_CHAR_LIST.add('+');
172         }
173         return ESCAPE_CHAR_LIST;
174     }
175
176     /**
177      * @see org.alfresco.repo.action.evaluator.compare.PropertyValueComparator#registerComparator(org.alfresco.repo.action.evaluator.ComparePropertyValueEvaluator)
178      */

179     public void registerComparator(ComparePropertyValueEvaluator evaluator)
180     {
181         evaluator.registerComparator(DataTypeDefinition.TEXT, this);
182     }
183 }
184
Popular Tags