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