Search in sources :

Example 31 with Result

use of javax.xml.transform.Result in project spring-framework by spring-projects.

the class StaxUtilsTests method isStaxResult.

@Test
public void isStaxResult() throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
    Result result = StaxUtils.createCustomStaxResult(streamWriter);
    assertTrue("Not a StAX Result", StaxUtils.isStaxResult(result));
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) StAXResult(javax.xml.transform.stax.StAXResult) DOMResult(javax.xml.transform.dom.DOMResult) Test(org.junit.Test)

Example 32 with Result

use of javax.xml.transform.Result in project spring-framework by spring-projects.

the class MarshallingMessageConverter method marshalToTextMessage.

/**
	 * Marshal the given object to a {@link TextMessage}.
	 * @param object the object to be marshalled
	 * @param session current JMS session
	 * @param marshaller the marshaller to use
	 * @return the resulting message
	 * @throws JMSException if thrown by JMS methods
	 * @throws IOException in case of I/O errors
	 * @throws XmlMappingException in case of OXM mapping errors
	 * @see Session#createTextMessage
	 * @see Marshaller#marshal(Object, Result)
	 */
protected TextMessage marshalToTextMessage(Object object, Session session, Marshaller marshaller) throws JMSException, IOException, XmlMappingException {
    StringWriter writer = new StringWriter();
    Result result = new StreamResult(writer);
    marshaller.marshal(object, result);
    return session.createTextMessage(writer.toString());
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 33 with Result

use of javax.xml.transform.Result in project ddf by codice.

the class AbstractFeatureConverterWfs20 method readGml.

protected Object readGml(String xml) {
    LOGGER.debug("readGml() input XML: {}", xml);
    //Add namespace into XML for processing
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    try {
        dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    } catch (ParserConfigurationException e) {
        LOGGER.debug("Unable to configure features on document builder.", e);
    }
    DocumentBuilder dBuilder = null;
    Document doc = null;
    Object gml = null;
    InputStream xmlIs = null;
    //Check if GML 3.2.1 namespace exist on XML chunk
    try {
        dBuilder = dbFactory.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = dBuilder.parse(is);
    } catch (ParserConfigurationException | SAXException | IOException e) {
        LOGGER.debug(XML_PARSE_FAILURE);
    }
    if (null != doc) {
        String[] namePrefix = doc.getDocumentElement().getNodeName().split(":");
        String prefix = "";
        if (namePrefix.length < 2) {
            LOGGER.debug("Incoming XML has no GML prefix");
        } else {
            prefix = ":" + namePrefix[0];
        }
        String xmlNs = doc.getDocumentElement().getAttribute("xmlns" + prefix);
        if (xmlNs.equals(Wfs20Constants.GML_3_2_NAMESPACE)) {
            LOGGER.debug("Namespace already exists.");
        } else {
            doc.createElementNS(Wfs20Constants.GML_3_2_NAMESPACE, doc.getDocumentElement().getNodeName());
        }
        //Convert DOM to InputStream
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        Source xmlSource = new DOMSource(doc);
        Result outputTarget = new StreamResult(outputStream);
        try {
            TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
        } catch (TransformerException | TransformerFactoryConfigurationError e) {
            LOGGER.debug(CREATE_TRANSFORMER_FAILURE);
        }
        xmlIs = new ByteArrayInputStream(outputStream.toByteArray());
        //Parse XML into a Geometry object
        Configuration configurationG = new org.geotools.gml3.v3_2.GMLConfiguration();
        Parser parser = new org.geotools.xml.Parser(configurationG);
        parser.setStrict(false);
        parser.setValidating(false);
        parser.setFailOnValidationError(false);
        parser.setForceParserDelegate(false);
        try {
            gml = parser.parse(xmlIs);
        } catch (IOException | SAXException | ParserConfigurationException e) {
            LOGGER.debug("{} {}", GML_FAILURE, xml);
        }
    }
    return gml;
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Configuration(org.geotools.xml.Configuration) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXException(org.xml.sax.SAXException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Parser(org.geotools.xml.Parser) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 34 with Result

use of javax.xml.transform.Result in project uPortal by Jasig.

the class ILFBuilder method printNodeToDebug.

private static void printNodeToDebug(Node n, String name) throws TransformerFactoryConfigurationError {
    if (!LOG.isDebugEnabled()) {
        return;
    }
    final StringWriter writer = new StringWriter();
    try {
        final TransformerFactory transFactory = TransformerFactory.newInstance();
        final Transformer trans = transFactory.newTransformer();
        final Source xmlSource = new DOMSource(n);
        final Result transResult = new StreamResult(writer);
        trans.transform(xmlSource, transResult);
        final String xmlStr = writer.toString();
        LOG.debug(name + " DOM Tree:\n\n" + xmlStr);
    } catch (Exception e) {
        LOG.error("Error printing out " + name + " DOM Tree", e);
        final String xmlStr = writer.toString();
        LOG.debug("Partial " + name + " DOM Tree:\n\n" + xmlStr);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) AuthorizationException(org.apereo.portal.AuthorizationException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 35 with Result

use of javax.xml.transform.Result in project stanbol by apache.

the class DOMUtils method printXML.

/**
     * This prints the given DOM document to System.out with indentation and
     * utf-8 encoding.
     *
     * @param doc
     *            a DOM <code>Document</code>
     */
public static void printXML(Document doc) {
    try {
        // prepare the DOM document for writing
        Source source = new DOMSource(doc);
        // prepare the output
        Result result = new StreamResult(System.out);
        // write the DOM document to the file
        // get Transformer
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        xformer.setOutputProperty(OutputKeys.METHOD, "xml");
        // write to System.out
        xformer.transform(source, result);
    } catch (TransformerConfigurationException tce) {
        // error generated during transformer configuration
        System.err.println(tce.getMessage());
        // use the contained exception, if any
        Throwable x = tce;
        if (tce.getException() != null) {
            x = tce.getException();
        }
        x.printStackTrace();
    } catch (TransformerException te) {
        // error generated by the transformer
        System.err.println(te.getMessage());
        // use the contained exception, if any
        Throwable x = te;
        if (te.getException() != null) {
            x = te.getException();
        }
        x.printStackTrace();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Aggregations

Result (javax.xml.transform.Result)80 StreamResult (javax.xml.transform.stream.StreamResult)72 Source (javax.xml.transform.Source)52 Transformer (javax.xml.transform.Transformer)52 DOMSource (javax.xml.transform.dom.DOMSource)42 TransformerFactory (javax.xml.transform.TransformerFactory)33 StringWriter (java.io.StringWriter)24 TransformerException (javax.xml.transform.TransformerException)21 IOException (java.io.IOException)20 StreamSource (javax.xml.transform.stream.StreamSource)20 SAXResult (javax.xml.transform.sax.SAXResult)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 File (java.io.File)15 Document (org.w3c.dom.Document)14 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)11 DOMResult (javax.xml.transform.dom.DOMResult)11 InputSource (org.xml.sax.InputSource)11 SAXException (org.xml.sax.SAXException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9