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