KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > XDocletTagSupport


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet;
6
7
8 import java.util.Properties JavaDoc;
9 import java.util.StringTokenizer JavaDoc;
10
11 import org.apache.commons.logging.Log;
12
13 import xjavadoc.*;
14 import xdoclet.template.TemplateEngine;
15 import xdoclet.template.TemplateException;
16 import xdoclet.template.TemplateTagHandler;
17 import xdoclet.util.LogUtil;
18 import xdoclet.util.Translator;
19 import xdoclet.util.TypeConversionUtil;
20
21 /**
22  * Derives from TemplateTagHandler and adds handy support methods for working with Javadoc Doclet classes.
23  *
24  * @author Dmitri Colebatch (dim@bigpond.net.au)
25  * @created October 12, 2001
26  * @version $Revision: 1.61 $
27  */

28 public abstract class XDocletTagSupport extends TemplateTagHandler
29 {
30     public final static int FOR_CLASS = 0;
31     public final static int FOR_METHOD = 1;
32     public final static int FOR_FIELD = 2;
33     public final static int FOR_CONSTRUCTOR = 3;
34
35     /**
36      * Default delimiter used inside a xdoclet tag attribute.
37      */

38     protected final static String JavaDoc PARAMETER_DELIMITER = ",";
39
40     /**
41      * @return The current tag.
42      */

43     public static XTag getCurrentMethodTag()
44     {
45         return getDocletContext().getActiveSubTask().getCurrentMethodTag();
46     }
47
48     /**
49      * @return The current tag.
50      */

51     public static XTag getCurrentClassTag()
52     {
53         return getDocletContext().getActiveSubTask().getCurrentClassTag();
54     }
55
56     /**
57      * @return The current tag.
58      */

59     public static XTag getCurrentFieldTag()
60     {
61         return getDocletContext().getActiveSubTask().getCurrentFieldTag();
62     }
63
64     /**
65      * Provides the current method in the XDoclet build, or null if there is no current method.
66      *
67      * @return current method
68      * @see #setCurrentMethod
69      */

70     public static XMethod getCurrentMethod()
71     {
72         return getDocletContext().getActiveSubTask().getCurrentMethod();
73     }
74
75     /**
76      * Provides the current field in the XDoclet build, or null if there is no current field.
77      *
78      * @return current field
79      * @see #setCurrentField
80      */

81     public static XField getCurrentField()
82     {
83         return getDocletContext().getActiveSubTask().getCurrentField();
84     }
85
86     /**
87      * Provides the current constructor in the XDoclet build, or null if there is no current constructor.
88      *
89      * @return current constructor
90      * @see #setCurrentConstructor
91      */

92     public static XConstructor getCurrentConstructor()
93     {
94         return getDocletContext().getActiveSubTask().getCurrentConstructor();
95     }
96
97     /**
98      * Provides the current class in the XDoclet build, or null if there is no current class.
99      *
100      * @return current class
101      * @see #pushCurrentClass
102      * @see #popCurrentClass
103      */

104     public static XClass getCurrentClass()
105     {
106         return getDocletContext().getActiveSubTask().getCurrentClass();
107     }
108
109     /**
110      * Provides the current package in the XDoclet build, or null if there is no current package.
111      *
112      * @return current package
113      */

114     public static XPackage getCurrentPackage()
115     {
116         return getDocletContext().getActiveSubTask().getCurrentPackage();
117     }
118
119     /**
120      * Set the current method.
121      *
122      * @param method The method to make the current method
123      * @see #getCurrentMethod
124      */

125     public static void setCurrentMethod(XMethod method)
126     {
127         getDocletContext().getActiveSubTask().setCurrentMethod(method);
128     }
129
130     /**
131      * Set the current constructor.
132      *
133      * @param constructor The constructor to make the current constructor
134      * @see #getCurrentConstructor
135      */

136     public static void setCurrentConstructor(XConstructor constructor)
137     {
138         getDocletContext().getActiveSubTask().setCurrentConstructor(constructor);
139     }
140
141     /**
142      * Sets current class to <code>clazz</code> by clearing <code>currentClassStack</code> stack and pushing <code>clazz</code>
143      * into top of it.
144      *
145      * @param clazz The class to make the current class
146      * @see #getCurrentClass()
147      */

148     public static void setCurrentClass(XClass clazz)
149     {
150         getDocletContext().getActiveSubTask().setCurrentClass(clazz);
151     }
152
153     /**
154      * Set the current package.
155      *
156      * @param pakkage The package to make the current package
157      * @see #getCurrentPackage
158      */

159     public static void setCurrentPackage(XPackage pakkage)
160     {
161         getDocletContext().getActiveSubTask().setCurrentPackage(pakkage);
162     }
163
164     /**
165      * Sets the current method tag.
166      *
167      * @param currentTag The tag to make the current method tag
168      */

169     public static void setCurrentMethodTag(XTag currentTag)
170     {
171         getDocletContext().getActiveSubTask().setCurrentMethodTag(currentTag);
172     }
173
174     /**
175      * Sets the current class tag.
176      *
177      * @param currentTag The tag to make the current class tag
178      */

179     public static void setCurrentClassTag(XTag currentTag)
180     {
181         getDocletContext().getActiveSubTask().setCurrentClassTag(currentTag);
182     }
183
184     /**
185      * Sets the current field tag.
186      *
187      * @param currentTag The tag to make the current field tag
188      */

189     public static void setCurrentFieldTag(XTag currentTag)
190     {
191         getDocletContext().getActiveSubTask().setCurrentFieldTag(currentTag);
192     }
193
194     /**
195      * Set the current field.
196      *
197      * @param field The field to make the current field
198      * @see #getCurrentField
199      */

200     public static void setCurrentField(XField field)
201     {
202         getDocletContext().getActiveSubTask().setCurrentField(field);
203     }
204
205     /**
206      * Push the specified class to the top of the current class stack making it effectively the current class.
207      *
208      * @param clazz The class to push onto the top of the class stack.
209      * @return The class on the top of the stack.
210      * @see #getCurrentClass
211      * @see #popCurrentClass
212      */

213     public static XClass pushCurrentClass(XClass clazz)
214     {
215         return getDocletContext().getActiveSubTask().pushCurrentClass(clazz);
216     }
217
218     /**
219      * Pop the current class off the top of the class stack.
220      *
221      * @return The class popped off the top of the stack.
222      * @see #getCurrentClass
223      * @see #pushCurrentClass
224      */

225     public static XClass popCurrentClass()
226     {
227         return getDocletContext().getActiveSubTask().popCurrentClass();
228     }
229
230     /**
231      * @return the context object casted to DocletContext
232      */

233     protected static DocletContext getDocletContext()
234     {
235         return DocletContext.getInstance();
236     }
237
238     /**
239      * The <code>getExpandedDelimitedTagValue</code> method returns a delimited version with class names expanded if
240      * requested of the tag value.
241      *
242      * @param attributes a <code>Properties</code> value
243      * @param forType an <code>int</code> value
244      * @return a <code>String</code> value
245      * @exception XDocletException if an error occurs
246      */

247     protected static String JavaDoc getExpandedDelimitedTagValue(Properties JavaDoc attributes, int forType) throws XDocletException
248     {
249         String JavaDoc tagValue = getTagValue(attributes, forType);
250
251         tagValue = delimit(tagValue, attributes);
252         tagValue = expandClassName(tagValue, attributes);
253         return tagValue;
254     }
255
256     /**
257      * Return the Value of a tag specified in a Properties object. This method work on the currentTag object variable,
258      * matchs it against the XTag specified in the attributes Properties and returns the value of the specified tag.
259      *
260      * @param attributes The attributes of the template tag
261      * @param forType if FOR_CLASS, then a fifth property superclasses is searched, if this is set to
262      * true, then the tag is also searched in all superclasses of current class. If forType is set to FOR_METHOD or
263      * FOR_CONSTRUCTOR or FOR_FIELD, current method or field is searched for the tag.
264      * @return The TagValue value
265      * @exception XDocletException Description of Exception
266      */

267     protected static String JavaDoc getTagValue(Properties JavaDoc attributes, int forType) throws XDocletException
268     {
269         String JavaDoc tagName = attributes.getProperty("tagName");
270         String JavaDoc paramName = attributes.getProperty("paramName");
271         String JavaDoc validValues = attributes.getProperty("values");
272         String JavaDoc defaultValue = attributes.getProperty("default");
273         String JavaDoc paramNum = attributes.getProperty("paramNum");
274         boolean superclasses = TypeConversionUtil.stringToBoolean(attributes.getProperty("superclasses"), true);
275         boolean mandatory = TypeConversionUtil.stringToBoolean(attributes.getProperty("mandatory"), false);
276
277         /*
278          * Handles multiple tags/parameters. Multiple tags/parameter are specified
279          * as alternatives, and are meant to be used as a "backward compatibility"
280          * helper. So, if the template has <hasTag tagName="tag1,tag2"
281          * paramName="param1,param2">, xDoclet will first look for param1 in tag1.
282          * If it's not found, it will look for param2 in tag2 and so on, until
283          * it finds a valid tag/parameter.
284          *
285          * If tagName has more alternatives than paramName, it is assumed that the
286          * corresponding paramName is null. If paramNum is supplied (it also can have
287          * multiple alternatives), the tag value is checked.
288          * (This is mainly to support jboss:table-name and friends. Ugly hack!)
289          */

290         StringTokenizer JavaDoc tagTokenizer = new StringTokenizer JavaDoc(tagName, ",");
291         StringTokenizer JavaDoc paramTokenizer = new StringTokenizer JavaDoc(paramName != null ? paramName : "", ",");
292         StringTokenizer JavaDoc paramNumTokenizer = new StringTokenizer JavaDoc(paramNum != null ? paramNum : "", ",");
293
294         // We can't have more parameters then tags
295
if (paramTokenizer.countTokens() > tagTokenizer.countTokens()) {
296             throw new XDocletException("paramName can't contain more alternatives than tagName");
297         }
298
299         // Loop until we get a non-null tag/parameter or there aren't any more alternatives
300
String JavaDoc tagValue = null;
301
302         while (tagTokenizer.hasMoreTokens() && tagValue == null) {
303             String JavaDoc currentTag = tagTokenizer.nextToken().trim();
304             String JavaDoc currentParam = null;
305
306             if (paramTokenizer.hasMoreTokens()) {
307                 currentParam = paramTokenizer.nextToken().trim();
308                 if ("".equals(currentParam)) {
309                     currentParam = null;
310                 }
311             }
312 // String currentTag = tagName;
313
// String currentParam = paramName;
314
// String
315
tagValue = getTagValue(
316                 forType,
317                 currentTag,
318                 currentParam,
319                 validValues,
320                 defaultValue,
321                 superclasses,
322                 mandatory
323                 );
324
325             // Case of jboss:table-name "abc"
326
if (tagValue == null && currentParam == null) {
327                 String JavaDoc currentParamNumber = null;
328
329                 if (paramNumTokenizer.hasMoreTokens()) {
330                     currentParamNumber = paramNumTokenizer.nextToken().trim();
331                 }
332
333 // String currentParamNumber = paramNum;
334

335                 if (currentParamNumber != null) {
336                     XProgramElement programElement = getProgramElement(forType);
337                     XDoc doc = programElement.getDoc();
338                     XTag tag = doc.getTag(currentTag, superclasses);
339
340                     if (tag != null) {
341                         tagValue = tag.getValue();
342                     }
343                     if (tagValue != null && tagValue.trim().length() == 0) {
344                         tagValue = null;
345                     }
346                 }
347             }
348             // end of hack
349

350             if (tagValue != null && tagValue.startsWith("\"")) {
351                 tagValue = tagValue.substring(1, tagValue.length() - 1);
352             }
353
354         }
355
356         // tagValue = delimit( tagValue, attributes );
357
return tagValue;
358     }
359
360     /**
361      * Gets the TagValue attribute of the XDocletTagSupport class
362      *
363      * @param forType Describe what the parameter does
364      * @param tagName Describe what the parameter does
365      * @param paramName Describe what the parameter does
366      * @param validValues Describe what the parameter does
367      * @param defaultValue Describe what the parameter does
368      * @param superclasses Describe what the parameter does
369      * @param mandatory Describe what the parameter does
370      * @return The TagValue value
371      * @exception XDocletException Describe the exception
372      */

373     protected static String JavaDoc getTagValue(
374         int forType,
375         String JavaDoc tagName,
376         String JavaDoc paramName,
377         String JavaDoc validValues,
378         String JavaDoc defaultValue,
379         boolean superclasses,
380         boolean mandatory
381         ) throws XDocletException
382     {
383
384         XProgramElement programElement = getProgramElement(forType);
385
386         if (programElement == null) {
387             return null;
388         }
389
390         XDoc doc = programElement.getDoc();
391
392         return getTagValue(
393             forType,
394             doc,
395             tagName,
396             paramName,
397             validValues,
398             defaultValue,
399             superclasses,
400             mandatory
401             );
402     }
403
404     /**
405      * @param doc Describe what the parameter does
406      * @param tagName Describe what the parameter does
407      * @param paramName Describe what the parameter does
408      * @param validValues Describe what the parameter does
409      * @param defaultValue Describe what the parameter does
410      * @param superclasses Describe what the parameter does
411      * @param mandatory Describe what the parameter does
412      * @param forType
413      * @return The TagValue value
414      * @exception XDocletException Describe the exception
415      * @todo (Aslak) maybe this method ought to be moved to xjavadoc.XDoc? Not a big deal though.
416      */

417     protected static String JavaDoc getTagValue(
418         int forType,
419         XDoc doc,
420         String JavaDoc tagName,
421         String JavaDoc paramName,
422         String JavaDoc validValues,
423         String JavaDoc defaultValue,
424         boolean superclasses,
425         boolean mandatory) throws XDocletException
426     {
427         XTag tag = null;
428
429         // first try to get current tag
430
if (forType == FOR_METHOD || forType == FOR_CONSTRUCTOR)
431             tag = getCurrentMethodTag();
432         else if (forType == FOR_CLASS)
433             tag = getCurrentClassTag();
434         else if (forType == FOR_FIELD)
435             tag = getCurrentFieldTag();
436
437         boolean noCurrentTag = tag == null;
438
439         if (tag != null && !tag.getName().equals(XDoc.dotted(tagName)))
440             tag = null;
441
442         if (tag == null) {
443             // if there is no current tag, look in the doc
444
tag = doc.getTag(tagName, superclasses);
445         }
446
447         String JavaDoc value = null;
448
449         // check if we have a tag at all
450
if (tag != null) {
451             if (paramName == null) {
452                 // the value of the tag is requested
453
value = tag.getValue();
454             }
455             else {
456                 value = tag.getAttributeValue(paramName);
457             }
458         }
459
460         // if there was no current tag, no value yet, and we are looking for a parameter, we must search
461
// all super class class tags in case THEY define the parameter we are looking for
462
//
463
if (noCurrentTag && value == null && paramName != null && superclasses) {
464             value = doc.getTagAttributeValue(tagName, paramName, superclasses);
465         }
466
467         if (value == null) {
468             // nothing found in javadocs
469
if (mandatory) {
470                 // throws XDocletException
471
mandatoryParamNotFound(doc, paramName, tagName);
472             }
473             if (defaultValue != null) {
474                 return defaultValue;
475             }
476             else {
477                 return null;
478             }
479         }
480         else {
481             // a value was found. perform sanity checks on valid values
482
if (validValues != null) {
483                 // check if the value is among the valid values
484
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(validValues, ",");
485
486                 while (st.hasMoreTokens()) {
487                     if (st.nextToken().equals(value)) {
488                         return value;
489                     }
490                 }
491                 invalidParamValueFound(doc, paramName, tagName, value, validValues);
492             }
493         }
494         return value;
495     }
496
497     /**
498      * A utility method used by ifMethodTagValueEquals/ifMethodTagValueNotEquals and
499      * ifClassTagValueEquals/ifClassTagValueNotEquals, return true if the value of the tag/XParameter equals with value.
500      *
501      * @param attributes The attributes of the template tag
502      * @param forType Describe what the parameter does
503      * @return Description of the Returned Value
504      * @exception XDocletException Description of Exception
505      */

506     protected static boolean isTagValueEqual(Properties JavaDoc attributes, int forType) throws XDocletException
507     {
508         // the value to check for
509
String JavaDoc value = attributes.getProperty("value");
510
511         if (value == null) {
512             throw new XDocletException("The value property is not specified");
513         }
514
515         String JavaDoc attributeValue = null;
516
517         // if currentTag is set first check against it, this is needed for forAll... tags
518
// where currentTag is set by these tags and we don't need/want to delegate the
519
// lookup to the surrounding current class or method.
520
String JavaDoc tagName = attributes.getProperty("tagName");
521         String JavaDoc paramName = attributes.getProperty("paramName");
522
523         XTag currentTag = null;
524
525         if (forType == FOR_METHOD || forType == FOR_CONSTRUCTOR)
526             currentTag = getCurrentMethodTag();
527         else if (forType == FOR_CLASS)
528             currentTag = getCurrentClassTag();
529         else if (forType == FOR_FIELD)
530             currentTag = getCurrentFieldTag();
531
532         if (currentTag != null && currentTag.getName().equals(XDoc.dotted(tagName))) {
533             attributeValue = currentTag.getAttributeValue(paramName);
534         }
535         else {
536             attributeValue = getTagValue(attributes, forType);
537         }
538         attributeValue = delimit(attributeValue, attributes);
539
540         return value.equals(attributeValue);
541     }
542
543     protected static String JavaDoc expandClassName(String JavaDoc value, Properties JavaDoc attributes)
544     {
545         boolean expand = TypeConversionUtil.stringToBoolean(attributes.getProperty("expandClassName"), false);
546
547         if (expand) {
548             value = getCurrentClass().qualify(value).getQualifiedName();
549         }
550         // end of if ()
551
return value;
552     }
553
554     /**
555      * Throws an XDocletException exception to stop the build process. The exception has an informative message to help
556      * user find out the cause of the error (not specifying a mandatory parameter for a tag).
557      *
558      * @param paramName tag parameter name
559      * @param tagName tag name
560      * @param doc member javadoc reference
561      * @exception XDocletException Description of Exception
562      */

563     protected static void mandatoryParamNotFound(XDoc doc, String JavaDoc paramName, String JavaDoc tagName) throws XDocletException
564     {
565         XProgramElement programElement = doc.getOwner();
566
567         if (programElement instanceof XMethod) {
568             XMethod method = (XMethod) programElement;
569
570             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.MANDATORY_TAG_PARAM_MISSING_METHOD,
571                 new String JavaDoc[]{paramName, tagName, method.getName(), method.getContainingClass().getQualifiedName()}));
572         }
573         else if (programElement instanceof XClass) {
574             XClass clazz = (XClass) programElement;
575
576             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.MANDATORY_TAG_PARAM_MISSING_CLASS,
577                 new String JavaDoc[]{paramName, tagName, clazz.getQualifiedName()}));
578         }
579         else if (programElement instanceof XConstructor) {
580             XConstructor constructor = (XConstructor) programElement;
581
582             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.MANDATORY_TAG_PARAM_MISSING_CONSTRUCTOR,
583                 new String JavaDoc[]{paramName, tagName, constructor.getContainingClass().getQualifiedName()}));
584         }
585         else if (programElement instanceof XField) {
586             XField field = (XField) programElement;
587
588             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.MANDATORY_TAG_PARAM_MISSING_FIELD,
589                 new String JavaDoc[]{paramName, tagName, field.getName(), field.getContainingClass().getQualifiedName()}));
590         }
591         else {
592             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.BAD_PRGELEMDOC_TYPE,
593                 new String JavaDoc[]{programElement.toString()}));
594         }
595     }
596
597     /**
598      * A utility method used by ifHasClassTag/ifDoesntHaveClassTag and ifHasMethodTag/ifDoesntHaveMethodTag, return true
599      * if at least one tag exists with the specified name.
600      *
601      * @param attributes The attributes of the template tag
602      * @param forType
603      * @return <code>true</code> if matching tag found
604      * @exception XDocletException Description of Exception
605      */

