KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > xdoclet > WapFacesTagHandler


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.xdoclet;
17
18 import java.util.Properties JavaDoc;
19
20 import xdoclet.XDocletException;
21 import xdoclet.XDocletTagSupport;
22 import xjavadoc.XField;
23
24 /**
25  * @xdoclet.taghandler namespace="WapFaces"
26  * @author <a HREF="mailto:Jiri.Zaloudek@ivancice.cz">Jiri Zaloudek</a> (latest modification by $Author: matzew $)
27  * @version $Revision: 1.1 $ $Date: 2004/12/30 09:37:27 $
28  * $Log: WapFacesTagHandler.java,v $
29  * Revision 1.1 2004/12/30 09:37:27 matzew
30  * added a new RenderKit for WML. Thanks to Jirí Žaloudek
31  *
32  */

33 public class WapFacesTagHandler extends XDocletTagSupport {
34     private int iter = 0;
35     
36     public String JavaDoc componentFamily() throws XDocletException {
37         return (getTagValue(FOR_CLASS, "wapfaces.tag", "componentFamily", null, null, false, false));
38     }
39     
40     public String JavaDoc rendererType() throws XDocletException {
41         return (getTagValue(FOR_CLASS, "wapfaces.tag", "rendererType", null, null, false, false));
42     }
43     
44     public String JavaDoc tagName() throws XDocletException {
45         return (getTagValue(FOR_CLASS, "wapfaces.tag", "tagName", null, null, false, false));
46     }
47
48     public String JavaDoc tagBaseClass() throws XDocletException {
49         return (getTagValue(FOR_CLASS, "wapfaces.tag", "tagBaseClass", null, null, false, false));
50     }
51     
52     public String JavaDoc bodyContent() throws XDocletException {
53         return (getTagValue(FOR_CLASS, "wapfaces.tag", "bodyContent", null, null, false, false));
54     }
55     
56     private String JavaDoc attributeName(){
57         XField field = getCurrentField();
58         return(field.getName());
59     }
60     
61     private String JavaDoc attributeType(){
62         XField field = getCurrentField();
63         return(field.getType().getQualifiedName());
64     }
65     
66     /** Return initial value for attribute.
67      * @return init value, if initValue isn't set reurn null
68      */

69     public String JavaDoc attributeInitValue() throws XDocletException {
70         String JavaDoc value = getTagValue(FOR_FIELD, "wapfaces.attribute", "initValue", null, null, false, false);
71         
72         if (value == null) return(attributeNullValue());
73         return (value);
74     }
75     
76     /** Return the right null value according to type of property*/
77     public String JavaDoc attributeNullValue() throws XDocletException {
78         String JavaDoc type = attributeType();
79         if (type == null) throw new XDocletException("Missing(or wrong) attribute 'type' in property declaration.");
80         if (type.equals("boolean")) return("false");
81         if (type.equals("byte")) return("0");
82         if (type.equals("short")) return("0");
83         if (type.equals("int")) return("0");
84         if (type.equals("long")) return("0");
85         if (type.equals("float")) return("0.0");
86         if (type.equals("double")) return("0.0");
87         if (type.equals("char")) return("\u0000");
88         return("null");
89     }
90     
91     public void ifIsAttributePrimitive(String JavaDoc template) throws XDocletException {
92         if (isAttributePrimitive())
93             generate(template);
94     }
95     
96     public void ifIsNotAttributePrimitive(String JavaDoc template) throws XDocletException {
97         if (!isAttributePrimitive())
98             generate(template);
99     }
100     
101     
102     private boolean isAttributePrimitive() throws XDocletException {
103         String JavaDoc type = attributeType();
104         if (type == null) throw new XDocletException("Missing(or wrong) attribute 'type' in property declaration.");
105         if (type.equals("boolean") || type.equals("byte") || type.equals("short") ||
106         type.equals("int") || type.equals("long") || type.equals("float") ||
107         type.equals("double") || type.equals("char"))
108             return (true);
109         
110         return(false);
111     }
112     
113     /** Return the class name according to primitive type. If field type isn't primitive, return the type. */
114     public String JavaDoc classForType() throws XDocletException {
115         //if (!isAttributePrimitive()) throw new XDocletException("Method 'classForPrimitiveType' can be call only for primitive types. You can test it with method 'ifIsAttributePrimitive'.");
116

117         String JavaDoc type = attributeType();
118         try {
119             if (isAttributePrimitive()){
120                 if (type != null && type.equals("int")) // int -> Integer
121
return("Integer");
122                 else
123                     return (firstLetterToUpperCase(type));
124             }
125             return (type);
126             
127         } catch (Exception JavaDoc ex){
128             throw new XDocletException(ex.getMessage());
129         }
130     }
131     
132     public String JavaDoc isAttributeRequired() throws XDocletException {
133         return(String.valueOf(attributeRequired()));
134     }
135     
136     public void ifIsAttributeRequired(String JavaDoc template) throws XDocletException {
137         if (attributeRequired())
138             generate(template);
139     }
140     
141     private boolean attributeRequired() throws XDocletException {
142         String JavaDoc value = getTagValue(FOR_FIELD, "wapfaces.attribute", "required", null, "false", false, false);
143
144         try {
145             return (stringToBoolean(value));
146         } catch (NumberFormatException JavaDoc ex) {
147             return(false);
148         }
149     }
150     
151     public String JavaDoc isRtExprValue() throws XDocletException {
152         return(String.valueOf(rtExprValue()));
153     }
154     
155     private boolean rtExprValue() throws XDocletException {
156         String JavaDoc value = getTagValue(FOR_FIELD, "wapfaces.attribute", "rtexprvalue", null, "false", false, false);
157
158         try {
159             return (stringToBoolean(value));
160         } catch (NumberFormatException JavaDoc ex) {
161             return(false);
162         }
163     }
164
165     public void ifIsAttributeValueBinding(String JavaDoc template) throws XDocletException {
166         if (isAttributeValueBinding())
167             generate(template);
168     }
169     
170     public void ifIsNotAttributeValueBinding(String JavaDoc template) throws XDocletException {
171         if (!isAttributeValueBinding())
172             generate(template);
173     }
174     
175     private boolean isAttributeValueBinding() throws XDocletException {
176         String JavaDoc value = getTagValue(FOR_FIELD, "wapfaces.attribute", "valueBinding", null, "false", false, false);
177         
178         try {
179             return (stringToBoolean(value));
180         } catch (NumberFormatException JavaDoc ex) {
181             return(false);
182         }
183     }
184     
185     public void ifIsNotAttributeAbstract(String JavaDoc template) throws XDocletException {
186         if (!isAttributeAbstract()) // if is not abstract
187
generate(template);
188     }
189     
190     private boolean isAttributeAbstract() throws XDocletException {
191         String JavaDoc value = getTagValue(FOR_FIELD, "wapfaces.attribute", "abstract", null, "false", false, false);
192         
193         try {
194             return (stringToBoolean(value));
195         } catch (NumberFormatException JavaDoc ex) {
196             return(false);
197         }
198     }
199     
200     public void ifIsNotAttributeInherit(String JavaDoc template) throws XDocletException {
201         if (!isAttributeInherid()) // if is not inherit
202
generate(template);
203     }
204     
205     private boolean isAttributeInherid() throws XDocletException {
206         String JavaDoc value = getTagValue(FOR_FIELD, "wapfaces.attribute", "inherit", null, "false", false, false);
207         
208         try {
209             return (stringToBoolean(value));
210         } catch (NumberFormatException JavaDoc ex) {
211             return(false);
212         }
213     }
214
215     public void ifIsAttributeReplaced(String JavaDoc template) throws XDocletException{
216         if (isAttributeReplaced())
217             generate(template);
218     }
219     
220     public void ifIsNotAttributeReplaced(String JavaDoc template) throws XDocletException{
221         if (!isAttributeReplaced()) // if is not attribute replaced
222
generate(template);
223     }
224     
225     private boolean isAttributeReplaced() throws XDocletException {
226         boolean isEmpty = getReplacedAttributeName() == null || "".endsWith(getReplacedAttributeName());
227         
228         return(!isEmpty);
229     }
230
231     public String JavaDoc getReplacedAttributeName() throws XDocletException {
232         String JavaDoc value = getTagValue(FOR_FIELD, "wapfaces.attribute", "replaceWith", null, null, false, false);
233         
234         return(value);
235     }
236     
237     public String JavaDoc getterMethodName() throws XDocletException {
238         String JavaDoc name = attributeName();
239         
240         try {
241             return (getterPrefix() + firstLetterToUpperCase(name));
242         } catch (Exception JavaDoc ex){
243             throw new XDocletException(ex.getMessage());
244         }
245     }
246     
247     /** returns the attribute name with first letter in upper case */
248     public String JavaDoc firstLetterToUpperCaseAttributeName() throws XDocletException {
249         String JavaDoc name = attributeName();
250         
251         try {
252             return (firstLetterToUpperCase(name));
253         } catch (Exception JavaDoc ex){
254             throw new XDocletException(ex.getMessage());
255         }
256     }
257     
258     public String JavaDoc setterMethodName() throws XDocletException {
259         String JavaDoc name = attributeName();
260         try {
261             return ("set" + firstLetterToUpperCase(name));
262         } catch (Exception JavaDoc ex){
263             throw new XDocletException(ex.getMessage());
264         }
265     }
266     
267     public void iterateValue() {
268         iter = iter + 1;
269     }
270     
271     public String JavaDoc iteratorValue() {
272         return (String.valueOf(iter));
273     }
274     
275     public void setIterator(Properties JavaDoc prop) throws XDocletException {
276         String JavaDoc value = prop.getProperty("value");
277         try {
278             iter = Integer.parseInt(value);
279         } catch (NumberFormatException JavaDoc ex) {
280             throw new XDocletException("You have to set property 'value' in method setIterator. 'Value' is a integer number.");
281         }
282     }
283     
284     private String JavaDoc getterPrefix(){
285         if ("boolean".equals(attributeType()))
286             return("is");
287         return("get");
288     }
289     
290     private String JavaDoc firstLetterToUpperCase(String JavaDoc str) throws Exception JavaDoc {
291         if (str != null && str.length() > 0){
292             str = str.substring(0, 1).toUpperCase() + str.substring(1);
293             return (str);
294         }
295         else throw new Exception JavaDoc("Attribut name error. Name must have at least one character.");
296     }
297     
298     private boolean stringToBoolean(String JavaDoc str){
299         Boolean JavaDoc bool = Boolean.valueOf(str);
300         return(bool.booleanValue());
301     }
302 }
303
Popular Tags