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