Search in sources :

Example 11 with StreamResult

use of javax.xml.transform.stream.StreamResult in project jOOQ by jOOQ.

the class XMLDatabase method info.

private InformationSchema info() {
    if (info == null) {
        String xml = getProperties().getProperty(P_XML_FILE);
        String xsl = getProperties().getProperty(P_XSL_FILE);
        InputStream xmlIs = null;
        InputStream xslIs = null;
        log.info("Using XML file", xml);
        try {
            xmlIs = XMLDatabase.class.getResourceAsStream(xml);
            if (xmlIs == null)
                xmlIs = new FileInputStream(xml);
            if (StringUtils.isBlank(xsl)) {
                info = JAXB.unmarshal(new File(xml), InformationSchema.class);
            } else {
                log.info("Using XSL file", xsl);
                xslIs = XMLDatabase.class.getResourceAsStream(xsl);
                if (xslIs == null)
                    xslIs = new FileInputStream(xsl);
                try {
                    StringWriter writer = new StringWriter();
                    TransformerFactory factory = TransformerFactory.newInstance();
                    Transformer transformer = factory.newTransformer(new StreamSource(xslIs));
                    transformer.transform(new StreamSource(xmlIs), new StreamResult(writer));
                    info = JAXB.unmarshal(new StringReader(writer.getBuffer().toString()), InformationSchema.class);
                } catch (TransformerException e) {
                    throw new RuntimeException("Error while transforming XML file " + xml + " with XSL file " + xsl, e);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("Error while opening files " + xml + " or " + xsl, e);
        } finally {
            if (xmlIs != null) {
                try {
                    xmlIs.close();
                } catch (Exception ignore) {
                }
            }
            if (xslIs != null) {
                try {
                    xslIs.close();
                } catch (Exception ignore) {
                }
            }
        }
    }
    return info;
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) InformationSchema(org.jooq.util.xml.jaxb.InformationSchema) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TransformerException(javax.xml.transform.TransformerException) SQLException(java.sql.SQLException) IOException(java.io.IOException) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 12 with StreamResult

use of javax.xml.transform.stream.StreamResult in project hadoop by apache.

the class TestQueueConfigurationParser method testQueueConfigurationParser.

/**
 * test xml generation 
 * @throws ParserConfigurationException
 * @throws Exception 
 */
@Test(timeout = 5000)
public void testQueueConfigurationParser() throws ParserConfigurationException, Exception {
    JobQueueInfo info = new JobQueueInfo("root", "rootInfo");
    JobQueueInfo infoChild1 = new JobQueueInfo("child1", "child1Info");
    JobQueueInfo infoChild2 = new JobQueueInfo("child2", "child1Info");
    info.addChild(infoChild1);
    info.addChild(infoChild2);
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
    Document document = builder.newDocument();
    // test QueueConfigurationParser.getQueueElement 
    Element e = QueueConfigurationParser.getQueueElement(document, info);
    // transform result to string for check
    DOMSource domSource = new DOMSource(e);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    String str = writer.toString();
    assertTrue(str.endsWith("<queue><name>root</name><properties/><state>running</state><queue><name>child1</name><properties/><state>running</state></queue><queue><name>child2</name><properties/><state>running</state></queue></queue>"));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 13 with StreamResult

use of javax.xml.transform.stream.StreamResult in project sonarqube by SonarSource.

the class DebtModelXMLExporter method prettyFormatXml.

private static String prettyFormatXml(String xml) {
    try {
        Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", DEFAULT_INDENT);
        Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));
        StreamResult res = new StreamResult(new ByteArrayOutputStream());
        serializer.transform(xmlSource, res);
        return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray(), StandardCharsets.UTF_8);
    } catch (TransformerException ignored) {
    // Ignore, raw XML will be returned
    }
    return xml;
}
Also used : InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) SAXSource(javax.xml.transform.sax.SAXSource) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) TransformerException(javax.xml.transform.TransformerException)

Example 14 with StreamResult

use of javax.xml.transform.stream.StreamResult in project che by eclipse.

the class Launching method serializeDocumentInt.

/**
     * Serializes a XML document into a string - encoded in UTF8 format,
     * with platform line separators.
     *
     * @param doc document to serialize
     * @return the document as a string
     * @throws TransformerException if an unrecoverable error occurs during the serialization
     * @throws IOException if the encoding attempted to be used is not supported
     */
private static String serializeDocumentInt(Document doc) throws TransformerException, IOException {
    ByteArrayOutputStream s = new ByteArrayOutputStream();
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    DOMSource source = new DOMSource(doc);
    StreamResult outputTarget = new StreamResult(s);
    transformer.transform(source, outputTarget);
    //$NON-NLS-1$
    return s.toString("UTF8");
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 15 with StreamResult

use of javax.xml.transform.stream.StreamResult in project jphp by jphp-compiler.

the class Debugger method write.

protected void write(CommandArguments args, AbstractCommand command) {
    try {
        Document document = xmlBuilder.newDocument();
        command.run(this, args, document);
        StringWriter xmlWriter = new StringWriter();
        StreamResult xmlResult = new StreamResult(xmlWriter);
        transformer.transform(new DOMSource(document), xmlResult);
        byte[] xmlBytes = xmlWriter.toString().getBytes();
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
        out.write(String.valueOf(xmlBytes.length).getBytes());
        out.write("\0".getBytes());
        out.write(xmlBytes);
        out.write("\0".getBytes());
        out.flush();
    } catch (IOException | TransformerException e) {
        throw new DebuggerException(e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) Document(org.w3c.dom.Document)

Aggregations

StreamResult (javax.xml.transform.stream.StreamResult)1328 DOMSource (javax.xml.transform.dom.DOMSource)881 Transformer (javax.xml.transform.Transformer)775 StringWriter (java.io.StringWriter)447 TransformerFactory (javax.xml.transform.TransformerFactory)445 Document (org.w3c.dom.Document)415 TransformerException (javax.xml.transform.TransformerException)366 ByteArrayOutputStream (java.io.ByteArrayOutputStream)337 DocumentBuilder (javax.xml.parsers.DocumentBuilder)332 ByteArrayInputStream (java.io.ByteArrayInputStream)288 IOException (java.io.IOException)266 InputStream (java.io.InputStream)249 Test (org.junit.Test)246 StreamSource (javax.xml.transform.stream.StreamSource)213 XMLStreamReader (javax.xml.stream.XMLStreamReader)185 Source (javax.xml.transform.Source)183 File (java.io.File)175 Element (org.w3c.dom.Element)157 InboundXMLSec (org.apache.xml.security.stax.ext.InboundXMLSec)149 XMLSecurityProperties (org.apache.xml.security.stax.ext.XMLSecurityProperties)149