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     */
017    package org.apache.camel.component.xslt;
018    
019    import java.util.Map;
020    import javax.xml.transform.TransformerConfigurationException;
021    import javax.xml.transform.TransformerFactory;
022    import javax.xml.transform.URIResolver;
023    
024    import org.apache.camel.Endpoint;
025    import org.apache.camel.Exchange;
026    import org.apache.camel.builder.xml.ResultHandlerFactory;
027    import org.apache.camel.builder.xml.XsltBuilder;
028    import org.apache.camel.builder.xml.XsltUriResolver;
029    import org.apache.camel.component.ResourceBasedComponent;
030    import org.apache.camel.converter.jaxp.XmlConverter;
031    import org.apache.camel.impl.ProcessorEndpoint;
032    import org.apache.camel.util.ObjectHelper;
033    import org.springframework.core.io.Resource;
034    
035    /**
036     * An <a href="http://camel.apache.org/xslt.html">XSLT Component</a>
037     * for performing XSLT transforms of messages
038     *
039     * @version $Revision: 22002 $
040     */
041    public class XsltComponent extends ResourceBasedComponent {
042        private XmlConverter xmlConverter;
043        private URIResolver uriResolver;
044        private boolean contentCache = true;
045    
046        public XmlConverter getXmlConverter() {
047            return xmlConverter;
048        }
049    
050        public void setXmlConverter(XmlConverter xmlConverter) {
051            this.xmlConverter = xmlConverter;
052        }
053    
054        public URIResolver getUriResolver() {
055            return uriResolver;
056        }
057    
058        public void setUriResolver(URIResolver uriResolver) {
059            this.uriResolver = uriResolver;
060        }
061    
062        public boolean isContentCache() {
063            return contentCache;
064        }
065    
066        public void setContentCache(boolean contentCache) {
067            this.contentCache = contentCache;
068        }
069    
070        protected Endpoint createEndpoint(String uri, final String remaining, Map<String, Object> parameters) throws Exception {
071            final Resource resource = resolveMandatoryResource(remaining);
072            if (log.isDebugEnabled()) {
073                log.debug(this + " using schema resource: " + resource);
074            }
075            final XsltBuilder xslt = getCamelContext().getInjector().newInstance(XsltBuilder.class);
076    
077            // lets allow the converter to be configured
078            XmlConverter converter = resolveAndRemoveReferenceParameter(parameters, "converter", XmlConverter.class);
079            if (converter == null) {
080                converter = getXmlConverter();
081            }
082            if (converter != null) {
083                xslt.setConverter(converter);
084            }
085            
086            String transformerFactoryClassName = getAndRemoveParameter(parameters, "transformerFactoryClass", String.class);
087            TransformerFactory factory = null;
088            if (transformerFactoryClassName != null) {
089                // provide the class loader of this component to work in OSGi environments
090                Class<?> factoryClass = getCamelContext().getClassResolver().resolveClass(transformerFactoryClassName, XsltComponent.class.getClassLoader());
091                if (factoryClass != null) {
092                    factory = (TransformerFactory) getCamelContext().getInjector().newInstance(factoryClass);
093                } else {
094                    log.warn("Cannot find the TransformerFactoryClass with the class name: " + transformerFactoryClassName);
095                }
096            }
097            
098            if (parameters.get("transformerFactory") != null) {
099                factory = resolveAndRemoveReferenceParameter(parameters, "transformerFactory", TransformerFactory.class);
100            }
101            
102            if (factory != null) {
103                xslt.getConverter().setTransformerFactory(factory);
104            }
105    
106            // lookup custom resolver to use
107            URIResolver resolver = resolveAndRemoveReferenceParameter(parameters, "uriResolver", URIResolver.class);
108            if (resolver == null) {
109                // not in endpoint then use component specific resolver
110                resolver = getUriResolver();
111            }
112            if (resolver == null) {
113                // fallback to use a Camel specific resolver
114                resolver = new XsltUriResolver(getCamelContext().getClassResolver(), remaining);
115            }
116            // set resolver before input stream as resolver is used when loading the input stream
117            xslt.setUriResolver(resolver);
118    
119            ResultHandlerFactory resultHandlerFactory = resolveAndRemoveReferenceParameter(parameters, "resultHandlerFactory", ResultHandlerFactory.class);
120            if (resultHandlerFactory != null) {
121                xslt.setResultHandlerFactory(resultHandlerFactory);
122            }
123    
124            Boolean failOnNullBody = getAndRemoveParameter(parameters, "failOnNullBody", Boolean.class);
125            if (failOnNullBody != null) {
126                xslt.setFailOnNullBody(failOnNullBody);
127            }
128            String output = getAndRemoveParameter(parameters, "output", String.class);
129            configureOutput(xslt, output);
130    
131            configureXslt(xslt, uri, remaining, parameters);
132            loadResource(xslt, resource);
133    
134            // default to use the cache option from the component if the endpoint did not have the contentCache parameter
135            boolean cache = getAndRemoveParameter(parameters, "contentCache", Boolean.class, contentCache);
136            if (!cache) {
137                return new ProcessorEndpoint(uri, this, xslt) {
138                    @Override
139                    protected void onExchange(Exchange exchange) throws Exception {
140                        // force to load the resource on each exchange as we are not cached
141                        loadResource(xslt, resource);
142                        super.onExchange(exchange);
143                    }
144                };
145            } else {
146                // we have already loaded xslt so we are cached
147                return new ProcessorEndpoint(uri, this, xslt);
148            }
149        }
150    
151        private void loadResource(XsltBuilder xslt, Resource resource) throws TransformerConfigurationException {
152            if (log.isTraceEnabled()) {
153                log.trace(this + " loading schema resource: " + resource);
154            }
155            try {
156                xslt.setTransformerInputStream(resource.getInputStream());
157            } catch (Exception e) {
158                // include information about the resource in the caused exception, so its easier for
159                // end users to know which resource failed
160                throw new TransformerConfigurationException(e.getMessage() + " " + resource.toString(), e);
161            }
162        }
163    
164        protected void configureXslt(XsltBuilder xslt, String uri, String remaining, Map<String, Object> parameters) throws Exception {
165            setProperties(xslt, parameters);
166        }
167    
168        protected void configureOutput(XsltBuilder xslt, String output) throws Exception {
169            if (ObjectHelper.isEmpty(output)) {
170                return;
171            }
172    
173            if ("string".equalsIgnoreCase(output)) {
174                xslt.outputString();
175            } else if ("bytes".equalsIgnoreCase(output)) {
176                xslt.outputBytes();
177            } else if ("DOM".equalsIgnoreCase(output)) {
178                xslt.outputDOM();
179            } else if ("file".equalsIgnoreCase(output)) {
180                xslt.outputFile();
181            } else {
182                throw new IllegalArgumentException("Unknown output type: " + output);
183            }
184        }
185    
186    }