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