Search in sources :

Example 1 with SetupException

use of org.apache.cocoon.pipeline.SetupException in project syncope by apache.

the class XSLTTransformer method load.

private void load(final Source source, final String localCacheKey, final Map<String, Object> attributes) {
    LOG.debug("{} local cache miss: {}", getClass().getSimpleName(), localCacheKey);
    // XSLT has to be parsed
    final SAXTransformerFactory transformerFactory;
    if (attributes == null || attributes.isEmpty()) {
        transformerFactory = TRAX_FACTORY;
    } else {
        transformerFactory = createNewSAXTransformerFactory();
        attributes.forEach((key, values) -> {
            transformerFactory.setAttribute(key, values);
        });
    }
    try {
        this.templates = transformerFactory.newTemplates(source);
    } catch (TransformerConfigurationException e) {
        throw new SetupException("Impossible to read XSLT from '" + source + "', see nested exception", e);
    }
}
Also used : SetupException(org.apache.cocoon.pipeline.SetupException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory)

Example 2 with SetupException

use of org.apache.cocoon.pipeline.SetupException in project syncope by apache.

the class XSLTTransformer method setSAXConsumer.

/**
 * {@inheritDoc}
 */
@Override
protected void setSAXConsumer(final SAXConsumer consumer) {
    TransformerHandler transformerHandler;
    try {
        transformerHandler = TRAX_FACTORY.newTransformerHandler(this.templates);
    } catch (Exception e) {
        throw new SetupException("Could not initialize transformer handler.", e);
    }
    if (this.parameters != null) {
        final Transformer transformer = transformerHandler.getTransformer();
        this.parameters.forEach((name, values) -> {
            // is valid XSLT parameter name
            if (XSLT_PARAMETER_NAME_PATTERN.matcher(name).matches()) {
                transformer.setParameter(name, values);
            }
        });
    }
    final SAXResult result = new SAXResult();
    result.setHandler(consumer);
    // According to TrAX specs, all TransformerHandlers are LexicalHandlers
    result.setLexicalHandler(consumer);
    transformerHandler.setResult(result);
    final SAXConsumerAdapter saxConsumerAdapter = new SAXConsumerAdapter();
    saxConsumerAdapter.setContentHandler(transformerHandler);
    super.setSAXConsumer(saxConsumerAdapter);
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) SetupException(org.apache.cocoon.pipeline.SetupException) Transformer(javax.xml.transform.Transformer) AbstractSAXTransformer(org.apache.cocoon.sax.AbstractSAXTransformer) SAXResult(javax.xml.transform.sax.SAXResult) SAXConsumerAdapter(org.apache.cocoon.sax.util.SAXConsumerAdapter) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SetupException(org.apache.cocoon.pipeline.SetupException)

Aggregations

TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 SetupException (org.apache.cocoon.pipeline.SetupException)2 Transformer (javax.xml.transform.Transformer)1 SAXResult (javax.xml.transform.sax.SAXResult)1 SAXTransformerFactory (javax.xml.transform.sax.SAXTransformerFactory)1 TransformerHandler (javax.xml.transform.sax.TransformerHandler)1 AbstractSAXTransformer (org.apache.cocoon.sax.AbstractSAXTransformer)1 SAXConsumerAdapter (org.apache.cocoon.sax.util.SAXConsumerAdapter)1