1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.internal.utils;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23
24 import java.io.IOException;
25 import java.io.StringReader;
26 import java.util.LinkedHashSet;
27 import java.util.Set;
28
29 import javax.xml.parsers.DocumentBuilder;
30 import javax.xml.parsers.DocumentBuilderFactory;
31 import javax.xml.parsers.ParserConfigurationException;
32
33 import org.w3c.dom.Document;
34 import org.w3c.dom.NamedNodeMap;
35 import org.w3c.dom.Node;
36 import org.xml.sax.InputSource;
37 import org.xml.sax.SAXException;
38
39 import com.puppycrawl.tools.checkstyle.XmlLoader;
40
41
42
43
44
45
46
47 public final class XmlUtil {
48
49 private XmlUtil() {
50 }
51
52 public static Document getRawXml(String fileName, String code, String unserializedSource)
53 throws ParserConfigurationException {
54 Document rawXml = null;
55 try {
56 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
57 factory.setValidating(false);
58 factory.setFeature(
59 XmlLoader.LoadExternalDtdFeatureProvider.EXTERNAL_GENERAL_ENTITIES, false);
60 factory.setFeature(
61 XmlLoader.LoadExternalDtdFeatureProvider.LOAD_EXTERNAL_DTD, false);
62
63 final DocumentBuilder builder = factory.newDocumentBuilder();
64
65 rawXml = builder.parse(new InputSource(new StringReader(code)));
66 }
67 catch (IOException | SAXException ex) {
68 assertWithMessage(fileName + " has invalid xml (" + ex.getMessage() + "): "
69 + unserializedSource).fail();
70 }
71
72 return rawXml;
73 }
74
75 public static Set<Node> getChildrenElements(Node node) {
76 final Set<Node> result = new LinkedHashSet<>();
77
78 for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
79 if (child.getNodeType() != Node.TEXT_NODE) {
80 result.add(child);
81 }
82 }
83
84 return result;
85 }
86
87 public static Node getFirstChildElement(Node node) {
88 Node firstChildElement = null;
89 for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
90 if (child.getNodeType() != Node.TEXT_NODE) {
91 firstChildElement = child;
92 break;
93 }
94 }
95
96 return firstChildElement;
97 }
98
99
100
101
102
103
104
105
106
107 public static Node findChildElementById(Node node, String id) {
108 Node childElement = null;
109 for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
110 final NamedNodeMap attributes = child.getAttributes();
111 if (attributes != null) {
112 final Node attribute = attributes.getNamedItem("id");
113 if (attribute != null && id.equals(attribute.getNodeValue())) {
114 childElement = child;
115 break;
116 }
117 }
118 }
119
120 return childElement;
121 }
122
123 public static Set<Node> findChildElementsByTag(Node node, String tag) {
124 final Set<Node> result = new LinkedHashSet<>();
125
126 for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
127 if (tag.equals(child.getNodeName())) {
128 result.add(child);
129 }
130 else if (child.hasChildNodes()) {
131 result.addAll(findChildElementsByTag(child, tag));
132 }
133 }
134
135 return result;
136 }
137
138
139
140
141
142
143
144 public static String getNameAttributeOfNode(Node node) {
145 return node.getAttributes().getNamedItem("name").getNodeValue();
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159 public static String sanitizeXml(String rawXml) {
160 return rawXml
161 .replaceAll("(^\\s+|\\s+$)", "")
162 .replaceAll("\\s+", " ")
163 .replace("<", "<")
164 .replace(">", ">");
165 }
166
167 }