1 17 package org.alfresco.repo.action.evaluator; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 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 37 public class MatchTextEvaluator extends ActionConditionEvaluatorAbstractBase 38 { 39 42 public final static String NAME = "match-text"; 43 public final static String PARAM_TEXT = "text"; 44 public final static String PARAM_OPERATION = "operation"; 45 46 49 public enum Operation {CONTAINS, BEGINS, ENDS, EXACT}; 50 51 54 private NodeService nodeService; 55 56 59 private DictionaryService dictionaryService; 60 61 64 private static final String STAR = "*"; 65 66 71 public void setNodeService(NodeService nodeService) 72 { 73 this.nodeService = nodeService; 74 } 75 76 81 public void setDictionaryService(DictionaryService dictionaryService) 82 { 83 this.dictionaryService = dictionaryService; 84 } 85 86 87 90 @Override 91 protected void addParameterDefintions(List <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 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 QName nodeType = nodeService.getType(actionedUponNodeRef); 110 if (dictionaryService.isSubClass(nodeType, ContentModel.TYPE_CONTENT)) 111 { 112 String matchText = (String )ruleCondition.getParameterValue(PARAM_TEXT); 114 115 Operation operation = null; 117 String stringOperation = (String )ruleCondition.getParameterValue(PARAM_OPERATION); 118 if (stringOperation != null) 119 { 120 operation = Operation.valueOf(stringOperation); 121 } 122 else 123 { 124 if (matchText.startsWith(STAR) == true) 126 { 127 operation = Operation.ENDS; 129 matchText = matchText.substring(1); 130 } 131 else if (matchText.endsWith(STAR) == true) 132 { 133 operation = Operation.BEGINS; 135 matchText = matchText.substring(0, (matchText.length()-2)); 136 } 137 else 138 { 139 operation = Operation.CONTAINS; 140 } 141 } 142 143 String regEx = buildRegEx(matchText, operation); 145 146 String name = (String )this.nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_NAME); 148 149 if (name != null) 151 { 152 result = name.matches(regEx); 153 } 154 } 155 } 156 157 return result; 158 } 159 160 167 private String buildRegEx(String matchText, Operation operation) 168 { 169 171 String 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 195 private String escapeText(String matchText) 196 { 197 StringBuilder builder = new StringBuilder (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 213 private static List <Character > ESCAPE_CHAR_LIST = null; 214 215 220 private List <Character > getEscapeCharList() 221 { 222 if (ESCAPE_CHAR_LIST == null) 223 { 224 ESCAPE_CHAR_LIST = new ArrayList <Character >(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 |