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    
021    import javax.xml.transform.TransformerFactory;
022    
023    import org.apache.camel.Endpoint;
024    import org.apache.camel.builder.xml.XsltBuilder;
025    import org.apache.camel.component.ResourceBasedComponent;
026    import org.apache.camel.converter.jaxp.XmlConverter;
027    import org.apache.camel.impl.ProcessorEndpoint;
028    import org.springframework.core.io.Resource;
029    
030    /**
031     * An <a href="http://camel.apache.org/xslt.html">XSLT Component</a>
032     * for performing XSLT transforms of messages
033     *
034     * @version $Revision: 749939 $
035     */
036    public class XsltComponent extends ResourceBasedComponent {
037        private XmlConverter xmlConverter;
038    
039        public XmlConverter getXmlConverter() {
040            return xmlConverter;
041        }
042    
043        public void setXmlConverter(XmlConverter xmlConverter) {
044            this.xmlConverter = xmlConverter;
045        }
046    
047        @SuppressWarnings("unchecked")
048        protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
049            Resource resource = resolveMandatoryResource(remaining);
050            if (log.isDebugEnabled()) {
051                log.debug(this + " using schema resource: " + resource);
052            }
053            XsltBuilder xslt = newInstance(XsltBuilder.class);
054    
055            // lets allow the converter to be configured
056            XmlConverter converter = null;
057            String converterName = getAndRemoveParameter(parameters, "converter", String.class);        
058            if (converterName != null) {
059                converter = mandatoryLookup(converterName, XmlConverter.class);
060            }
061            if (converter == null) {
062                converter = getXmlConverter();
063            }
064            if (converter != null) {
065                xslt.setConverter(converter);
066            }
067            
068            String transformerFactoryClassName = getAndRemoveParameter(parameters, "transformerFactoryClass", String.class);
069            TransformerFactory factory = null;
070            if (transformerFactoryClassName != null) {
071                // provide the class loader of this component to work in OSGi environments
072                Class factoryClass = getCamelContext().getClassResolver().resolveClass(transformerFactoryClassName, XsltComponent.class.getClassLoader());
073                if (factoryClass != null) {
074                    factory = (TransformerFactory) newInstance(factoryClass);
075                } else {
076                    log.warn("Cannot find the TransformerFactoryClass with the class name: " + transformerFactoryClassName);
077                }
078            }
079            
080            String transformerFactoryName = getAndRemoveParameter(parameters, "transformerFactory", String.class);        
081            if (transformerFactoryName != null) {
082                factory = mandatoryLookup(transformerFactoryName, TransformerFactory.class);
083            }
084            
085            if (factory != null) {
086                xslt.getConverter().setTransformerFactory(factory);
087            }
088            xslt.setTransformerInputStream(resource.getInputStream());
089            configureXslt(xslt, uri, remaining, parameters);
090            return new ProcessorEndpoint(uri, this, xslt);
091        }
092    
093        protected void configureXslt(XsltBuilder xslt, String uri, String remaining, Map parameters) throws Exception {
094            setProperties(xslt, parameters);
095        }
096    }