1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.johnzon.jaxrs.xml; |
20 | |
|
21 | |
import org.w3c.dom.Document; |
22 | |
import org.w3c.dom.NamedNodeMap; |
23 | |
import org.w3c.dom.Node; |
24 | |
import org.w3c.dom.NodeList; |
25 | |
|
26 | |
import javax.json.Json; |
27 | |
import javax.json.JsonArrayBuilder; |
28 | |
import javax.json.JsonBuilderFactory; |
29 | |
import javax.json.JsonObjectBuilder; |
30 | |
import javax.xml.stream.XMLStreamException; |
31 | |
import java.util.Collection; |
32 | |
import java.util.Collections; |
33 | |
import java.util.LinkedHashMap; |
34 | |
import java.util.LinkedList; |
35 | |
import java.util.Map; |
36 | |
|
37 | 1 | public class WadlDocumentToJson { |
38 | 1 | private final JsonBuilderFactory builderFactory = Json.createBuilderFactory(Collections.<String, Object>emptyMap()); |
39 | |
|
40 | |
public String convert(final Document doc) throws XMLStreamException { |
41 | 1 | final JsonObjectBuilder builder = builderFactory.createObjectBuilder(); |
42 | 1 | if (doc.getChildNodes().getLength() != 1) { |
43 | 0 | return "{}"; |
44 | |
} |
45 | 1 | final Node item = doc.getChildNodes().item(0); |
46 | 1 | return builder.add(item.getNodeName(), createNode(item)).build().toString(); |
47 | |
} |
48 | |
|
49 | |
private void addChildrens(final JsonObjectBuilder builder, final NodeList children) { |
50 | 8 | final Map<String, Collection<Node>> nodesByName = new LinkedHashMap<String, Collection<Node>>(); |
51 | 44 | for (int i = 0; i < children.getLength(); i++) { |
52 | 36 | final Node node = children.item(i); |
53 | 36 | if ("#text".equals(node.getNodeName())) { |
54 | 22 | continue; |
55 | |
} |
56 | |
|
57 | 14 | final String name = node.getNodeName(); |
58 | 14 | Collection<Node> nodes = nodesByName.get(name); |
59 | 14 | if (nodes == null) { |
60 | 11 | nodes = new LinkedList<Node>(); |
61 | 11 | nodesByName.put(name, nodes); |
62 | |
} |
63 | 14 | nodes.add(node); |
64 | |
} |
65 | |
|
66 | 8 | for (final Map.Entry<String, Collection<Node>> entry : nodesByName.entrySet()) { |
67 | 11 | final JsonArrayBuilder arrayBuilder = builderFactory.createArrayBuilder(); |
68 | 11 | for (final Node n : entry.getValue()) { |
69 | 14 | final JsonObjectBuilder jsonObjectBuilder = createNode(n); |
70 | 14 | if (jsonObjectBuilder != null) { |
71 | 14 | arrayBuilder.add(jsonObjectBuilder); |
72 | |
} |
73 | 14 | } |
74 | 11 | builder.add(entry.getKey(), arrayBuilder); |
75 | 11 | } |
76 | 8 | } |
77 | |
|
78 | |
private JsonObjectBuilder createNode(final Node node) { |
79 | 15 | JsonObjectBuilder childBuilder = null; |
80 | |
|
81 | 15 | if (node.hasAttributes()) { |
82 | 14 | childBuilder = builderFactory.createObjectBuilder(); |
83 | 14 | final NamedNodeMap attributes = node.getAttributes(); |
84 | 36 | for (int j = 0; j < attributes.getLength(); j++) { |
85 | 22 | final Node namedItem = attributes.item(j); |
86 | 22 | childBuilder.add(namedItem.getNodeName(), namedItem.getNodeValue()); |
87 | |
} |
88 | |
} |
89 | |
|
90 | 15 | if (node.hasChildNodes()) { |
91 | 8 | if (childBuilder == null) { |
92 | 1 | childBuilder = builderFactory.createObjectBuilder(); |
93 | |
} |
94 | 8 | addChildrens( childBuilder, node.getChildNodes()); |
95 | |
} |
96 | 15 | return childBuilder; |
97 | |
} |
98 | |
} |