View Javadoc

1   /*
2    * $Id: XmlPrettyPrinter.java 21933 2011-05-17 18:31:30Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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  import org.mule.transformer.types.DataTypeFactory;
17  import org.mule.util.StringUtils;
18  
19  import org.apache.commons.io.output.ByteArrayOutputStream;
20  import org.dom4j.Document;
21  import org.dom4j.DocumentException;
22  import org.dom4j.io.OutputFormat;
23  import org.dom4j.io.XMLWriter;
24  
25  public class XmlPrettyPrinter extends AbstractTransformer
26  {
27      protected OutputFormat outputFormat = OutputFormat.createPrettyPrint();
28  
29      public XmlPrettyPrinter()
30      {
31          super();
32          this.registerSourceType(DataTypeFactory.STRING);
33          this.registerSourceType(DataTypeFactory.create(org.dom4j.Document.class));
34          this.registerSourceType(DataTypeFactory.create(org.w3c.dom.Document.class));
35          this.setReturnDataType(DataTypeFactory.STRING);
36      }
37  
38      public synchronized OutputFormat getOutputFormat()
39      {
40          return outputFormat;
41      }
42  
43      @Override
44      protected Object doTransform(Object src, String outputEncoding) throws TransformerException
45      {
46          try
47          {
48              Document document = XMLUtils.toDocument(src, muleContext);
49              if (document != null)
50              {
51                  ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
52                  XMLWriter writer = new XMLWriter(resultStream, this.getOutputFormat());
53                  writer.write(document);
54                  writer.close();
55                  return resultStream.toString(outputEncoding);
56              }
57              else
58              {
59                  throw new DocumentException("Payload is not valid XML");
60              }
61          }
62          catch (Exception e)
63          {
64              throw new TransformerException(this, e);
65          }
66      }
67  
68      /**
69       * @see OutputFormat#getEncoding()
70       */
71      @Override
72      public synchronized String getEncoding()
73      {
74          return outputFormat.getEncoding();
75      }
76  
77      /**
78       * @see OutputFormat#setEncoding(String)
79       */
80      @Override
81      public synchronized void setEncoding(String encoding)
82      {
83          outputFormat.setEncoding(encoding);
84      }
85  
86      /**
87       * @see OutputFormat#getIndent()
88       */
89      public synchronized boolean getIndentEnabled()
90      {
91          return outputFormat.getIndent() != null;
92      }
93  
94      /**
95       * @see OutputFormat#setIndent(boolean)
96       */
97      public synchronized void setIndentEnabled(boolean doIndent)
98      {
99          outputFormat.setIndent(doIndent);
100     }
101 
102     /**
103      * @see OutputFormat#getIndent()
104      */
105     public synchronized String getIndentString()
106     {
107         return outputFormat.getIndent();
108     }
109 
110     /**
111      * @see OutputFormat#setIndent(boolean)
112      */
113     public synchronized void setIndentString(String indentString)
114     {
115         outputFormat.setIndent(indentString);
116     }
117 
118     /**
119      * @see OutputFormat#setIndentSize(int)
120      */
121     public synchronized int getIndentSize()
122     {
123         return StringUtils.defaultIfEmpty(outputFormat.getIndent(), "").length();
124     }
125 
126     /**
127      * @see OutputFormat#setIndentSize(int)
128      */
129     public synchronized void setIndentSize(int indentSize)
130     {
131         outputFormat.setIndentSize(indentSize);
132     }
133 
134     /**
135      * @see OutputFormat#getLineSeparator()
136      */
137     public synchronized String getLineSeparator()
138     {
139         return outputFormat.getLineSeparator();
140     }
141 
142     /**
143      * @see OutputFormat#setLineSeparator(String)
144      */
145     public synchronized void setLineSeparator(String separator)
146     {
147         outputFormat.setLineSeparator(separator);
148     }
149 
150     /**
151      * @see OutputFormat#getNewLineAfterNTags()
152      */
153     public synchronized int getNewLineAfterNTags()
154     {
155         return outputFormat.getNewLineAfterNTags();
156     }
157 
158     /**
159      * @see OutputFormat#setNewLineAfterNTags(int)
160      */
161     public synchronized void setNewLineAfterNTags(int tagCount)
162     {
163         outputFormat.setNewLineAfterNTags(tagCount);
164     }
165 
166     /**
167      * @see OutputFormat#isExpandEmptyElements()
168      */
169     public synchronized boolean isExpandEmptyElements()
170     {
171         return outputFormat.isExpandEmptyElements();
172     }
173 
174     /**
175      * @see OutputFormat#setExpandEmptyElements(boolean)
176      */
177     public synchronized void setExpandEmptyElements(boolean expandEmptyElements)
178     {
179         outputFormat.setExpandEmptyElements(expandEmptyElements);
180     }
181 
182     /**
183      * @see OutputFormat#isNewlines()
184      */
185     public synchronized boolean isNewlines()
186     {
187         return outputFormat.isNewlines();
188     }
189 
190     /**
191      * @see OutputFormat#setNewlines(boolean)
192      */
193     public synchronized void setNewlines(boolean newlines)
194     {
195         outputFormat.setNewlines(newlines);
196     }
197 
198     /**
199      * @see OutputFormat#isNewLineAfterDeclaration()
200      */
201     public synchronized boolean isNewLineAfterDeclaration()
202     {
203         return outputFormat.isNewLineAfterDeclaration();
204     }
205 
206     /**
207      * @see OutputFormat#setNewLineAfterDeclaration(boolean)
208      */
209     public synchronized void setNewLineAfterDeclaration(boolean newline)
210     {
211         outputFormat.setNewLineAfterDeclaration(newline);
212     }
213 
214     /**
215      * @see OutputFormat#isOmitEncoding()
216      */
217     public synchronized boolean isOmitEncoding()
218     {
219         return outputFormat.isOmitEncoding();
220     }
221 
222     /**
223      * @see OutputFormat#setOmitEncoding(boolean)
224      */
225     public synchronized void setOmitEncoding(boolean omitEncoding)
226     {
227         outputFormat.setOmitEncoding(omitEncoding);
228     }
229 
230     /**
231      * @see OutputFormat#getEncoding()
232      */
233     public synchronized boolean isPadText()
234     {
235         return outputFormat.isPadText();
236     }
237 
238     /**
239      * @see OutputFormat#getEncoding()
240      */
241     public synchronized void setPadText(boolean padText)
242     {
243         outputFormat.setPadText(padText);
244     }
245 
246     /**
247      * @see OutputFormat#getEncoding()
248      */
249     public synchronized boolean isSuppressDeclaration()
250     {
251         return outputFormat.isSuppressDeclaration();
252     }
253 
254     /**
255      * @see OutputFormat#getEncoding()
256      */
257     public synchronized void setSuppressDeclaration(boolean suppressDeclaration)
258     {
259         outputFormat.setSuppressDeclaration(suppressDeclaration);
260     }
261 
262     /**
263      * @see OutputFormat#isTrimText()
264      */
265     public synchronized boolean isTrimText()
266     {
267         return outputFormat.isTrimText();
268     }
269 
270     /**
271      * @see OutputFormat#setTrimText(boolean)
272      */
273     public synchronized void setTrimText(boolean trimText)
274     {
275         outputFormat.setTrimText(trimText);
276     }
277 
278     /**
279      * @see OutputFormat#isXHTML()
280      */
281     public synchronized boolean isXHTML()
282     {
283         return outputFormat.isXHTML();
284     }
285 
286     /**
287      * @see OutputFormat#setXHTML(boolean)
288      */
289     public synchronized void setXHTML(boolean xhtml)
290     {
291         outputFormat.setXHTML(xhtml);
292     }
293 
294 }