606     protected static boolean hasTag(Properties JavaDoc attributes, int forType) throws XDocletException
607     {
608         return getTagValue(attributes, forType) != null;
609     }
610
611     /**
612      * @param attributeValue Describe what the parameter does
613      * @param attributes Describe what the parameter does
614      * @return Describe the return value
615      * @todo fix the () equals test, it is not nice. Test : finder Home definition on AccountBean
616      */

617     protected static String JavaDoc delimit(String JavaDoc attributeValue, Properties JavaDoc attributes)
618     {
619         // Optional Parameter
620
String JavaDoc delim = attributes.getProperty("delimiter");
621         String JavaDoc tokenNumberStr = attributes.getProperty("tokenNumber");
622         int tokenNumber = 0;
623
624         if (tokenNumberStr != null) {
625             tokenNumber = Integer.parseInt(tokenNumberStr);
626         }
627         if (delim != null) {
628             if (delim.equals("()") && attributeValue.indexOf(delim) != -1) {
629                 attributeValue = null;
630             }
631             else {
632                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(attributeValue, delim);
633                 String JavaDoc tok = null;
634
635                 for (int i = 0; i <= tokenNumber; i++) {
636                     if (st.hasMoreTokens()) {
637                         tok = st.nextToken();
638                     }
639                     else {
640                         tok = null;
641                     }
642                 }
643                 attributeValue = tok;
644             }
645         }
646         return attributeValue;
647     }
648
649     /**
650      * Gets the current program element of the specified type.
651      *
652      * @param forType FOR_XXX type constant
653      * @return program element javadoc
654      * @exception XDocletException Describe the exception
655      */

