Skip to content

Commit 43998c9

Browse files
feat(#213): improve code quality and remove the 213 puzzle
1 parent 41b3879 commit 43998c9

3 files changed

Lines changed: 50 additions & 28 deletions

File tree

src/main/java/com/jcabi/xml/SaxonDocument.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@
4343
import net.sf.saxon.s9api.XdmNode;
4444
import org.w3c.dom.Node;
4545

46-
public class SaxonDocument implements XML {
46+
/**
47+
* Saxon XML document.
48+
*
49+
* <p>Objects of this class are immutable, but NOT thread-safe.
50+
*
51+
* @since 0.28
52+
*/
53+
public final class SaxonDocument implements XML {
4754

4855
/**
4956
* Saxon processor.
@@ -53,7 +60,7 @@ public class SaxonDocument implements XML {
5360
/**
5461
* Saxon document builder.
5562
*/
56-
private static final DocumentBuilder DOCUMENT_BUILDER = SaxonDocument.SAXON.newDocumentBuilder();
63+
private static final DocumentBuilder DOC_BUILDER = SaxonDocument.SAXON.newDocumentBuilder();
5764

5865
/**
5966
* Saxon XPath compiler.
@@ -69,7 +76,7 @@ public class SaxonDocument implements XML {
6976
/**
7077
* Saxon XML document node.
7178
*/
72-
private final XdmNode node;
79+
private final XdmNode xdm;
7380

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

8289
/**
8390
* Public constructor from Saxon XML document node.
84-
* @param node Saxon XML document node.
91+
* @param xml Saxon XML document node.
8592
*/
86-
public SaxonDocument(final XdmNode node) {
87-
this.node = node;
93+
public SaxonDocument(final XdmNode xml) {
94+
this.xdm = xml;
8895
}
8996

9097
@Override
9198
public List<String> xpath(final String query) {
9299
try {
93100
final XPathSelector selector = SaxonDocument.XPATH_COMPILER.compile(query).load();
94-
selector.setContextItem(this.node);
101+
selector.setContextItem(this.xdm);
95102
return selector.evaluate()
96103
.stream()
97104
.map(XdmItem::getStringValue)
@@ -139,7 +146,7 @@ public Node node() {
139146
*/
140147
private static XdmNode node(final String text) {
141148
try {
142-
return SaxonDocument.DOCUMENT_BUILDER
149+
return SaxonDocument.DOC_BUILDER
143150
.build(new StreamSource(new StringReader(text)));
144151
} catch (final SaxonApiException exception) {
145152
throw new IllegalArgumentException(

src/test/java/com/jcabi/xml/SaxonDocumentTest.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
1+
/*
2+
* Copyright (c) 2012-2022, jcabi.com
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met: 1) Redistributions of source code must retain the above
8+
* copyright notice, this list of conditions and the following
9+
* disclaimer. 2) Redistributions in binary form must reproduce the above
10+
* copyright notice, this list of conditions and the following
11+
* disclaimer in the documentation and/or other materials provided
12+
* with the distribution. 3) Neither the name of the jcabi.com nor
13+
* the names of its contributors may be used to endorse or promote
14+
* products derived from this software without specific prior written
15+
* permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19+
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20+
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21+
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28+
* OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
130
package com.jcabi.xml;
231

332
import org.hamcrest.MatcherAssert;
433
import org.hamcrest.Matchers;
534
import org.junit.jupiter.api.Test;
635

7-
class SaxonDocumentTest {
36+
/**
37+
* Test case for {@link SaxonDocument}.
38+
* @since 0.28
39+
*/
40+
final class SaxonDocumentTest {
841

942
@Test
1043
void findsXpathWithFunctionThatReturnsSeveralItems() {
@@ -16,4 +49,4 @@ void findsXpathWithFunctionThatReturnsSeveralItems() {
1649
Matchers.hasItems("a|1", "b|2")
1750
);
1851
}
19-
}
52+
}

src/test/java/com/jcabi/xml/XMLDocumentTest.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@
5656
*
5757
* @since 0.1
5858
* @checkstyle AbbreviationAsWordInNameCheck (20 lines)
59-
* @todo #221:30min Implement XPath 2.0 evaluations.
60-
* We have to implement XPath 2.0 evaluations in order to support more complex XPath queries.
61-
* For example, the following query is not supported:
62-
* - "//o[@base and @ver]/concat(@base,'|',@ver)"
63-
* When we implement XPath 2.0 evaluations, we should remove the @Disabled annotation from
64-
* findsXpathWithFunctionThatReturnsSeveralItems test.
6559
*/
6660
@SuppressWarnings({"PMD.TooManyMethods", "PMD.DoNotUseThreads"})
6761
final class XMLDocumentTest {
@@ -414,16 +408,4 @@ void appliesXpathToClonedNode() {
414408
Matchers.equalTo("433")
415409
);
416410
}
417-
418-
@Test
419-
@Disabled
420-
void findsXpathWithFunctionThatReturnsSeveralItems() {
421-
MatcherAssert.assertThat(
422-
"XMLDocument can handle XPath 2.0 feature - XPath evaluation of concat method, but it can't",
423-
new XMLDocument(
424-
"<o><o base='a' ver='1'/><o base='b' ver='2'/></o>"
425-
).xpath("//o[@base and @ver]/concat(@base,'|',@ver)"),
426-
Matchers.hasItems("a|1", "b|2")
427-
);
428-
}
429411
}

0 commit comments

Comments
 (0)