1
2
3
4
5 package com.jcabi.xml;
6
7 import com.jcabi.log.Logger;
8 import java.io.ByteArrayInputStream;
9 import java.io.File;
10 import java.io.IOException;
11 import java.nio.charset.StandardCharsets;
12 import javax.xml.parsers.DocumentBuilder;
13 import javax.xml.parsers.DocumentBuilderFactory;
14 import javax.xml.parsers.ParserConfigurationException;
15 import lombok.EqualsAndHashCode;
16 import lombok.ToString;
17 import org.w3c.dom.Document;
18 import org.xml.sax.SAXException;
19
20
21
22
23
24
25
26
27 @ToString
28 @EqualsAndHashCode
29 final class DomParser {
30
31
32
33
34 private final transient DocumentBuilderFactory factory;
35
36
37
38
39 private final DocSource source;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 DomParser(final DocumentBuilderFactory fct, final String txt) {
55 this(fct, new BytesSource(txt));
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69 @SuppressWarnings("PMD.ArrayIsStoredDirectly")
70 DomParser(final DocumentBuilderFactory fct, final byte[] bytes) {
71 this(fct, new BytesSource(bytes));
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85 DomParser(final DocumentBuilderFactory fct, final File file) {
86 this(fct, new FileSource(file));
87 }
88
89
90
91
92
93
94 private DomParser(final DocumentBuilderFactory factory, final DocSource source) {
95 this.factory = factory;
96 this.source = source;
97 }
98
99
100
101
102
103 @SuppressWarnings("PMD.PrematureDeclaration")
104 public Document document() {
105 final DocumentBuilder builder;
106 try {
107 builder = this.factory.newDocumentBuilder();
108 } catch (final ParserConfigurationException ex) {
109 throw new IllegalArgumentException(
110 String.format(
111 "Failed to create document builder by %s",
112 this.factory.getClass().getName()
113 ),
114 ex
115 );
116 }
117 final long start = System.nanoTime();
118 final Document doc;
119 try {
120 doc = this.source.apply(builder);
121 } catch (final IOException | SAXException ex) {
122 throw new IllegalArgumentException(
123 String.format(
124 "Can't parse by %s, most probably the XML is invalid",
125 builder.getClass().getName()
126 ),
127 ex
128 );
129 }
130 if (Logger.isTraceEnabled(this)) {
131 Logger.trace(
132 this,
133 "%s parsed %d bytes of XML in %[nano]s",
134 builder.getClass().getName(),
135 this.source.length(),
136 System.nanoTime() - start
137 );
138 }
139 return doc;
140 }
141
142
143
144
145
146 private interface DocSource {
147
148
149
150
151
152
153
154
155 Document apply(DocumentBuilder builder) throws IOException, SAXException;
156
157
158
159
160
161 long length();
162 }
163
164
165
166
167
168 private static class FileSource implements DocSource {
169
170
171
172
173 private final File file;
174
175
176
177
178
179 FileSource(final File file) {
180 this.file = file;
181 }
182
183 @Override
184 public Document apply(final DocumentBuilder builder) throws IOException, SAXException {
185 return builder.parse(this.file);
186 }
187
188 @Override
189 public long length() {
190 return this.file.length();
191 }
192 }
193
194
195
196
197
198 private static class BytesSource implements DocSource {
199
200
201
202
203 private final byte[] xml;
204
205
206
207
208
209 BytesSource(final String xml) {
210 this(xml.getBytes(StandardCharsets.UTF_8));
211 }
212
213
214
215
216
217 @SuppressWarnings("PMD.ArrayIsStoredDirectly")
218 BytesSource(final byte[] xml) {
219 this.xml = xml;
220 }
221
222 @Override
223 public Document apply(final DocumentBuilder builder) throws IOException, SAXException {
224 return builder.parse(new ByteArrayInputStream(this.xml));
225 }
226
227 @Override
228 public long length() {
229 return this.xml.length;
230 }
231 }
232 }