1
2
3
4
5
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.AbstractTransformer;
12 import org.mule.transformer.types.DataTypeFactory;
13 import org.mule.util.StringUtils;
14
15 import org.apache.commons.io.output.ByteArrayOutputStream;
16 import org.dom4j.Document;
17 import org.dom4j.DocumentException;
18 import org.dom4j.io.OutputFormat;
19 import org.dom4j.io.XMLWriter;
20
21 public class XmlPrettyPrinter extends AbstractTransformer
22 {
23 protected OutputFormat outputFormat = OutputFormat.createPrettyPrint();
24
25 public XmlPrettyPrinter()
26 {
27 super();
28 this.registerSourceType(DataTypeFactory.STRING);
29 this.registerSourceType(DataTypeFactory.create(org.dom4j.Document.class));
30 this.registerSourceType(DataTypeFactory.create(org.w3c.dom.Document.class));
31 this.setReturnDataType(DataTypeFactory.STRING);
32 }
33
34 public synchronized OutputFormat getOutputFormat()
35 {
36 return outputFormat;
37 }
38
39 @Override
40 protected Object doTransform(Object src, String outputEncoding) throws TransformerException
41 {
42 try
43 {
44 Document document = XMLUtils.toDocument(src, muleContext);
45 if (document != null)
46 {
47 ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
48 XMLWriter writer = new XMLWriter(resultStream, this.getOutputFormat());
49 writer.write(document);
50 writer.close();
51 return resultStream.toString(outputEncoding);
52 }
53 else
54 {
55 throw new DocumentException("Payload is not valid XML");
56 }
57 }
58 catch (Exception e)
59 {
60 throw new TransformerException(this, e);
61 }
62 }
63
64
65
66
67 @Override
68 public synchronized String getEncoding()
69 {
70 return outputFormat.getEncoding();
71 }
72
73
74
75
76 @Override
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 isNewLineAfterDeclaration()
198 {
199 return outputFormat.isNewLineAfterDeclaration();
200 }
201
202
203
204
205 public synchronized void setNewLineAfterDeclaration(boolean newline)
206 {
207 outputFormat.setNewLineAfterDeclaration(newline);
208 }
209
210
211
212
213 public synchronized boolean isOmitEncoding()
214 {
215 return outputFormat.isOmitEncoding();
216 }
217
218
219
220
221 public synchronized void setOmitEncoding(boolean omitEncoding)
222 {
223 outputFormat.setOmitEncoding(omitEncoding);
224 }
225
226
227
228
229 public synchronized boolean isPadText()
230 {
231 return outputFormat.isPadText();
232 }
233
234
235
236
237 public synchronized void setPadText(boolean padText)
238 {
239 outputFormat.setPadText(padText);
240 }
241
242
243
244
245 public synchronized boolean isSuppressDeclaration()
246 {
247 return outputFormat.isSuppressDeclaration();
248 }
249
250
251
252
253 public synchronized void setSuppressDeclaration(boolean suppressDeclaration)
254 {
255 outputFormat.setSuppressDeclaration(suppressDeclaration);
256 }
257
258
259
260
261 public synchronized boolean isTrimText()
262 {
263 return outputFormat.isTrimText();
264 }
265
266
267
268
269 public synchronized void setTrimText(boolean trimText)
270 {
271 outputFormat.setTrimText(trimText);
272 }
273
274
275
276
277 public synchronized boolean isXHTML()
278 {
279 return outputFormat.isXHTML();
280 }
281
282
283
284
285 public synchronized void setXHTML(boolean xhtml)
286 {
287 outputFormat.setXHTML(xhtml);
288 }
289
290 }