tim@71: package de.intevation.gnv.utils;
tim@71:
sascha@779: import de.intevation.artifacts.ArtifactNamespaceContext;
sascha@779:
tim@79: import java.io.ByteArrayInputStream;
tim@71: import java.io.IOException;
tim@71: import java.io.InputStream;
tim@75: import java.io.Serializable;
tim@71: import java.io.StringWriter;
tim@79: import java.io.UnsupportedEncodingException;
tim@71:
tim@71: import javax.xml.parsers.DocumentBuilder;
tim@71: import javax.xml.parsers.DocumentBuilderFactory;
tim@71: import javax.xml.parsers.ParserConfigurationException;
sascha@779:
tim@71: import javax.xml.transform.Transformer;
tim@71: import javax.xml.transform.TransformerConfigurationException;
tim@71: import javax.xml.transform.TransformerException;
tim@71: import javax.xml.transform.TransformerFactory;
tim@71: import javax.xml.transform.TransformerFactoryConfigurationError;
sascha@779:
tim@71: import javax.xml.transform.dom.DOMSource;
sascha@779:
tim@71: import javax.xml.transform.stream.StreamResult;
tim@71:
tim@71: import org.apache.log4j.Logger;
sascha@779:
tim@71: import org.w3c.dom.Document;
tim@71: import org.w3c.dom.Element;
tim@217: import org.w3c.dom.Node;
sascha@779:
tim@71: import org.xml.sax.SAXException;
tim@71:
tim@71: /**
ingo@806: * This class provides some methods for creating and working with xml documents.
ingo@806: *
sascha@780: * @author Tim Englich
sascha@778: *
tim@71: */
tim@171: public class ArtifactXMLUtilities implements Serializable {
tim@71: /**
tim@79: *
tim@79: */
tim@79: private static final long serialVersionUID = -6236340358303411758L;
tim@79:
tim@79: /**
tim@71: * the logger, used to log exceptions and additonaly information
tim@71: */
tim@171: private static Logger log = Logger
tim@335: .getLogger(ArtifactXMLUtilities.class);
tim@171:
tim@76: public static final String XFORM_URL = "http://www.w3.org/2002/xforms";
tim@76: public static final String XFORM_PREFIX = "xform";
tim@171:
tim@71: /**
ingo@806: * Constructor.
ingo@806: * Note: It should not be necessary to create an object of this
ingo@806: * class - which is a helper class. Call static methods instead.
tim@71: */
tim@71: public ArtifactXMLUtilities() {
tim@71: }
tim@171:
tim@71: /**
ingo@806: * Creates an Element
and returns it.
sascha@807: *
ingo@806: * @param document A document
ingo@806: * @param name Name of a node.
ingo@806: * @return an Element.
tim@71: */
ingo@725: public static Element createArtifactElement(Document document, String name) {
tim@171: Element node = document.createElementNS(
tim@171: ArtifactNamespaceContext.NAMESPACE_URI, name);
tim@71: node.setPrefix(ArtifactNamespaceContext.NAMESPACE_PREFIX);
tim@71: return node;
tim@71: }
tim@171:
ingo@806:
ingo@806: /**
ingo@806: * Turns an xml document into a string.
ingo@806: *
ingo@806: * @param document An xml document.
ingo@806: * @return String representation of document.
ingo@806: */
ingo@725: public static String writeDocument2String(Document document) {
tim@71: try {
tim@171: TransformerFactory transformerFactory = TransformerFactory
tim@171: .newInstance();
tim@71: Transformer transformer = transformerFactory.newTransformer();
tim@71: DOMSource source = new DOMSource(document);
tim@71: StringWriter sw = new StringWriter();
tim@171: StreamResult result = new StreamResult(sw);
tim@71: transformer.transform(source, result);
tim@71: return sw.getBuffer().toString();
tim@71: } catch (TransformerConfigurationException e) {
tim@171: log.error(e, e);
tim@71: } catch (TransformerFactoryConfigurationError e) {
tim@171: log.error(e, e);
tim@71: } catch (TransformerException e) {
tim@171: log.error(e, e);
tim@71: }
tim@71: return null;
tim@71: }
tim@79:
ingo@806: /**
ingo@806: * Read a document from input stream.
ingo@806: *
ingo@806: * @param inputStream The input stream.
ingo@806: * @return the document read from stream.
ingo@806: */
ingo@358: public static Document readDocument(InputStream inputStream) {
tim@71: Document returnValue = null;
tim@71: try {
tim@171: DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
tim@171: .newInstance();
tim@71: DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
tim@171: returnValue = docBuilder.parse(inputStream);
tim@71: } catch (ParserConfigurationException e) {
tim@171: log.error(e, e);
tim@71: } catch (SAXException e) {
tim@171: log.error(e, e);
tim@71: } catch (IOException e) {
tim@171: log.error(e, e);
tim@71: }
tim@71: return returnValue;
tim@71: }
tim@171:
ingo@806:
ingo@806: @SuppressWarnings("static-access")
tim@171: public Document reInitDocument(Document document) {
tim@79: try {
ingo@806: @SuppressWarnings("static-access")
ingo@806: byte[] barray = ArtifactXMLUtilities.writeDocument2String(document).getBytes(
tim@171: "UTF-8");
tim@171: InputStream inputStream = new ByteArrayInputStream(barray);
ingo@806: return ArtifactXMLUtilities.readDocument(inputStream);
tim@79: } catch (UnsupportedEncodingException e) {
tim@171: log.error(e, e);
tim@79: }
tim@79: return document;
tim@71: }
tim@171:
ingo@806:
ingo@806: /**
ingo@806: * Creates an Element
with {@link #XFORM_PREFIX} and name
ingo@806: * .
ingo@806: *
ingo@806: * @param document A document.
ingo@806: * @param name The node name.
ingo@806: * @return the created element.
ingo@806: */
ingo@456: public static Element createXFormElement(Document document, String name) {
tim@76: Element node = document.createElementNS(XFORM_URL, name);
tim@76: node.setPrefix(XFORM_PREFIX);
tim@76: return node;
tim@76: }
tim@171:
ingo@806: /**
ingo@806: * Creates an exception node.
ingo@806: *
ingo@806: * @param message The message in the node.
ingo@806: * @param document A document.
ingo@806: * @return the document containing the exception node.
ingo@806: */
ingo@725: public static Document createExceptionReport(String message, Document document) {
tim@88: log.debug("ArtifactXMLUtilities.createExceptionReport");
ingo@725: Element exceptionReportNode = createArtifactElement(document,
tim@171: "exceptionreport");
tim@88: document.appendChild(exceptionReportNode);
ingo@725: Element exceptionNode = createArtifactElement(document,
tim@171: "exception");
tim@88: exceptionNode.setTextContent(message);
tim@88: exceptionReportNode.appendChild(exceptionNode);
tim@88: return document;
tim@88: }
tim@171:
ingo@806: /**
ingo@806: * Creates an input exception node.
ingo@806: *
ingo@806: * @param msg The message in the node.
ingo@806: * @param doc A document.
ingo@806: * @return the document containing the exception node.
ingo@806: */
ingo@725: public static Document createInputExceptionReport(String msg, Document doc) {
ingo@725: Element exceptionReportNode = createArtifactElement(
ingo@725: doc,"exceptionreport");
ingo@725: Element exceptionNode = createArtifactElement(
ingo@725: doc,"exception");
ingo@725: Element inputNode = createArtifactElement(
ingo@725: doc, "input");
ingo@725: inputNode.setTextContent(msg);
ingo@725: exceptionNode.appendChild(inputNode);
ingo@725: exceptionReportNode.appendChild(exceptionNode);
ingo@725: doc.appendChild(exceptionReportNode);
ingo@725: return doc;
ingo@725: }
ingo@725:
ingo@806: /**
ingo@806: * Creates a success node.
ingo@806: *
ingo@806: * @param message The message.
ingo@806: * @param document A document.
ingo@806: * @return the document containing the success node.
ingo@806: */
ingo@725: public static Document createSuccessReport(String message, Document document) {
tim@89: log.debug("ArtifactXMLUtilities.creatSuccessReport");
ingo@725: Element reportNode = createArtifactElement(document, "result");
tim@89: document.appendChild(reportNode);
ingo@725: Element successNode = createArtifactElement(document, "success");
tim@89: successNode.setTextContent(message);
tim@89: reportNode.appendChild(successNode);
tim@89: return document;
tim@89: }
sascha@778:
ingo@806: /**
ingo@806: * Read fileName and return the first child node.
sascha@807: *
ingo@806: * @param fileName An xml document.
ingo@806: * @return the first child node in this document.
ingo@806: */
tim@217: public Node readConfiguration(String fileName){
tim@217: try {
tim@217: DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
sascha@778: factory.setValidating(false);
tim@217: return factory.newDocumentBuilder().parse(fileName).getChildNodes().item(0);
tim@217: } catch (SAXException e) {
tim@217: log.error(e,e);
tim@217: return null;
tim@217: } catch (IOException e) {
tim@217: log.error(e,e);
tim@217: return null;
tim@217: } catch (ParserConfigurationException e) {
tim@217: log.error(e,e);
tim@217: return null;
tim@217: }
tim@217: }
tim@71: }
ingo@806: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :