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.Exchange;
025    import org.apache.camel.builder.xml.XsltBuilder;
026    import org.apache.camel.component.ResourceBasedComponent;
027    import org.apache.camel.converter.jaxp.XmlConverter;
028    import org.apache.camel.impl.ProcessorEndpoint;
029    import org.apache.camel.util.ObjectHelper;
030    import org.springframework.core.io.Resource;
031    
032    /**
033     * An <a href="http://activemq.apache.org/camel/xslt.html">XSLT Component</a>
034     * for performing XSLT transforms of messages
035     *
036     * @version $Revision: 2252 $
037     */
038    public class XsltComponent extends ResourceBasedComponent {
039        private XmlConverter xmlConverter;
040    
041        public XmlConverter getXmlConverter() {
042            return xmlConverter;
043        }
044    
045        public void setXmlConverter(XmlConverter xmlConverter) {
046            this.xmlConverter = xmlConverter;
047        }
048    
049        protected Endpoint<Exchange> createEndpoint(String uri, String remaining, Map parameters) throws Exception {
050            Resource resource = resolveMandatoryResource(remaining);
051            if (log.isDebugEnabled()) {
052                log.debug(this + " using schema resource: " + resource);
053            }
054            XsltBuilder xslt = newInstance(XsltBuilder.class);
055    
056            // lets allow the converter to be configured
057            XmlConverter converter = null;
058            String converterName = getAndRemoveParameter(parameters, "converter", String.class);        
059            if (converterName != null) {
060                converter = mandatoryLookup(converterName, XmlConverter.class);
061            }
062            if (converter == null) {
063                converter = getXmlConverter();
064            }
065            if (converter != null) {
066                xslt.setConverter(converter);
067            }
068            
069            String transformerFactoryClassName = getAndRemoveParameter(parameters, "transformerFactoryClass", String.class);
070            TransformerFactory factory = null;
071            if (transformerFactoryClassName != null) {
072                Class factoryClass = ObjectHelper.loadClass(transformerFactoryClassName);
073                if (factoryClass != null) {
074                    factory = (TransformerFactory) newInstance(factoryClass);
075                } else {
076                    log.warn("Can't 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    }