diff --git a/src/main/java/com/jcabi/xml/SaxonDocument.java b/src/main/java/com/jcabi/xml/SaxonDocument.java index ab737c8..841f0b8 100644 --- a/src/main/java/com/jcabi/xml/SaxonDocument.java +++ b/src/main/java/com/jcabi/xml/SaxonDocument.java @@ -30,6 +30,7 @@ package com.jcabi.xml; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.net.URI; @@ -39,7 +40,6 @@ import java.util.List; import java.util.stream.Collectors; import javax.xml.namespace.NamespaceContext; -import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import net.sf.saxon.s9api.DocumentBuilder; import net.sf.saxon.s9api.Processor; @@ -117,24 +117,30 @@ public SaxonDocument(final File file) { this(SaxonDocument.node(new StreamSource(file))); } - public SaxonDocument(final Source source) { - this(SaxonDocument.node(new StreamSource(source.getSystemId()))); - } - - public SaxonDocument(final Node node) { - this(SaxonDocument.node(node.getTextContent())); - } - - public SaxonDocument(final URL url) { - this.xdm = null; + /** + * Public constructor from XML reached by URL. + * @param url URL of XML document. + * @throws IOException If fails. + */ + public SaxonDocument(final URL url) throws IOException { + this(SaxonDocument.node(new TextResource(url).toString())); } - public SaxonDocument(final URI uri) { - this.xdm = null; + /** + * Public constructor from XML reached by URI. + * @param uri URI of XML document. + * @throws IOException If fails. + */ + public SaxonDocument(final URI uri) throws IOException { + this(SaxonDocument.node(new TextResource(uri).toString())); } + /** + * Public constructor from XML as input stream. + * @param stream Input stream with XML document. + */ public SaxonDocument(final InputStream stream) { - this.xdm = null; + this(SaxonDocument.node(new StreamSource(stream))); } /** @@ -201,13 +207,12 @@ private static XdmNode node(final String text) { /** * Build Saxon XML document node from XML source. - * @param source of XML. + * @param source XML. * @return Saxon XML document node. */ private static XdmNode node(final StreamSource source) { try { - return SaxonDocument.DOC_BUILDER - .build(source); + return SaxonDocument.DOC_BUILDER.build(source); } catch (final SaxonApiException exception) { throw new IllegalArgumentException( String.format("SaxonDocument can't parse XML from source '%s'", source), diff --git a/src/test/java/com/jcabi/xml/SaxonDocumentTest.java b/src/test/java/com/jcabi/xml/SaxonDocumentTest.java index 6f221da..9460047 100644 --- a/src/test/java/com/jcabi/xml/SaxonDocumentTest.java +++ b/src/test/java/com/jcabi/xml/SaxonDocumentTest.java @@ -29,13 +29,16 @@ */ package com.jcabi.xml; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; @@ -89,6 +92,38 @@ void createsFromByteArray() { ); } + @Test + void createsFromUrl() throws IOException { + final URL resource = this.getClass().getResource("simple.xml"); + MatcherAssert.assertThat( + String.format(SaxonDocumentTest.ASSERTION_MSG, resource), + new SaxonDocument(resource).xpath("//simple"), + Matchers.hasSize(1) + ); + } + + @Test + void createsFromUri() throws URISyntaxException, IOException { + final URI resource = this.getClass().getResource("simple.xml").toURI(); + MatcherAssert.assertThat( + String.format(SaxonDocumentTest.ASSERTION_MSG, resource), + new SaxonDocument(resource).xpath("//simple"), + Matchers.hasSize(1) + ); + } + + @Test + void createsFromStream() { + MatcherAssert.assertThat( + String.format(SaxonDocumentTest.ASSERTION_MSG, SaxonDocumentTest.DEFAULT_XML), + new SaxonDocument( + new ByteArrayInputStream( + SaxonDocumentTest.DEFAULT_XML.getBytes(StandardCharsets.UTF_8) + ) + ).xpath("//o[@base]"), + Matchers.hasSize(1) + ); + } @Test void findsXpathWithConcatFunctionThatReturnsSeveralItems() {