Search in sources :

Example 66 with Transformer

use of javax.xml.transform.Transformer in project voltdb by VoltDB.

the class JDBCSQLXML method init.

/**
     * Initializes this object's SQLXML value from the given Source
     * object. <p>
     *
     * @param source the Source representing the SQLXML value
     * @throws SQLException if the argument does not represent a
     *      valid SQLXML value
     */
protected void init(Source source) throws SQLException {
    if (source == null) {
        throw Util.nullArgument("source");
    }
    Transformer transformer = JDBCSQLXML.getIdentityTransformer();
    StreamResult result = new StreamResult();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos;
    try {
        gzos = new GZIPOutputStream(baos);
    } catch (IOException ex) {
        throw Exceptions.transformFailed(ex);
    }
    result.setOutputStream(gzos);
    try {
        transformer.transform(source, result);
    } catch (TransformerException ex) {
        throw Exceptions.transformFailed(ex);
    }
    try {
        gzos.close();
    } catch (IOException ex) {
        throw Exceptions.transformFailed(ex);
    }
    byte[] data = baos.toByteArray();
    setGZipData(data);
    setReadable(true);
    setWritable(false);
}
Also used : Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) GZIPOutputStream(java.util.zip.GZIPOutputStream) ClosableByteArrayOutputStream(org.hsqldb_voltpatches.lib.ClosableByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) TransformerException(javax.xml.transform.TransformerException)

Example 67 with Transformer

use of javax.xml.transform.Transformer in project voltdb by VoltDB.

the class JDBCSQLXML method createDOMSource.

/**
     * Retrieves a new DOMSource for reading the XML value designated by this
     * SQLXML instance. <p>
     *
     * @param sourceClass The class of the source
     * @throws java.sql.SQLException if there is an error processing the XML
     *      value or if the given <tt>sourceClass</tt> is not supported.
     * @return a new DOMSource for reading the XML value designated by this
     *      SQLXML instance
     */
@SuppressWarnings("unchecked")
protected <T extends Source> T createDOMSource(Class<T> sourceClass) throws SQLException {
    DOMSource source = null;
    try {
        source = (sourceClass == null) ? new DOMSource() : (DOMSource) sourceClass.newInstance();
    } catch (SecurityException ex) {
        throw Exceptions.sourceInstantiation(ex);
    } catch (IllegalAccessException ex) {
        throw Exceptions.sourceInstantiation(ex);
    } catch (InstantiationException ex) {
        throw Exceptions.sourceInstantiation(ex);
    } catch (ClassCastException ex) {
        throw Exceptions.sourceInstantiation(ex);
    }
    Transformer transformer = JDBCSQLXML.getIdentityTransformer();
    InputStream inputStream = this.getBinaryStreamImpl();
    StreamSource streamSource = new StreamSource();
    DOMResult domResult = new DOMResult();
    streamSource.setInputStream(inputStream);
    try {
        transformer.transform(streamSource, domResult);
    } catch (TransformerException ex) {
        throw Exceptions.transformFailed(ex);
    }
    source.setNode(domResult.getNode());
    return (T) source;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) TransformerException(javax.xml.transform.TransformerException)

Example 68 with Transformer

use of javax.xml.transform.Transformer in project pcgen by PCGen.

the class FopTask method run.

/**
	 * Run the FO to PDF/AWT conversion. This automatically closes any provided OutputStream for
	 * this FopTask.
	 */
@Override
public void run() {
    try (OutputStream out = outputStream) {
        userAgent.setProducer("PC Gen Character Generator");
        userAgent.setAuthor(System.getProperty("user.name"));
        userAgent.setCreationDate(new Date());
        userAgent.getEventBroadcaster().addEventListener(new FOPEventListener());
        String mimeType;
        if (renderer != null) {
            userAgent.setKeywords("PCGEN FOP PREVIEW");
            mimeType = MimeConstants.MIME_FOP_AWT_PREVIEW;
        } else {
            userAgent.setKeywords("PCGEN FOP PDF");
            mimeType = MimeConstants.MIME_PDF;
        }
        Fop fop;
        if (out != null) {
            fop = FOP_FACTORY.newFop(mimeType, userAgent, out);
        } else {
            fop = FOP_FACTORY.newFop(mimeType, userAgent);
        }
        Transformer transformer;
        if (xsltSource != null) {
            transformer = TRANS_FACTORY.newTransformer(xsltSource);
        } else {
            // identity transformer		
            transformer = TRANS_FACTORY.newTransformer();
        }
        transformer.setErrorListener(new FOPErrorListener());
        transformer.transform(inputSource, new SAXResult(fop.getDefaultHandler()));
    } catch (TransformerException | FOPException | IOException e) {
        errorBuilder.append(e.getMessage()).append(Constants.LINE_SEPARATOR);
        Logging.errorPrint("Exception in FopTask:run", e);
    } catch (RuntimeException ex) {
        errorBuilder.append(ex.getMessage()).append(Constants.LINE_SEPARATOR);
        Logging.errorPrint("Unexpected exception in FopTask:run: ", ex);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) Fop(org.apache.fop.apps.Fop) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Date(java.util.Date) FOPException(org.apache.fop.apps.FOPException) SAXResult(javax.xml.transform.sax.SAXResult) TransformerException(javax.xml.transform.TransformerException)

Example 69 with Transformer

use of javax.xml.transform.Transformer in project rhino by PLOS.

the class AbstractXpathReader method recoverXml.

private static String recoverXml(Node node) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(node), new StreamResult(outputStream));
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    // TODO: Encoding?
    return new String(outputStream.toByteArray());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TransformerException(javax.xml.transform.TransformerException)

Example 70 with Transformer

use of javax.xml.transform.Transformer in project rhino by PLOS.

the class AuthorsXmlExtractor method getAsXMLString.

private static String getAsXMLString(Node node) throws TransformerException {
    final Transformer tf = TransformerFactory.newInstance().newTransformer();
    final StringWriter stringWriter = new StringWriter();
    tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    tf.transform(new DOMSource(node), new StreamResult(stringWriter));
    return stringWriter.toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult)

Aggregations

Transformer (javax.xml.transform.Transformer)449 StreamResult (javax.xml.transform.stream.StreamResult)354 DOMSource (javax.xml.transform.dom.DOMSource)272 TransformerFactory (javax.xml.transform.TransformerFactory)222 TransformerException (javax.xml.transform.TransformerException)175 StringWriter (java.io.StringWriter)153 Document (org.w3c.dom.Document)114 IOException (java.io.IOException)105 StreamSource (javax.xml.transform.stream.StreamSource)98 Source (javax.xml.transform.Source)97 DocumentBuilder (javax.xml.parsers.DocumentBuilder)70 File (java.io.File)66 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)64 SAXException (org.xml.sax.SAXException)62 Element (org.w3c.dom.Element)59 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)57 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)57 ByteArrayOutputStream (java.io.ByteArrayOutputStream)53 StringReader (java.io.StringReader)52 Result (javax.xml.transform.Result)52