KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > caucho > ResinWebTagsHandler


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

5 package xdoclet.modules.caucho;
6
7 import java.util.Properties JavaDoc;
8 import xjavadoc.XProgramElement;
9 import xjavadoc.XTag;
10 import xdoclet.XDocletException;
11 import xdoclet.XDocletTagSupport;
12
13 /**
14  * Template tags handler for Resin web.xml generation.
15  *
16  * @author Yoritaka Sakakura (yori@teardrop.org)
17  * @created June 5, 2002
18  * @xdoclet.taghandler namespace="Resin"
19  */

20 public class ResinWebTagsHandler extends XDocletTagSupport
21 {
22     private final static int WAIT_NAME = 0;
23     private final static int IN_NAME = 1;
24     private final static int WAIT_VALUE = 2;
25     private final static int IN_VALUE = 3;
26
27     private String JavaDoc name;
28     private String JavaDoc value;
29
30
31     private static boolean isDelimiter(char c)
32     {
33         return c == '"' || c == '=';
34     }
35
36     private static boolean isSpace(char c)
37     {
38         return Character.isSpaceChar(c);
39     }
40
41     private static boolean isSpecial(char c)
42     {
43         return isDelimiter(c) || isSpace(c);
44     }
45
46
47     /**
48      * Iterates over all parameters of the current javadoc tag.
49      *
50      * @param template
51      * @exception XDocletException
52      * @doc.tag type="block"
53      */

54     public void forAllCurrentTagParams(String JavaDoc template)
55          throws XDocletException
56     {
57         clear();
58
59         int state = WAIT_NAME;
60         final StringBuffer JavaDoc name = new StringBuffer JavaDoc();
61         final StringBuffer JavaDoc value = new StringBuffer JavaDoc();
62
63         final String JavaDoc text = getCurrentTag().getValue();
64
65         for (int i = 0; i < text.length(); i++) {
66             char c = text.charAt(i);
67
68             switch (state) {
69             case WAIT_NAME:
70                 if (!isSpecial(c)) {
71                     name.append(c);
72                     state = IN_NAME;
73                 }
74                 break;
75             case IN_NAME:
76                 if (c == '=')
77                     state = WAIT_VALUE;
78                 else if (isSpace(c)) {
79                     // peek ahead
80
int index = i + 1;
81
82                     while (index < text.length()) {
83                         char c2 = text.charAt(index);
84
85                         if (isSpace(c2))
86                             index++;
87                         else if (c2 == '=') {
88                             i = index;
89                             state = WAIT_VALUE;
90                             break;
91                         }
92                         else {
93                             name.delete(0, name.length());
94                             state = WAIT_NAME;
95                             i = index - 1;
96                             break;
97                         }
98                     }
99                 }
100                 else
101                     name.append(c);
102                 break;
103             case WAIT_VALUE:
104                 if (c == '"')
105                     state = IN_VALUE;
106                 else if (!isSpace(c)) {
107                     name.delete(0, name.length());
108                     state = WAIT_NAME;
109                 }
110                 break;
111             case IN_VALUE:
112                 if (c == '"') {
113                     // empty values ok
114
this.name = name.toString();
115                     this.value = value.toString();
116                     name.delete(0, name.length());
117                     value.delete(0, value.length());
118                     state = WAIT_NAME;
119                     generate(template);
120                     clear();
121                 }
122                 else
123                     value.append(c);
124             }
125         }
126     }
127
128     /**
129      * Writes the current javadoc parameter as an xml element of the form: <PRE>
130      * <parameter-name>
131      *
132      * parameter-value</parameter-name> </PRE> If the parameter maps to a non-empty value in the attributes, value is
133      * used as the xml element name instead of the javadoc parameter name.
134      *
135      * @param props
136      * @return
137      * @exception XDocletException
138      * @doc.tag type="content"
139      */

140     public String JavaDoc parameterAsElement(Properties JavaDoc props)
141          throws XDocletException
142     {
143         String JavaDoc name = props.getProperty(parameterName());
144
145         if (name == null || name.length() < 1)
146             name = parameterName();
147
148         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
149
150         buffer.append('<');
151         buffer.append(name);
152         buffer.append('>');
153         buffer.append(parameterValue());
154         buffer.append("</");
155         buffer.append(name);
156         buffer.append('>');
157
158         return buffer.toString();
159     }
160
161     /**
162      * Writes the current javadoc parameter as an xml element of the form: <PRE>
163      * <init-param parameter-name="parameter-value"/>
164      * </PRE>
165      *
166      * @return
167      * @exception XDocletException
168      * @doc.tag type="content"
169      */

170     public String JavaDoc parameterAsInitParam()
171          throws XDocletException
172     {
173         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
174
175         buffer.append("<init-param ");
176         buffer.append(parameterName());
177         buffer.append("=\"");
178         buffer.append(parameterValue());
179         buffer.append("\"/>");
180
181         return buffer.toString();
182     }
183
184     /**
185      * Writes the current javadoc parameter as an element or an init-param, depending on the tag attributes; if the
186      * parameter name is contained in the attributes, the 'element' form is used, else the 'init-parm' form.
187      *
188      * @param props
189      * @return
190      * @exception XDocletException
191      * @see #parameterAsElement(java.util.Properties)
192      * @see #parameterAsInitParam()
193      * @doc.tag type="content"
194      */

195     public String JavaDoc parameterAsXml(Properties JavaDoc props)
196          throws XDocletException
197     {
198         if (props.containsKey(parameterName()))
199             return parameterAsElement(props);
200         else
201             return parameterAsInitParam();
202     }
203
204     /**
205      * Returns the current javadoc parameter name.
206      *
207      * @return
208      * @exception XDocletException
209      * @doc.tag type="content"
210      */

211     public String JavaDoc parameterName()
212          throws XDocletException
213     {
214         if (name == null)
215             throw L.error(L.NO_CURRENT_PARAMETER);
216         else
217             return name;
218     }
219
220     /**
221      * Returns the current javadoc parameter value.
222      *
223      * @return
224      * @exception XDocletException
225      * @doc.tag type="content"
226      */

227     public String JavaDoc parameterValue()
228          throws XDocletException
229     {
230         if (value == null)
231             throw L.error(L.NO_CURRENT_PARAMETER);
232         else
233             return value;
234     }
235
236     /**
237      * Writes an xml comment indicating the current method or class name.
238      *
239      * @return
240      * @exception XDocletException
241      * @doc.tag type="content"
242      */

243     public String JavaDoc sourceComment()
244          throws XDocletException
245     {
246         XProgramElement doc = getCurrentMethod();
247
248         if (doc == null)
249             doc = getCurrentClass();
250         if (doc == null)
251             return "";
252         else
253             return "<!-- " + L.l(L.GENERATED_FROM, new String JavaDoc[]{doc.getName()}) + " -->";
254     }
255
256     private XTag getCurrentTag()
257          throws XDocletException
258     {
259         XTag tag = getCurrentMethodTag();
260
261         if (tag == null)
262             tag = getCurrentClassTag();
263         if (tag == null)
264             throw L.error(L.NO_CURRENT_JAVADOC_TAG);
265         return tag;
266     }
267
268
269     private void clear()
270     {
271         name = null;
272         value = null;
273     }
274 }
275
Popular Tags