1 package xdoclet.modules.ojb; 2 3 17 18 import java.util.*; 19 20 import xjavadoc.*; 21 import xdoclet.XDocletException; 22 import xdoclet.tagshandler.AbstractProgramElementTagsHandler; 23 import xdoclet.tagshandler.ClassTagsHandler; 24 import xdoclet.tagshandler.MethodTagsHandler; 25 import xdoclet.tagshandler.XDocletTagshandlerMessages; 26 import xdoclet.util.Translator; 27 import xdoclet.util.TypeConversionUtil; 28 29 34 public class OjbMemberTagsHandler extends AbstractProgramElementTagsHandler 35 { 36 37 44 public static String getMemberName() throws XDocletException 45 { 46 if (getCurrentField() != null) { 47 return getCurrentField().getName(); 48 } 49 else if (getCurrentMethod() != null) { 50 return MethodTagsHandler.getPropertyNameFor(getCurrentMethod()); 51 } 52 else { 53 return null; 54 } 55 } 56 57 64 public static XClass getMemberType() throws XDocletException 65 { 66 if (getCurrentField() != null) { 67 return getCurrentField().getType(); 68 } 69 else if (getCurrentMethod() != null) { 70 XMethod method = getCurrentMethod(); 71 72 if (MethodTagsHandler.isGetterMethod(method)) { 73 return method.getReturnType().getType(); 74 } 75 else if (MethodTagsHandler.isSetterMethod(method)) { 76 XParameter param = (XParameter)method.getParameters().iterator().next(); 77 78 return param.getType(); 79 } 80 } 81 return null; 82 } 83 84 91 public static int getMemberDimension() throws XDocletException 92 { 93 if (getCurrentField() != null) { 94 return getCurrentField().getDimension(); 95 } 96 else if (getCurrentMethod() != null) { 97 XMethod method = getCurrentMethod(); 98 99 if (MethodTagsHandler.isGetterMethod(method)) { 100 return method.getReturnType().getDimension(); 101 } 102 else if (MethodTagsHandler.isSetterMethod(method)) { 103 XParameter param = (XParameter)method.getParameters().iterator().next(); 104 105 return param.getDimension(); 106 } 107 } 108 return 0; 109 } 110 111 119 public void isField(String template, Properties attributes) throws XDocletException 120 { 121 if (getCurrentField() != null) { 122 generate(template); 123 } 124 } 125 126 134 public void isMethod(String template, Properties attributes) throws XDocletException 135 { 136 if (getCurrentMethod() != null) { 137 generate(template); 138 } 139 } 140 141 164 public void forAllMembers(String template, Properties attributes) throws XDocletException 165 { 166 if (getCurrentClass() == null) { 167 return; 168 } 169 170 String className = attributes.getProperty("class"); 171 XClass type = null; 172 173 if ((className == null) || (className.length() == 0)) { 174 type = getCurrentClass(); 175 } 176 else { 177 XClass curType; 178 179 for (Iterator it = ClassTagsHandler.getAllClasses().iterator(); it.hasNext(); ) { 180 curType = (XClass)it.next(); 181 if (className.equals(curType.getQualifiedName())) { 182 type = curType; 183 break; 184 } 185 } 186 if (type == null) { 187 throw new XDocletException(Translator.getString(XDocletModulesOjbMessages.class, 188 XDocletModulesOjbMessages.COULD_NOT_FIND_TYPE, 189 new String []{className})); 190 } 191 } 192 193 String tagName = attributes.getProperty("tagName"); 194 String paramName = attributes.getProperty("paramName"); 195 String paramValue = attributes.getProperty("value"); 196 boolean superTypes = TypeConversionUtil.stringToBoolean(attributes.getProperty("superclasses"), true); 197 boolean sort = TypeConversionUtil.stringToBoolean(attributes.getProperty("sort"), true); 198 ArrayList allMemberNames = new ArrayList(); 199 HashMap allMembers = new HashMap(); 200 201 if (superTypes) { 202 addMembersInclSupertypes(allMemberNames, allMembers, type, tagName, paramName, paramValue); 203 } 204 else { 205 addMembers(allMemberNames, allMembers, type, tagName, paramName, paramValue); 206 } 207 if (sort) { 208 Collections.sort(allMemberNames); 209 } 210 for (Iterator it = allMemberNames.iterator(); it.hasNext(); ) { 211 XMember member = (XMember) allMembers.get(it.next()); 212 213 if (member instanceof XField) { 214 setCurrentField((XField) member); 215 } 216 else if (member instanceof XMethod) { 217 setCurrentMethod((XMethod) member); 218 } 219 generate(template); 220 if (member instanceof XField) { 221 setCurrentField(null); 222 } 223 else if (member instanceof XMethod) { 224 setCurrentMethod(null); 225 } 226 } 227 } 228 229 239 public void forAllMemberTags(String template, Properties attributes) throws XDocletException 240 { 241 if (getCurrentField() != null) { 242 forAllMemberTags(template, attributes, FOR_FIELD, XDocletTagshandlerMessages.ONLY_CALL_FIELD_NOT_NULL, new String []{"forAllMemberTags"}); 243 } 244 else if (getCurrentMethod() != null) { 245 forAllMemberTags(template, attributes, FOR_METHOD, XDocletTagshandlerMessages.ONLY_CALL_METHOD_NOT_NULL, new String []{"forAllMemberTags"}); 246 } 247 } 248 249 261 public void forAllMemberTagTokens(String template, Properties attributes) throws XDocletException 262 { 263 if (getCurrentField() != null) { 264 forAllMemberTagTokens(template, attributes, FOR_FIELD); 265 } 266 else if (getCurrentMethod() != null) { 267 forAllMemberTagTokens(template, attributes, FOR_METHOD); 268 } 269 } 270 271 280 public String memberName(Properties attributes) throws XDocletException 281 { 282 return getMemberName(); 283 } 284 285 299 public void ifDoesntHaveMemberTag(String template, Properties attributes) throws XDocletException 300 { 301 boolean result = false; 302 303 if (getCurrentField() != null) { 304 if (!hasTag(attributes, FOR_FIELD)) { 305 result = true; 306 generate(template); 307 } 308 } 309 else if (getCurrentMethod() != null) { 310 if (!hasTag(attributes, FOR_METHOD)) { 311 result = true; 312 generate(template); 313 } 314 } 315 if (!result) { 316 String error = attributes.getProperty("error"); 317 318 if (error != null) { 319 getEngine().print(error); 320 } 321 } 322 } 323 324 336 public void ifHasMemberWithTag(String template, Properties attributes) throws XDocletException 337 { 338 ArrayList allMemberNames = new ArrayList(); 339 HashMap allMembers = new HashMap(); 340 boolean hasTag = false; 341 342 addMembers(allMemberNames, allMembers, getCurrentClass(), null, null, null); 343 for (Iterator it = allMemberNames.iterator(); it.hasNext(); ) { 344 XMember member = (XMember) allMembers.get(it.next()); 345 346 if (member instanceof XField) { 347 setCurrentField((XField)member); 348 if (hasTag(attributes, FOR_FIELD)) { 349 hasTag = true; 350 } 351 setCurrentField(null); 352 } 353 else if (member instanceof XMethod) { 354 setCurrentMethod((XMethod)member); 355 if (hasTag(attributes, FOR_METHOD)) { 356 hasTag = true; 357 } 358 setCurrentMethod(null); 359 } 360 if (hasTag) { 361 generate(template); 362 break; 363 } 364 } 365 } 366 367 381 public void ifHasMemberTag(String template, Properties attributes) throws XDocletException 382 { 383 boolean result = false; 384 385 if (getCurrentField() != null) { 386 if (hasTag(attributes, FOR_FIELD)) { 387 result = true; 388 generate(template); 389 } 390 } 391 else if (getCurrentMethod() != null) { 392 if (hasTag(attributes, FOR_METHOD)) { 393 result = true; 394 generate(template); 395 } 396 } 397 if (!result) { 398 String error = attributes.getProperty("error"); 399 400 if (error != null) { 401 getEngine().print(error); 402 } 403 } 404 } 405 406 423 public String memberTagValue(Properties attributes) throws XDocletException 424 { 425 if (getCurrentField() != null) { 426 attributes.setProperty("field", "true"); 428 return getExpandedDelimitedTagValue(attributes, FOR_FIELD); 429 } 430 else if (getCurrentMethod() != null) { 431 return getExpandedDelimitedTagValue(attributes, FOR_METHOD); 432 } 433 else { 434 return null; 435 } 436 } 437 438 452 public void ifMemberTagValueEquals(String template, Properties attributes) throws XDocletException 453 { 454 if (getCurrentField() != null) { 455 if (isTagValueEqual(attributes, FOR_FIELD)) { 456 generate(template); 457 } 458 } 459 else if (getCurrentMethod() != null) { 460 if (isTagValueEqual(attributes, FOR_METHOD)) { 461 generate(template); 462 } 463 } 464 } 465 466 477 private void addMembersInclSupertypes(Collection memberNames, HashMap members, XClass type, String tagName, String paramName, String paramValue) throws XDocletException 478 { 479 addMembers(memberNames, members, type, tagName, paramName, paramValue); 480 if (type.getInterfaces() != null) { 481 for (Iterator it = type.getInterfaces().iterator(); it.hasNext(); ) { 482 addMembersInclSupertypes(memberNames, members, (XClass)it.next(), tagName, paramName, paramValue); 483 } 484 } 485 if (!type.isInterface() && (type.getSuperclass() != null)) { 486 addMembersInclSupertypes(memberNames, members, type.getSuperclass(), tagName, paramName, paramValue); 487 } 488 } 489 490 501 private void addMembers(Collection memberNames, HashMap members, XClass type, String tagName, String paramName, String paramValue) throws XDocletException 502 { 503 if (!type.isInterface() && (type.getFields() != null)) { 504 XField field; 505 506 for (Iterator it = type.getFields().iterator(); it.hasNext(); ) { 507 field = (XField)it.next(); 508 if (!field.isFinal() && !field.isStatic() && !field.isTransient()) { 509 if (checkTagAndParam(field.getDoc(), tagName, paramName, paramValue)) { 510 if (!members.containsKey(field.getName())) { 512 memberNames.add(field.getName()); 513 members.put(field.getName(), field); 514 } 515 } 516 } 517 } 518 } 519 520 if (type.getMethods() != null) { 521 XMethod method; 522 String propertyName; 523 524 for (Iterator it = type.getMethods().iterator(); it.hasNext(); ) { 525 method = (XMethod)it.next(); 526 if (!method.isConstructor() && !method.isNative() && !method.isStatic()) { 527 if (checkTagAndParam(method.getDoc(), tagName, paramName, paramValue)) { 528 if (MethodTagsHandler.isGetterMethod(method) || MethodTagsHandler.isSetterMethod(method)) { 529 propertyName = MethodTagsHandler.getPropertyNameFor(method); 530 if (!members.containsKey(propertyName)) { 531 memberNames.add(propertyName); 532 members.put(propertyName, method); 533 } 534 } 535 } 536 } 537 } 538 } 539 } 540 541 551 private boolean checkTagAndParam(XDoc doc, String tagName, String paramName, String paramValue) 552 { 553 if (tagName == null) { 554 return true; 555 } 556 if (!doc.hasTag(tagName)) { 557 return false; 558 } 559 if (paramName == null) { 560 return true; 561 } 562 if (!doc.getTag(tagName).getAttributeNames().contains(paramName)) { 563 return false; 564 } 565 return (paramValue == null) || paramValue.equals(doc.getTagAttributeValue(tagName, paramName)); 566 } 567 } 568 | Popular Tags |