Skip to content

Commit

Permalink
feat(#277): refactor constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Dec 5, 2024
1 parent 20c9606 commit 6a7004f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 52 deletions.
29 changes: 29 additions & 0 deletions src/main/java/com/jcabi/xml/DomParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import javax.print.Doc;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -97,6 +99,11 @@ final class DomParser {
this(fct, new BytesSource(bytes));
}


DomParser(final DocumentBuilderFactory fct, final InputStream stream) {
this(fct, new StreamSource(stream));
}

DomParser(final DocumentBuilderFactory fct, final File file) {
this(fct, new FileSource(file));
}
Expand Down Expand Up @@ -179,6 +186,28 @@ public long length() {
}


private static class StreamSource implements DocSource {

private final InputStream stream;

public StreamSource(final InputStream stream) {
this.stream = stream;
}

@Override
public Document apply(final DocumentBuilder builder) throws IOException, SAXException {
final Document res = builder.parse(this.stream);
this.stream.close();
return res;
}

@Override
public long length() {
return 0;
}
}


private static class BytesSource implements DocSource {

private final byte[] xml;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jcabi/xml/XML.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public interface XML {
* This method works exactly the same as {@link #deepCopy()}.
* @return DOM node
*/
@Deprecated
// @Deprecated
Node node();

/**
Expand Down
90 changes: 39 additions & 51 deletions src/main/java/com/jcabi/xml/XMLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ public final class XMLDocument implements XML {
*/
private final transient Node cache;

/**
* Public ctor, from a source.
*
* <p>The object is created with a default implementation of
* {@link NamespaceContext}, which already defines a
* number of namespaces, see {@link XMLDocument#XMLDocument(String)}.
*
* <p>An {@link IllegalArgumentException} is thrown if the parameter
* passed is not in XML format.
*
* @param source Source of XML document
*/
public XMLDocument(final Source source) {
this(XMLDocument.transform(source));
}

/**
* Public ctor, from XML as a text.
*
Expand All @@ -125,11 +141,7 @@ public final class XMLDocument implements XML {
* @param text XML document body
*/
public XMLDocument(final String text) {
this(
new DomParser(FACTORY, text).document(),
new XPathContext(),
false
);
this(new DomParser(XMLDocument.configuredDFactory(), text).document());
}

/**
Expand All @@ -151,29 +163,11 @@ public XMLDocument(final String text) {
* @param data The XML body
*/
public XMLDocument(final byte[] data) {
this(
new DomParser(FACTORY, data).document(),
new XPathContext(),
false
);
this(new DomParser(XMLDocument.configuredDFactory(), data).document());
}

/**
* Public ctor, from a DOM node.
*
* <p>The object is created with a default implementation of
* {@link NamespaceContext}, which already defines a
* number of namespaces, see {@link XMLDocument#XMLDocument(String)}.
*
* @param node DOM source
* @since 0.2
*/
public XMLDocument(final Node node) {
this(node, new XPathContext(), !(node instanceof Document));
}

/**
* Public ctor, from a source.
* Public ctor, from XML in a file.
*
* <p>The object is created with a default implementation of
* {@link NamespaceContext}, which already defines a
Expand All @@ -182,14 +176,15 @@ public XMLDocument(final Node node) {
* <p>An {@link IllegalArgumentException} is thrown if the parameter
* passed is not in XML format.
*
* @param source Source of XML document
* @param file XML file
* @throws FileNotFoundException In case of I/O problems
*/
public XMLDocument(final Source source) {
this(XMLDocument.transform(source), new XPathContext(), false);
public XMLDocument(final File file) throws FileNotFoundException {
this(new DomParser(XMLDocument.configuredDFactory(), file).document());
}

/**
* Public ctor, from XML in a file.
* Public ctor, from input stream.
*
* <p>The object is created with a default implementation of
* {@link NamespaceContext}, which already defines a
Expand All @@ -198,12 +193,15 @@ public XMLDocument(final Source source) {
* <p>An {@link IllegalArgumentException} is thrown if the parameter
* passed is not in XML format.
*
* @param file XML file
* @throws FileNotFoundException In case of I/O problems
* <p>The provided input stream will be closed automatically after
* getting data from it.
*
* @param stream The input stream, which will be closed automatically
* @throws IOException In case of I/O problem
*/
public XMLDocument(final File file) throws FileNotFoundException {
// this(new TextResource(file).toString());
this(new DomParser(FACTORY, file).document());
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
public XMLDocument(final InputStream stream) throws IOException {
this(new DomParser(XMLDocument.configuredDFactory(), stream).document());
}

/**
Expand All @@ -220,7 +218,7 @@ public XMLDocument(final File file) throws FileNotFoundException {
* @throws FileNotFoundException In case of I/O problems
*/
public XMLDocument(final Path file) throws FileNotFoundException {
this(file.toFile());
this(new DomParser(XMLDocument.configuredDFactory(), file.toFile()).document());
}

/**
Expand Down Expand Up @@ -258,25 +256,17 @@ public XMLDocument(final URI uri) throws IOException {
}

/**
* Public ctor, from input stream.
* Public ctor, from a DOM node.
*
* <p>The object is created with a default implementation of
* {@link NamespaceContext}, which already defines a
* number of namespaces, see {@link XMLDocument#XMLDocument(String)}.
*
* <p>An {@link IllegalArgumentException} is thrown if the parameter
* passed is not in XML format.
*
* <p>The provided input stream will be closed automatically after
* getting data from it.
*
* @param stream The input stream, which will be closed automatically
* @throws IOException In case of I/O problem
* @param node DOM source
* @since 0.2
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
public XMLDocument(final InputStream stream) throws IOException {
this(new TextResource(stream).toString());
stream.close();
public XMLDocument(final Node node) {
this(node, new XPathContext(), !(node instanceof Document));
}

/**
Expand Down Expand Up @@ -488,7 +478,7 @@ public Collection<SAXParseException> validate(final Schema schema) {
* @return A cloned node imported in a dedicated document.
*/
private static Node createImportedNode(final Node node) {
final DocumentBuilderFactory factory = FACTORY;
final DocumentBuilderFactory factory = XMLDocument.configuredDFactory();
final DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
Expand Down Expand Up @@ -609,8 +599,6 @@ private static Node transform(final Source source) {
return result.getNode();
}

private static final DocumentBuilderFactory FACTORY = XMLDocument.configuredDFactory();

/**
* Create new {@link DocumentBuilderFactory} and configure it.
* @return Configured factory
Expand Down

0 comments on commit 6a7004f

Please sign in to comment.