Search in sources :

Example 21 with Transformer

use of javax.xml.transform.Transformer in project opennms by OpenNMS.

the class OnmsPdfViewResolver method resolveView.

@Override
public void resolveView(ServletRequest request, ServletResponse response, Preferences preferences, Object viewData) throws Exception {
    InputStream is = new ByteArrayInputStream(((String) viewData).getBytes(StandardCharsets.UTF_8));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    FopFactory fopFactory = FopFactory.newInstance();
    fopFactory.setStrictValidation(false);
    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
    TransformerFactory tfact = TransformerFactory.newInstance();
    Transformer transformer = tfact.newTransformer();
    Source src = new StreamSource(is);
    Result res = new SAXResult(fop.getDefaultHandler());
    transformer.transform(src, res);
    byte[] contents = out.toByteArray();
    response.setContentLength(contents.length);
    response.getOutputStream().write(contents);
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) FOUserAgent(org.apache.fop.apps.FOUserAgent) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Fop(org.apache.fop.apps.Fop) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FopFactory(org.apache.fop.apps.FopFactory) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) SAXResult(javax.xml.transform.sax.SAXResult) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 22 with Transformer

use of javax.xml.transform.Transformer in project tdi-studio-se by Talend.

the class FeaturesModel method internalGetContent.

private InputStream internalGetContent() throws Exception {
    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Element features = document.createElement("features");
    features.setAttribute("xmlns", "http://karaf.apache.org/xmlns/features/v1.0.0");
    features.setAttribute("name", getArtifactId());
    document.appendChild(features);
    Element feature = document.createElement("feature");
    feature.setAttribute("name", getArtifactId());
    feature.setAttribute("version", getVersion());
    features.appendChild(feature);
    // add sub features
    for (FeatureModel fm : subFeatures) {
        Element subFeature = document.createElement("feature");
        if (null != fm.getVersion() && !fm.getVersion().isEmpty()) {
            subFeature.setAttribute("version", fm.getVersion());
        }
        subFeature.appendChild(document.createTextNode(fm.getArtifactId()));
        feature.appendChild(subFeature);
    }
    // add sub bundles
    for (BundleModel bm : subBundles) {
        Element bundle = document.createElement("bundle");
        bundle.appendChild(document.createTextNode(toBundleString(bm)));
        feature.appendChild(bundle);
    }
    if (null == contexts || contexts.isEmpty()) {
        // add config
        Element config = document.createElement("config");
        config.setAttribute("name", configName);
        StringBuilder sb = new StringBuilder("talendcontext=\"");
        for (int i = 0; i < contextList.length; i++) {
            if (i != 0) {
                sb.append(',');
            }
            sb.append(contextList[i]);
        }
        sb.append('"');
        config.appendChild(document.createTextNode(sb.toString()));
        feature.appendChild(config);
    } else {
        // add contexts config
        for (Map.Entry<String, Map<String, String>> context : contexts.entrySet()) {
            Element config = document.createElement("config");
            config.setAttribute("name", name + ".talendcontext." + context.getKey());
            StringBuilder sb = new StringBuilder("\n");
            for (Map.Entry<String, String> property : context.getValue().entrySet()) {
                sb.append(property.getKey());
                sb.append('=');
                sb.append(property.getValue());
                sb.append('\n');
            }
            config.appendChild(document.createTextNode(sb.toString()));
            feature.appendChild(config);
        }
    }
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(document), new StreamResult(os));
    return new ByteArrayInputStream(os.toByteArray());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Example 23 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 24 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 25 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)

Aggregations

Transformer (javax.xml.transform.Transformer)1034 StreamResult (javax.xml.transform.stream.StreamResult)799 DOMSource (javax.xml.transform.dom.DOMSource)644 TransformerFactory (javax.xml.transform.TransformerFactory)511 TransformerException (javax.xml.transform.TransformerException)398 StringWriter (java.io.StringWriter)322 IOException (java.io.IOException)264 Document (org.w3c.dom.Document)260 StreamSource (javax.xml.transform.stream.StreamSource)204 Source (javax.xml.transform.Source)188 File (java.io.File)162 DocumentBuilder (javax.xml.parsers.DocumentBuilder)149 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)145 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)142 Element (org.w3c.dom.Element)136 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)124 SAXException (org.xml.sax.SAXException)112 StringReader (java.io.StringReader)108 Result (javax.xml.transform.Result)106 ByteArrayOutputStream (java.io.ByteArrayOutputStream)104