Skip to content

Commit

Permalink
feat(#215): add more useful constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Jul 20, 2023
1 parent 6336b2b commit d95c40c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
39 changes: 22 additions & 17 deletions src/main/java/com/jcabi/xml/SaxonDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)));
}

/**
Expand Down Expand Up @@ -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),
Expand Down
37 changes: 36 additions & 1 deletion src/test/java/com/jcabi/xml/SaxonDocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit d95c40c

Please sign in to comment.