|
1 | 1 | package com.fasterxml.jackson.databind.ext;
|
2 | 2 |
|
3 |
| -import java.io.IOException; |
4 |
| - |
5 |
| -import org.w3c.dom.Node; |
6 |
| -import org.w3c.dom.bootstrap.DOMImplementationRegistry; |
7 |
| -import org.w3c.dom.ls.DOMImplementationLS; |
8 |
| -import org.w3c.dom.ls.LSSerializer; |
9 |
| - |
10 |
| -import com.fasterxml.jackson.core.*; |
| 3 | +import com.fasterxml.jackson.core.JsonGenerator; |
11 | 4 | import com.fasterxml.jackson.databind.JavaType;
|
12 | 5 | import com.fasterxml.jackson.databind.JsonMappingException;
|
13 | 6 | import com.fasterxml.jackson.databind.JsonNode;
|
14 | 7 | import com.fasterxml.jackson.databind.SerializerProvider;
|
15 | 8 | import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
|
16 | 9 | import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
| 10 | +import org.w3c.dom.Node; |
17 | 11 |
|
| 12 | +import javax.xml.XMLConstants; |
| 13 | +import javax.xml.transform.*; |
| 14 | +import javax.xml.transform.dom.DOMSource; |
| 15 | +import javax.xml.transform.stream.StreamResult; |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.StringWriter; |
18 | 18 | @SuppressWarnings("serial")
|
19 | 19 | public class DOMSerializer extends StdSerializer<Node>
|
20 | 20 | {
|
21 |
| - protected final DOMImplementationLS _domImpl; |
| 21 | + |
| 22 | + private final TransformerFactory transformerFactory; |
22 | 23 |
|
23 | 24 | public DOMSerializer() {
|
24 | 25 | super(Node.class);
|
25 |
| - DOMImplementationRegistry registry; |
26 | 26 | try {
|
27 |
| - registry = DOMImplementationRegistry.newInstance(); |
| 27 | + transformerFactory = TransformerFactory.newInstance(); |
| 28 | + transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); |
28 | 29 | } catch (Exception e) {
|
29 |
| - throw new IllegalStateException("Could not instantiate DOMImplementationRegistry: "+e.getMessage(), e); |
| 30 | + throw new IllegalStateException("Could not instantiate TransformerFactory: "+e.getMessage(), e); |
30 | 31 | }
|
31 |
| - _domImpl = (DOMImplementationLS)registry.getDOMImplementation("LS"); |
32 | 32 | }
|
33 | 33 |
|
34 | 34 | @Override
|
35 | 35 | public void serialize(Node value, JsonGenerator jgen, SerializerProvider provider)
|
36 | 36 | throws IOException
|
37 | 37 | {
|
38 |
| - if (_domImpl == null) throw new IllegalStateException("Could not find DOM LS"); |
39 |
| - LSSerializer writer = _domImpl.createLSSerializer(); |
40 |
| - jgen.writeString(writer.writeToString(value)); |
| 38 | + try { |
| 39 | + Transformer transformer = transformerFactory.newTransformer(); |
| 40 | + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 41 | + transformer.setOutputProperty(OutputKeys.INDENT, "no"); |
| 42 | + StreamResult result = new StreamResult(new StringWriter()); |
| 43 | + DOMSource source = new DOMSource(value); |
| 44 | + transformer.transform(source, result); |
| 45 | + jgen.writeString(result.getWriter().toString()); |
| 46 | + } catch (TransformerConfigurationException e) { |
| 47 | + throw new IllegalStateException("Could not create XML Transformer: "+e.getMessage(), e); |
| 48 | + } catch (TransformerException e) { |
| 49 | + provider.reportMappingProblem(e,"XML Transformation failed: %s", e.getMessage()); |
| 50 | + } |
41 | 51 | }
|
42 | 52 |
|
43 | 53 | @Override
|
|
0 commit comments