Search in sources :

Example 36 with Templates

use of javax.xml.transform.Templates in project nhin-d by DirectProject.

the class XslConversion method run.

/**
     * Perform the XSL conversion using the provided map file and message.
     * 
     * @param mapFile
     *            The map file.
     * @param message
     *            The message.
     * @return an XSL conversion.
     * @throws Exception
     */
public String run(String mapFile, String message) throws Exception {
    long start = System.currentTimeMillis();
    String retXml = "";
    Transformer transformer = null;
    try {
        if (conversions.containsKey(mapFile)) {
            Templates temp = conversions.get(mapFile);
            transformer = temp.newTransformer();
            LOGGER.info("From xsl cache");
        } else {
            synchronized (conversions) {
                if (!conversions.containsKey(mapFile)) {
                    /*
                         * Use the static TransformerFactory.newInstance()
                         * method to instantiate a TransformerFactory. The
                         * javax.xml.transform.TransformerFactory system
                         * property setting determines the actual class to
                         * instantiate --
                         * org.apache.xalan.transformer.TransformerImpl.
                         */
                    TransformerFactory tFactory = TransformerFactory.newInstance();
                    /*
                         * Use the TransformerFactory to instantiate a Template
                         * that is thread safe for use in generating Transfomers
                         */
                    InputStream is = this.getClass().getClassLoader().getResourceAsStream(mapFile);
                    if (is == null) {
                        LOGGER.info("Mapfile did not read " + mapFile);
                    }
                    Templates temp = tFactory.newTemplates(new StreamSource(is));
                    transformer = temp.newTransformer();
                    conversions.put(mapFile, temp);
                }
            }
        }
        CharArrayWriter to = new CharArrayWriter();
        transformer.transform(new StreamSource(new CharArrayReader(message.toCharArray())), new StreamResult(to));
        retXml = to.toString();
    } catch (TransformerConfigurationException e) {
        LOGGER.error("Exception occured during XSL conversion", e);
        throw e;
    } catch (TransformerException e) {
        LOGGER.error("Exception occured during XSL conversion", e);
        throw e;
    }
    if (LOGGER.isInfoEnabled()) {
        long elapse = System.currentTimeMillis() - start;
        LOGGER.info("Started at " + new Timestamp(start).toString());
        LOGGER.info("Elapsed conversion time was " + elapse + "ms");
    }
    return retXml;
}
Also used : Transformer(javax.xml.transform.Transformer) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Templates(javax.xml.transform.Templates) Timestamp(java.sql.Timestamp) CharArrayWriter(java.io.CharArrayWriter) CharArrayReader(java.io.CharArrayReader) TransformerException(javax.xml.transform.TransformerException)

Example 37 with Templates

use of javax.xml.transform.Templates in project intellij-community by JetBrains.

the class XsltDocumentationProvider method getTemplate.

private Templates getTemplate() throws TransformerConfigurationException, IOException {
    Templates t = com.intellij.reference.SoftReference.dereference(myTemplates);
    if (t == null) {
        t = TransformerFactory.newInstance().newTemplates(makeSource("resources/documentation.xsl"));
        myTemplates = new SoftReference<>(t);
    }
    return t;
}
Also used : Templates(javax.xml.transform.Templates)

Example 38 with Templates

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

the class XsltBuilder method setTransformerSource.

/**
     * Sets the XSLT transformer from a Source
     *
     * @param source  the source
     * @throws TransformerConfigurationException is thrown if creating a XSLT transformer failed.
     */
