1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package net.sf.maventaglib.checker;
25
26 import java.util.Set;
27 import java.util.TreeSet;
28
29 import org.apache.commons.lang3.StringUtils;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Node;
32 import org.w3c.dom.NodeList;
33
34 import net.sf.maventaglib.util.XmlHelper;
35
36
37
38
39
40
41
42 public final class TldParser
43 {
44
45
46
47
48 private TldParser()
49 {
50
51 }
52
53
54
55
56
57
58
59 public static Tld parse(Document tldDoc, String tldName)
60 {
61
62 Tld tld = new Tld();
63 tld.setFilename(tldName);
64 Set<Tag> tags = new TreeSet<>();
65 Set<ELFunction> functions = new TreeSet<>();
66 Set<TagFile> tagfiles = new TreeSet<>();
67
68 NodeList tagList = tldDoc.getElementsByTagName("taglib").item(0).getChildNodes();
69
70 for (int i = 0; i < tagList.getLength(); i++)
71 {
72 Node tagNode = tagList.item(i);
73
74 if ("shortname".equals(tagNode.getNodeName()) || "short-name".equals(tagNode.getNodeName()))
75 {
76 Node child = tagNode.getFirstChild();
77 if (child != null)
78 {
79 tld.setShortname(child.getNodeValue());
80 }
81 }
82 else if ("display-name".equals(tagNode.getNodeName()))
83 {
84 Node child = tagNode.getFirstChild();
85 if (child != null)
86 {
87 tld.setName(child.getNodeValue());
88 }
89 }
90 else if ("info".equals(tagNode.getNodeName()) || "description".equals(tagNode.getNodeName()))
91 {
92 tld.setInfo(XmlHelper.getTextContent(tagNode));
93 }
94 else if ("tlib-version".equals(tagNode.getNodeName()) || "tlibversion".equals(tagNode.getNodeName()))
95 {
96 Node child = tagNode.getFirstChild();
97 if (child != null)
98 {
99 tld.setTlibversion(child.getNodeValue());
100 }
101 }
102 else if ("uri".equals(tagNode.getNodeName()))
103 {
104 Node child = tagNode.getFirstChild();
105 if (child != null)
106 {
107 tld.setUri(child.getNodeValue());
108 }
109 }
110 else if ("tag".equals(tagNode.getNodeName()))
111 {
112 Tag tag = parseTag(tagNode);
113 tags.add(tag);
114 }
115 else if ("function".equals(tagNode.getNodeName()))
116 {
117 ELFunction tag = parseFunction(tagNode);
118 functions.add(tag);
119 }
120 else if ("tag-file".equals(tagNode.getNodeName()))
121 {
122 TagFile tag = parseTagFile(tagNode);
123 tagfiles.add(tag);
124 }
125
126 tld.setTags(tags.toArray(new Tag[tags.size()]));
127 tld.setFunctions(functions.toArray(new ELFunction[functions.size()]));
128 tld.setTagfiles(tagfiles.toArray(new TagFile[tagfiles.size()]));
129 }
130
131 return tld;
132 }
133
134
135
136
137
138 private static TagFile parseTagFile(Node tagNode)
139 {
140 TagFile tag = new TagFile();
141
142 NodeList tagAttributes = tagNode.getChildNodes();
143
144 for (int k = 0; k < tagAttributes.getLength(); k++)
145 {
146 Node tagAttribute = tagAttributes.item(k);
147 String nodeName = tagAttribute.getNodeName();
148
149 if ("name".equals(nodeName))
150 {
151
152 tag.setName(tagAttribute.getFirstChild().getNodeValue());
153 }
154 else if ("description".equals(nodeName))
155 {
156 tag.setDescription(XmlHelper.getTextContent(tagAttribute));
157 }
158 else if ("path".equals(nodeName))
159 {
160 tag.setPath(tagAttribute.getFirstChild().getNodeValue());
161 }
162 else if ("example".equals(nodeName))
163 {
164 tag.setExample(XmlHelper.getTextContent(tagAttribute));
165 }
166
167 }
168
169 tag.setDeprecated(StringUtils.contains(tag.getDescription(), "@deprecated"));
170
171 return tag;
172 }
173
174
175
176
177
178 private static ELFunction parseFunction(Node tagNode)
179 {
180 ELFunction tag = new ELFunction();
181 NodeList tagAttributes = tagNode.getChildNodes();
182
183 for (int k = 0; k < tagAttributes.getLength(); k++)
184 {
185 Node tagAttribute = tagAttributes.item(k);
186 String nodeName = tagAttribute.getNodeName();
187
188 if ("name".equals(nodeName))
189 {
190 tag.setName(tagAttribute.getFirstChild().getNodeValue());
191 }
192 else if ("description".equals(nodeName))
193 {
194 tag.setDescription(XmlHelper.getTextContent(tagAttribute));
195 }
196 else if ("example".equals(nodeName))
197 {
198 tag.setExample(XmlHelper.getTextContent(tagAttribute));
199 }
200 else if ("function-class".equals(nodeName))
201 {
202 tag.setFunctionClass(StringUtils.trim(XmlHelper.getTextContent(tagAttribute)));
203 }
204 else if ("function-signature".equals(nodeName))
205 {
206 String signature = XmlHelper.getTextContent(tagAttribute);
207 tag.setFunctionSignature(signature);
208 tag.setParameters(StringUtils.substringBetween(signature, "(", ")"));
209 }
210 }
211
212 tag.setDeprecated(StringUtils.contains(tag.getDescription(), "@deprecated"));
213
214 return tag;
215 }
216
217
218
219
220
221
222 private static Tag parseTag(Node tagNode)
223 {
224 Tag tag = new Tag();
225 Set<TagAttribute> attributes = new TreeSet<>();
226 Set<TagVariable> variables = new TreeSet<>();
227 NodeList tagAttributes = tagNode.getChildNodes();
228
229 for (int k = 0; k < tagAttributes.getLength(); k++)
230 {
231 Node tagAttribute = tagAttributes.item(k);
232 String nodeName = tagAttribute.getNodeName();
233
234 if ("name".equals(nodeName))
235 {
236
237 tag.setName(tagAttribute.getFirstChild().getNodeValue());
238 }
239 else if ("description".equals(nodeName) || "info".equals(nodeName))
240 {
241 tag.setDescription(XmlHelper.getTextContent(tagAttribute));
242 }
243 else if ("tag-class".equals(nodeName) || "tagclass".equals(nodeName))
244 {
245 tag.setTagClass(StringUtils.trim(tagAttribute.getFirstChild().getNodeValue()));
246 }
247 else if ("body-content".equals(nodeName) || "bodycontent".equals(nodeName))
248 {
249 tag.setBodycontent(tagAttribute.getFirstChild().getNodeValue());
250 }
251 else if ("example".equals(nodeName))
252 {
253 tag.setExample(XmlHelper.getTextContent(tagAttribute));
254 }
255 else if ("tei-class".equals(nodeName) || "teiclass".equals(nodeName))
256 {
257
258 tag.setTeiClass(StringUtils.trim(tagAttribute.getFirstChild().getNodeValue()));
259 }
260 else if ("attribute".equals(nodeName))
261 {
262 TagAttribute attribute = parseTagAttribute(tagAttribute);
263 attributes.add(attribute);
264 }
265 else if ("variable".equals(nodeName))
266 {
267 TagVariable variable = parseTagVariable(tagAttribute);
268 variables.add(variable);
269 }
270 tag.setAttributes(attributes.toArray(new TagAttribute[attributes.size()]));
271 tag.setVariables(variables.toArray(new TagVariable[variables.size()]));
272 }
273
274 tag.setDeprecated(StringUtils.contains(tag.getDescription(), "@deprecated"));
275
276 return tag;
277 }
278
279
280
281
282
283
284 private static TagAttribute parseTagAttribute(Node tagAttribute)
285 {
286 TagAttribute attribute = new TagAttribute();
287
288 NodeList attributeParams = tagAttribute.getChildNodes();
289 for (int z = 0; z < attributeParams.getLength(); z++)
290 {
291 Node param = attributeParams.item(z);
292 if (param.getNodeType() != Node.TEXT_NODE && param.hasChildNodes())
293 {
294 if ("name".equals(param.getNodeName()))
295 {
296 attribute.setName(param.getFirstChild().getNodeValue());
297 }
298 else if ("type".equals(param.getNodeName()))
299 {
300 attribute.setType(StringUtils.trim(param.getFirstChild().getNodeValue()));
301 }
302 else if ("description".equals(param.getNodeName()))
303 {
304 attribute.setDescription(XmlHelper.getTextContent(param));
305 }
306 else if ("required".equals(param.getNodeName()))
307 {
308 attribute.setRequired(StringUtils.contains(StringUtils.lowerCase(param
309 .getFirstChild()
310 .getNodeValue()), "true"));
311 }
312 else if ("rtexprvalue".equals(param.getNodeName()))
313 {
314 attribute.setRtexprvalue(StringUtils.contains(StringUtils.lowerCase(param
315 .getFirstChild()
316 .getNodeValue()), "true"));
317 }
318 }
319 }
320
321 attribute.setDeprecated(StringUtils.contains(attribute.getDescription(), "@deprecated"));
322
323 return attribute;
324 }
325
326
327
328
329
330
331 private static TagVariable parseTagVariable(Node node)
332 {
333 TagVariable variable = new TagVariable();
334
335 NodeList attributeParams = node.getChildNodes();
336 for (int z = 0; z < attributeParams.getLength(); z++)
337 {
338 Node param = attributeParams.item(z);
339 if (param.getNodeType() != Node.TEXT_NODE && param.hasChildNodes())
340 {
341 if ("name-given".equals(param.getNodeName()))
342 {
343 variable.setNameGiven(param.getFirstChild().getNodeValue());
344 }
345 if ("name-from-attribute".equals(param.getNodeName()))
346 {
347 variable.setNameFromAttribute(param.getFirstChild().getNodeValue());
348 }
349 else if ("variable-class".equals(param.getNodeName()))
350 {
351 variable.setType(param.getFirstChild().getNodeValue());
352 }
353 else if ("scope".equals(param.getNodeName()))
354 {
355 variable.setScope(param.getFirstChild().getNodeValue());
356 }
357 else if ("description".equals(param.getNodeName()) || "info".equals(param.getNodeName()))
358 {
359 variable.setDescription(XmlHelper.getTextContent(param));
360 }
361
362 }
363 }
364
365 variable.setDeprecated(StringUtils.contains(variable.getDescription(), "@deprecated"));
366
367 return variable;
368 }
369
370 }