1 15 package org.apache.tapestry.form.validator; 16 17 import java.util.ArrayList ; 18 import java.util.Collections ; 19 import java.util.HashMap ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.hivemind.HiveMind; 25 import org.apache.hivemind.util.PropertyUtils; 26 import org.apache.tapestry.util.RegexpMatch; 27 import org.apache.tapestry.util.RegexpMatcher; 28 29 36 public class ValidatorFactoryImpl implements ValidatorFactory 37 { 38 private static final String PATTERN = "^\\s*(\\w+)\\s*(=\\s*(((?!,|\\[).)*))?"; 39 40 43 44 private Map _masterCache = new HashMap (); 45 46 private RegexpMatcher _matcher = new RegexpMatcher(); 47 48 51 52 private Map _validators; 53 54 public synchronized List constructValidatorList(String specification) 55 { 56 List result = (List ) _masterCache.get(specification); 57 58 if (result == null) 59 { 60 result = buildValidatorList(specification); 61 _masterCache.put(specification, result); 62 } 63 64 return result; 65 } 66 67 private List buildValidatorList(String specification) 68 { 69 if (HiveMind.isBlank(specification)) 70 return Collections.EMPTY_LIST; 71 72 List result = new ArrayList (); 73 String chopped = specification; 74 75 while (true) 76 { 77 if (chopped.length() == 0) 78 break; 79 80 if (!result.isEmpty()) 81 { 82 if (chopped.charAt(0) != ',') 83 throw new ApplicationRuntimeException(ValidatorMessages 84 .badSpecification(specification)); 85 86 chopped = chopped.substring(1); 87 } 88 89 RegexpMatch[] matches = _matcher.getMatches(PATTERN, chopped); 90 91 if (matches.length != 1) 92 throw new ApplicationRuntimeException(ValidatorMessages 93 .badSpecification(specification)); 94 95 RegexpMatch match = matches[0]; 96 97 String name = match.getGroup(1); 98 String value = match.getGroup(3); 99 String message = null; 100 101 int length = match.getMatchLength(); 102 103 if (chopped.length() > length) 104 { 105 char lastChar = chopped.charAt(length); 106 if (lastChar == ',') 107 length--; 108 else if (lastChar == '[') 109 { 110 int messageClose = chopped.indexOf(']', length); 111 message = chopped.substring(length + 1, messageClose); 112 length = messageClose; 113 } 114 } 115 116 Validator validator = buildValidator(name, value, message); 117 118 result.add(validator); 119 120 if (length >= chopped.length()) 121 break; 122 else 123 chopped = chopped.substring(length + 1); 124 125 } 126 127 return Collections.unmodifiableList(result); 128 } 129 130 private Validator buildValidator(String name, String value, String message) 131 { 132 ValidatorContribution vc = (ValidatorContribution) _validators.get(name); 133 134 if (vc == null) 135 throw new ApplicationRuntimeException(ValidatorMessages.unknownValidator(name)); 136 137 if (value == null && vc.isConfigurable()) 138 throw new ApplicationRuntimeException(ValidatorMessages.needsConfiguration("name")); 139 140 if (value != null && !vc.isConfigurable()) 141 throw new ApplicationRuntimeException(ValidatorMessages.notConfigurable(name, value)); 142 143 try 144 { 145 Object result = vc.getValidatorClass().newInstance(); 146 147 if (vc.isConfigurable()) 148 PropertyUtils.smartWrite(result, name, value); 149 150 if (message != null) 151 PropertyUtils.write(result, "message", message); 152 153 return (Validator) result; 154 } 155 catch (Exception ex) 156 { 157 throw new ApplicationRuntimeException(ValidatorMessages.errorInitializingValidator( 158 name, 159 vc.getValidatorClass(), 160 ex), ex); 161 } 162 } 163 164 public void setValidators(Map validators) 165 { 166 _validators = validators; 167 } 168 } 169 | Popular Tags |