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 | |
package ca.uhn.hl7v2.util; |
27 | |
|
28 | |
import java.io.InputStream; |
29 | |
import java.io.StringWriter; |
30 | |
import java.io.Writer; |
31 | |
|
32 | |
import org.w3c.dom.DOMConfiguration; |
33 | |
import org.w3c.dom.DOMErrorHandler; |
34 | |
import org.w3c.dom.DOMImplementation; |
35 | |
import org.w3c.dom.Document; |
36 | |
import org.w3c.dom.bootstrap.DOMImplementationRegistry; |
37 | |
import org.w3c.dom.ls.DOMImplementationLS; |
38 | |
import org.w3c.dom.ls.LSInput; |
39 | |
import org.w3c.dom.ls.LSOutput; |
40 | |
import org.w3c.dom.ls.LSParser; |
41 | |
import org.w3c.dom.ls.LSResourceResolver; |
42 | |
import org.w3c.dom.ls.LSSerializer; |
43 | |
|
44 | 0 | public class XMLUtils { |
45 | |
|
46 | |
private static DOMImplementation IMPL; |
47 | |
|
48 | |
@SuppressWarnings("unchecked") |
49 | |
public synchronized static <T> T getDOMImpl() { |
50 | 710 | if (IMPL == null) { |
51 | |
try { |
52 | 5 | DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); |
53 | 5 | IMPL = registry.getDOMImplementation("LS 3.0"); |
54 | 0 | } catch (Exception e) { |
55 | 0 | throw new RuntimeException(e); |
56 | 5 | } |
57 | |
} |
58 | 710 | return (T) IMPL; |
59 | |
} |
60 | |
|
61 | |
@SuppressWarnings("unchecked") |
62 | |
public static <T> T getDOMImplUncached() { |
63 | |
try { |
64 | 0 | DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); |
65 | 0 | return (T) registry.getDOMImplementation("LS 3.0"); |
66 | 0 | } catch (Exception e) { |
67 | 0 | throw new RuntimeException(e); |
68 | |
} |
69 | |
} |
70 | |
|
71 | |
public static Document parse(String s) { |
72 | 125 | return parse(s, false); |
73 | |
} |
74 | |
|
75 | |
public static Document parse(String s, boolean validateIfSchema) { |
76 | 200 | DOMImplementationLS impl = getDOMImpl(); |
77 | 200 | LSInput input = impl.createLSInput(); |
78 | 200 | input.setStringData(s); |
79 | 200 | return parse(input, validateIfSchema); |
80 | |
} |
81 | |
|
82 | |
public static Document parse(InputStream s, boolean validateIfSchema) { |
83 | 0 | DOMImplementationLS impl = getDOMImpl(); |
84 | 0 | LSInput input = impl.createLSInput(); |
85 | 0 | input.setByteStream(s); |
86 | 0 | return parse(input, validateIfSchema); |
87 | |
} |
88 | |
|
89 | |
private static Document parse(LSInput input, boolean validateIfSchema) { |
90 | 200 | DOMImplementationLS impl = getDOMImpl(); |
91 | 200 | LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); |
92 | 200 | DOMConfiguration config = parser.getDomConfig(); |
93 | 200 | config.setParameter("element-content-whitespace", false); |
94 | 200 | config.setParameter("namespaces", true); |
95 | 200 | config.setParameter("validate-if-schema", validateIfSchema); |
96 | 200 | return parser.parse(input); |
97 | |
} |
98 | |
|
99 | |
public static void validate(Document d, String schema, DOMErrorHandler handler) { |
100 | 30 | DOMConfiguration config = d.getDomConfig(); |
101 | 30 | config.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema"); |
102 | 30 | config.setParameter("validate", true); |
103 | 30 | config.setParameter("schema-location", schema); |
104 | 30 | config.setParameter("resource-resolver", new ClasspathResourceResolver()); |
105 | 30 | config.setParameter("error-handler", handler); |
106 | 30 | d.normalizeDocument(); |
107 | 30 | } |
108 | |
|
109 | |
public static String serialize(Document document, boolean prettyPrint) { |
110 | 150 | DOMImplementationLS impl = getDOMImpl(); |
111 | 150 | LSSerializer serializer = impl.createLSSerializer(); |
112 | |
|
113 | 150 | DOMConfiguration config = serializer.getDomConfig(); |
114 | 150 | if (prettyPrint && config.canSetParameter("format-pretty-print", Boolean.TRUE)) { |
115 | 150 | config.setParameter("format-pretty-print", true); |
116 | |
} |
117 | 150 | config.setParameter("xml-declaration", true); |
118 | 150 | LSOutput output = impl.createLSOutput(); |
119 | 150 | output.setEncoding("UTF-8"); |
120 | 150 | Writer writer = new StringWriter(); |
121 | 150 | output.setCharacterStream(writer); |
122 | 150 | serializer.write(document, output); |
123 | 150 | return writer.toString(); |
124 | |
} |
125 | |
|
126 | |
public static Document emptyDocument(String title) { |
127 | 125 | DOMImplementation impl = getDOMImpl(); |
128 | 125 | Document doc = impl.createDocument("urn:hl7-org:v2xml", title, null); |
129 | 125 | return doc; |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
private static class ClasspathResourceResolver implements LSResourceResolver { |
137 | |
private DOMImplementationLS impl; |
138 | |
|
139 | 30 | ClasspathResourceResolver() { |
140 | 30 | impl = getDOMImpl(); |
141 | 30 | } |
142 | |
|
143 | |
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, |
144 | |
String baseURI) { |
145 | 120 | LSInput lsInput = impl.createLSInput(); |
146 | 120 | InputStream is = getClass().getResourceAsStream("/" + systemId); |
147 | 120 | if (is == null) |
148 | 110 | return null; |
149 | 10 | lsInput.setByteStream(is); |
150 | 10 | return lsInput; |
151 | |
} |
152 | |
} |
153 | |
|
154 | |
} |