tim@71: package de.intevation.gnv.utils; tim@71: 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; 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; tim@71: import javax.xml.transform.dom.DOMSource; tim@71: import javax.xml.transform.stream.StreamResult; tim@71: tim@71: import org.apache.log4j.Logger; tim@71: import org.w3c.dom.Document; tim@71: import org.w3c.dom.Element; tim@217: import org.w3c.dom.Node; tim@71: import org.xml.sax.SAXException; tim@71: tim@71: import de.intevation.artifacts.ArtifactNamespaceContext; tim@71: tim@71: /** tim@71: * @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: /** tim@71: * Constructor tim@71: */ tim@71: public ArtifactXMLUtilities() { tim@71: } tim@171: tim@71: /** tim@71: * @param document tim@71: * @return 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@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@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: tim@171: public Document reInitDocument(Document document) { tim@79: try { tim@171: byte[] barray = this.writeDocument2String(document).getBytes( tim@171: "UTF-8"); tim@171: InputStream inputStream = new ByteArrayInputStream(barray); tim@79: return this.readDocument(inputStream); tim@79: } catch (UnsupportedEncodingException e) { tim@171: log.error(e, e); tim@79: } tim@79: return document; tim@71: } tim@171: 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@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@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@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: 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: tim@71: }