Search in sources :

Example 56 with Templates

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

the class XSLTJaxbProvider method getInTemplates.

protected Templates getInTemplates(Annotation[] anns, MediaType mt) {
    Templates t = createTemplatesFromContext();
    if (t != null) {
        return t;
    }
    t = inTemplates != null ? inTemplates : inMediaTemplates != null ? inMediaTemplates.get(mt.getType() + "/" + mt.getSubtype()) : null;
    if (t == null) {
        t = getAnnotationTemplates(anns);
    }
    return t;
}
Also used : Templates(javax.xml.transform.Templates)

Example 57 with Templates

use of javax.xml.transform.Templates in project wcomponents by BorderTech.

the class TransformXMLInterceptor method initTemplates.

/**
 * Statically initialize the XSLT templates that are cached for all future transforms.
 *
 * @return the XSLT Templates.
 */
private static Templates initTemplates() {
    try {
        URL xsltURL = ThemeUtil.class.getResource(RESOURCE_NAME);
        if (xsltURL != null) {
            try (InputStream inStream = xsltURL.openStream()) {
                Source xsltSource = new StreamSource(inStream, xsltURL.toExternalForm());
                TransformerFactory factory = new net.sf.saxon.TransformerFactoryImpl();
                Templates templates = factory.newTemplates(xsltSource);
                LOG.debug("Generated XSLT templates for: " + RESOURCE_NAME);
                return templates;
            }
        } else {
            // Server-side XSLT enabled but theme resource not on classpath.
            throw new IllegalStateException(RESOURCE_NAME + " not on classpath");
        }
    } catch (IOException | TransformerConfigurationException ex) {
        throw new SystemException("Could not create transformer for " + RESOURCE_NAME, ex);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Templates(javax.xml.transform.Templates) IOException(java.io.IOException) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SystemException(com.github.bordertech.wcomponents.util.SystemException)

Example 58 with Templates

use of javax.xml.transform.Templates in project mondrian by pentaho.

the class DomBuilder method debug.

public static void debug(Document doc) {
    try {
        TransformerFactory tf = TransformerFactory.newInstance();
        StringReader input = new StringReader(PRETTY_PRINTER);
        Templates templates = tf.newTemplates(new StreamSource(input));
        OutputStream result = new ByteArrayOutputStream();
        templates.newTransformer().transform(new DOMSource(doc), new StreamResult(result));
        LOGGER.debug(result.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) Templates(javax.xml.transform.Templates)

Example 59 with Templates

use of javax.xml.transform.Templates in project mycore by MyCoRe-Org.

the class MCRXSLTransformer method getTransformHandlerList.

protected LinkedList<TransformerHandler> getTransformHandlerList(MCRParameterCollector parameterCollector) throws TransformerConfigurationException, SAXException, ParserConfigurationException {
    checkTemplateUptodate();
    LinkedList<TransformerHandler> xslSteps = new LinkedList<>();
    // every transformhandler shares the same ErrorListener instance
    MCRErrorListener errorListener = MCRErrorListener.getInstance();
    for (Templates template : templates) {
        TransformerHandler handler = tFactory.newTransformerHandler(template);
        parameterCollector.setParametersTo(handler.getTransformer());
        handler.getTransformer().setErrorListener(errorListener);
        if (TRACE_LISTENER_ENABLED) {
            TransformerImpl transformer = (TransformerImpl) handler.getTransformer();
            TraceManager traceManager = transformer.getTraceManager();
            try {
                traceManager.addTraceListener(TRACE_LISTENER);
            } catch (TooManyListenersException e) {
                LOGGER.warn("Could not add MCRTraceListener.", e);
            }
        }
        if (!xslSteps.isEmpty()) {
            Result result = new SAXResult(handler);
            xslSteps.getLast().setResult(result);
        }
        xslSteps.add(handler);
    }
    return xslSteps;
}
Also used : TransformerImpl(org.apache.xalan.transformer.TransformerImpl) TransformerHandler(javax.xml.transform.sax.TransformerHandler) TooManyListenersException(java.util.TooManyListenersException) MCRErrorListener(org.mycore.common.xsl.MCRErrorListener) SAXResult(javax.xml.transform.sax.SAXResult) Templates(javax.xml.transform.Templates) TraceManager(org.apache.xalan.trace.TraceManager) LinkedList(java.util.LinkedList) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult)

Example 60 with Templates

use of javax.xml.transform.Templates in project symja_android_library by axkr.

the class StylesheetManager method getStylesheet.

/**
 * Obtains the XSLT stylesheet at the given ClassPathURI, using the {@link StylesheetCache} (if
 * set) to cache stylesheets for efficiency.
 *
 * @param classPathUri location of the XSLT stylesheet in the ClassPath, following the URI scheme
 *     in {@link ClassPathURIResolver}.
 * @param requireXSLT20 if false uses the JAXP default {@link TransformerFactory}, otherwise
 *     specifies that we require an XSLT 2.0-compliant transformer, of which the only currently
 *     supported implementation is SAXON 9.x.
 * @return compiled XSLT stylesheet.
 */
public Templates getStylesheet(final String classPathUri, final boolean requireXSLT20) {
    Templates result;
    if (stylesheetCache == null) {
        result = compileInternalStylesheet(classPathUri, requireXSLT20);
    } else {
        synchronized (stylesheetCache) {
            result = stylesheetCache.getStylesheet(classPathUri);
            if (result == null) {
                result = compileInternalStylesheet(classPathUri, requireXSLT20);
                stylesheetCache.putStylesheet(classPathUri, result);
            }
        }
    }
    return result;
}
Also used : Templates(javax.xml.transform.Templates)

Aggregations

Templates (javax.xml.transform.Templates)61 TransformerFactory (javax.xml.transform.TransformerFactory)26 StreamSource (javax.xml.transform.stream.StreamSource)25 Transformer (javax.xml.transform.Transformer)21 Source (javax.xml.transform.Source)20 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)18 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