Search in sources :

Example 26 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project camel by apache.

the class SchematronEndpoint method createTransformerFactory.

private void createTransformerFactory() throws ClassNotFoundException {
    // provide the class loader of this component to work in OSGi environments
    Class<TransformerFactory> factoryClass = getCamelContext().getClassResolver().resolveMandatoryClass(SAXON_TRANSFORMER_FACTORY_CLASS_NAME, TransformerFactory.class, SchematronComponent.class.getClassLoader());
    LOG.debug("Using TransformerFactoryClass {}", factoryClass);
    transformerFactory = getCamelContext().getInjector().newInstance(factoryClass);
    transformerFactory.setURIResolver(new ClassPathURIResolver(Constants.SCHEMATRON_TEMPLATES_ROOT_DIR, this.uriResolver));
    transformerFactory.setAttribute(LINE_NUMBERING, true);
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) ClassPathURIResolver(org.apache.camel.component.schematron.processor.ClassPathURIResolver)

Example 27 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project liquibase by liquibase.

the class DefaultXmlWriter method write.

@Override
public void write(Document doc, OutputStream outputStream) throws IOException {
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        try {
            factory.setAttribute("indent-number", 4);
        } catch (Exception e) {
            //guess we can't set it, that's ok
            ;
        }
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
        //need to nest outputStreamWriter to get around JDK 5 bug.  See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6296446
        OutputStreamWriter writer = new OutputStreamWriter(outputStream, LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        writer.flush();
        writer.close();
    } catch (TransformerException e) {
        throw new IOException(e.getMessage());
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) TransformerException(javax.xml.transform.TransformerException)

Example 28 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project cas by apereo.

the class SamlUtils method transformSamlObject.

/**
     * Transform saml object to String.
     *
     * @param configBean the config bean
     * @param samlObject the saml object
     * @return the string
     * @throws SamlException the saml exception
     */
public static StringWriter transformSamlObject(final OpenSamlConfigBean configBean, final XMLObject samlObject) throws SamlException {
    final StringWriter writer = new StringWriter();
    try {
        final Marshaller marshaller = configBean.getMarshallerFactory().getMarshaller(samlObject.getElementQName());
        if (marshaller != null) {
            final Element element = marshaller.marshall(samlObject);
            final DOMSource domSource = new DOMSource(element);
            final StreamResult result = new StreamResult(writer);
            final TransformerFactory tf = TransformerFactory.newInstance();
            final Transformer transformer = tf.newTransformer();
            transformer.transform(domSource, result);
        }
    } catch (final Exception e) {
        throw new SamlException(e.getMessage(), e);
    }
    return writer;
}
Also used : Marshaller(org.opensaml.core.xml.io.Marshaller) 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) Element(org.w3c.dom.Element)

Example 29 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project asciidoctor-fopub by asciidoctor.

the class InputHandler method transformTo.

/**
     * Transforms the input document to the input format expected by FOP using XSLT.
     * @param result the Result object where the result of the XSL transformation is sent to
     * @throws FOPException in case of an error during processing
     */
protected void transformTo(Result result) throws FOPException {
    try {
        // Setup XSLT
        TransformerFactory factory = TransformerFactory.newInstance();
        if (uriResolver != null) {
            factory.setURIResolver(uriResolver);
        }
        factory.setErrorListener(this);
        Transformer transformer;
        Source xsltSource = createXSLTSource();
        if (xsltSource == null) {
            // FO Input
            transformer = factory.newTransformer();
        } else {
            // XML/XSLT input
            transformer = factory.newTransformer(xsltSource);
            // Set the value of parameters, if any, defined for stylesheet
            if (xsltParams != null) {
                for (int i = 0; i < xsltParams.size(); i += 2) {
                    transformer.setParameter((String) xsltParams.elementAt(i), (String) xsltParams.elementAt(i + 1));
                }
            }
        }
        transformer.setErrorListener(this);
        // Create a SAXSource from the input Source file
        Source src = createMainSource();
        // Start XSLT transformation and FOP processing
        transformer.transform(src, result);
    } catch (Exception e) {
        throw new FOPException(e);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) TransformerException(javax.xml.transform.TransformerException) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 30 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project hazelcast by hazelcast.

the class ConfigXmlGenerator method format.

private String format(final String input, int indent) {
    if (!formatted) {
        return input;
    }
    StreamResult xmlOutput = null;
    try {
        final Source xmlInput = new StreamSource(new StringReader(input));
        xmlOutput = new StreamResult(new StringWriter());
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        /* Older versions of Xalan still use this method of setting indent values.
            * Attempt to make this work but don't completely fail if it's a problem.
            */
        try {
            transformerFactory.setAttribute("indent-number", indent);
        } catch (IllegalArgumentException e) {
            if (LOGGER.isFinestEnabled()) {
                LOGGER.finest("Failed to set indent-number attribute; cause: " + e.getMessage());
            }
        }
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        /* Newer versions of Xalan will look for a fully-qualified output property in order to specify amount of
            * indentation to use.  Attempt to make this work as well but again don't completely fail if it's a problem.
            */
        try {
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
        } catch (IllegalArgumentException e) {
            if (LOGGER.isFinestEnabled()) {
                LOGGER.finest("Failed to set indent-amount property; cause: " + e.getMessage());
            }
        }
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        LOGGER.warning(e);
        return input;
    } finally {
        if (xmlOutput != null) {
            closeResource(xmlOutput.getWriter());
        }
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StringWriter(java.io.StringWriter) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Aggregations

TransformerFactory (javax.xml.transform.TransformerFactory)188 Transformer (javax.xml.transform.Transformer)158 StreamResult (javax.xml.transform.stream.StreamResult)137 DOMSource (javax.xml.transform.dom.DOMSource)113 TransformerException (javax.xml.transform.TransformerException)63 StreamSource (javax.xml.transform.stream.StreamSource)60 StringWriter (java.io.StringWriter)58 Document (org.w3c.dom.Document)53 IOException (java.io.IOException)42 Source (javax.xml.transform.Source)41 DocumentBuilder (javax.xml.parsers.DocumentBuilder)37 File (java.io.File)36 Element (org.w3c.dom.Element)31 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)29 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)27 Result (javax.xml.transform.Result)24 SAXException (org.xml.sax.SAXException)24 StringReader (java.io.StringReader)23 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)23 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20