1 7 package com.inversoft.verge.mvc.config; 8 9 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 14 import org.jdom.Attribute; 15 import org.jdom.Element; 16 17 import com.inversoft.beans.BeanException; 18 import com.inversoft.beans.JavaBeanTools; 19 import com.inversoft.config.ConfigurationException; 20 import com.inversoft.error.ErrorList; 21 import com.inversoft.util.ReflectionException; 22 import com.inversoft.verge.mvc.MVCException; 23 import com.inversoft.verge.mvc.validator.Validator; 24 import com.inversoft.verge.mvc.validator.ValidatorTools; 25 import com.inversoft.verge.util.WebBean; 26 import com.inversoft.verge.util.WebBeanProperty; 27 28 29 39 public class BaseValidatorConfigBuilder extends BaseConfigBuilder { 40 41 42 48 public String getFailureElementName() { 49 return Constants.FAILURE_ATTRIBUTE; 50 } 51 52 59 public BaseConfig build(Element element) throws ConfigurationException { 60 61 ErrorList errors = new ErrorList(); 62 String validator = element.getAttributeValue(Constants.CLASS_ATTRIBUTE); 63 Class validatorClass = null; 64 if (validator != null) { 65 try { 66 validatorClass = ValidatorTools.findValidator(validator); 67 } catch (MVCException mvce) { 68 errors.addError("The validator class: " + validator + 69 " is invalid"); 70 } 71 } 72 73 String failureDef = element.getAttributeValue(getFailureElementName()); 74 75 BaseValidatorConfig validatorConfig = new BaseValidatorConfig(validatorClass, 77 failureDef); 78 79 buildProperties(element, validatorConfig, errors); 80 81 if (!errors.isEmpty()) { 82 throw new ConfigurationException(errors); 83 } 84 85 return validatorConfig; 86 } 87 88 91 protected void buildProperties(Element element, BaseValidatorConfig validatorConfig, 92 ErrorList errors) { 93 Iterator iter = element.getChildren(Constants.PROPERTY_ELEMENT).iterator(); 94 Element elem; 95 while (iter.hasNext()) { 96 elem = (Element) iter.next(); 97 98 Iterator attrIter = elem.getAttributes().iterator(); 99 Attribute attr; 100 String definition = null; 101 String id = null; 102 String property = null; 103 String type = null; 104 String key = null; 105 String bundleName = null; 106 String errorMsg = null; 107 String name; 108 JavaBeanTools.NameInfo ni; 109 Map parameters = new HashMap (); 110 boolean valid = true; 111 while (attrIter.hasNext()) { 112 113 attr = (Attribute) attrIter.next(); 114 name = attr.getName(); 115 if (name.equals(Constants.NAME_ATTRIBUTE)) { 116 definition = attr.getValue(); 117 ni = JavaBeanTools.splitNameFront(definition); 118 if (!ni.nested) { 119 errors.addError("Property name must include the form " + 120 "bean name and the property name (i.e. id.property)"); 121 } else { 122 id = ni.localPropertyName; 123 property = ni.nestedPropertyName; 124 } 125 } else if (name.equals(Constants.TYPE_ATTRIBUTE)) { 126 type = attr.getValue(); 127 } else if (name.equals(Constants.KEY_ATTRIBUTE)) { 128 key = attr.getValue(); 129 } else if (name.equals(Constants.BUNDLE_NAME_ATTRIBUTE)) { 130 bundleName = attr.getValue(); 131 } else if (name.equals(Constants.ERROR_MSG_ATTRIBUTE)) { 132 errorMsg = attr.getValue(); 133 } else { 134 parameters.put(name, attr.getValue()); 135 } 136 } 137 138 if (property == null) { 139 errors.addError("The property validation must specify a property"); 140 valid = false; 141 } 142 if (type == null) { 143 errors.addError("The property validation for the property named: " + 144 id + "." + property + " must specify a validator type"); 145 valid = false; 146 } 147 148 if (valid) { 149 validatorConfig.addProperty(new PropertyConfig(id, property, type, 150 key, bundleName, errorMsg, parameters)); 151 } 152 } 153 } 154 155 158 public void validate(BaseValidatorConfig validator, BaseFormConfig config, 159 FormConfigRegistry registry) 160 throws ConfigurationException { 161 ErrorList errors = new ErrorList(); 163 Iterator iter = validator.getProperties().iterator(); 164 PropertyConfig propConfig; 165 while (iter.hasNext()) { 166 propConfig = (PropertyConfig) iter.next(); 167 if (config.getFormBean(propConfig.getID()) == null) { 168 errors.addError("The form named: " + config.getName() + 169 " does not have a form bean named: " + propConfig.getID()); 170 continue; 171 } 172 173 if (propConfig.isPrimitiveOrWrapper()) { 176 validatePrimitive(validator, config, propConfig, errors); 177 } 178 } 179 } 180 181 185 private void validatePrimitive(BaseValidatorConfig validator, 186 BaseFormConfig config, PropertyConfig propConfig, ErrorList errors) { 187 188 WebBean wb = config.getFormBean(propConfig.getID()); 191 WebBeanProperty wbp = null; 192 try { 193 wbp = wb.getWebBeanProperty(propConfig.getProperty()); 194 } catch (BeanException be) { 195 errors.addError("Invalid bean property: " + be.toString()); 196 return; 197 } 198 199 Class propType = wbp.getPropertyType(); 200 boolean needsConv = false; 201 double min = 0; 202 double max = 0; 203 if (propConfig.getType().equals("char")) { 204 needsConv = (propType == Character.TYPE || propType == Character .class); 205 } else if (propConfig.getType().equals("byte")) { 206 needsConv = (propType == Byte.TYPE || propType == Byte .class); 207 min = Byte.MIN_VALUE; 208 max = Byte.MAX_VALUE; 209 } else if (propConfig.getType().equals("short")) { 210 needsConv = (propType == Short.TYPE || propType == Short .class); 211 min = Short.MIN_VALUE; 212 max = Short.MAX_VALUE; 213 } else if (propConfig.getType().equals("int")) { 214 needsConv = (propType == Integer.TYPE || propType == Integer .class); 215 min = Integer.MIN_VALUE; 216 max = Integer.MAX_VALUE; 217 } else if (propConfig.getType().equals("long")) { 218 needsConv = (propType == Long.TYPE || propType == Long .class); 219 min = Long.MIN_VALUE; 220 max = Long.MAX_VALUE; 221 } else if (propConfig.getType().equals("float")) { 222 needsConv = (propType == Float.TYPE || propType == Float .class); 223 min = Float.MIN_VALUE; 224 max = Float.MAX_VALUE; 225 } else if (propConfig.getType().equals("double")) { 226 needsConv = (propType == Double.TYPE || propType == Double .class); 227 min = Double.MIN_VALUE; 228 max = Double.MAX_VALUE; 229 } 230 231 if (needsConv) { 234 validator.removeProperty(propConfig); 235 config.addValidatorConfig(createProxy(validator, propConfig)); 236 } else if (min != 0 && max != 0) { 237 Map map = propConfig.getLiveParameters(); 239 if (map != null) { 240 map = new HashMap (); 241 } 242 243 if (map.get("min") == null) { 244 map.put("min", new Double (min)); 245 } 246 247 if (map.get("max") == null) { 248 map.put("max", new Double (max)); 249 } 250 251 propConfig.setType("number"); 252 propConfig.setParameters(map); 253 } else { 254 Map map = new HashMap (); 256 map.put("min", new Double (0d)); 257 map.put("max", new Double (1d)); 258 propConfig.setType("string"); 259 propConfig.setParameters(map); 260 } 261 } 262 263 272 protected BaseValidatorConfig createProxy(BaseValidatorConfig config, 273 final PropertyConfig propConfig) { 274 275 return new BaseValidatorConfig(null, config.getFailureDefinition()) { 276 public boolean isValidating() { 278 return true; 279 } 280 281 public Validator newValidator() throws ReflectionException { 283 return new OverrideValidator(propConfig); 284 } 285 }; 286 } 287 } | Popular Tags |