656     private static XProgramElement getProgramElement(int forType) throws XDocletException
657     {
658         XProgramElement programElement = null;
659
660         switch (forType) {
661         case FOR_CLASS:
662             programElement = getCurrentClass();
663             break;
664         case FOR_METHOD:
665             programElement = getCurrentMethod();
666             break;
667         case FOR_CONSTRUCTOR:
668             programElement = getCurrentConstructor();
669             break;
670         case FOR_FIELD:
671             programElement = getCurrentField();
672             break;
673         default:
674             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.BAD_TAGVALUE_TYPE));
675         }
676         return programElement;
677     }
678
679     /**
680      * Throws an XDocletException exception to stop the build process. The exception has an informative message to help
681      * user find out the cause of the error (specifying an incorrect value for a parameter of a tag).
682      *
683      * @param paramName tag parameter name
684      * @param tagName tag name
685      * @param value invalid parameter value
686      * @param validValues valid parameter values
687      * @param doc program element javadoc
688      * @exception XDocletException Description of Exception
689      */

690     private static void invalidParamValueFound(XDoc doc, String JavaDoc paramName, String JavaDoc tagName, String JavaDoc value, String JavaDoc validValues) throws XDocletException
691     {
692         XProgramElement programElement = doc.getOwner();
693
694         if (programElement instanceof XMethod) {
695             XMethod method = (XMethod) programElement;
696
697             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.INVALID_TAG_PARAM_VALUE_METHOD,
698                 new String JavaDoc[]{value, paramName, tagName, method.getName(), method.getContainingClass().getQualifiedName(), validValues}));
699         }
700         else if (programElement instanceof XClass) {
701             XClass clazz = (XClass) programElement;
702
703             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.INVALID_TAG_PARAM_VALUE_CLASS,
704                 new String JavaDoc[]{value, paramName, tagName, clazz.getQualifiedName(), validValues}));
705         }
706         else if (programElement instanceof XConstructor) {
707             XConstructor constructor = (XConstructor) programElement;
708
709             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.INVALID_TAG_PARAM_VALUE_CONSTRUCTOR,
710                 new String JavaDoc[]{value, paramName, tagName, constructor.getContainingClass().getQualifiedName(), validValues}));
711         }
712         else if (programElement instanceof XField) {
713             XField field = (XField) programElement;
714
715             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.INVALID_TAG_PARAM_VALUE_FIELD,
716                 new String JavaDoc[]{value, paramName, tagName, field.getName(), field.getContainingClass().getQualifiedName(), validValues}));
717         }
718         else {
719             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.INVALID_TAG_PARAM_VALUE_METHOD,
720                 new String JavaDoc[]{programElement.toString()}));
721         }
722     }
723
724     /**
725      * Gets the Engine attribute of the TemplateTagHandler object.
726      *
727      * @return The Engine value
728      */

