Coverage Report - org.apache.camel.builder.xml.XsltBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
XsltBuilder
61% 
75% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.builder.xml;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.IOException;
 21  
 import java.io.InputStream;
 22  
 import java.net.URL;
 23  
 import java.util.HashMap;
 24  
 import java.util.Map;
 25  
 import java.util.Set;
 26  
 
 27  
 import javax.xml.parsers.ParserConfigurationException;
 28  
 import javax.xml.transform.Result;
 29  
 import javax.xml.transform.Source;
 30  
 import javax.xml.transform.Transformer;
 31  
 import javax.xml.transform.TransformerConfigurationException;
 32  
 import javax.xml.transform.stream.StreamSource;
 33  
 
 34  
 import org.apache.camel.Exchange;
 35  
 import org.apache.camel.ExpectedBodyTypeException;
 36  
 import org.apache.camel.Message;
 37  
 import org.apache.camel.Processor;
 38  
 import org.apache.camel.RuntimeTransformException;
 39  
 import org.apache.camel.converter.jaxp.XmlConverter;
 40  
 
 41  
 import static org.apache.camel.util.ObjectHelper.notNull;
 42  
 
 43  
 /**
 44  
  * Creates a <a href="http://activemq.apache.org/camel/processor.html">Processor</a>
 45  
  * which performs an XSLT transformation of the IN message body
 46  
  * 
 47  
  * @version $Revision: 531854 $
 48  
  */
 49  
 public class XsltBuilder implements Processor {
 50  3
     private Map<String, Object> parameters = new HashMap<String, Object>();
 51  3
     private XmlConverter converter = new XmlConverter();
 52  
     private Transformer transformer;
 53  3
     private ResultHandler resultHandler = new StringResultHandler();
 54  3
     private boolean failOnNullBody = true;
 55  
 
 56  3
     public XsltBuilder() {
 57  3
     }
 58  
 
 59  0
     public XsltBuilder(Transformer transformer) {
 60  0
         this.transformer = transformer;
 61  0
     }
 62  
 
 63  
     @Override
 64  
     public String toString() {
 65  3
         return "XSLT[" + transformer + "]";
 66  
     }
 67  
 
 68  
     public synchronized void process(Exchange exchange) throws Exception {
 69  3
         Transformer transformer = getTransformer();
 70  3
         if (transformer == null) {
 71  0
             throw new IllegalArgumentException("No transformer configured!");
 72  
         }
 73  3
         configureTransformer(transformer, exchange);
 74  3
         Source source = getSource(exchange);
 75  3
         Result result = resultHandler.getResult();
 76  3
         transformer.transform(source, result);
 77  3
         resultHandler.setBody(exchange.getIn());
 78  3
     }
 79  
 
 80  
     // Builder methods
 81  
     // -------------------------------------------------------------------------
 82  
 
 83  
     /**
 84  
      * Creates an XSLT processor using the given transformer instance
 85  
      */
 86  
     public static XsltBuilder xslt(Transformer transformer) {
 87  0
         return new XsltBuilder(transformer);
 88  
     }
 89  
 
 90  
     /**
 91  
      * Creates an XSLT processor using the given XSLT source
 92  
      */
 93  
     public static XsltBuilder xslt(Source xslt) throws TransformerConfigurationException {
 94  3
         notNull(xslt, "xslt");
 95  3
         XsltBuilder answer = new XsltBuilder();
 96  3
         answer.setTransformerSource(xslt);
 97  3
         return answer;
 98  
     }
 99  
 
 100  
     /**
 101  
      * Creates an XSLT processor using the given XSLT source
 102  
      */
 103  
     public static XsltBuilder xslt(File xslt) throws TransformerConfigurationException {
 104  0
         notNull(xslt, "xslt");
 105  0
         return xslt(new StreamSource(xslt));
 106  
     }
 107  
 
 108  
     /**
 109  
      * Creates an XSLT processor using the given XSLT source
 110  
      */
 111  
     public static XsltBuilder xslt(URL xslt) throws TransformerConfigurationException, IOException {
 112  3
         notNull(xslt, "xslt");
 113  3
         return xslt(xslt.openStream());
 114  
     }
 115  
 
 116  
     /**
 117  
      * Creates an XSLT processor using the given XSLT source
 118  
      */
 119  
     public static XsltBuilder xslt(InputStream xslt) throws TransformerConfigurationException, IOException {
 120  3
         notNull(xslt, "xslt");
 121  3
         return xslt(new StreamSource(xslt));
 122  
     }
 123  
 
 124  
     /**
 125  
      * Sets the output as being a byte[]
 126  
      */
 127  
     public XsltBuilder outputBytes() {
 128  0
         setResultHandler(new StreamResultHandler());
 129  0
         return this;
 130  
     }
 131  
 
 132  
     /**
 133  
      * Sets the output as being a String
 134  
      */
 135  
     public XsltBuilder outputString() {
 136  0
         setResultHandler(new StringResultHandler());
 137  0
         return this;
 138  
     }
 139  
 
 140  
     /**
 141  
      * Sets the output as being a DOM
 142  
      */
 143  
     public XsltBuilder outputDOM() {
 144  0
         setResultHandler(new DomResultHandler());
 145  0
         return this;
 146  
     }
 147  
 
 148  
     public XsltBuilder parameter(String name, Object value) {
 149  0
         parameters.put(name, value);
 150  0
         return this;
 151  
     }
 152  
 
 153  
     // Properties
 154  
     // -------------------------------------------------------------------------
 155  
 
 156  
     public Map<String, Object> getParameters() {
 157  3
         return parameters;
 158  
     }
 159  
 
 160  
     public void setParameters(Map<String, Object> parameters) {
 161  0
         this.parameters = parameters;
 162  0
     }
 163  
 
 164  
     public Transformer getTransformer() {
 165  3
         return transformer;
 166  
     }
 167  
 
 168  
     public void setTransformer(Transformer transformer) {
 169  3
         this.transformer = transformer;
 170  3
     }
 171  
 
 172  
     public boolean isFailOnNullBody() {
 173  0
         return failOnNullBody;
 174  
     }
 175  
 
 176  
     public void setFailOnNullBody(boolean failOnNullBody) {
 177  0
         this.failOnNullBody = failOnNullBody;
 178  0
     }
 179  
 
 180  
     public ResultHandler getResultHandler() {
 181  0
         return resultHandler;
 182  
     }
 183  
 
 184  
     public void setResultHandler(ResultHandler resultHandler) {
 185  0
         this.resultHandler = resultHandler;
 186  0
     }
 187  
 
 188  
     public void setTransformerSource(Source source) throws TransformerConfigurationException {
 189  3
         setTransformer(converter.getTransformerFactory().newTransformer(source));
 190  3
     }
 191  
 
 192  
     // Implementation methods
 193  
     // -------------------------------------------------------------------------
 194  
 
 195  
     /**
 196  
      * Converts the inbound body to a {@link Source}
 197  
      */
 198  
     protected Source getSource(Exchange exchange) {
 199  3
         Message in = exchange.getIn();
 200  3
         Source source = in.getBody(Source.class);
 201  3
         if (source == null) {
 202  0
             if (isFailOnNullBody()) {
 203  0
                 throw new ExpectedBodyTypeException(exchange, Source.class);
 204  
             } else {
 205  
                 try {
 206  0
                     source = converter.toSource(converter.createDocument());
 207  0
                 } catch (ParserConfigurationException e) {
 208  0
                     throw new RuntimeTransformException(e);
 209  0
                 }
 210  
             }
 211  
         }
 212  3
         return source;
 213  
     }
 214  
 
 215  
     /**
 216  
      * Configures the transformerwith exchange specific parameters
 217  
      */
 218  
     protected void configureTransformer(Transformer transformer, Exchange exchange) {
 219  3
         transformer.clearParameters();
 220  
 
 221  3
         addParameters(transformer, exchange.getProperties());
 222  3
         addParameters(transformer, exchange.getIn().getHeaders());
 223  3
         addParameters(transformer, getParameters());
 224  
 
 225  3
         transformer.setParameter("exchange", exchange);
 226  3
         transformer.setParameter("in", exchange.getIn());
 227  3
         transformer.setParameter("out", exchange.getOut());
 228  3
     }
 229  
 
 230  
     protected void addParameters(Transformer transformer, Map<String, Object> map) {
 231  9
         Set<Map.Entry<String, Object>> propertyEntries = map.entrySet();
 232  9
         for (Map.Entry<String, Object> entry : propertyEntries) {
 233  3
             transformer.setParameter(entry.getKey(), entry.getValue());
 234  3
         }
 235  9
     }
 236  
 }