Skip to content

Commit 6a7004f

Browse files
committedDec 5, 2024
feat(#277): refactor constructors
1 parent 20c9606 commit 6a7004f

File tree

3 files changed

+69
-52
lines changed

3 files changed

+69
-52
lines changed
 

‎src/main/java/com/jcabi/xml/DomParser.java

+29
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333
import java.io.ByteArrayInputStream;
3434
import java.io.File;
3535
import java.io.IOException;
36+
import java.io.InputStream;
3637
import java.nio.charset.StandardCharsets;
3738
import java.nio.file.Path;
39+
import javax.print.Doc;
3840
import javax.xml.parsers.DocumentBuilder;
3941
import javax.xml.parsers.DocumentBuilderFactory;
4042
import javax.xml.parsers.ParserConfigurationException;
@@ -97,6 +99,11 @@ final class DomParser {
9799
this(fct, new BytesSource(bytes));
98100
}
99101

102+
103+
DomParser(final DocumentBuilderFactory fct, final InputStream stream) {
104+
this(fct, new StreamSource(stream));
105+
}
106+
100107
DomParser(final DocumentBuilderFactory fct, final File file) {
101108
this(fct, new FileSource(file));
102109
}
@@ -179,6 +186,28 @@ public long length() {
179186
}
180187

181188

189+
private static class StreamSource implements DocSource {
190+
191+
private final InputStream stream;
192+
193+
public StreamSource(final InputStream stream) {
194+
this.stream = stream;
195+
}
196+
197+
@Override
198+
public Document apply(final DocumentBuilder builder) throws IOException, SAXException {
199+
final Document res = builder.parse(this.stream);
200+
this.stream.close();
201+
return res;
202+
}
203+
204+
@Override
205+
public long length() {
206+
return 0;
207+
}
208+
}
209+
210+
182211
private static class BytesSource implements DocSource {
183212

184213
private final byte[] xml;

‎src/main/java/com/jcabi/xml/XML.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public interface XML {
165165
* This method works exactly the same as {@link #deepCopy()}.
166166
* @return DOM node
167167
*/
168-
@Deprecated
168+
// @Deprecated
169169
Node node();
170170

171171
/**

‎src/main/java/com/jcabi/xml/XMLDocument.java

+39-51
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ public final class XMLDocument implements XML {
106106
*/
107107
private final transient Node cache;
108108

109+
/**
110+
* Public ctor, from a source.
111+
*
112+
* <p>The object is created with a default implementation of
113+
* {@link NamespaceContext}, which already defines a
114+
* number of namespaces, see {@link XMLDocument#XMLDocument(String)}.
115+
*
116+
* <p>An {@link IllegalArgumentException} is thrown if the parameter
117+
* passed is not in XML format.
118+
*
119+
* @param source Source of XML document
120+
*/
121+
public XMLDocument(final Source source) {
122+
this(XMLDocument.transform(source));
123+
}
124+
109125
/**
110126
* Public ctor, from XML as a text.
111127
*
@@ -125,11 +141,7 @@ public final class XMLDocument implements XML {
125141
* @param text XML document body
126142
*/
127143
public XMLDocument(final String text) {
128-
this(
129-
new DomParser(FACTORY, text).document(),
130-
new XPathContext(),
131-
false
132-
);
144+
this(new DomParser(XMLDocument.configuredDFactory(), text).document());
133145
}
134146

135147
/**
@@ -151,29 +163,11 @@ public XMLDocument(final String text) {
151163
* @param data The XML body
152164
*/
153165
public XMLDocument(final byte[] data) {
154-
this(
155-
new DomParser(FACTORY, data).document(),
156-
new XPathContext(),
157-
false
158-
);
166+
this(new DomParser(XMLDocument.configuredDFactory(), data).document());
159167
}
160168

161169
/**
162-
* Public ctor, from a DOM node.
163-
*
164-
* <p>The object is created with a default implementation of
165-
* {@link NamespaceContext}, which already defines a
166-
* number of namespaces, see {@link XMLDocument#XMLDocument(String)}.
167-
*
168-
* @param node DOM source
169-
* @since 0.2
170-
*/
171-
public XMLDocument(final Node node) {
172-
this(node, new XPathContext(), !(node instanceof Document));
173-
}
174-
175-
/**
176-
* Public ctor, from a source.
170+
* Public ctor, from XML in a file.
177171
*
178172
* <p>The object is created with a default implementation of
179173
* {@link NamespaceContext}, which already defines a
@@ -182,14 +176,15 @@ public XMLDocument(final Node node) {
182176
* <p>An {@link IllegalArgumentException} is thrown if the parameter
183177
* passed is not in XML format.
184178
*
185-
* @param source Source of XML document
179+
* @param file XML file
180+
* @throws FileNotFoundException In case of I/O problems
186181
*/
187-
public XMLDocument(final Source source) {
188-
this(XMLDocument.transform(source), new XPathContext(), false);
182+
public XMLDocument(final File file) throws FileNotFoundException {
183+
this(new DomParser(XMLDocument.configuredDFactory(), file).document());
189184
}
190185

191186
/**
192-
* Public ctor, from XML in a file.
187+
* Public ctor, from input stream.
193188
*
194189
* <p>The object is created with a default implementation of
195190
* {@link NamespaceContext}, which already defines a
@@ -198,12 +193,15 @@ public XMLDocument(final Source source) {
198193
* <p>An {@link IllegalArgumentException} is thrown if the parameter
199194
* passed is not in XML format.
200195
*
201-
* @param file XML file
202-
* @throws FileNotFoundException In case of I/O problems
196+
* <p>The provided input stream will be closed automatically after
197+
* getting data from it.
198+
*
199+
* @param stream The input stream, which will be closed automatically
200+
* @throws IOException In case of I/O problem
203201
*/
204-
public XMLDocument(final File file) throws FileNotFoundException {
205-
// this(new TextResource(file).toString());
206-
this(new DomParser(FACTORY, file).document());
202+
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
203+
public XMLDocument(final InputStream stream) throws IOException {
204+
this(new DomParser(XMLDocument.configuredDFactory(), stream).document());
207205
}
208206

209207
/**
@@ -220,7 +218,7 @@ public XMLDocument(final File file) throws FileNotFoundException {
220218
* @throws FileNotFoundException In case of I/O problems
221219
*/
222220
public XMLDocument(final Path file) throws FileNotFoundException {
223-
this(file.toFile());
221+
this(new DomParser(XMLDocument.configuredDFactory(), file.toFile()).document());
224222
}
225223

226224
/**
@@ -258,25 +256,17 @@ public XMLDocument(final URI uri) throws IOException {
258256
}
259257

260258
/**
261-
* Public ctor, from input stream.
259+
* Public ctor, from a DOM node.
262260
*
263261
* <p>The object is created with a default implementation of
264262
* {@link NamespaceContext}, which already defines a
265263
* number of namespaces, see {@link XMLDocument#XMLDocument(String)}.
266264
*
267-
* <p>An {@link IllegalArgumentException} is thrown if the parameter
268-
* passed is not in XML format.
269-
*
270-
* <p>The provided input stream will be closed automatically after
271-
* getting data from it.
272-
*
273-
* @param stream The input stream, which will be closed automatically
274-
* @throws IOException In case of I/O problem
265+
* @param node DOM source
266+
* @since 0.2
275267
*/
276-
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
277-
public XMLDocument(final InputStream stream) throws IOException {
278-
this(new TextResource(stream).toString());
279-
stream.close();
268+
public XMLDocument(final Node node) {
269+
this(node, new XPathContext(), !(node instanceof Document));
280270
}
281271

282272
/**
@@ -488,7 +478,7 @@ public Collection<SAXParseException> validate(final Schema schema) {
488478
* @return A cloned node imported in a dedicated document.
489479
*/
490480
private static Node createImportedNode(final Node node) {
491-
final DocumentBuilderFactory factory = FACTORY;
481+
final DocumentBuilderFactory factory = XMLDocument.configuredDFactory();
492482
final DocumentBuilder builder;
493483
try {
494484
builder = factory.newDocumentBuilder();
@@ -609,8 +599,6 @@ private static Node transform(final Source source) {
609599
return result.getNode();
610600
}
611601

612-
private static final DocumentBuilderFactory FACTORY = XMLDocument.configuredDFactory();
613-
614602
/**
615603
* Create new {@link DocumentBuilderFactory} and configure it.
616604
* @return Configured factory

0 commit comments

Comments
 (0)
Please sign in to comment.