Skip to content

Commit

Permalink
feat(#213): improve code quality and remove the 213 puzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Jul 19, 2023
1 parent 41b3879 commit 43998c9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
23 changes: 15 additions & 8 deletions src/main/java/com/jcabi/xml/SaxonDocument.java
Expand Up @@ -43,7 +43,14 @@
import net.sf.saxon.s9api.XdmNode;
import org.w3c.dom.Node;

public class SaxonDocument implements XML {
/**
* Saxon XML document.
*
* <p>Objects of this class are immutable, but NOT thread-safe.
*
* @since 0.28
*/
public final class SaxonDocument implements XML {

/**
* Saxon processor.
Expand All @@ -53,7 +60,7 @@ public class SaxonDocument implements XML {
/**
* Saxon document builder.
*/
private static final DocumentBuilder DOCUMENT_BUILDER = SaxonDocument.SAXON.newDocumentBuilder();
private static final DocumentBuilder DOC_BUILDER = SaxonDocument.SAXON.newDocumentBuilder();

/**
* Saxon XPath compiler.
Expand All @@ -69,7 +76,7 @@ public class SaxonDocument implements XML {
/**
* Saxon XML document node.
*/
private final XdmNode node;
private final XdmNode xdm;

/**
* Public constructor from XML as string text.
Expand All @@ -81,17 +88,17 @@ public SaxonDocument(final String text) {

/**
* Public constructor from Saxon XML document node.
* @param node Saxon XML document node.
* @param xml Saxon XML document node.
*/
public SaxonDocument(final XdmNode node) {
this.node = node;
public SaxonDocument(final XdmNode xml) {
this.xdm = xml;
}

@Override
public List<String> xpath(final String query) {
try {
final XPathSelector selector = SaxonDocument.XPATH_COMPILER.compile(query).load();
selector.setContextItem(this.node);
selector.setContextItem(this.xdm);
return selector.evaluate()
.stream()
.map(XdmItem::getStringValue)
Expand Down Expand Up @@ -139,7 +146,7 @@ public Node node() {
*/
private static XdmNode node(final String text) {
try {
return SaxonDocument.DOCUMENT_BUILDER
return SaxonDocument.DOC_BUILDER
.build(new StreamSource(new StringReader(text)));
} catch (final SaxonApiException exception) {
throw new IllegalArgumentException(
Expand Down
37 changes: 35 additions & 2 deletions src/test/java/com/jcabi/xml/SaxonDocumentTest.java
@@ -1,10 +1,43 @@
/*
* Copyright (c) 2012-2022, jcabi.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the jcabi.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jcabi.xml;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

class SaxonDocumentTest {
/**
* Test case for {@link SaxonDocument}.
* @since 0.28
*/
final class SaxonDocumentTest {

@Test
void findsXpathWithFunctionThatReturnsSeveralItems() {
Expand All @@ -16,4 +49,4 @@ void findsXpathWithFunctionThatReturnsSeveralItems() {
Matchers.hasItems("a|1", "b|2")
);
}
}
}
18 changes: 0 additions & 18 deletions src/test/java/com/jcabi/xml/XMLDocumentTest.java
Expand Up @@ -56,12 +56,6 @@
*
* @since 0.1
* @checkstyle AbbreviationAsWordInNameCheck (20 lines)
* @todo #221:30min Implement XPath 2.0 evaluations.
* We have to implement XPath 2.0 evaluations in order to support more complex XPath queries.
* For example, the following query is not supported:
* - "//o[@base and @ver]/concat(@base,'|',@ver)"
* When we implement XPath 2.0 evaluations, we should remove the @Disabled annotation from
* findsXpathWithFunctionThatReturnsSeveralItems test.
*/
@SuppressWarnings({"PMD.TooManyMethods", "PMD.DoNotUseThreads"})
final class XMLDocumentTest {
Expand Down Expand Up @@ -414,16 +408,4 @@ void appliesXpathToClonedNode() {
Matchers.equalTo("433")
);
}

@Test
@Disabled
void findsXpathWithFunctionThatReturnsSeveralItems() {
MatcherAssert.assertThat(
"XMLDocument can handle XPath 2.0 feature - XPath evaluation of concat method, but it can't",
new XMLDocument(
"<o><o base='a' ver='1'/><o base='b' ver='2'/></o>"
).xpath("//o[@base and @ver]/concat(@base,'|',@ver)"),
Matchers.hasItems("a|1", "b|2")
);
}
}

0 comments on commit 43998c9

Please sign in to comment.