forked from OpenIntegrationEngine/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentSerializer.java
More file actions
130 lines (105 loc) · 4.64 KB
/
DocumentSerializer.java
File metadata and controls
130 lines (105 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (c) Mirth Corporation. All rights reserved.
*
* http://www.mirthcorp.com
*
* The software in this package is published under the terms of the MPL license a copy of which has
* been included with this distribution in the LICENSE.txt file.
*/
package com.mirth.connect.model.converters;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class DocumentSerializer {
private Logger logger = LogManager.getLogger(this.getClass());
private String[] cDataElements = null;
private boolean omitXmlDeclaration = false;
public DocumentSerializer() {
this(null, false);
}
public DocumentSerializer(boolean omitXmlDeclaration) {
this(null, omitXmlDeclaration);
}
public DocumentSerializer(String[] cDataElements) {
this(cDataElements, false);
}
public DocumentSerializer(String[] cDataElements, boolean omitXmlDeclaration) {
this.cDataElements = cDataElements;
this.omitXmlDeclaration = omitXmlDeclaration;
}
public void toXML(Document source, Writer writer) {
try {
TransformerFactory factory = TransformerFactory.newInstance();
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
// When Saxon-B is on the classpath setting this attribute throws an
// IllegalArgumentException.
try {
factory.setAttribute("indent-number", 4);
} catch (IllegalArgumentException ex) {
logger.warn("Could not set Document Serializer attribute: indent-number", ex);
}
Transformer transformer = factory.newTransformer();
if (omitXmlDeclaration) {
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
} else {
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
}
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
if (source.getDoctype() != null) {
String publicDoctype = source.getDoctype().getPublicId();
String systemDoctype = source.getDoctype().getSystemId();
if ((publicDoctype != null) && (publicDoctype.length() > 0)) {
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, publicDoctype);
}
if ((systemDoctype != null) && (systemDoctype.length() > 0)) {
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemDoctype);
}
}
if (cDataElements != null) {
StringBuilder cDataElementsString = new StringBuilder();
for (int i = 0; i < cDataElements.length; i++) {
cDataElementsString.append(cDataElements[i] + " ");
}
transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, cDataElementsString.toString());
}
transformer.transform(new DOMSource(source), new StreamResult(writer));
} catch (Exception e) {
logger.error(e.getMessage());
}
}
public String toXML(Document source) {
Writer writer = new StringWriter();
toXML(source, writer);
return writer.toString();
}
public Document fromXML(String source) {
Document document = null;
try {
DocumentBuilderFactory dbf = getSecureDocumentBuilderFactory();
DocumentBuilder db = dbf.newDocumentBuilder();
document = db.parse(new InputSource(new StringReader(source)));
} catch (Exception e) {
logger.error(e);
}
return document;
}
public static DocumentBuilderFactory getSecureDocumentBuilderFactory() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
return dbf;
}
}