view gnv-artifacts/src/main/java/de/intevation/gnv/utils/ArtifactXMLUtilities.java @ 1118:c01c220312d0

Made it compile again with an updated ArtifactDatabase. gnv-artifacts/trunk@4137 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 12 Mar 2012 08:31:46 +0000
parents f953c9a559d8
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;

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.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import de.intevation.artifacts.ArtifactNamespaceContext;

/**
 * This class provides some methods for creating and working with xml documents.
 *
 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
 *
 */
public class ArtifactXMLUtilities implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = -6236340358303411758L;

    /**
     * the logger, used to log exceptions and additonaly information
     */
    private static Logger log = Logger
            .getLogger(ArtifactXMLUtilities.class);

    public static final String XFORM_URL = "http://www.w3.org/2002/xforms";
    public static final String XFORM_PREFIX = "xform";

    /**
     * Constructor.<br>
     * <b>Note:</b> It should not be necessary to create an object of this
     * class - which is a helper class. Call static methods instead.
     */
    public ArtifactXMLUtilities() {
    }

    /**
     * Creates an <code>Element</code> and returns it.
     *
     * @param document A document
     * @param name Name of a node.
     * @return an Element.
     */
    public static Element createArtifactElement(Document document, String name) {
        Element node = document.createElementNS(
                ArtifactNamespaceContext.NAMESPACE_URI, name);
        node.setPrefix(ArtifactNamespaceContext.NAMESPACE_PREFIX);
        return node;
    }


    /**
     * Turns an xml document into a string.
     *
     * @param document An xml document.
     * @return String representation of <i>document</i>.
     */
    public static String writeDocument2String(Document document) {
        try {
            TransformerFactory transformerFactory = TransformerFactory
                    .newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(document);
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            transformer.transform(source, result);
            return sw.getBuffer().toString();
        } catch (TransformerConfigurationException e) {
            log.error(e, e);
        } catch (TransformerFactoryConfigurationError e) {
            log.error(e, e);
        } catch (TransformerException e) {
            log.error(e, e);
        }
        return null;
    }

    /**
     * Read a document from input stream.
     *
     * @param inputStream The input stream.
     * @return the document read from stream.
     */
    public static Document readDocument(InputStream inputStream) {
        Document returnValue = null;
        try {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            returnValue = docBuilder.parse(inputStream);
        } catch (ParserConfigurationException e) {
            log.error(e, e);
        } catch (SAXException e) {
            log.error(e, e);
        } catch (IOException e) {
            log.error(e, e);
        }
        return returnValue;
    }


    public Document reInitDocument(Document document) {
        try {
            byte[] barray = ArtifactXMLUtilities.writeDocument2String(document).getBytes(
                    "UTF-8");
            InputStream inputStream = new ByteArrayInputStream(barray);
            return ArtifactXMLUtilities.readDocument(inputStream);
        } catch (UnsupportedEncodingException e) {
            log.error(e, e);
        }
        return document;
    }


    /**
     * Creates an <code>Element</code> with {@link #XFORM_PREFIX} and <i>name
     * </i>.
     *
     * @param document A document.
     * @param name The node name.
     * @return the created element.
     */
    public static Element createXFormElement(Document document, String name) {
        Element node = document.createElementNS(XFORM_URL, name);
        node.setPrefix(XFORM_PREFIX);
        return node;
    }

    /**
     * Creates an exception node.
     *
     * @param message The message in the node.
     * @param document A document.
     * @return the document containing the exception node.
     */
    public static Document createExceptionReport(String message, Document document) {
        log.debug("ArtifactXMLUtilities.createExceptionReport");
        Element exceptionReportNode = createArtifactElement(document,
                "exceptionreport");
        document.appendChild(exceptionReportNode);
        Element exceptionNode = createArtifactElement(document,
                "exception");
        exceptionNode.setTextContent(message);
        exceptionReportNode.appendChild(exceptionNode);
        return document;
    }

    /**
     * Creates an input exception node.
     *
     * @param msg The message in the node.
     * @param doc A document.
     * @return the document containing the exception node.
     */
    public static Document createInputExceptionReport(String msg, Document doc) {
        Element exceptionReportNode = createArtifactElement(
            doc,"exceptionreport");
        Element exceptionNode = createArtifactElement(
            doc,"exception");
        Element inputNode = createArtifactElement(
            doc, "input");
        inputNode.setTextContent(msg);
        exceptionNode.appendChild(inputNode);
        exceptionReportNode.appendChild(exceptionNode);
        doc.appendChild(exceptionReportNode);
        return doc;
    }

    /**
     * Creates a success node.
     *
     * @param message The message.
     * @param document A document.
     * @return the document containing the success node.
     */
    public static Document createSuccessReport(String message, Document document) {
        log.debug("ArtifactXMLUtilities.creatSuccessReport");
        Element reportNode = createArtifactElement(document, "result");
        document.appendChild(reportNode);
        Element successNode = createArtifactElement(document, "success");
        successNode.setTextContent(message);
        reportNode.appendChild(successNode);
        return document;
    }

    /**
     * Read <i>fileName</i> and return the first child node.
     *
     * @param fileName An xml document.
     * @return the first child node in this document.
     */
    public Node readConfiguration(String fileName){
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(false);
            return factory.newDocumentBuilder().parse(fileName).getChildNodes().item(0);
        } catch (SAXException e) {
            log.error(e,e);
            return null;
        } catch (IOException e) {
            log.error(e,e);
            return null;
        } catch (ParserConfigurationException e) {
            log.error(e,e);
            return null;
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org