package ezvcard.util; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; import java.io.UncheckedIOException; import java.io.Writer; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /* Copyright (c) 2012-2023, Michael Angstadt All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project. */ /** * Generic XML utility methods. * @author Michael Angstadt */ public final class XmlUtils { /** * Creates a new XML document. * @return the XML document */ public static Document createDocument() { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.newDocument(); } catch (ParserConfigurationException e) { //should never be thrown because we're not doing anything fancy with the configuration throw new RuntimeException(e); } } /** * Parses an XML string into a DOM. * @param xml the XML string * @return the parsed DOM * @throws SAXException if the string is not valid XML */ public static Document toDocument(String xml) throws SAXException { try { return toDocument(new StringReader(xml)); } catch (IOException e) { //should never be thrown because we're reading from a string throw new UncheckedIOException(e); } } /** * Parses an XML document from a file. * @param file the file * @return the parsed DOM * @throws SAXException if the XML is not valid * @throws IOException if there is a problem reading from the file */ public static Document toDocument(Path file) throws SAXException, IOException { try (InputStream in = new BufferedInputStream(Files.newInputStream(file))) { return XmlUtils.toDocument(in); } } /** * Parses an XML document from an input stream. * @param in the input stream * @return the parsed DOM * @throws SAXException if the XML is not valid * @throws IOException if there is a problem reading from the input stream */ public static Document toDocument(InputStream in) throws SAXException, IOException { return toDocument(new InputSource(in)); } /** *
* Parses an XML document from a reader. *
** Note that use of this method is discouraged. It ignores the character * encoding that is defined within the XML document itself, and should only * be used if the encoding is undefined or if the encoding needs to be * ignored for whatever reason. The {@link #toDocument(InputStream)} method * should be used instead, since it takes the XML document's character * encoding into account when parsing. *
* @param reader the reader * @return the parsed DOM * @throws SAXException if the XML is not valid * @throws IOException if there is a problem reading from the reader * @see http://stackoverflow.com/q/3482494/13379 */ public static Document toDocument(Reader reader) throws SAXException, IOException { return toDocument(new InputSource(reader)); } private static Document toDocument(InputSource in) throws SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setIgnoringComments(true); applyXXEProtection(factory); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { //should never be thrown because we're not doing anything fancy with the configuration throw new RuntimeException(e); } return builder.parse(in); } /** * Configures a {@link DocumentBuilderFactory} to protect it against XML * External Entity attacks. * @param factory the factory * @see * XXE Cheat Sheet */ public static void applyXXEProtection(DocumentBuilderFactory factory) { Map