KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > tagshandler > IdTagsHandler


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

5 package xdoclet.tagshandler;
6
7 import java.util.HashMap JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.Properties JavaDoc;
10 import java.util.StringTokenizer JavaDoc;
11 import org.apache.commons.logging.Log;
12
13 import xdoclet.XDocletException;
14 import xdoclet.XDocletMessages;
15 import xdoclet.XDocletTagSupport;
16 import xdoclet.util.LogUtil;
17 import xdoclet.util.Translator;
18 import xdoclet.util.TypeConversionUtil;
19
20 /**
21  * @author Ara Abrahamian (ara_e@email.com)
22  * @created Oct 15, 2001
23  * @xdoclet.taghandler namespace="Id"
24  * @version $Revision: 1.9 $
25  */

26 public class IdTagsHandler extends XDocletTagSupport
27 {
28     private static Map JavaDoc prefixHash = new HashMap JavaDoc();
29
30     /**
31      * Resets the hashtable which backs the prefixId tag.
32      */

33     public static void reset()
34     {
35         prefixHash.clear();
36     }
37
38     /**
39      * @param paramNames comma-separated of parameter names, first params are in higher priority.
40      * @param tagName
41      * @return Description of the Returned Value
42      * @exception XDocletException
43      */

44     private static String JavaDoc getIdByTagValues(String JavaDoc tagName, String JavaDoc paramNames) throws XDocletException
45     {
46         Log log = LogUtil.getLog(IdTagsHandler.class, "getIdByTagValues");
47
48         if (tagName == null) {
49             log.error(Translator.getString(XDocletMessages.class, XDocletTagshandlerMessages.ID_PARAM_MISSING, new String JavaDoc[]{"tagName"}));
50             return "";
51         }
52
53         if (paramNames == null) {
54             log.error(Translator.getString(XDocletMessages.class, XDocletTagshandlerMessages.ID_PARAM_MISSING, new String JavaDoc[]{"paramNames"}));
55             return "";
56         }
57
58         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(paramNames, ",", false);
59
60         while (st.hasMoreTokens()) {
61             String JavaDoc paramValue = getTagValue(
62                 FOR_CLASS,
63                 tagName,
64                 st.nextToken(),
65                 null,
66                 null,
67                 true,
68                 false
69                 );
70
71             if (paramValue != null) {
72                 return paramValue.replace('/', '_');
73             }
74         }
75
76         return tagName;
77     }
78
79     /**
80      * Generates an id attribute based on the given prefix. This is used for generating id attribute for XML elements.
81      *
82      * @param attributes The attributes of the template tag
83      * @return An id in the form of <prefix>_<num>
84      * @exception XDocletException Description of Exception
85      * @doc.tag type="content"
86      * @doc.param name="prefix" optional="false" description="The tag from which the value of the id
87      * is calculated."
88      */

89     public String JavaDoc prefixedId(Properties JavaDoc attributes) throws XDocletException
90     {
91         String JavaDoc useIdsParamName = getDocletContext().getActiveSubTask().getSubTaskName() + ".useIds";
92         boolean useIds = ((Boolean JavaDoc) getDocletContext().getConfigParam(useIdsParamName)).booleanValue();
93
94         if (useIds == false)
95             return "";
96
97         String JavaDoc prefixName = attributes.getProperty("prefix");
98         String JavaDoc wrapInIdEqualsStr = attributes.getProperty("wrapInIdEquals");
99         boolean wrapInIdEquals = TypeConversionUtil.stringToBoolean(wrapInIdEqualsStr, true);
100
101         if (prefixName == null)
102             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletTagshandlerMessages.ATTRIBUTE_NOT_SET_ERROR, new String JavaDoc[]{"prefix"}));
103
104         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc("");
105
106         if (wrapInIdEquals)
107             sbuf.append("id=\"");
108
109         if (prefixHash.containsKey(prefixName)) {
110             Integer JavaDoc val = (Integer JavaDoc) prefixHash.get(prefixName);
111             Integer JavaDoc valPlusOne = new Integer JavaDoc((val.intValue()) + 1);
112
113             prefixHash.put(prefixName, valPlusOne);
114             sbuf.append(prefixName);
115             sbuf.append('_');
116             sbuf.append(valPlusOne);
117         }
118         else {
119             prefixHash.put(prefixName, new Integer JavaDoc(1));
120             sbuf.append(prefixName);
121             sbuf.append("_1");
122         }
123
124         if (wrapInIdEquals) {
125             sbuf.append('"');
126         }
127
128         return sbuf.toString();
129     }
130
131     /**
132      * Generates an id attribute based on the given tag values. This is used for generating id attribute for XML
133      * elements.
134      *
135      * @param attributes The attributes of the template tag
136      * @return Description of the Returned Value
137      * @exception XDocletException Description of Exception
138      * @doc.tag type="content"
139      * @doc.param name="tagName" optional="false" description="The tag from which the value of the id
140      * is calculated."
141      * @doc.param name="paramNames" optional="false" description="Comma separated list of parameter
142      * names. The list is ordered, preferred param is before another param which is less important. If the param
143      * exists, its value is taken and used as the id value."
144      */

145     public String JavaDoc id(Properties JavaDoc attributes) throws XDocletException
146     {
147         String JavaDoc tagName = attributes.getProperty("tagName");
148         String JavaDoc paramNames = attributes.getProperty("paramNames");
149
150         return getIdByTagValues(tagName, paramNames);
151     }
152 }
153
Popular Tags