001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.component.xslt;
018
019import java.util.Map;
020import javax.xml.transform.URIResolver;
021
022import org.apache.camel.Endpoint;
023import org.apache.camel.builder.xml.XsltUriResolver;
024import org.apache.camel.converter.jaxp.XmlConverter;
025import org.apache.camel.impl.UriEndpointComponent;
026import org.apache.camel.spi.Metadata;
027import org.apache.camel.util.ResourceHelper;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031/**
032 * The <a href="http://camel.apache.org/xslt.html">XSLT Component</a> is for performing XSLT transformations of messages
033 */
034public class XsltComponent extends UriEndpointComponent {
035
036    private static final Logger LOG = LoggerFactory.getLogger(XsltComponent.class);
037
038    private XmlConverter xmlConverter;
039    private URIResolver uriResolver;
040    @Metadata(defaultValue = "true")
041    private boolean contentCache = true;
042    @Metadata(defaultValue = "false")
043    private boolean saxon;
044
045    public XsltComponent() {
046        super(XsltEndpoint.class);
047    }
048
049    public XmlConverter getXmlConverter() {
050        return xmlConverter;
051    }
052
053    /**
054     * To use a custom implementation of {@link org.apache.camel.converter.jaxp.XmlConverter}
055     */
056    public void setXmlConverter(XmlConverter xmlConverter) {
057        this.xmlConverter = xmlConverter;
058    }
059
060    public URIResolver getUriResolver() {
061        return uriResolver;
062    }
063
064    /**
065     * To use a custom javax.xml.transform.URIResolver
066     */
067    public void setUriResolver(URIResolver uriResolver) {
068        this.uriResolver = uriResolver;
069    }
070
071    public boolean isContentCache() {
072        return contentCache;
073    }
074
075    /**
076     * Cache for the resource content (the stylesheet file) when it is loaded.
077     * If set to false Camel will reload the stylesheet file on each message processing. This is good for development.
078     * A cached stylesheet can be forced to reload at runtime via JMX using the clearCachedStylesheet operation.
079     */
080    public void setContentCache(boolean contentCache) {
081        this.contentCache = contentCache;
082    }
083
084    public boolean isSaxon() {
085        return saxon;
086    }
087
088    /**
089     * Whether to use Saxon as the transformerFactoryClass.
090     * If enabled then the class net.sf.saxon.TransformerFactoryImpl. You would need to add Saxon to the classpath.
091     */
092    public void setSaxon(boolean saxon) {
093        this.saxon = saxon;
094    }
095
096    protected Endpoint createEndpoint(String uri, final String remaining, Map<String, Object> parameters) throws Exception {
097        XsltEndpoint endpoint = new XsltEndpoint(uri, this);
098        endpoint.setConverter(getXmlConverter());
099        endpoint.setContentCache(isContentCache());
100        endpoint.setSaxon(isSaxon());
101
102        String resourceUri = remaining;
103
104        // if its a http uri, then append additional parameters as they are part of the uri
105        if (ResourceHelper.isHttpUri(resourceUri)) {
106            resourceUri = ResourceHelper.appendParameters(resourceUri, parameters);
107        }
108        LOG.debug("{} using schema resource: {}", this, resourceUri);
109        endpoint.setResourceUri(resourceUri);
110
111        // lookup custom resolver to use
112        URIResolver resolver = resolveAndRemoveReferenceParameter(parameters, "uriResolver", URIResolver.class);
113        if (resolver == null) {
114            // not in endpoint then use component specific resolver
115            resolver = getUriResolver();
116        }
117        if (resolver == null) {
118            // fallback to use a Camel specific resolver
119            resolver = new XsltUriResolver(getCamelContext().getClassResolver(), remaining);
120        }
121        endpoint.setUriResolver(resolver);
122
123        setProperties(endpoint, parameters);
124        if (!parameters.isEmpty()) {
125            // additional parameters need to be stored on endpoint as they can be used to configure xslt builder additionally
126            endpoint.setParameters(parameters);
127        }
128
129        return endpoint;
130    }
131
132}