729     public TemplateEngine getEngine()
730     {
731         return ((TemplateSubTask) getDocletContext().getActiveSubTask()).getEngine();
732     }
733
734     /**
735      * @param template
736      * @exception XDocletException
737      * @todo throw TemplateException instead
738      */

739     public void generate(String JavaDoc template) throws XDocletException
740     {
741         try {
742             getEngine().generate(template);
743         }
744         catch (TemplateException e) {
745             if (e instanceof XDocletException) {
746                 throw (XDocletException) e;
747             }
748             else {
749
750                 throw new XDocletException(e, Translator.getString(XDocletMessages.class, XDocletMessages.RUNNING_FAILED) + ": " + e.toString());
751             }
752         }
753     }
754
755     /**
756      * Return the modifiers (static, private, etc.) for the current program element of the specified type.
757      *
758      * @param forType FOR_XXX type constant
759      * @return modifiers
760      * @exception XDocletException Describe the exception
761      */

762     protected String JavaDoc modifiers(int forType) throws XDocletException
763     {
764         return getProgramElement(forType).getModifiers();
765     }
766
767     /**
768      * Throws an XDocletException exception to stop the build process. The exception has an informative message to help
769      * user find out the cause of the error (omitting a mandatory parameter on a tag).
770      *
771      * @param templateTagName tag name
772      * @param paramName tag parameter name
773      * @exception XDocletException
774      */

