1 5 package xdoclet.modules.ejb.env; 6 7 import java.lang.reflect.Modifier ; 8 import java.util.Collection ; 9 import java.util.Collections ; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Map ; 14 import java.util.Properties ; 15 import java.util.StringTokenizer ; 16 import xjavadoc.XClass; 17 import xjavadoc.XField; 18 import xjavadoc.XMember; 19 import xjavadoc.XMethod; 20 import xjavadoc.XTag; 21 22 import xdoclet.XDocletException; 23 import xdoclet.XDocletTagSupport; 24 import xdoclet.util.TypeConversionUtil; 25 26 34 public class EnvTagsHandler extends XDocletTagSupport 35 { 36 39 private final static Map wrappers; 40 41 protected XTag currentTag; 42 protected XMember currentMember; 43 protected int currentTagType; 44 static { 45 Map wps = new HashMap (); 46 47 wps.put("boolean", "java.lang.Boolean"); 48 wps.put("byte", "java.lang.Byte"); 49 wps.put("char", "java.lang.Character"); 50 wps.put("short", "java.lang.Short"); 51 wps.put("int", "java.lang.Integer"); 52 wps.put("float", "java.lang.Float"); 53 wps.put("long", "java.lang.Long"); 54 wps.put("double", "java.lang.Double"); 55 wrappers = Collections.unmodifiableMap(wps); 56 } 57 58 69 public void forAllTags(String template, Properties attributes) throws XDocletException 70 { 71 forTags(template, attributes, true, true, true); 72 } 73 74 85 public void forAllMemberTags(String template, Properties attributes) throws XDocletException 86 { 87 forTags(template, attributes, false, true, true); 88 89 } 90 91 102 public void forAllMethodTags(String template, Properties attributes) throws XDocletException 103 { 104 forTags(template, attributes, false, true, false); 105 } 106 107 118 public String name(Properties attributes) throws XDocletException 119 { 120 String paramName = attributes.getProperty("paramName"); 121 122 if (paramName == null) { 123 throw new XDocletException("paramName attribute is mandatory"); 124 } 125 126 String name = null; 127 StringTokenizer st = new StringTokenizer (paramName, ","); 128 129 while (name == null && st.hasMoreTokens()) { 130 name = currentTag.getAttributeValue(st.nextToken()); 131 } 132 133 if (name == null) { 134 if (currentMember == null) { 135 mandatoryParamNotFound(currentTag.getDoc(), paramName, currentTag.getName()); 137 } 138 else { 139 name = currentMember.getName(); 141 } 142 } 143 return name; 144 } 145 146 162 public String type(Properties attributes) throws XDocletException 163 { 164 if (currentMember == null) { 165 return paramValue(attributes); 167 } 168 else { 169 String type = currentMemberType(); 171 String wrapper = (String ) wrappers.get(type); 172 173 return wrapper == null ? type : wrapper; 174 } 175 } 176 177 186 public void ifHasType(String template, Properties attributes) throws XDocletException 187 { 188 String paramName = attributes.getProperty("paramName"); 189 190 if (paramName == null) { 191 throw new XDocletException("paramName attribute is mandatory"); 192 } 193 194 if (currentMember == null) { 195 String type = currentTag.getAttributeValue(paramName); 197 198 if (type != null) { 199 generate(template); 200 } 201 } 202 else { 203 generate(template); 205 } 206 } 207 208 218 public String memberName(Properties attributes) throws XDocletException 219 { 220 if (currentMember == null) { 221 throw new XDocletException("XDtEjbEnv:memberName can only be used inside forAllMemberTags or forAllMethodTags"); 222 } 223 224 String name = currentMember.getName(); 225 String prefix = attributes.getProperty("prefix"); 226 227 if (prefix != null) { 228 name = prefix + "/" + name; 229 } 230 return name; 231 } 232 233 241 public String memberType() throws XDocletException 242 { 243 if (currentMember == null) { 244 throw new XDocletException("XDtEjbEnv:memberType can only be used inside forAllMemberTags or forAllMethodTags"); 245 } 246 return currentMemberType(); 247 } 248 249 256 public String methodSignature() throws XDocletException 257 { 258 if (currentMember == null || !(currentMember instanceof XMethod)) { 259 throw new XDocletException("XDtEjbEnv:methodSignature can only be used inside forAllMemberTags or forAllMethodTags"); 260 } 261 262 XMethod method = (XMethod) currentMember; 263 StringBuffer sb = new StringBuffer (); 264 265 if (Modifier.isProtected(method.getModifierSpecifier())) { 266 sb.append("protected "); 267 } 268 if (Modifier.isPublic(method.getModifierSpecifier())) { 269 sb.append("public "); 270 } 271 sb.append(method.getReturnType().getType().getQualifiedName()); 272 sb.append(' '); 273 sb.append(method.getNameWithSignature(true)); 274 275 List exceptions = method.getThrownExceptions(); 276 277 if (exceptions.size() > 0) { 278 sb.append(" throws "); 279 for (Iterator it = exceptions.iterator(); it.hasNext(); ) { 280 XClass exception = (XClass) it.next(); 281 282 sb.append(exception.getQualifiedName()); 283 if (it.hasNext()) { 284 sb.append(", "); 285 } 286 } 287 } 288 289 return sb.toString(); 290 } 291 292 306 public String paramValue(Properties attributes) throws XDocletException 307 { 308 attributes.setProperty("tagName", currentTag.getName()); 309 return getTagValue(attributes, currentTagType); 310 } 311 312 321 public void ifHasParam(String template, Properties attributes) throws XDocletException 322 { 323 if (paramValue(attributes) != null) { 324 generate(template); 325 } 326 } 327 328 338 public void ifParamValueEquals(String template, Properties attributes) throws XDocletException 339 { 340 if (isParamValueEqual(attributes)) { 341 generate(template); 342 } 343 } 344 345 355 public void ifParamValueNotEquals(String template, Properties attributes) throws XDocletException 356 { 357 if (!isParamValueEqual(attributes)) { 358 generate(template); 359 } 360 } 361 362 363 371 public void ifPrimitiveMember(String template, Properties attributes) throws XDocletException 372 { 373 if (isPrimitiveMember()) { 374 generate(template); 375 } 376 } 377 378 386 public void ifNotPrimitiveMember(String template, Properties attributes) throws XDocletException 387 { 388 if (!isPrimitiveMember()) { 389 generate(template); 390 } 391 } 392 393 405 public void ifHasTag(String template, Properties attributes) throws XDocletException 406 { 407 String tags = attributes.getProperty("tagName"); 408 409 if (tags == null) { 410 throw new XDocletException("tagName is mandatory"); 411 } 412 413 String paramName = attributes.getProperty("paramName"); 414 String paramValue = attributes.getProperty("paramValue"); 415 416 if (hasMemberWithTag(getCurrentClass().getMethods(true), tags, paramName, paramValue)) { 417 generate(template); 418 } 419 else { 420 if (hasMemberWithTag(getCurrentClass().getFields(true), tags, paramName, paramValue)) { 421 generate(template); 422 } 423 } 424 } 425 426 427 437 protected void forTags(String template, Properties attributes, boolean forClass, boolean forMethod, boolean forField) throws XDocletException 438 { 439 boolean superclasses = TypeConversionUtil.stringToBoolean(attributes.getProperty("superclasses"), true); 440 441 String tagName = attributes.getProperty("tagName"); 442 443 if (tagName == null) { 444 throw new XDocletException("tagName is mandatory"); 445 } 446 447 StringTokenizer st = new StringTokenizer (tagName, ","); 448 449 while (st.hasMoreTokens()) { 450 attributes.setProperty("tagName", st.nextToken()); 451 forTagsInternal(template, attributes, superclasses, forClass, forMethod, forField); 452 } 453 } 454 455 461 protected void doGenerate(String template) throws XDocletException 462 { 463 generate(template); 464 } 465 466 467 472 private boolean isPrimitiveMember() 473 { 474 if (currentMember != null) { 475 String type = currentMemberType(); 476 String wrapper = (String ) wrappers.get(type); 477 478 if (wrapper != null) { 479 return true; 480 } 481 } 482 return false; 483 } 484 485 492 private boolean isParamValueEqual(Properties attributes) throws XDocletException 493 { 494 String value = attributes.getProperty("value"); 495 496 if (value == null) { 497 throw new XDocletException("value is mandatory"); 498 } 499 return value.equals(paramValue(attributes)); 500 } 501 502 513 private void forTagsInternal(String template, Properties attributes, boolean superclasses, boolean forClass, boolean forMethod, boolean forField) throws XDocletException 514 { 515 516 String tagName = attributes.getProperty("tagName"); 517 String paramName = attributes.getProperty("paramName"); 518 String paramValue = attributes.getProperty("paramValue"); 519 520 if (forClass) { 522 currentTagType = FOR_CLASS; 523 524 Collection tags = getCurrentClass().getDoc().getTags(tagName, superclasses); 525 526 for (Iterator it = tags.iterator(); it.hasNext(); ) { 527 currentTag = (XTag) it.next(); 528 if (tagMatches(currentTag, paramName, paramValue)) { 529 setCurrentClassTag(currentTag); 530 currentMember = null; 531 doGenerate(template); 532 setCurrentClassTag(null); 533 } 534 } 535 } 536 537 if (forMethod) { 539 currentTagType = FOR_METHOD; 540 541 Collection methods = getCurrentClass().getMethods(superclasses); 542 543 for (Iterator it = methods.iterator(); it.hasNext(); ) { 544 XMethod method = (XMethod) it.next(); 545 546 setCurrentMethod(method); 547 548 Collection tags = method.getDoc().getTags(tagName); 549 550 for (Iterator it2 = tags.iterator(); it2.hasNext(); ) { 551 currentTag = (XTag) it2.next(); 552 if (tagMatches(currentTag, paramName, paramValue)) { 553 setCurrentMethodTag(currentTag); 554 currentMember = method; 555 doGenerate(template); 556 setCurrentMethodTag(null); 557 } 558 } 559 setCurrentMethod(null); 560 } 561 } 562 563 if (forField) { 565 currentTagType = FOR_FIELD; 566 567 Collection fields = getCurrentClass().getFields(superclasses); 568 569 for (Iterator it = fields.iterator(); it.hasNext(); ) { 570 XField field = (XField) it.next(); 571 572 setCurrentField(field); 573 574 Collection tags = field.getDoc().getTags(tagName); 575 576 for (Iterator it2 = tags.iterator(); it2.hasNext(); ) { 577 currentTag = (XTag) it2.next(); 578 if (tagMatches(currentTag, paramName, paramValue)) { 579 setCurrentFieldTag(currentTag); 580 currentMember = field; 581 doGenerate(template); 582 setCurrentFieldTag(null); 583 } 584 } 585 setCurrentField(null); 586 } 587 } 588 589 currentTagType = 0; 590 } 591 592 601 private boolean tagMatches(XTag tag, String paramName, String paramValue) 602 { 603 if (paramName == null) { 604 return true; 605 } 606 607 String value = tag.getAttributeValue(paramName); 608 609 return value != null && (paramValue == null || value.equals(paramValue)); 610 } 611 612 623 private boolean hasMemberWithTag(Collection members, String tagNames, String paramName, String paramValue) 624 { 625 626 for (Iterator it = members.iterator(); it.hasNext(); ) { 627 XMember member = (XMember) it.next(); 628 629 StringTokenizer st = new StringTokenizer (tagNames, ","); 630 631 while (st.hasMoreTokens()) { 632 Collection tags = member.getDoc().getTags(st.nextToken()); 633 634 if (tags.size() > 0) { 635 if (paramName == null) { 636 return true; 637 } 638 else { 639 for (Iterator it2 = tags.iterator(); it2.hasNext(); ) { 640 XTag tag = (XTag) it2.next(); 641 String value = tag.getAttributeValue(paramName); 642 643 if (value != null && (paramValue == null || paramValue.equals(value))) { 644 return true; 645 } 646 } 647 } 648 } 649 } 650 } 651 return false; 652 } 653 654 659 private String currentMemberType() 660 { 661 if (currentMember instanceof XField) { 662 return ((XField) currentMember).getType().getQualifiedName(); 663 } 664 else { 665 return ((XMethod) currentMember).getReturnType().getType().getQualifiedName(); 666 } 667 668 } 669 670 } 671 | Popular Tags |