View Javadoc

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