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