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