public void setTransformerSource(Source source) throws TransformerConfigurationException {
    TransformerFactory factory = converter.getTransformerFactory();
    if (errorListener != null) {
        factory.setErrorListener(errorListener);
    } else {
        // use a logger error listener so users can see from the logs what the error may be
        factory.setErrorListener(new XsltErrorListener());
    }
    if (getUriResolver() != null) {
        factory.setURIResolver(getUriResolver());
    }
    // Check that the call to newTemplates() returns a valid template instance.
    // In case of an xslt parse error, it will return null and we should stop the
    // deployment and raise an exception as the route will not be setup properly.
    Templates templates = factory.newTemplates(source);
    if (templates != null) {
        setTemplate(templates);
    } else {
        throw new TransformerConfigurationException("Error creating XSLT template. " + "This is most likely be caused by a XML parse error. " + "Please verify your XSLT file configured.");
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Templates(javax.xml.transform.Templates)

Example 39 with Templates

use of javax.xml.transform.Templates in project robovm by robovm.

the class TransformerFactoryImpl method newTransformer.

/**
   * Process the source into a Transformer object.  Care must
   * be given to know that this object can not be used concurrently
   * in multiple threads.
   *
   * @param source An object that holds a URL, input stream, etc.
   *
   * @return A Transformer object capable of
   * being used for transformation purposes in a single thread.
   *
   * @throws TransformerConfigurationException May throw this during the parse when it
   *            is constructing the Templates object and fails.
   */
public Transformer newTransformer(Source source) throws TransformerConfigurationException {
    try {
        Templates tmpl = newTemplates(source);
        /* this can happen if an ErrorListener is present and it doesn't
         throw any exception in fatalError. 
         The spec says: "a Transformer must use this interface
         instead of throwing an exception" - the newTemplates() does
         that, and returns null.
      */
        if (tmpl == null)
            return null;
        Transformer transformer = tmpl.newTransformer();
        transformer.setURIResolver(m_uriResolver);
        return transformer;
    } catch (TransformerConfigurationException ex) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(ex);
                // TODO: but the API promises to never return null...
                return null;
            } catch (TransformerConfigurationException ex1) {
                throw ex1;
            } catch (TransformerException ex1) {
                throw new TransformerConfigurationException(ex1);
            }
        }
        throw ex;
    }
}
Also used : Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Templates(javax.xml.transform.Templates) TransformerException(javax.xml.transform.TransformerException)

Example 40 with Templates

use of javax.xml.transform.Templates in project jdk8u_jdk by JetBrains.

the class XslSubstringTest method testTransform.

private String testTransform(String xsl) throws Exception {
    //Prepare sources for transormation
    Source src = new StreamSource(new StringReader(xml));
    Source xslsrc = new StreamSource(new StringReader(xslPre + xsl + xslPost));
    //Create factory, template and transformer
    TransformerFactory tf = TransformerFactory.newInstance();
    Templates tmpl = tf.newTemplates(xslsrc);
    Transformer t = tmpl.newTransformer();
    //Prepare output stream
    StringWriter xmlResultString = new StringWriter();
    StreamResult xmlResultStream = new StreamResult(xmlResultString);
    //Transform
    t.transform(src, xmlResultStream);
    return xmlResultString.toString().trim();
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) Templates(javax.xml.transform.Templates) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Aggregations

Templates (javax.xml.transform.Templates)60 TransformerFactory (javax.xml.transform.TransformerFactory)25 StreamSource (javax.xml.transform.stream.StreamSource)25 Transformer (javax.xml.transform.Transformer)20 Source (javax.xml.transform.Source)19 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)17 StreamResult (javax.xml.transform.stream.StreamResult)16 TransformerException (javax.xml.transform.TransformerException)10 InputStream (java.io.InputStream)8 IOException (java.io.IOException)7 OutputStream (java.io.OutputStream)7 DOMResult (javax.xml.transform.dom.DOMResult)7 DOMSource (javax.xml.transform.dom.DOMSource)7 File (java.io.File)5 Result (javax.xml.transform.Result)5 SAXSource (javax.xml.transform.sax.SAXSource)5 FileOutputStream (java.io.FileOutputStream)4 StringReader (java.io.StringReader)4 BufferedOutputStream (java.io.BufferedOutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3