KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > build > TldUtils


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

18 package net.sf.uitags.build;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.LinkedHashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.xml.parsers.DocumentBuilder JavaDoc;
29 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.NodeList JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36
37 public final class TldUtils {
38   static Document JavaDoc getDocument(String JavaDoc filename) {
39     DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
40     factory.setCoalescing(true);
41     factory.setIgnoringElementContentWhitespace(true);
42
43     try {
44       DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
45       try {
46         return builder.parse(new File JavaDoc(filename));
47       }
48       catch (SAXException JavaDoc e) {
49         throw new RuntimeException JavaDoc(e);
50       }
51       catch (IOException JavaDoc e) {
52         throw new RuntimeException JavaDoc(e);
53       }
54     }
55     catch (ParserConfigurationException JavaDoc e) {
56       throw new RuntimeException JavaDoc(e);
57     }
58   }
59
60   private static Node JavaDoc[] findDirectTagsByName(Node JavaDoc rootNode, String JavaDoc tagName) {
61     List JavaDoc resultNodes = new ArrayList JavaDoc();
62     NodeList JavaDoc childNodes = rootNode.getChildNodes();
63     for (int i = 0; i < childNodes.getLength(); ++i) {
64       Node JavaDoc currentNode = childNodes.item(i);
65       if (currentNode.getNodeName().equals(tagName)) {
66         resultNodes.add(currentNode);
67       }
68     }
69     return (Node JavaDoc[]) resultNodes.toArray(new Node JavaDoc[resultNodes.size()]);
70   }
71
72   static Map JavaDoc extractTags(Document JavaDoc tldDocument, Document JavaDoc elTldDocument) {
73     Map JavaDoc tags = new LinkedHashMap JavaDoc();
74     NodeList JavaDoc tagNodes = tldDocument.getElementsByTagName(Tag.TAGNAME_TAG);
75     for (int i = 0; i < tagNodes.getLength(); ++i) {
76       Tag tag = constructTag(tagNodes.item(i));
77       tags.put(tag.getName(), tag);
78     }
79
80     tagNodes = elTldDocument.getElementsByTagName(Tag.TAGNAME_TAG);
81     for (int i = 0; i < tagNodes.getLength(); ++i) {
82       Node JavaDoc node = tagNodes.item(i);
83       Tag tag = (Tag) tags.get(getTagName(node));
84       tag.setElHandlerName(getTagClassName(node));
85     }
86
87     return tags;
88   }
89
90   static Map JavaDoc extractTags(Document JavaDoc tldDocument) {
91     Map JavaDoc tags = new LinkedHashMap JavaDoc();
92     NodeList JavaDoc tagNodes = tldDocument.getElementsByTagName(Tag.TAGNAME_TAG);
93     for (int i = 0; i < tagNodes.getLength(); ++i) {
94       Tag tag = constructTag(tagNodes.item(i));
95       tags.put(tag.getName(), tag);
96     }
97
98     return tags;
99   }
100
101   private static Tag constructTag(Node JavaDoc node) {
102     Tag tag = new Tag(getTagName(node));
103     tag.setHandlerName(getTagClassName(node));
104
105     Node JavaDoc[] attributeNodes = findDirectTagsByName(node, Tag.TAGNAME_ATTRIBUTE);
106     for (int i = 0; i < attributeNodes.length; ++i) {
107       tag.addAttribute(constructAttribute(attributeNodes[i]));
108     }
109
110     return tag;
111   }
112
113   private static String JavaDoc getValueOf(String JavaDoc tagName, Node JavaDoc rootNode) {
114     return findDirectTagsByName(rootNode,
115         tagName)[0].getFirstChild().getNodeValue();
116   }
117
118   private static String JavaDoc getTagName(Node JavaDoc tagNode) {
119     return getValueOf(Tag.TAGNAME_NAME, tagNode);
120   }
121
122   private static String JavaDoc getTagClassName(Node JavaDoc tagNode) {
123     return getValueOf(Tag.TAGNAME_HANDLER, tagNode);
124   }
125
126   private static Attribute constructAttribute(Node JavaDoc node) {
127     Attribute attribute = new Attribute(getAttributeName(node));
128     attribute.setType(getAttributeType(node));
129     return attribute;
130   }
131
132   private static String JavaDoc getAttributeName(Node JavaDoc tagNode) {
133     return getValueOf(Tag.TAGNAME_NAME, tagNode);
134   }
135
136   private static String JavaDoc getAttributeType(Node JavaDoc tagNode) {
137     try {
138       return getValueOf(Tag.TAGNAME_TYPE, tagNode);
139     }
140     catch (ArrayIndexOutOfBoundsException JavaDoc e) {
141       // return the default value if this optional property was
142
// not specified
143
return Attribute.DEFAULT_TYPE;
144     }
145   }
146
147
148   ///////////////////////////////////
149
////////// Inner Classes //////////
150
///////////////////////////////////
151

152   // made public to allow access from VM template
153
public static class Attribute {
154     private static final String JavaDoc DEFAULT_TYPE = "java.lang.String";
155     private static final String JavaDoc GENERIC_TYPE = "java.lang.Object";
156
157     private String JavaDoc name;
158     private String JavaDoc type;
159     private static Map JavaDoc predefinedEvaluators;
160
161     static {
162       predefinedEvaluators = new HashMap JavaDoc();
163       predefinedEvaluators.put("int", "toIntegerValue");
164       predefinedEvaluators.put("boolean", "toBooleanValue");
165       predefinedEvaluators.put(DEFAULT_TYPE, "toString");
166     }
167
168     private Attribute(String JavaDoc name) {
169       this.name = name;
170     }
171
172     private void setType(String JavaDoc type) {
173       this.type = type;
174     }
175
176     // the following are made public to allow access from VM template
177

178     public String JavaDoc getName() {
179       return this.name;
180     }
181
182     public String JavaDoc getHandlerName() {
183       return "set" + Character.toUpperCase(
184           this.name.charAt(0)) + this.name.substring(1);
185     }
186
187     public String JavaDoc getEvaluatorName() {
188       String JavaDoc evaluator = (String JavaDoc) predefinedEvaluators.get(this.type);
189       if (evaluator == null) {
190         evaluator = "toObject";
191       }
192       return evaluator;
193     }
194
195     public String JavaDoc getCastTypeName() {
196       if (GENERIC_TYPE.equals(getCustomClassName())) {
197         return null;
198       }
199       return getCustomClassName();
200     }
201
202     public String JavaDoc getCustomClassName() {
203       if (predefinedEvaluators.containsKey(this.type)) {
204         return null;
205       }
206       return this.type;
207     }
208   }
209
210   static class Tag {
211     private static final String JavaDoc TAGNAME_TAG = "tag";
212     private static final String JavaDoc TAGNAME_NAME = "name";
213     private static final String JavaDoc TAGNAME_TYPE = "type";
214     private static final String JavaDoc TAGNAME_HANDLER = "tag-class";
215     private static final String JavaDoc TAGNAME_ATTRIBUTE = "attribute";
216
217     private String JavaDoc name;
218     private String JavaDoc tagHandlerName;
219     private String JavaDoc elTagHandlerName;
220     private List JavaDoc attributeList;
221
222     private Tag(String JavaDoc name) {
223       this.name = name;
224       this.attributeList = new ArrayList JavaDoc();
225     }
226
227     private void setHandlerName(String JavaDoc name) {
228       this.tagHandlerName = name;
229     }
230
231     private void setElHandlerName(String JavaDoc name) {
232       this.elTagHandlerName = name;
233     }
234
235     private void addAttribute(Attribute attribute) {
236       this.attributeList.add(attribute);
237     }
238
239     String JavaDoc getName() {
240       return this.name;
241     }
242
243     List JavaDoc getAttributes() {
244       return this.attributeList;
245     }
246
247     String JavaDoc getFullyQualifiedHandlerName() {
248       return this.tagHandlerName;
249     }
250
251     String JavaDoc getFullyQualifiedElHandlerName() {
252       return this.elTagHandlerName;
253     }
254
255     String JavaDoc getHandlerPath() {
256       return this.tagHandlerName.replace('.', '/');
257     }
258
259     String JavaDoc getElHandlerPath() {
260       return this.elTagHandlerName.replace('.', '/');
261     }
262
263     String JavaDoc getHandlerNameFraction() {
264       return getClassNameFraction(this.tagHandlerName);
265     }
266
267     String JavaDoc getElHandlerNameFraction() {
268       return getClassNameFraction(this.elTagHandlerName);
269     }
270
271     String JavaDoc getPackageNameFraction() {
272       return getPackageNameFraction(this.tagHandlerName);
273     }
274
275     String JavaDoc getElPackageNameFraction() {
276       return getPackageNameFraction(this.elTagHandlerName);
277     }
278
279     private String JavaDoc getClassNameFraction(String JavaDoc classFullname) {
280       int startIndex = classFullname.lastIndexOf('.') + 1;
281       return classFullname.substring(startIndex);
282     }
283
284     private String JavaDoc getPackageNameFraction(String JavaDoc classFullname) {
285       int endIndex = classFullname.lastIndexOf('.') + 1;
286       return classFullname.substring(0, endIndex - 1);
287     }
288   }
289 }
290
Popular Tags