1
2
3
4
5
6
7
8
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
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
71
72 public synchronized String getEncoding()
73 {
74 return outputFormat.getEncoding();
75 }
76
77
78
79
80 public synchronized void setEncoding(String encoding)
81 {
82 outputFormat.setEncoding(encoding);
83 }
84
85
86
87
88 public synchronized boolean getIndentEnabled()
89 {
90 return outputFormat.getIndent() != null;
91 }
92
93
94
95
96 public synchronized void setIndentEnabled(boolean doIndent)
97 {
98 outputFormat.setIndent(doIndent);
99 }
100
101
102
103
104 public synchronized String getIndentString()
105 {
106 return outputFormat.getIndent();
107 }
108
109
110
111
112 public synchronized void setIndentString(String indentString)
113 {
114 outputFormat.setIndent(indentString);
115 }
116
117
118
119
120 public synchronized int getIndentSize()
121 {
122 return StringUtils.defaultIfEmpty(outputFormat.getIndent(), "").length();
123 }
124
125
126
127
128 public synchronized void setIndentSize(int indentSize)
129 {
130 outputFormat.setIndentSize(indentSize);
131 }
132
133
134
135
136 public synchronized String getLineSeparator()
137 {
138 return outputFormat.getLineSeparator();
139 }
140
141
142
143
144 public synchronized void setLineSeparator(String separator)
145 {
146 outputFormat.setLineSeparator(separator);
147 }
148
149
150
151
152 public synchronized int getNewLineAfterNTags()
153 {
154 return outputFormat.getNewLineAfterNTags();
155 }
156
157
158
159
160 public synchronized void setNewLineAfterNTags(int tagCount)
161 {
162 outputFormat.setNewLineAfterNTags(tagCount);
163 }
164
165
166
167
168 public synchronized boolean isExpandEmptyElements()
169 {
170 return outputFormat.isExpandEmptyElements();
171 }
172
173
174
175
176 public synchronized void setExpandEmptyElements(boolean expandEmptyElements)
177 {
178 outputFormat.setExpandEmptyElements(expandEmptyElements);
179 }
180
181
182
183
184 public synchronized boolean isNewlines()
185 {
186 return outputFormat.isNewlines();
187 }
188
189
190
191
192 public synchronized void setNewlines(boolean newlines)
193 {
194 outputFormat.setNewlines(newlines);
195 }
196
197
198
199
200 public synchronized boolean isOmitEncoding()
201 {
202 return outputFormat.isOmitEncoding();
203 }
204
205
206
207
208 public synchronized void setOmitEncoding(boolean omitEncoding)
209 {
210 outputFormat.setOmitEncoding(omitEncoding);
211 }
212
213
214
215
216 public synchronized boolean isPadText()
217 {
218 return outputFormat.isPadText();
219 }
220
221
222
223
224 public synchronized void setPadText(boolean padText)
225 {
226 outputFormat.setPadText(padText);
227 }
228
229
230
231
232 public synchronized boolean isSuppressDeclaration()
233 {
234 return outputFormat.isSuppressDeclaration();
235 }
236
237
238
239
240 public synchronized void setSuppressDeclaration(boolean suppressDeclaration)
241 {
242 outputFormat.setSuppressDeclaration(suppressDeclaration);
243 }
244
245
246
247
248 public synchronized boolean isTrimText()
249 {
250 return outputFormat.isTrimText();
251 }
252
253
254
255
256 public synchronized void setTrimText(boolean trimText)
257 {
258 outputFormat.setTrimText(trimText);
259 }
260
261
262
263
264 public synchronized boolean isXHTML()
265 {
266 return outputFormat.isXHTML();
267 }
268
269
270
271
272 public synchronized void setXHTML(boolean xhtml)
273 {
274 outputFormat.setXHTML(xhtml);
275 }
276
277 }