775     protected void mandatoryTemplateTagParamNotFound(String JavaDoc templateTagName, String JavaDoc paramName) throws XDocletException
776     {
777         throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.MANDATORY_TAG_PARAM_MISSING_TEMPLATE,
778             new String JavaDoc[]{paramName, templateTagName}));
779     }
780
781     /**
782      * Tests whether the passed class has the tag which is specfied in the <code>havingClassTag</code> attribute of the
783      * current subtask.
784      *
785      * @param clazz the Class to check
786      * @return <code>true</code> if the current subtask has no <code>havingClassTag</code> or the passed class has
787      * a tag with the same name as the <code>havingClassTag</code> attribute of the current subtask, otherwise
788      * <code>false</code>
789      */

790     protected boolean hasHavingClassTag(XClass clazz)
791     {
792         Log log = LogUtil.getLog(getClass(), "hasHavingClassTag");
793
794         DocletContext ctx = getDocletContext();
795         SubTask task = ctx.getActiveSubTask();
796
797         if (!(task instanceof TemplateSubTask)) {
798             return true;
799         }
800
801         TemplateSubTask templTask = (TemplateSubTask) task;
802
803         if (templTask.getHavingClassTag() == null) {
804             return true;
805         }
806
807         if (!clazz.getDoc().hasTag(templTask.getHavingClassTag(), false)) {
808             if (log.isDebugEnabled()) {
809                 log.debug("Reject class '" + clazz.getQualifiedName() + "' because it doesn't have class tag '" + templTask.getHavingClassTag() + "'.");
810             }
811             return false;
812         }
813         else {
814             if (log.isDebugEnabled()) {
815                 log.debug("Accept class '" + clazz.getQualifiedName() + "' because it has class tag '" + templTask.getHavingClassTag() + "'.");
816             }
817             return true;
818         }
819     }
820 }
821
Popular Tags