Debian Codegraph @

src/main/java/ezvcard/util/XmlUtils.java

raw symbols SCIP
1 package ezvcard.util;
2
3 import java.io.BufferedInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.Reader;
7 import java.io.StringReader;
8 import java.io.StringWriter;
9 import java.io.UncheckedIOException;
10 import java.io.Writer;
11 import java.nio.file.Files;
12 import java.nio.file.Path;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17
18 import javax.xml.namespace.QName;
19 import javax.xml.parsers.DocumentBuilder;
20 import javax.xml.parsers.DocumentBuilderFactory;
21 import javax.xml.parsers.ParserConfigurationException;
22 import javax.xml.transform.Transformer;
23 import javax.xml.transform.TransformerConfigurationException;
24 import javax.xml.transform.TransformerException;
25 import javax.xml.transform.TransformerFactory;
26 import javax.xml.transform.TransformerFactoryConfigurationError;
27 import javax.xml.transform.dom.DOMSource;
28 import javax.xml.transform.stream.StreamResult;
29
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Element;
32 import org.w3c.dom.Node;
33 import org.w3c.dom.NodeList;
34 import org.xml.sax.InputSource;
35 import org.xml.sax.SAXException;
36
37 /*
38 Copyright (c) 2012-2023, Michael Angstadt
39 All rights reserved.
40
41 Redistribution and use in source and binary forms, with or without
42 modification, are permitted provided that the following conditions are met:
43
44 1. Redistributions of source code must retain the above copyright notice, this
45 list of conditions and the following disclaimer.
46 2. Redistributions in binary form must reproduce the above copyright notice,
47 this list of conditions and the following disclaimer in the documentation
48 and/or other materials provided with the distribution.
49
50 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
51 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
54 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60
61 The views and conclusions contained in the software and documentation are those
62 of the authors and should not be interpreted as representing official policies,
63 either expressed or implied, of the FreeBSD Project.
64 */
65
66 /**
67 * Generic XML utility methods.
68 * @author Michael Angstadt
69 */
70 public final class XmlUtils {
71 /**
72 * Creates a new XML document.
73 * @return the XML document
74 */
75 public static Document createDocument() {
76 try {
77 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
78 DocumentBuilder builder = factory.newDocumentBuilder();
79 return builder.newDocument();
80 } catch (ParserConfigurationException e) {
81 //should never be thrown because we're not doing anything fancy with the configuration
82 throw new RuntimeException(e);
83 }
84 }
85
86 /**
87 * Parses an XML string into a DOM.
88 * @param xml the XML string
89 * @return the parsed DOM
90 * @throws SAXException if the string is not valid XML
91 */
92 public static Document toDocument(String xml) throws SAXException {
93 try {
94 return toDocument(new StringReader(xml));
95 } catch (IOException e) {
96 //should never be thrown because we're reading from a string
97 throw new UncheckedIOException(e);
98 }
99 }
100
101 /**
102 * Parses an XML document from a file.
103 * @param file the file
104 * @return the parsed DOM
105 * @throws SAXException if the XML is not valid
106 * @throws IOException if there is a problem reading from the file
107 */
108 public static Document toDocument(Path file) throws SAXException, IOException {
109 try (InputStream in = new BufferedInputStream(Files.newInputStream(file))) {
110 return XmlUtils.toDocument(in);
111 }
112 }
113
114 /**
115 * Parses an XML document from an input stream.
116 * @param in the input stream
117 * @return the parsed DOM
118 * @throws SAXException if the XML is not valid
119 * @throws IOException if there is a problem reading from the input stream
120 */
121 public static Document toDocument(InputStream in) throws SAXException, IOException {
122 return toDocument(new InputSource(in));
123 }
124
125 /**
126 * <p>
127 * Parses an XML document from a reader.
128 * </p>
129 * <p>
130 * Note that use of this method is discouraged. It ignores the character
131 * encoding that is defined within the XML document itself, and should only
132 * be used if the encoding is undefined or if the encoding needs to be
133 * ignored for whatever reason. The {@link #toDocument(InputStream)} method
134 * should be used instead, since it takes the XML document's character
135 * encoding into account when parsing.
136 * </p>
137 * @param reader the reader
138 * @return the parsed DOM
139 * @throws SAXException if the XML is not valid
140 * @throws IOException if there is a problem reading from the reader
141 * @see <a
142 * href="http://stackoverflow.com/q/3482494/13379">http://stackoverflow.com/q/3482494/13379</a>
143 */
144 public static Document toDocument(Reader reader) throws SAXException, IOException {
145 return toDocument(new InputSource(reader));
146 }
147
148 private static Document toDocument(InputSource in) throws SAXException, IOException {
149 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
150 factory.setNamespaceAware(true);
151 factory.setIgnoringComments(true);
152 applyXXEProtection(factory);
153
154 DocumentBuilder builder;
155 try {
156 builder = factory.newDocumentBuilder();
157 } catch (ParserConfigurationException e) {
158 //should never be thrown because we're not doing anything fancy with the configuration
159 throw new RuntimeException(e);
160 }
161
162 return builder.parse(in);
163 }
164
165 /**
166 * Configures a {@link DocumentBuilderFactory} to protect it against XML
167 * External Entity attacks.
168 * @param factory the factory
169 * @see <a href=
170 * "https://www.owasp.org/index.php/XML_External_Entity_%28XXE%29_Prevention_Cheat_Sheet#Java">
171 * XXE Cheat Sheet</a>
172 */
173 public static void applyXXEProtection(DocumentBuilderFactory factory) {
174 Map<String, Boolean> features = new HashMap<>();
175 features.put("http://apache.org/xml/features/disallow-doctype-decl", true);
176 features.put("http://xml.org/sax/features/external-general-entities", false);
177 features.put("http://xml.org/sax/features/external-parameter-entities", false);
178 features.put("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
179
180 for (Map.Entry<String, Boolean> entry : features.entrySet()) {
181 String feature = entry.getKey();
182 Boolean value = entry.getValue();
183 try {
184 factory.setFeature(feature, value);
185 } catch (ParserConfigurationException e) {
186 //feature is not supported by the local XML engine, skip it
187 }
188 }
189
190 factory.setXIncludeAware(false);
191 factory.setExpandEntityReferences(false);
192 }
193
194 /**
195 * Configures a {@link TransformerFactory} to protect it against XML
196 * External Entity attacks.
197 * @param factory the factory
198 * @see <a href=
199 * "https://www.owasp.org/index.php/XML_External_Entity_%28XXE%29_Prevention_Cheat_Sheet#Java">
200 * XXE Cheat Sheet</a>
201 */
202 public static void applyXXEProtection(TransformerFactory factory) {
203 //@formatter:off
204 String[] attributes = {
205 //XMLConstants.ACCESS_EXTERNAL_DTD (Java 7 only)
206 "http://javax.xml.XMLConstants/property/accessExternalDTD",
207
208 //XMLConstants.ACCESS_EXTERNAL_STYLESHEET (Java 7 only)
209 "http://javax.xml.XMLConstants/property/accessExternalStylesheet"
210 };
211 //@formatter:on
212
213 for (String attribute : attributes) {
214 try {
215 factory.setAttribute(attribute, "");
216 } catch (IllegalArgumentException e) {
217 //attribute is not supported by the local XML engine, skip it
218 }
219 }
220 }
221
222 /**
223 * Converts an XML node to a string.
224 * @param node the XML node
225 * @return the string
226 */
227 public static String toString(Node node) {
228 return toString(node, new HashMap<>());
229 }
230
231 /**
232 * Converts an XML node to a string.
233 * @param node the XML node
234 * @param outputProperties the output properties
235 * @return the string
236 */
237 public static String toString(Node node, Map<String, String> outputProperties) {
238 try {
239 StringWriter writer = new StringWriter();
240 toWriter(node, writer, outputProperties);
241 return writer.toString();
242 } catch (TransformerException e) {
243 //should never be thrown because we're writing to a string
244 throw new RuntimeException(e);
245 }
246 }
247
248 /**
249 * Writes an XML node to a writer.
250 * @param node the XML node
251 * @param writer the writer
252 * @throws TransformerException if there's a problem writing to the writer
253 */
254 public static void toWriter(Node node, Writer writer) throws TransformerException {
255 toWriter(node, writer, new HashMap<>());
256 }
257
258 /**
259 * Writes an XML node to a writer.
260 * @param node the XML node
261 * @param writer the writer
262 * @param outputProperties the output properties
263 * @throws TransformerException if there's a problem writing to the writer
264 */
265 public static void toWriter(Node node, Writer writer, Map<String, String> outputProperties) throws TransformerException {
266 Transformer transformer;
267 try {
268 transformer = TransformerFactory.newInstance().newTransformer();
269 } catch (TransformerConfigurationException e) {
270 //should never be thrown because we're not doing anything fancy with the configuration
271 throw new RuntimeException(e);
272 } catch (TransformerFactoryConfigurationError e) {
273 //should never be thrown because we're not doing anything fancy with the configuration
274 throw new RuntimeException(e);
275 }
276
277 assignOutputProperties(transformer, outputProperties);
278
279 DOMSource source = new DOMSource(node);
280 StreamResult result = new StreamResult(writer);
281 transformer.transform(source, result);
282 }
283
284 /**
285 * Assigns the given output properties to the given transformer, ignoring
286 * invalid output properties.
287 * @param transformer the transformer
288 * @param outputProperties the output properties
289 */
290 public static void assignOutputProperties(Transformer transformer, Map<String, String> outputProperties) {
291 for (Map.Entry<String, String> property : outputProperties.entrySet()) {
292 try {
293 transformer.setOutputProperty(property.getKey(), property.getValue());
294 } catch (IllegalArgumentException e) {
295 //ignore invalid output properties
296 }
297 }
298 }
299
300 /**
301 * Gets all the elements out of a {@link NodeList}.
302 * @param nodeList the node list
303 * @return the elements
304 */
305 public static List<Element> toElementList(NodeList nodeList) {
306 List<Element> elements = new ArrayList<>();
307 for (int i = 0; i < nodeList.getLength(); i++) {
308 Node node = nodeList.item(i);
309 if (node instanceof Element) {
310 elements.add((Element) node);
311 }
312 }
313 return elements;
314 }
315
316 /**
317 * Gets the first child element of an element.
318 * @param parent the parent element
319 * @return the first child element or null if there are no child elements
320 */
321 public static Element getFirstChildElement(Element parent) {
322 return getFirstChildElement((Node) parent);
323 }
324
325 /**
326 * Gets the first child element of a node.
327 * @param parent the node
328 * @return the first child element or null if there are no child elements
329 */
330 private static Element getFirstChildElement(Node parent) {
331 NodeList nodeList = parent.getChildNodes();
332 for (int i = 0; i < nodeList.getLength(); i++) {
333 Node node = nodeList.item(i);
334 if (node instanceof Element) {
335 return (Element) node;
336 }
337 }
338 return null;
339 }
340
341 /**
342 * Determines if a node has a particular qualified name.
343 * @param node the node
344 * @param qname the qualified name
345 * @return true if the node has the given qualified name, false if not
346 */
347 public static boolean hasQName(Node node, QName qname) {
348 return qname.getNamespaceURI().equals(node.getNamespaceURI()) && qname.getLocalPart().equals(node.getLocalName());
349 }
350
351 private XmlUtils() {
352 //hide
353 }
354 }