1 | |
package org.codehaus.mojo.jaxb2.schemageneration.postprocessing.javadoc; |
2 | |
|
3 | |
import org.codehaus.mojo.jaxb2.schemageneration.postprocessing.javadoc.location.ClassLocation; |
4 | |
import org.codehaus.mojo.jaxb2.schemageneration.postprocessing.javadoc.location.FieldLocation; |
5 | |
import org.codehaus.mojo.jaxb2.schemageneration.postprocessing.javadoc.location.MethodLocation; |
6 | |
import org.w3c.dom.CDATASection; |
7 | |
import org.w3c.dom.Document; |
8 | |
import org.w3c.dom.Element; |
9 | |
import org.w3c.dom.NamedNodeMap; |
10 | |
import org.w3c.dom.Node; |
11 | |
|
12 | |
import javax.xml.XMLConstants; |
13 | |
import java.util.ArrayList; |
14 | |
import java.util.Arrays; |
15 | |
import java.util.Collections; |
16 | |
import java.util.List; |
17 | |
import java.util.ListIterator; |
18 | |
import java.util.Set; |
19 | |
import java.util.SortedMap; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public final class DomHelper { |
28 | |
|
29 | |
private static final String NAME_ATTRIBUTE = "name"; |
30 | |
private static final String VALUE_ATTRIBUTE = "value"; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public static final String ANNOTATION_ELEMENT_NAME = "annotation"; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public static final String DOCUMENTATION_ELEMENT_NAME = "documentation"; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public static final String XSD_SCHEMA_NAMESPACE_PREFIX = "xs"; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 1 | public static final List<String> CLASS_FIELD_METHOD_ELEMENT_NAMES = Arrays.asList("element", "attribute"); |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 1 | public static final List<String> ENUMERATION_FIELD_METHOD_ELEMENT_NAMES = Collections.singletonList("enumeration"); |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | 0 | private DomHelper() { |
64 | 0 | } |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public static String getNameAttribute(final Node aNode) { |
73 | 718 | return getNamedAttribute(aNode, NAME_ATTRIBUTE); |
74 | |
} |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public static String getValueAttribute(final Node aNode) { |
83 | 174 | return getNamedAttribute(aNode, VALUE_ATTRIBUTE); |
84 | |
} |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public static boolean isNamedElement(final Node aNode) { |
93 | |
|
94 | 176 | final boolean isElementNode = aNode != null && aNode.getNodeType() == Node.ELEMENT_NODE; |
95 | |
|
96 | 352 | return isElementNode |
97 | 89 | && getNamedAttribute(aNode, NAME_ATTRIBUTE) != null |
98 | 27 | && !getNamedAttribute(aNode, NAME_ATTRIBUTE).isEmpty(); |
99 | |
} |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public static String getElementTagName(final Node aNode) { |
108 | |
|
109 | 0 | if (aNode != null && aNode.getNodeType() == Node.ELEMENT_NODE) { |
110 | |
|
111 | 0 | final Element theElement = (Element) aNode; |
112 | 0 | return theElement.getTagName(); |
113 | |
} |
114 | |
|
115 | |
|
116 | 0 | return null; |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
public static void addXmlDocumentAnnotationTo(final Node aNode, final String formattedDocumentation) { |
135 | |
|
136 | 35 | if (aNode != null && formattedDocumentation != null && !formattedDocumentation.isEmpty()) { |
137 | |
|
138 | |
|
139 | 33 | final Document doc = aNode.getOwnerDocument(); |
140 | 33 | final Element annotation = doc.createElementNS( |
141 | |
XMLConstants.W3C_XML_SCHEMA_NS_URI, ANNOTATION_ELEMENT_NAME); |
142 | 33 | final Element docElement = doc.createElementNS( |
143 | |
XMLConstants.W3C_XML_SCHEMA_NS_URI, DOCUMENTATION_ELEMENT_NAME); |
144 | 33 | final CDATASection xsdDocumentation = doc.createCDATASection(formattedDocumentation); |
145 | |
|
146 | |
|
147 | 33 | annotation.setPrefix(XSD_SCHEMA_NAMESPACE_PREFIX); |
148 | 33 | docElement.setPrefix(XSD_SCHEMA_NAMESPACE_PREFIX); |
149 | |
|
150 | |
|
151 | 33 | annotation.appendChild(docElement); |
152 | 33 | final Node firstChildOfCurrentNode = aNode.getFirstChild(); |
153 | 33 | if (firstChildOfCurrentNode == null) { |
154 | 17 | aNode.appendChild(annotation); |
155 | |
} else { |
156 | 16 | aNode.insertBefore(annotation, firstChildOfCurrentNode); |
157 | |
} |
158 | |
|
159 | |
|
160 | 33 | docElement.appendChild(xsdDocumentation); |
161 | |
} |
162 | 35 | } |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
public static String getXPathFor(final Node aNode) { |
171 | |
|
172 | 3 | List<String> nodeNameList = new ArrayList<String>(); |
173 | |
|
174 | 18 | for (Node current = aNode; current != null; current = current.getParentNode()) { |
175 | |
|
176 | 15 | final String currentNodeName = current.getNodeName(); |
177 | 15 | final String nameAttribute = DomHelper.getNameAttribute(current); |
178 | |
|
179 | 15 | if (currentNodeName.toLowerCase().endsWith("enumeration")) { |
180 | |
|
181 | |
|
182 | 3 | nodeNameList.add(currentNodeName + "[@value='" + getValueAttribute(current) + "']"); |
183 | |
|
184 | 12 | } else if (nameAttribute == null) { |
185 | |
|
186 | |
|
187 | 9 | nodeNameList.add(current.getNodeName()); |
188 | |
|
189 | |
} else { |
190 | |
|
191 | |
|
192 | 3 | nodeNameList.add(current.getNodeName() + "[@name='" + nameAttribute + "']"); |
193 | |
} |
194 | |
} |
195 | |
|
196 | 3 | StringBuilder builder = new StringBuilder(); |
197 | 3 | for (ListIterator<String> it = nodeNameList.listIterator(nodeNameList.size()); it.hasPrevious(); ) { |
198 | 15 | builder.append(it.previous()); |
199 | 15 | if (it.hasPrevious()) { |
200 | 12 | builder.append("/"); |
201 | |
} |
202 | |
} |
203 | |
|
204 | 3 | return builder.toString(); |
205 | |
} |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
public static ClassLocation getClassLocation(final Node aNode, final Set<ClassLocation> classLocations) { |
215 | |
|
216 | |
|
217 | 52 | if (aNode != null) { |
218 | |
|
219 | |
|
220 | 52 | final String nodeLocalName = aNode.getLocalName(); |
221 | 52 | final boolean acceptableType = "complexType".equalsIgnoreCase(nodeLocalName) |
222 | 42 | || "simpleType".equalsIgnoreCase(nodeLocalName); |
223 | |
|
224 | 52 | if (acceptableType) { |
225 | |
|
226 | 18 | final String nodeClassName = DomHelper.getNameAttribute(aNode); |
227 | 18 | for (ClassLocation current : classLocations) { |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | 30 | final String effectiveClassName = current.getAnnotationRenamedTo() == null |
233 | 30 | ? current.getClassName() |
234 | 0 | : current.getAnnotationRenamedTo(); |
235 | 30 | if (effectiveClassName.equalsIgnoreCase(nodeClassName)) { |
236 | 18 | return current; |
237 | |
} |
238 | 12 | } |
239 | |
} |
240 | |
} |
241 | |
|
242 | |
|
243 | 34 | return null; |
244 | |
} |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
public static MethodLocation getMethodLocation(final Node aNode, final Set<MethodLocation> methodLocations) { |
254 | |
|
255 | 28 | MethodLocation toReturn = null; |
256 | |
|
257 | 28 | if (aNode != null && CLASS_FIELD_METHOD_ELEMENT_NAMES.contains(aNode.getLocalName().toLowerCase())) { |
258 | |
|
259 | 42 | final MethodLocation validLocation = getFieldOrMethodLocationIfValid(aNode, |
260 | 21 | getContainingClassOrNull(aNode), |
261 | |
methodLocations); |
262 | |
|
263 | |
|
264 | 21 | if (validLocation != null |
265 | 2 | && MethodLocation.NO_PARAMETERS.equalsIgnoreCase(validLocation.getParametersAsString())) { |
266 | 2 | toReturn = validLocation; |
267 | |
} |
268 | |
} |
269 | |
|
270 | |
|
271 | 28 | return toReturn; |
272 | |
} |
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
public static FieldLocation getFieldLocation(final Node aNode, final Set<FieldLocation> fieldLocations) { |
282 | |
|
283 | 50 | FieldLocation toReturn = null; |
284 | |
|
285 | 50 | if (aNode != null) { |
286 | |
|
287 | 50 | if (CLASS_FIELD_METHOD_ELEMENT_NAMES.contains(aNode.getLocalName().toLowerCase())) { |
288 | |
|
289 | |
|
290 | 29 | toReturn = getFieldOrMethodLocationIfValid(aNode, getContainingClassOrNull(aNode), fieldLocations); |
291 | 21 | } else if (ENUMERATION_FIELD_METHOD_ELEMENT_NAMES.contains(aNode.getLocalName().toLowerCase())) { |
292 | |
|
293 | |
|
294 | 14 | toReturn = getFieldOrMethodLocationIfValid(aNode, getContainingClassOrNull(aNode), fieldLocations); |
295 | |
} |
296 | |
} |
297 | |
|
298 | |
|
299 | 50 | return toReturn; |
300 | |
} |
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
public static <T extends FieldLocation> T getFieldOrMethodLocationIfValid( |
313 | |
final Node aNode, |
314 | |
final Node containingClassNode, |
315 | |
final Set<? extends FieldLocation> locations) { |
316 | |
|
317 | 64 | T toReturn = null; |
318 | |
|
319 | 64 | if (containingClassNode != null) { |
320 | |
|
321 | |
|
322 | 58 | for (FieldLocation current : locations) { |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | 406 | final String fieldName = current.getAnnotationRenamedTo() == null |
337 | 236 | ? current.getMemberName() |
338 | 170 | : current.getAnnotationRenamedTo(); |
339 | 406 | final String className = current.getClassName(); |
340 | |
|
341 | |
try { |
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | 406 | final String attributeValue = DomHelper.getNameAttribute(aNode) == null |
353 | 168 | ? DomHelper.getValueAttribute(aNode) |
354 | 238 | : DomHelper.getNameAttribute(aNode); |
355 | 406 | if (fieldName.equalsIgnoreCase(attributeValue) |
356 | 37 | && className.equalsIgnoreCase(DomHelper.getNameAttribute(containingClassNode))) { |
357 | 34 | toReturn = (T) current; |
358 | |
} |
359 | 0 | } catch (Exception e) { |
360 | 0 | throw new IllegalStateException("Could not acquire FieldLocation for fieldName [" |
361 | |
+ fieldName + "] and className [" + className + "]", e); |
362 | 406 | } |
363 | 406 | } |
364 | |
} |
365 | |
|
366 | |
|
367 | 64 | return toReturn; |
368 | |
} |
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
public static void insertXmlDocumentationAnnotationsFor( |
380 | |
final Node aNode, |
381 | |
final SortedMap<ClassLocation, JavaDocData> classJavaDocs, |
382 | |
final SortedMap<FieldLocation, JavaDocData> fieldJavaDocs, |
383 | |
final SortedMap<MethodLocation, JavaDocData> methodJavaDocs, |
384 | |
final JavaDocRenderer renderer) { |
385 | |
|
386 | 35 | JavaDocData javaDocData = null; |
387 | 35 | SortableLocation location = null; |
388 | |
|
389 | |
|
390 | 35 | final ClassLocation classLocation = DomHelper.getClassLocation(aNode, classJavaDocs.keySet()); |
391 | 35 | if (classLocation != null) { |
392 | 11 | javaDocData = classJavaDocs.get(classLocation); |
393 | 11 | location = classLocation; |
394 | |
} else { |
395 | |
|
396 | 24 | final FieldLocation fieldLocation = DomHelper.getFieldLocation(aNode, fieldJavaDocs.keySet()); |
397 | 24 | if (fieldLocation != null) { |
398 | 23 | javaDocData = fieldJavaDocs.get(fieldLocation); |
399 | 23 | location = fieldLocation; |
400 | |
} else { |
401 | |
|
402 | 1 | final MethodLocation methodLocation = DomHelper.getMethodLocation(aNode, methodJavaDocs.keySet()); |
403 | 1 | if (methodLocation != null) { |
404 | 1 | javaDocData = methodJavaDocs.get(methodLocation); |
405 | 1 | location = methodLocation; |
406 | |
} |
407 | |
} |
408 | |
} |
409 | |
|
410 | |
|
411 | 35 | if (javaDocData == null) { |
412 | |
|
413 | 0 | final String nodeName = aNode.getNodeName(); |
414 | 0 | String humanReadableName = DomHelper.getNameAttribute(aNode); |
415 | |
|
416 | 0 | if (humanReadableName == null && nodeName.toLowerCase().endsWith("enumeration")) { |
417 | 0 | humanReadableName = "enumeration#" + getValueAttribute(aNode); |
418 | |
} |
419 | |
|
420 | 0 | throw new IllegalStateException("Could not find JavaDocData for XSD node [" |
421 | 0 | + humanReadableName + "] with XPath [" + DomHelper.getXPathFor(aNode) + "]"); |
422 | |
} |
423 | |
|
424 | |
|
425 | 35 | final String processedJavaDoc = renderer.render(javaDocData, location).trim(); |
426 | 35 | DomHelper.addXmlDocumentAnnotationTo(aNode, processedJavaDoc); |
427 | 35 | } |
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
private static Node getContainingClassOrNull(final Node aNode) { |
434 | |
|
435 | 131 | for (Node current = aNode.getParentNode(); current != null; current = current.getParentNode()) { |
436 | |
|
437 | 125 | final String localName = current.getLocalName(); |
438 | 125 | final boolean foundClassMatch = "complexType".equalsIgnoreCase(localName) |
439 | 81 | || "simpleType".equalsIgnoreCase(localName); |
440 | |
|
441 | 125 | if (foundClassMatch) { |
442 | 58 | return current; |
443 | |
} |
444 | |
} |
445 | |
|
446 | |
|
447 | 6 | return null; |
448 | |
} |
449 | |
|
450 | |
private static String getNamedAttribute(final Node aNode, final String attributeName) { |
451 | |
|
452 | |
|
453 | 1008 | if (aNode == null) { |
454 | 0 | return null; |
455 | |
} |
456 | |
|
457 | 1008 | final NamedNodeMap attributes = aNode.getAttributes(); |
458 | 1008 | if (attributes != null) { |
459 | |
|
460 | 1005 | final Node nameNode = attributes.getNamedItem(attributeName); |
461 | 1005 | if (nameNode != null) { |
462 | 765 | return nameNode.getNodeValue().trim(); |
463 | |
} |
464 | |
} |
465 | |
|
466 | |
|
467 | 243 | return null; |
468 | |
} |
469 | |
} |