view gnv-artifacts/src/main/java/de/intevation/gnv/utils/ArtifactXMLUtilities.java @ 612:e86d37008fd1

Added new Transition and State for using the PreSettingsValue for the decision if a Transition could be used and the usage of PreSettings as InputValues. gnv-artifacts/trunk@678 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Fri, 12 Feb 2010 09:11:49 +0000
parents b8080695ea62
children dde7f51dbe1e
line wrap: on
line source
/**
 *
 */
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;

/**
 * @author Tim Englich <tim.englich@intevation.de>
 * 
 */
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
     */
    public ArtifactXMLUtilities() {
    }

    /**
     * @param document
     * @return
     */
    public Element createArtifactElement(Document document, String name) {
        Element node = document.createElementNS(
                ArtifactNamespaceContext.NAMESPACE_URI, name);
        node.setPrefix(ArtifactNamespaceContext.NAMESPACE_PREFIX);
        return node;
    }

    public 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;
    }

    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 = this.writeDocument2String(document).getBytes(
                    "UTF-8");
            InputStream inputStream = new ByteArrayInputStream(barray);
            return this.readDocument(inputStream);
        } catch (UnsupportedEncodingException e) {
            log.error(e, e);
        }
        return document;
    }

    public static Element createXFormElement(Document document, String name) {
        Element node = document.createElementNS(XFORM_URL, name);
        node.setPrefix(XFORM_PREFIX);
        return node;
    }

    public Document createExceptionReport(String message, Document document) {
        log.debug("ArtifactXMLUtilities.createExceptionReport");
        Element exceptionReportNode = this.createArtifactElement(document,
                "exceptionreport");
        document.appendChild(exceptionReportNode);
        Element exceptionNode = this.createArtifactElement(document,
                "exception");
        exceptionNode.setTextContent(message);
        exceptionReportNode.appendChild(exceptionNode);
        return document;
    }

    public Document createSuccessReport(String message, Document document) {
        log.debug("ArtifactXMLUtilities.creatSuccessReport");
        Element reportNode = this.createArtifactElement(document, "result");
        document.appendChild(reportNode);
        Element successNode = this.createArtifactElement(document, "success");
        successNode.setTextContent(message);
        reportNode.appendChild(successNode);
        return 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;
        }
    }

}

http://dive4elements.wald.intevation.org