1 17 package org.alfresco.repo.action.evaluator.compare; 18 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.List ; 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 32 public class TextPropertyValueComparator implements PropertyValueComparator 33 { 34 37 private static final String MSGID_INVALID_OPERATION = "text_property_value_comparator.invalid_operation"; 38 39 42 private static final String STAR = "*"; 43 44 47 public boolean compare( 48 Serializable propertyValue, 49 Serializable compareValue, 50 ComparePropertyValueOperation operation) 51 { 52 String compareText = (String )compareValue; 53 54 boolean result = false; 55 if (operation == null) 56 { 57 if (compareText.startsWith(STAR) == true) 59 { 60 operation = ComparePropertyValueOperation.ENDS; 62 compareText = compareText.substring(1); 63 } 64 else if (compareText.endsWith(STAR) == true) 65 { 66 operation = ComparePropertyValueOperation.BEGINS; 68 compareText = compareText.substring(0, (compareText.length()-2)); 69 } 70 else 71 { 72 operation = ComparePropertyValueOperation.CONTAINS; 73 } 74 } 75 76 String regEx = buildRegEx(compareText, operation); 78 79 if (propertyValue != null) 81 { 82 result = ((String )propertyValue).toLowerCase().matches(regEx); 83 } 84 85 return result; 86 } 87 88 95 private String buildRegEx(String matchText, ComparePropertyValueOperation operation) 96 { 97 String 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 throw new ActionServiceException( 114 MSGID_INVALID_OPERATION, 115 new Object []{operation.toString()}); 116 } 117 return result; 118 } 119 120 126 private String escapeText(String matchText) 127 { 128 StringBuilder builder = new StringBuilder (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 148 private static List <Character > ESCAPE_CHAR_LIST = null; 149 150 155 private List <Character > getEscapeCharList() 156 { 157 if (ESCAPE_CHAR_LIST == null) 158 { 159 ESCAPE_CHAR_LIST = new ArrayList <Character >(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 179 public void registerComparator(ComparePropertyValueEvaluator evaluator) 180 { 181 evaluator.registerComparator(DataTypeDefinition.TEXT, this); 182 } 183 } 184 | Popular Tags |