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