Search in sources :

Example 51 with StreamResult

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

the class XStreamMarshallerTests method marshalStreamResultOutputStream.

@Test
public void marshalStreamResultOutputStream() throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(os);
    marshaller.marshal(flight, result);
    String s = new String(os.toByteArray(), "UTF-8");
    assertThat("Marshaller writes invalid StreamResult", s, isSimilarTo(EXPECTED_STRING));
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 52 with StreamResult

use of javax.xml.transform.stream.StreamResult in project dbeaver by serge-rider.

the class DBDDocumentXML method serializeDocument.

@Override
public void serializeDocument(@NotNull DBRProgressMonitor monitor, @NotNull OutputStream stream, String encoding) throws DBException {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        Result output = new StreamResult(new OutputStreamWriter(stream, encoding));
        transformer.transform(new DOMSource(document), output);
    } catch (Exception e) {
        throw new DBException("Error serializing XML document", e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) DBException(org.jkiss.dbeaver.DBException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) DOMResult(javax.xml.transform.dom.DOMResult)

Example 53 with StreamResult

use of javax.xml.transform.stream.StreamResult in project nokogiri by sparklemotion.

the class XmlRelaxng method getSchema.

private Schema getSchema(Source source, ThreadContext context) {
    InputStream is;
    VerifierFactory factory = new com.thaiopensource.relaxng.jarv.VerifierFactoryImpl();
    if (source instanceof StreamSource) {
        StreamSource ss = (StreamSource) source;
        is = ss.getInputStream();
    } else {
        //if (this.source instanceof DOMSource)
        DOMSource ds = (DOMSource) source;
        StringWriter xmlAsWriter = new StringWriter();
        StreamResult result = new StreamResult(xmlAsWriter);
        try {
            TransformerFactory.newInstance().newTransformer().transform(ds, result);
        } catch (TransformerConfigurationException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        } catch (TransformerException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        }
        try {
            is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        }
    }
    try {
        return factory.compileSchema(is);
    } catch (VerifierConfigurationException ex) {
        throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
    } catch (SAXException ex) {
        throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
    } catch (IOException ex) {
        throw context.getRuntime().newIOError(ex.getMessage());
    }
}
Also used : VerifierFactory(org.iso_relax.verifier.VerifierFactory) DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) VerifierConfigurationException(org.iso_relax.verifier.VerifierConfigurationException) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) TransformerException(javax.xml.transform.TransformerException)

Example 54 with StreamResult

use of javax.xml.transform.stream.StreamResult in project nokogiri by sparklemotion.

the class XsltStylesheet method retryXsltTransformation.

private String retryXsltTransformation(ThreadContext context, IRubyObject[] args, DOMSource domSource, NokogiriXsltErrorListener elistener) throws TransformerException, IOException {
    Templates templates = getTemplatesFromStreamSource();
    Transformer transf = templates.newTransformer();
    transf.setErrorListener(elistener);
    if (args.length > 1) {
        addParametersToTransformer(context, transf, args[1]);
    }
    PipedWriter pwriter = new PipedWriter();
    PipedReader preader = new PipedReader();
    pwriter.connect(preader);
    StreamResult result = new StreamResult(pwriter);
    transf.transform(domSource, result);
    char[] cbuf = new char[1024];
    int len = preader.read(cbuf, 0, 1024);
    StringBuilder builder = new StringBuilder(len);
    builder.append(cbuf, 0, len);
    // judge from the first chunk
    htmlish = isHtml(builder);
    while (len == 1024) {
        len = preader.read(cbuf, 0, 1024);
        if (len > 0) {
            builder.append(cbuf, 0, len);
        }
    }
    preader.close();
    pwriter.close();
    return builder.toString();
}
Also used : PipedWriter(java.io.PipedWriter) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Templates(javax.xml.transform.Templates) PipedReader(java.io.PipedReader)

Example 55 with StreamResult

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

the class XQueryBuilder method evaluateAsBytes.

public byte[] evaluateAsBytes(Exchange exchange) throws Exception {
    LOG.debug("evaluateAsBytes: {} for exchange: {}", expression, exchange);
    initialize(exchange);
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    Result result = new StreamResult(buffer);
    getExpression().pull(createDynamicContext(exchange), result, properties);
    byte[] answer = buffer.toByteArray();
    buffer.close();
    return answer;
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) DOMResult(javax.xml.transform.dom.DOMResult)

Aggregations

StreamResult (javax.xml.transform.stream.StreamResult)448 Transformer (javax.xml.transform.Transformer)267 DOMSource (javax.xml.transform.dom.DOMSource)234 StringWriter (java.io.StringWriter)206 TransformerFactory (javax.xml.transform.TransformerFactory)138 TransformerException (javax.xml.transform.TransformerException)125 Document (org.w3c.dom.Document)103 IOException (java.io.IOException)94 StreamSource (javax.xml.transform.stream.StreamSource)88 Source (javax.xml.transform.Source)74 Test (org.junit.Test)73 DocumentBuilder (javax.xml.parsers.DocumentBuilder)65 ByteArrayOutputStream (java.io.ByteArrayOutputStream)64 Element (org.w3c.dom.Element)59 File (java.io.File)58 Result (javax.xml.transform.Result)57 StringReader (java.io.StringReader)56 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)53 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)50 ByteArrayInputStream (java.io.ByteArrayInputStream)44