View Javadoc
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      private boolean useStaxSource = false;
43      
44      public AbstractXmlTransformer()
45      {
46          registerSourceType(DataTypeFactory.STRING);
47          registerSourceType(DataTypeFactory.BYTE_ARRAY);
48          registerSourceType(DataTypeFactory.create(javax.xml.transform.Source.class));
49          registerSourceType(DataTypeFactory.create(org.xml.sax.InputSource.class));
50          registerSourceType(DataTypeFactory.create(org.dom4j.Document.class));
51          registerSourceType(DataTypeFactory.create(org.w3c.dom.Document.class));
52          registerSourceType(DataTypeFactory.create(org.w3c.dom.Element.class));
53          registerSourceType(DataTypeFactory.create(java.io.InputStream.class));
54          registerSourceType(DataTypeFactory.create(org.mule.api.transport.OutputHandler.class));
55          registerSourceType(DataTypeFactory.create(javax.xml.stream.XMLStreamReader.class));
56          registerSourceType(DataTypeFactory.create(org.mule.module.xml.transformer.DelayedResult.class));
57          setReturnDataType(DataTypeFactory.BYTE_ARRAY);
58          
59          xmlInputFactory = XMLInputFactory.newInstance();
60          xmlOutputFactory = XMLOutputFactory.newInstance();
61      }
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          if (desiredClass == null)
84          {
85              return null;
86          }
87          if (byte[].class.equals(desiredClass) || InputStream.class.isAssignableFrom(desiredClass))
88          {
89              return new ResultHolder()
90              {
91                  ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
92                  StreamResult result = new StreamResult(resultStream);
93  
94                  public Result getResult()
95                  {
96                      return result;
97                  }
98  
99                  public Object getResultObject()
100                 {
101                     return resultStream.toByteArray();
102                 }
103             };
104         }
105         else if (String.class.equals(desiredClass))
106         {
107             return new ResultHolder()
108             {
109                 StringWriter writer = new StringWriter();
110                 StreamResult result = new StreamResult(writer);
111 
112                 public Result getResult()
113                 {
114                     return result;
115                 }
116 
117                 public Object getResultObject()
118                 {
119                     return writer.getBuffer().toString();
120                 }
121             };
122         }
123         else if (org.w3c.dom.Document.class.isAssignableFrom(desiredClass))
124         {
125             return new ResultHolder()
126             {
127                 DOMResult result = new DOMResult();
128 
129                 public Result getResult()
130                 {
131                     return result;
132                 }
133 
134                 public Object getResultObject()
135                 {
136                     return result.getNode();
137                 }
138             };
139         }
140         else if (org.dom4j.io.DocumentResult.class.isAssignableFrom(desiredClass))
141         {
142             return new ResultHolder()
143             {
144                 DocumentResult result = new DocumentResult();
145 
146                 public Result getResult()
147                 {
148                     return result;
149                 }
150 
151                 public Object getResultObject()
152                 {
153                     return result;
154                 }
155             };
156         }
157         else if (org.dom4j.Document.class.isAssignableFrom(desiredClass))
158         {
159             return new ResultHolder()
160             {
161                 DocumentResult result = new DocumentResult();
162 
163                 public Result getResult()
164                 {
165                     return result;
166                 }
167 
168                 public Object getResultObject()
169                 {
170                     return result.getDocument();
171                 }
172             };
173         }
174         
175         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         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         if (obj instanceof String)
216         {
217             return (String) obj;
218         }
219         else if (obj instanceof Document)
220         {
221             return ((Document) obj).asXML();
222         }
223         // No easy fix, so use the transformer.
224         Source src = XMLUtils.toXmlSource(xmlInputFactory, useStaxSource, obj);
225         if (src == null)
226         {
227             return null;
228         }
229 
230         StringWriter writer = new StringWriter();
231         StreamResult result = new StreamResult(writer);
232 
233         Transformer idTransformer = TransformerFactory.newInstance().newTransformer();
234         if (outputEncoding != null)
235         {
236             idTransformer.setOutputProperty(OutputKeys.ENCODING, outputEncoding);
237         }
238         idTransformer.transform(src, result);
239         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         Source src = XMLUtils.toXmlSource(xmlInputFactory, useStaxSource, obj);
260         if (src == null)
261         {
262             return null;
263         }
264 
265         StringWriter writer = new StringWriter();
266         StreamResult result = new StreamResult(writer);
267 
268         Transformer idTransformer = XMLUtils.getTransformer();
269         idTransformer.setOutputProperty(OutputKeys.ENCODING, outputEncoding);
270         idTransformer.transform(src, result);
271         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         Source src = XMLUtils.toXmlSource(xmlInputFactory, useStaxSource, obj);
278         if (src == null)
279         {
280             return;
281         }
282 
283         StreamResult result = new StreamResult(output);
284 
285         Transformer idTransformer = XMLUtils.getTransformer();
286         idTransformer.setOutputProperty(OutputKeys.ENCODING, outputEncoding);
287         idTransformer.transform(src, result);
288     }
289     
290     /** @return the outputEncoding */
291     public String getOutputEncoding()
292     {
293         return outputEncoding;
294     }
295 
296     /** @param outputEncoding the outputEncoding to set */
297     public void setOutputEncoding(String outputEncoding)
298     {
299         this.outputEncoding = outputEncoding;
300     }
301     
302     public boolean isUseStaxSource()
303     {
304         return useStaxSource;
305     }
306 
307     public void setUseStaxSource(boolean useStaxSource)
308     {
309         this.useStaxSource = useStaxSource;
310     }
311 
312     public XMLInputFactory getXMLInputFactory()
313     {
314         return xmlInputFactory;
315     }
316 
317     public void setXMLInputFactory(XMLInputFactory xmlInputFactory)
318     {
319         this.xmlInputFactory = xmlInputFactory;
320     }
321 
322     public XMLOutputFactory getXMLOutputFactory()
323     {
324         return xmlOutputFactory;
325     }
326 
327     public void setXMLOutputFactory(XMLOutputFactory xmlOutputFactory)
328     {
329         this.xmlOutputFactory = xmlOutputFactory;
330     }
331     
332 }