com.c4om.utils.xmlutils.JavaXMLUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.c4om.utils.xmlutils.JavaXMLUtils.java

Source

/*
Copyright 2014 Universidad Politcnica de Madrid - Center for Open Middleware (http://www.centeropenmiddleware.com)
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.c4om.utils.xmlutils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.io.input.XmlStreamReader;
import org.apache.commons.io.output.XmlStreamWriter;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Static methods on this help with some common tasks performed while working 
 * with JDOM.
 * @author Pablo Alonso Rodrguez (Center for Open Middleware - UPM)
 *
 */
public class JavaXMLUtils {

    /**
     * Reads a {@link org.w3c.dom.Document} from an {@link InputSource}
     * @param inputSource the input source
     * @return the read {@link Document}
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
    public static Document loadW3CDocumentFromInputSource(InputSource inputSource)
            throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        Document resultingDocument = documentBuilder.parse(inputSource);
        return resultingDocument;
    }

    /**
     * Reads a {@link org.w3c.dom.Document} from an {@link InputStream}. Encoding is automatically 
     * determined as specified by corresponding RFCs.
     * @param is the input stream.
     * @return the read {@link Document}
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
    public static Document loadW3CDocumentFromInputStream(InputStream is)
            throws ParserConfigurationException, SAXException, IOException {
        Reader xmlStreamReader = new XmlStreamReader(is, true);
        return loadW3CDocumentFromInputSource(new InputSource(xmlStreamReader));
    }

    /**
     * Reads a {@link org.w3c.dom.Document} from an {@link InputStream}. Encoding is automatically 
     * determined as specified by corresponding RFCs.
     * @param is the input stream.
     * @param encoding the fallback encoding, if the actual one could not be determined
     * @return the read {@link Document}
     * @throws ParserConfigurationException 
     * @throws SAXException
     * @throws IOException
     */
    public static Document loadW3CDocumentFromInputStream(InputStream is, String encoding)
            throws ParserConfigurationException, SAXException, IOException {
        Reader xmlStreamReader = new XmlStreamReader(is, true, encoding);
        return loadW3CDocumentFromInputSource(new InputSource(xmlStreamReader));
    }

    /**
     * Reads a {@link org.w3c.dom.Document} from a {@link File}. Encoding is automatically 
     * determined as specified by corresponding RFCs.
     * @param file a {@link File} object.
     * @return the read {@link Document}
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
    public static Document loadW3CDocumentFromInputFile(File file)
            throws ParserConfigurationException, SAXException, IOException {
        Reader xmlStreamReader = new XmlStreamReader(file);
        return loadW3CDocumentFromInputSource(new InputSource(xmlStreamReader));
    }

    /**
     * Method that converts a W3C {@link Document} object to a String
     * @param document the document to convert
     * @param indent whether lines must be indented or not
     * @param omitDeclaration whether the XML declaration should be omitted or not. 
     * @param encoding the encoding placed at the XML declaration. Be carefully: Specifying here an encoding only takes effect at the XML declaration. If you are going to write the string to an output stream (e.g. to a file), you must also take care of the encoding there, for example, by means of {@link org.apache.commons.io.output.XmlStreamWriter}.
     * @return the {@link String} representation of the document
     * @throws TransformerException if there are problems during the conversion
     */
    public static String convertW3CDocumentToString(Document document, boolean indent, boolean omitDeclaration,
            String encoding) throws TransformerException {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration ? "yes" : "no");
        transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
        if (encoding != null) {
            transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        }
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        return output;
    }

    /**
     * Method that prints a W3C {@link Document} object to a provided {@link OutputStream}. 
     * Encoding should be correctly specified.
     * @param document the document to convert
     * @param os the output stream to print to
     * @param indent whether lines must be indented or not
     * @param omitDeclaration whether the XML declaration should be omitted or not. 
     * @param encoding the encoding placed at the XML declaration and used to encode the file.
     * @return the {@link String} representation of the document
     * @throws TransformerException if there are problems during the conversion
     */
    public static void printW3CDocumentToOutputStream(Document document, OutputStream os, boolean indent,
            boolean omitDeclaration, String encoding) throws TransformerException {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration ? "yes" : "no");
        transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
        XmlStreamWriter writer;
        if (encoding != null) {
            transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
            writer = new XmlStreamWriter(os, encoding);
        } else {
            writer = new XmlStreamWriter(os);
        }
        StreamResult streamResult = new StreamResult(writer);
        transformer.transform(new DOMSource(document), streamResult);
    }

    /**
     * Private constructor to prevent instantiation.
     */
    private JavaXMLUtils() {
    }
}