1 5 package xdoclet.modules.spring; 6 7 import java.beans.Introspector ; 8 import java.util.ArrayList ; 9 import java.util.Collection ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.Map ; 13 import java.util.Properties ; 14 15 import org.apache.commons.collections.SequencedHashMap; 16 import xjavadoc.XClass; 17 import xjavadoc.XMethod; 18 import xjavadoc.XParameter; 19 import xjavadoc.XTag; 20 21 import xdoclet.DocletSupport; 22 import xdoclet.XDocletException; 23 import xdoclet.tagshandler.AbstractProgramElementTagsHandler; 24 import xdoclet.tagshandler.MethodTagsHandler; 25 26 34 public class SpringValidatorTagsHandler extends AbstractProgramElementTagsHandler 35 { 36 private final static List supportedTypes = new ArrayList (); 37 private String curFieldName; 38 private String currentArgKey; 39 private Map args; 40 41 static { 42 supportedTypes.add("java.lang.String"); 43 supportedTypes.add("java.lang.Integer"); 44 supportedTypes.add("int"); 45 supportedTypes.add("java.lang.Float"); 46 supportedTypes.add("float"); 47 supportedTypes.add("java.lang.Long"); 48 supportedTypes.add("long"); 49 supportedTypes.add("java.lang.Double"); 50 supportedTypes.add("double"); 51 supportedTypes.add("java.lang.Boolean"); 52 supportedTypes.add("boolean"); 53 supportedTypes.add("java.util.Date"); 54 } 55 56 64 public void forAllForms(String template, Properties attributes) throws XDocletException 65 { 66 Collection classes = getAllClasses(); 67 68 for (Iterator i = classes.iterator(); i.hasNext(); ) { 69 XClass currentClass = (XClass) i.next(); 70 71 setCurrentClass(currentClass); 72 if (DocletSupport.isDocletGenerated(getCurrentClass()) || (getCurrentClass().isAbstract())) { 73 continue; 74 } 75 generate(template); 76 } 77 } 78 79 88 public String formName(Properties attributes) throws XDocletException 89 { 90 return Introspector.decapitalize(getCurrentClass().getTransformedName()); 91 } 92 93 101 public void forAllFieldArgs(String template, Properties attributes) throws XDocletException 102 { 103 for (Iterator iterator = args.keySet().iterator(); iterator.hasNext(); ) { 104 currentArgKey = (String ) iterator.next(); 105 generate(template); 106 } 107 } 108 109 116 public String argIndex(Properties props) 117 { 118 return currentArgKey.charAt(3) + ""; 119 } 120 121 128 public String argName(Properties props) 129 { 130 String name = currentArgKey.substring(currentArgKey.indexOf('_') + 1); 131 132 return name; 133 } 134 135 142 public String argValue(Properties props) 143 { 144 return (String ) args.get(currentArgKey); 145 } 146 147 155 public void ifArgIsResource(String template, Properties attributes) throws XDocletException 156 { 157 if (currentArgKey.indexOf("resource") > 0) { 158 generate(template); 159 } 160 } 161 162 170 public void ifArgIsValue(String template, Properties attributes) throws XDocletException 171 { 172 if (currentArgKey.indexOf("value") > 0) { 173 generate(template); 174 } 175 } 176 177 185 public void ifArgIsForType(String template, Properties attributes) throws XDocletException 186 { 187 if (currentArgKey.indexOf('_') > 0) { 188 generate(template); 189 } 190 } 191 192 200 public void ifNoArg0(String template, Properties attributes) throws XDocletException 201 { 202 if (args.get("arg0resource") == null && args.get("arg0value") == null) { 203 generate(template); 204 } 205 } 206 207 215 public void ifFormHasFields(String template, Properties attributes) throws XDocletException 216 { 217 if (getFields(getCurrentClass()).size() > 0) { 218 generate(template); 219 } 220 } 221 222 230 public void forAllFields(String template, Properties attributes) throws XDocletException 231 { 232 XClass clazz = getCurrentClass(); 233 Map setters = getFields(clazz); 234 235 for (Iterator iterator = setters.keySet().iterator(); iterator.hasNext(); ) { 236 curFieldName = (String ) iterator.next(); 237 238 XMethod field = (XMethod) setters.get(curFieldName); 239 240 setCurrentMethod(field); 241 loadFieldArguments(); 242 generate(template); 243 } 244 } 245 246 253 public String fieldName(Properties props) 254 { 255 return curFieldName; 256 } 257 258 265 public String validatorList(Properties props) 266 { 267 XMethod method = getCurrentMethod(); 268 Collection tags = method.getDoc().getTags("spring.validator"); 269 StringBuffer buffer = new StringBuffer (); 270 271 for (Iterator iterator = tags.iterator(); iterator.hasNext(); ) { 272 XTag tag = (XTag) iterator.next(); 273 274 buffer.append(tag.getAttributeValue("type")); 275 if (iterator.hasNext()) { 276 buffer.append(","); 277 } 278 } 279 return buffer.toString(); 280 } 281 282 private Map getFields(XClass clazz) throws XDocletException 283 { 284 return getFields(clazz, ""); 285 } 286 287 private Map getFields(XClass clazz, String prefix) throws XDocletException 288 { 289 Map fields = new SequencedHashMap(); 290 291 Collection curFields = clazz.getMethods(true); 292 293 for (Iterator iterator = curFields.iterator(); iterator.hasNext(); ) { 298 XMethod setter = (XMethod) iterator.next(); 299 300 if (MethodTagsHandler.isSetterMethod(setter)) { 301 if (setter.getDoc().getTag("spring.validator") != null) { 302 String name = MethodTagsHandler.getPropertyNameFor(setter); 303 XParameter param = (XParameter) setter.getParameters().iterator().next(); 304 String type = param.getType().getQualifiedName(); 305 306 if (supportedTypes.contains(type)) { 307 fields.put(prefix + name, setter); 308 } 309 else { 310 fields.putAll(getFields(param.getType(), prefix + (prefix.length() == 0 ? "" : ".") + name + ".")); 311 } 312 } 313 } 314 } 315 316 return fields; 317 } 318 319 private void loadFieldArguments() 320 { 321 334 args = new SequencedHashMap(); 335 336 XMethod method = getCurrentMethod(); 337 338 Collection argTags = method.getDoc().getTags("spring.validator-args"); 340 341 for (Iterator argsIterator = argTags.iterator(); argsIterator.hasNext(); ) { 342 XTag tag = (XTag) argsIterator.next(); 343 Collection attributeNames = tag.getAttributeNames(); 344 345 for (Iterator attributesIterator = attributeNames.iterator(); attributesIterator.hasNext(); ) { 346 String name = (String ) attributesIterator.next(); 347 348 if (name.startsWith("arg")) { 349 args.put(name, tag.getAttributeValue(name)); 350 } 351 } 352 } 353 354 Collection argTypeTags = method.getDoc().getTags("spring.validator"); 356 357 for (Iterator typeTagsIterator = argTypeTags.iterator(); typeTagsIterator.hasNext(); ) { 358 XTag tag = (XTag) typeTagsIterator.next(); 359 Collection attributeNames = tag.getAttributeNames(); 360 String type = tag.getAttributeValue("type"); 361 362 for (Iterator attributesIterator = attributeNames.iterator(); attributesIterator.hasNext(); ) { 363 String name = (String ) attributesIterator.next(); 364 365 if (name.startsWith("arg")) { 366 args.put(name + "_" + type, tag.getAttributeValue(name)); 367 } 368 } 369 } 370 } 371 } 372 | Popular Tags |