/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 Lachlan Dowding
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package permafrost.tundra.xml.dom;

import com.wm.app.b2b.server.ServiceException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import permafrost.tundra.io.CloseableHelper;
import permafrost.tundra.lang.ExceptionHelper;
import permafrost.tundra.xml.sax.InputSourceHelper;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

/**
 * Collection of convenience methods for working with Document objects.
 */
public class DocumentHelper {
    /**
     * Disallow instantiation of this class.
     */
    private DocumentHelper() {}

    /**
     * Parses the given content to an XML document.
     *
     * @param content           The XML content to parse.
     * @param charset           The character set that was used to encode the text data in the given stream.
     * @param close             If true, the given input stream will be closed after the parse is complete.
     * @return                  The parsed XML document.
     * @throws ServiceException If a parsing or I/O error occurs.
     */
    public static Document parse(InputStream content, Charset charset, boolean close) throws ServiceException {
        try {
            return parse(InputSourceHelper.normalize(content, charset));
        } finally {
            if (close) CloseableHelper.close(content);
        }
    }

    /**
     * Parses the given content to an XML document.
     *
     * @param content           The XML content to parse.
     * @return                  The parsed XML document.
     * @throws ServiceException If a parsing or I/O error occurs.
     */
    public static Document parse(InputSource content) throws ServiceException {
        return parse(content, null);
    }

    /**
     * Parses the given content to an XML document.
     *
     * @param content           The XML content to parse.
     * @param namespaceContext  Any namespace declarations used in the XML content.
     * @return                  The parsed XML document.
     * @throws ServiceException If a parsing or I/O error occurs.
     */
    public static Document parse(InputSource content, NamespaceContext namespaceContext) throws ServiceException {
        if (content == null) return null;

        Document document = null;

        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(namespaceContext != null);
            DocumentBuilder parser = factory.newDocumentBuilder();
            document = parser.parse(content);
        } catch (ParserConfigurationException ex) {
            ExceptionHelper.raise(ex);
        } catch (SAXException ex) {
            ExceptionHelper.raise(ex);
        } catch (IOException ex) {
            ExceptionHelper.raise(ex);
        }

        return document;
    }

    /**
     * Serializes a document to a stream using the default character set.
     *
     * @param document          The document to be serialized.
     * @return                  The serialized document.
     * @throws ServiceException If an XML transformation error occurs.
     */
    public static InputStream emit(Document document) throws ServiceException {
        return emit(document, null);
    }

    /**
     * Serializes a document to a stream using the given character set.
     *
     * @param document          The document to be serialized.
     * @param charset           The character encoding to use.
     * @return                  The serialized document.
     * @throws ServiceException If an XML transformation error occurs.
     */
    public static InputStream emit(Document document, Charset charset) throws ServiceException {
        return NodeHelper.emit(document, charset);
    }
}

@@ -2525,32 +2525,796 @@
 viceException %7B%0A
+        return parse(content, charset, close, null);%0A    %7D%0A%0A    /**%0A     * Parses the given content to an XML document.%0A     *%0A     * @param content           The XML content to parse.%0A     * @param charset           The character set that was used to encode the text data in the given stream.%0A     * @param close             If true, the given input stream will be closed after the parse is complete.%0A     * @param namespaceContext  Any namespace declarations used in the XML content.%0A     * @return                  The parsed XML document.%0A     * @throws ServiceException If a parsing or I/O error occurs.%0A     */%0A    public static Document parse(InputStream content, Charset charset, boolean close, NamespaceContext namespaceContext) throws ServiceException %7B%0A
         try %7B%0A  
@@ -3381,16 +3381,34 @@
 charset)
+, namespaceContext
 );%0A     
@@ -4565,24 +4565,187 @@
 t != null);%0A
+            factory.setExpandEntityReferences(true);%0A            factory.setIgnoringElementContentWhitespace(true);%0A            factory.setIgnoringComments(true);%0A
             

Change DocumentHelper.parse to use a NamespaceContext when provided