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    @Metadata(label = "advanced")
039    private XmlConverter xmlConverter;
040    @Metadata(label = "advanced")
041    private URIResolver uriResolver;
042    @Metadata(defaultValue = "true")
043    private boolean contentCache = true;
044    private boolean saxon;
045
046    public XsltComponent() {
047        super(XsltEndpoint.class);
048    }
049
050    public XmlConverter getXmlConverter() {
051        return xmlConverter;
052    }
053
054    /**
055     * To use a custom implementation of {@link org.apache.camel.converter.jaxp.XmlConverter}
056     */
057    public void setXmlConverter(XmlConverter xmlConverter) {
058        this.xmlConverter = xmlConverter;
059    }
060
061    public URIResolver getUriResolver() {
062        return uriResolver;
063    }
064
065    /**
066     * To use a custom javax.xml.transform.URIResolver
067     */
068    public void setUriResolver(URIResolver uriResolver) {
069        this.uriResolver = uriResolver;
070    }
071
072    public boolean isContentCache() {
073        return contentCache;
074    }
075
076    /**
077     * Cache for the resource content (the stylesheet file) when it is loaded.
078     * If set to false Camel will reload the stylesheet file on each message processing. This is good for development.
079     * A cached stylesheet can be forced to reload at runtime via JMX using the clearCachedStylesheet operation.
080     */
081    public void setContentCache(boolean contentCache) {
082        this.contentCache = contentCache;
083    }
084
085    public boolean isSaxon() {
086        return saxon;
087    }
088
089    /**
090     * Whether to use Saxon as the transformerFactoryClass.
091     * If enabled then the class net.sf.saxon.TransformerFactoryImpl. You would need to add Saxon to the classpath.
092     */
093    public void setSaxon(boolean saxon) {
094        this.saxon = saxon;
095    }
096
097    protected Endpoint createEndpoint(String uri, final String remaining, Map<String, Object> parameters) throws Exception {
098        XsltEndpoint endpoint = new XsltEndpoint(uri, this);
099        endpoint.setConverter(getXmlConverter());
100        endpoint.setContentCache(isContentCache());
101        endpoint.setSaxon(isSaxon());
102
103        String resourceUri = remaining;
104
105        // if its a http uri, then append additional parameters as they are part of the uri
106        if (ResourceHelper.isHttpUri(resourceUri)) {
107            resourceUri = ResourceHelper.appendParameters(resourceUri, parameters);
108        }
109        LOG.debug("{} using schema resource: {}", this, resourceUri);
110        endpoint.setResourceUri(resourceUri);
111
112        // lookup custom resolver to use
113        URIResolver resolver = resolveAndRemoveReferenceParameter(parameters, "uriResolver", URIResolver.class);
114        if (resolver == null) {
115            // not in endpoint then use component specific resolver
116            resolver = getUriResolver();
117        }
118        if (resolver == null) {
119            // fallback to use a Camel specific resolver
120            resolver = new XsltUriResolver(getCamelContext().getClassResolver(), remaining);
121        }
122        endpoint.setUriResolver(resolver);
123
124        setProperties(endpoint, parameters);
125        if (!parameters.isEmpty()) {
126            // additional parameters need to be stored on endpoint as they can be used to configure xslt builder additionally
127            endpoint.setParameters(parameters);
128        }
129
130        return endpoint;
131    }
132
133}