Coverage Report - org.mule.module.xml.transformer.AbstractXmlTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractXmlTransformer
0%
0/74
0%
0/26
0
AbstractXmlTransformer$1
0%
0/5
N/A
0
AbstractXmlTransformer$2
0%
0/5
N/A
0
AbstractXmlTransformer$3
0%
0/4
N/A
0
AbstractXmlTransformer$4
0%
0/4
N/A
0
AbstractXmlTransformer$5
0%
0/4
N/A
0
AbstractXmlTransformer$ResultHolder
N/A
N/A
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.module.xml.transformer;
 8  
 
 9  
 import org.mule.api.transformer.TransformerException;
 10  
 import org.mule.module.xml.util.XMLUtils;
 11  
 import org.mule.transformer.AbstractMessageTransformer;
 12  
 import org.mule.transformer.types.DataTypeFactory;
 13  
 
 14  
 import java.io.InputStream;
 15  
 import java.io.OutputStream;
 16  
 import java.io.StringWriter;
 17  
 
 18  
 import javax.xml.stream.XMLInputFactory;
 19  
 import javax.xml.stream.XMLOutputFactory;
 20  
 import javax.xml.transform.OutputKeys;
 21  
 import javax.xml.transform.Result;
 22  
 import javax.xml.transform.Source;
 23  
 import javax.xml.transform.Transformer;
 24  
 import javax.xml.transform.TransformerFactory;
 25  
 import javax.xml.transform.TransformerFactoryConfigurationError;
 26  
 import javax.xml.transform.dom.DOMResult;
 27  
 import javax.xml.transform.stream.StreamResult;
 28  
 
 29  
 import org.apache.commons.io.output.ByteArrayOutputStream;
 30  
 import org.dom4j.Document;
 31  
 import org.dom4j.io.DocumentResult;
 32  
 
 33  
 /**
 34  
  * <code>AbstractXmlTransformer</code> offers some XSLT transform on a DOM (or
 35  
  * other XML-ish) object.
 36  
  */
 37  
 public abstract class AbstractXmlTransformer extends AbstractMessageTransformer
 38  
 {
 39  
     private String outputEncoding;
 40  
     private XMLInputFactory xmlInputFactory;
 41  
     private XMLOutputFactory xmlOutputFactory;
 42  0
     private boolean useStaxSource = false;
 43  
     
 44  
     public AbstractXmlTransformer()
 45  0
     {
 46  0
         registerSourceType(DataTypeFactory.STRING);
 47  0
         registerSourceType(DataTypeFactory.BYTE_ARRAY);
 48  0
         registerSourceType(DataTypeFactory.create(javax.xml.transform.Source.class));
 49  0
         registerSourceType(DataTypeFactory.create(org.xml.sax.InputSource.class));
 50  0
         registerSourceType(DataTypeFactory.create(org.dom4j.Document.class));
 51  0
         registerSourceType(DataTypeFactory.create(org.w3c.dom.Document.class));
 52  0
         registerSourceType(DataTypeFactory.create(org.w3c.dom.Element.class));
 53  0
         registerSourceType(DataTypeFactory.create(java.io.InputStream.class));
 54  0
         registerSourceType(DataTypeFactory.create(org.mule.api.transport.OutputHandler.class));
 55  0
         registerSourceType(DataTypeFactory.create(javax.xml.stream.XMLStreamReader.class));
 56  0
         registerSourceType(DataTypeFactory.create(org.mule.module.xml.transformer.DelayedResult.class));
 57  0
         setReturnDataType(DataTypeFactory.BYTE_ARRAY);
 58  
         
 59  0
         xmlInputFactory = XMLInputFactory.newInstance();
 60  0
         xmlOutputFactory = XMLOutputFactory.newInstance();
 61  0
     }
 62  
 
 63  
     /** Result callback interface used when processing XML through JAXP */
 64  
     protected static interface ResultHolder
 65  
     {
 66  
         /**
 67  
          * @return A Result to use in a transformation (e.g. writing a DOM to a
 68  
          *         stream)
 69  
          */
 70  
         Result getResult();
 71  
 
 72  
         /** @return The actual result as produced after the call to 'transform'. */
 73  
         Object getResultObject();
 74  
     }
 75  
 
 76  
     /**
 77  
      * @param desiredClass Java class representing the desired format
 78  
      * @return Callback interface representing the desiredClass - or null if the
 79  
      *         return class isn't supported (or is null).
 80  
      */
 81  
     protected static ResultHolder getResultHolder(Class<?> desiredClass)
 82  
     {
 83  0
         if (desiredClass == null)
 84  
         {
 85  0
             return null;
 86  
         }
 87  0
         if (byte[].class.equals(desiredClass) || InputStream.class.isAssignableFrom(desiredClass))
 88  
         {
 89  0
             return new ResultHolder()
 90  0
             {
 91  0
                 ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
 92  0
                 StreamResult result = new StreamResult(resultStream);
 93  
 
 94  
                 public Result getResult()
 95  
                 {
 96  0
                     return result;
 97  
                 }
 98  
 
 99  
                 public Object getResultObject()
 100  
                 {
 101  0
                     return resultStream.toByteArray();
 102  
                 }
 103  
             };
 104  
         }
 105  0
         else if (String.class.equals(desiredClass))
 106  
         {
 107  0
             return new ResultHolder()
 108  0
             {
 109  0
                 StringWriter writer = new StringWriter();
 110  0
                 StreamResult result = new StreamResult(writer);
 111  
 
 112  
                 public Result getResult()
 113  
                 {
 114  0
                     return result;
 115  
                 }
 116  
 
 117  
                 public Object getResultObject()
 118  
                 {
 119  0
                     return writer.getBuffer().toString();
 120  
                 }
 121  
             };
 122  
         }
 123  0
         else if (org.w3c.dom.Document.class.isAssignableFrom(desiredClass))
 124  
         {
 125  0
             return new ResultHolder()
 126  0
             {
 127  0
                 DOMResult result = new DOMResult();
 128  
 
 129  
                 public Result getResult()
 130  
                 {
 131  0
                     return result;
 132  
                 }
 133  
 
 134  
                 public Object getResultObject()
 135  
                 {
 136  0
                     return result.getNode();
 137  
                 }
 138  
             };
 139  
         }
 140  0
         else if (org.dom4j.io.DocumentResult.class.isAssignableFrom(desiredClass))
 141  
         {
 142  0
             return new ResultHolder()
 143  0
             {
 144  0
                 DocumentResult result = new DocumentResult();
 145  
 
 146  
                 public Result getResult()
 147  
                 {
 148  0
                     return result;
 149  
                 }
 150  
 
 151  
                 public Object getResultObject()
 152  
                 {
 153  0
                     return result;
 154  
                 }
 155  
             };
 156  
         }
 157  0
         else if (org.dom4j.Document.class.isAssignableFrom(desiredClass))
 158  
         {
 159  0
             return new ResultHolder()
 160  0
             {
 161  0
                 DocumentResult result = new DocumentResult();
 162  
 
 163  
                 public Result getResult()
 164  
                 {
 165  0
                     return result;
 166  
                 }
 167  
 
 168  
                 public Object getResultObject()
 169  
                 {
 170  0
                     return result.getDocument();
 171  
                 }
 172  
             };
 173  
         }
 174  
         
 175  0
         return null;
 176  
     }
 177  
 
 178  
     /**
 179  
      * Converts an XML in-memory representation to a String
 180  
      *
 181  
      * @param obj Object to convert (could be byte[], String, DOM, DOM4J)
 182  
      * @return String including XML header using default (UTF-8) encoding
 183  
      * @throws TransformerFactoryConfigurationError
 184  
      *          On error
 185  
      * @throws javax.xml.transform.TransformerException
 186  
      *          On error
 187  
      * @throws TransformerException
 188  
      * @deprecated Replaced by convertToText(Object obj, String ouputEncoding)
 189  
      */
 190  
     @Deprecated
 191  
     protected String convertToText(Object obj) throws Exception
 192  
     {
 193  0
         return convertToText(obj, null);
 194  
     }
 195  
 
 196  
     /**
 197  
      * Converts an XML in-memory representation to a String using a specific encoding.
 198  
      * If using an encoding which cannot represent specific characters, these are
 199  
      * written as entities, even if they can be represented as a Java String.
 200  
      *
 201  
      * @param obj            Object to convert (could be byte[], String, DOM, or DOM4J Document).
 202  
      *                       If the object is a byte[], the character
 203  
      *                       encoding used MUST match the declared encoding standard, or a parse error will occur.
 204  
      * @param outputEncoding Name of the XML encoding to use, e.g. US-ASCII, or null for UTF-8
 205  
      * @return String including XML header using the specified encoding
 206  
      * @throws TransformerFactoryConfigurationError
 207  
      *          On error
 208  
      * @throws javax.xml.transform.TransformerException
 209  
      *          On error
 210  
      * @throws TransformerException
 211  
      */
 212  
     protected String convertToText(Object obj, String outputEncoding) throws Exception
 213  
     {
 214  
         // Catch the direct translations
 215  0
         if (obj instanceof String)
 216  
         {
 217  0
             return (String) obj;
 218  
         }
 219  0
         else if (obj instanceof Document)
 220  
         {
 221  0
             return ((Document) obj).asXML();
 222  
         }
 223  
         // No easy fix, so use the transformer.
 224  0
         Source src = XMLUtils.toXmlSource(xmlInputFactory, useStaxSource, obj);
 225  0
         if (src == null)
 226  
         {
 227  0
             return null;
 228  
         }
 229  
 
 230  0
         StringWriter writer = new StringWriter();
 231  0
         StreamResult result = new StreamResult(writer);
 232  
 
 233  0
         Transformer idTransformer = TransformerFactory.newInstance().newTransformer();
 234  0
         if (outputEncoding != null)
 235  
         {
 236  0
             idTransformer.setOutputProperty(OutputKeys.ENCODING, outputEncoding);
 237  
         }
 238  0
         idTransformer.transform(src, result);
 239  0
         return writer.getBuffer().toString();
 240  
     }
 241  
 
 242  
     /**
 243  
      * Converts an XML in-memory representation to a String using a specific encoding.
 244  
      *
 245  
      * @param obj            Object to convert (could be byte[], String, DOM, or DOM4J Document).
 246  
      *                       If the object is a byte[], the character
 247  
      *                       encoding used MUST match the declared encoding standard, or a parse error will occur.
 248  
      * @param outputEncoding Name of the XML encoding to use, e.g. US-ASCII, or null for UTF-8
 249  
      * @return String including XML header using the specified encoding
 250  
      * @throws TransformerFactoryConfigurationError
 251  
      *          On error
 252  
      * @throws javax.xml.transform.TransformerException
 253  
      *          On error
 254  
      * @throws TransformerException
 255  
      */
 256  
     protected String convertToBytes(Object obj, String outputEncoding) throws Exception
 257  
     {
 258  
         // Always use the transformer, even for byte[] (to get the encoding right!)
 259  0
         Source src = XMLUtils.toXmlSource(xmlInputFactory, useStaxSource, obj);
 260  0
         if (src == null)
 261  
         {
 262  0
             return null;
 263  
         }
 264  
 
 265  0
         StringWriter writer = new StringWriter();
 266  0
         StreamResult result = new StreamResult(writer);
 267  
 
 268  0
         Transformer idTransformer = XMLUtils.getTransformer();
 269  0
         idTransformer.setOutputProperty(OutputKeys.ENCODING, outputEncoding);
 270  0
         idTransformer.transform(src, result);
 271  0
         return writer.getBuffer().toString();
 272  
     }
 273  
     
 274  
     protected void writeToStream(Object obj, String outputEncoding, OutputStream output) throws Exception
 275  
     {
 276  
         // Always use the transformer, even for byte[] (to get the encoding right!)
 277  0
         Source src = XMLUtils.toXmlSource(xmlInputFactory, useStaxSource, obj);
 278  0
         if (src == null)
 279  
         {
 280  0
             return;
 281  
         }
 282  
 
 283  0
         StreamResult result = new StreamResult(output);
 284  
 
 285  0
         Transformer idTransformer = XMLUtils.getTransformer();
 286  0
         idTransformer.setOutputProperty(OutputKeys.ENCODING, outputEncoding);
 287  0
         idTransformer.transform(src, result);
 288  0
     }
 289  
     
 290  
     /** @return the outputEncoding */
 291  
     public String getOutputEncoding()
 292  
     {
 293  0
         return outputEncoding;
 294  
     }
 295  
 
 296  
     /** @param outputEncoding the outputEncoding to set */
 297  
     public void setOutputEncoding(String outputEncoding)
 298  
     {
 299  0
         this.outputEncoding = outputEncoding;
 300  0
     }
 301  
     
 302  
     public boolean isUseStaxSource()
 303  
     {
 304  0
         return useStaxSource;
 305  
     }
 306  
 
 307  
     public void setUseStaxSource(boolean useStaxSource)
 308  
     {
 309  0
         this.useStaxSource = useStaxSource;
 310  0
     }
 311  
 
 312  
     public XMLInputFactory getXMLInputFactory()
 313  
     {
 314  0
         return xmlInputFactory;
 315  
     }
 316  
 
 317  
     public void setXMLInputFactory(XMLInputFactory xmlInputFactory)
 318  
     {
 319  0
         this.xmlInputFactory = xmlInputFactory;
 320  0
     }
 321  
 
 322  
     public XMLOutputFactory getXMLOutputFactory()
 323  
     {
 324  0
         return xmlOutputFactory;
 325  
     }
 326  
 
 327  
     public void setXMLOutputFactory(XMLOutputFactory xmlOutputFactory)
 328  
     {
 329  0
         this.xmlOutputFactory = xmlOutputFactory;
 330  0
     }
 331  
     
 332  
 }