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