1 5 package xdoclet.modules.apache.struts; 6 7 import java.util.ArrayList ; 8 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 17 import xjavadoc.XClass; 18 import xjavadoc.XMethod; 19 import xjavadoc.XParameter; 20 import xjavadoc.XTag; 21 22 import xdoclet.DocletSupport; 23 import xdoclet.XDocletException; 24 import xdoclet.tagshandler.AbstractProgramElementTagsHandler; 25 import xdoclet.tagshandler.MethodTagsHandler; 26 import xdoclet.tagshandler.TypeTagsHandler; 27 import xdoclet.util.Translator; 28 29 37 public class StrutsValidatorTagsHandler extends AbstractProgramElementTagsHandler 38 { 39 protected final static List supportedTypes = new ArrayList (); 40 protected String curFieldName; 41 protected String currentArgKey; 42 protected Map args; 43 44 static { 45 supportedTypes.add("java.lang.String"); 46 supportedTypes.add("java.lang.Integer"); 47 supportedTypes.add("int"); 48 supportedTypes.add("java.lang.Float"); 49 supportedTypes.add("float"); 50 supportedTypes.add("java.lang.Long"); 51 supportedTypes.add("long"); 52 supportedTypes.add("java.lang.Double"); 53 supportedTypes.add("double"); 54 supportedTypes.add("java.lang.Boolean"); 55 supportedTypes.add("boolean"); 56 } 57 58 66 public void forAllForms(String template, Properties attributes) throws XDocletException 67 { 68 Collection classes = getAllClasses(); 69 70 for (Iterator i = classes.iterator(); i.hasNext(); ) { 71 XClass currentClass = (XClass) i.next(); 72 73 setCurrentClass(currentClass); 74 if (DocletSupport.isDocletGenerated(getCurrentClass()) || (getCurrentClass().isAbstract())) { 75 continue; 76 } 77 if (TypeTagsHandler.isOfType(currentClass, "org.apache.struts.validator.ValidatorForm", TypeTagsHandler.TYPE_HIERARCHY)) { 78 generate(template); 79 } 80 } 81 } 82 83 92 public String formName(Properties attributes) throws XDocletException 93 { 94 if (getCurrentClass().isA("org.apache.struts.validator.ValidatorActionForm")) 95 return getTagValue(FOR_CLASS, "struts.form", "path", null, null, false, true); 96 else 97 return getTagValue(FOR_CLASS, "struts.form", "name", null, null, false, true); 98 } 99 100 108 public void forAllFieldArgs(String template, Properties attributes) throws XDocletException 109 { 110 for (Iterator iterator = args.keySet().iterator(); iterator.hasNext(); ) { 111 currentArgKey = (String ) iterator.next(); 112 generate(template); 113 } 114 } 115 116 123 public String argIndex(Properties props) 124 { 125 return currentArgKey.charAt(3) + ""; 126 } 127 128 135 public String argName(Properties props) 136 { 137 String name = currentArgKey.substring(currentArgKey.indexOf('_') + 1); 138 139 return name; 140 } 141 142 149 public String argValue(Properties props) 150 { 151 return (String ) args.get(currentArgKey); 152 } 153 154 162 public void ifArgIsResource(String template, Properties attributes) throws XDocletException 163 { 164 if (currentArgKey.indexOf("resource") > 0) { 165 generate(template); 166 } 167 } 168 169 177 public void ifArgIsValue(String template, Properties attributes) throws XDocletException 178 { 179 if (currentArgKey.indexOf("value") > 0) { 180 generate(template); 181 } 182 } 183 184 192 public void ifArgIsForType(String template, Properties attributes) throws XDocletException 193 { 194 if (currentArgKey.indexOf('_') > 0) { 195 generate(template); 196 } 197 } 198 199 207 public void ifNoArg0(String template, Properties attributes) throws XDocletException 208 { 209 if (args.get("arg0resource") == null && args.get("arg0value") == null) { 210 generate(template); 211 } 212 } 213 214 222 public void ifFormHasFields(String template, Properties attributes) throws XDocletException 223 { 224 if (getFields(getCurrentClass()).size() > 0) { 225 generate(template); 226 } 227 } 228 229 237 public void ifFieldIsIndexed(String template, Properties attributes) throws XDocletException 238 { 239 if (curFieldName.indexOf("[]") >= 0) { 240 generate(template); 241 } 242 } 243 244 252 public void forAllFields(String template, Properties attributes) throws XDocletException 253 { 254 XClass clazz = getCurrentClass(); 255 Map setters = getFields(clazz); 256 257 for (Iterator iterator = setters.keySet().iterator(); iterator.hasNext(); ) { 258 curFieldName = (String ) iterator.next(); 259 260 XMethod field = (XMethod) setters.get(curFieldName); 261 262 setCurrentMethod(field); 263 loadFieldArguments(); 264 generate(template); 265 } 266 } 267 268 275 public String fieldName(Properties props) 276 { 277 int listIdx = curFieldName.indexOf("[]"); 278 279 if (listIdx == -1) 280 return curFieldName; 281 282 String result = curFieldName.substring(listIdx + 3); 283 284 if (result.length() == 0) 285 return curFieldName.substring(0, listIdx); 286 287 return result; 288 } 289 290 297 public String indexedListProperty(Properties props) 298 { 299 int listIdx = curFieldName.indexOf("[]"); 300 301 if (listIdx == -1) 302 return ""; 303 304 return curFieldName.substring(0, listIdx); 305 } 306 307 314 public String validatorList(Properties props) 315 { 316 XMethod method = getCurrentMethod(); 317 Collection tags = method.getDoc().getTags("struts.validator"); 318 StringBuffer buffer = new StringBuffer (); 319 320 for (Iterator iterator = tags.iterator(); iterator.hasNext(); ) { 321 XTag tag = (XTag) iterator.next(); 322 323 buffer.append(tag.getAttributeValue("type")); 324 if (iterator.hasNext()) { 325 buffer.append(","); 326 } 327 } 328 return buffer.toString(); 329 } 330 331 protected Map getFields(XClass clazz) throws XDocletException 332 { 333 return getFields(clazz, ""); 334 } 335 336 protected Map getFields(XClass clazz, String prefix) throws XDocletException 337 { 338 Map fields = new SequencedHashMap(); 339 340 Collection curFields = clazz.getMethods(true); 341 342 for (Iterator iterator = curFields.iterator(); iterator.hasNext(); ) { 347 XMethod method = (XMethod) iterator.next(); 348 XTag tag = method.getDoc().getTag("struts.validator"); 349 String override = null; 350 351 if (tag != null) { 352 override = tag.getAttributeValue("override"); 353 } 354 355 if (tag != null) { 356 List params = method.getParameters(); 357 String name = method.getPropertyName(); 358 XParameter param = null; 359 360 if (MethodTagsHandler.isSetterMethod(method)) { 361 param = (XParameter) params.get(0); 362 } 363 else if (params.size() == 2) { 364 if (!MethodTagsHandler.isSetter(method.getName())) 366 continue; 367 368 Iterator paramIter = params.iterator(); 369 370 if (!((XParameter) paramIter.next()).getType().isA("int")) 371 continue; 372 373 if (name.indexOf("[]") >= 0) { 374 throw new XDocletException(Translator.getString(StrutsValidatorMessages.class, 375 StrutsValidatorMessages.ONLY_ONE_LEVEL_LIST_PROPS, 376 new String []{clazz.getName() + '.' + name + "[]"})); 377 } 378 379 name = name + "[]"; 380 param = (XParameter) paramIter.next(); 381 } 382 else 383 continue; 384 385 String type = param.getType().getQualifiedName(); 386 387 if (supportedTypes.contains(type)) { 388 fields.put(prefix + name, method); 389 } 390 else if ((override != null) && (override.equals("true"))) { 391 fields.put(prefix + name, method); 392 } 393 else { 394 395 boolean preDot = (prefix.length() > 0 && prefix.charAt(prefix.length() - 1) != '.'); 396 397 fields.putAll(getFields(param.getType(), prefix + (preDot ? "." : "") + name + ".")); 398 } 399 } 400 } 401 402 return fields; 403 } 404 405 protected void loadFieldArguments() 406 { 407 420 args = new SequencedHashMap(); 421 422 XMethod method = getCurrentMethod(); 423 424 Collection argTags = method.getDoc().getTags("struts.validator-args"); 426 427 for (Iterator argsIterator = argTags.iterator(); argsIterator.hasNext(); ) { 428 XTag tag = (XTag) argsIterator.next(); 429 Collection attributeNames = tag.getAttributeNames(); 430 431 for (Iterator attributesIterator = attributeNames.iterator(); attributesIterator.hasNext(); ) { 432 String name = (String ) attributesIterator.next(); 433 434 if (name.startsWith("arg")) { 435 args.put(name, tag.getAttributeValue(name)); 436 } 437 } 438 } 439 440 Collection argTypeTags = method.getDoc().getTags("struts.validator"); 442 443 for (Iterator typeTagsIterator = argTypeTags.iterator(); typeTagsIterator.hasNext(); ) { 444 XTag tag = (XTag) typeTagsIterator.next(); 445 Collection attributeNames = tag.getAttributeNames(); 446 String type = tag.getAttributeValue("type"); 447 448 for (Iterator attributesIterator = attributeNames.iterator(); attributesIterator.hasNext(); ) { 449 String name = (String ) attributesIterator.next(); 450 451 if (name.startsWith("arg")) { 452 args.put(name + "_" + type, tag.getAttributeValue(name)); 453 } 454 } 455 } 456 } 457 } 458 | Popular Tags |