From 43998c97e98a7e7d1ed750650537b63a3d1e3c5c Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Wed, 19 Jul 2023 15:19:52 +0300 Subject: [PATCH] feat(#213): improve code quality and remove the 213 puzzle --- .../java/com/jcabi/xml/SaxonDocument.java | 23 ++++++++---- .../java/com/jcabi/xml/SaxonDocumentTest.java | 37 ++++++++++++++++++- .../java/com/jcabi/xml/XMLDocumentTest.java | 18 --------- 3 files changed, 50 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/jcabi/xml/SaxonDocument.java b/src/main/java/com/jcabi/xml/SaxonDocument.java index bbdb95e..69f6350 100644 --- a/src/main/java/com/jcabi/xml/SaxonDocument.java +++ b/src/main/java/com/jcabi/xml/SaxonDocument.java @@ -43,7 +43,14 @@ import net.sf.saxon.s9api.XdmNode; import org.w3c.dom.Node; -public class SaxonDocument implements XML { +/** + * Saxon XML document. + * + *

Objects of this class are immutable, but NOT thread-safe. + * + * @since 0.28 + */ +public final class SaxonDocument implements XML { /** * Saxon processor. @@ -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. @@ -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. @@ -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 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) @@ -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( diff --git a/src/test/java/com/jcabi/xml/SaxonDocumentTest.java b/src/test/java/com/jcabi/xml/SaxonDocumentTest.java index cd15432..8600925 100644 --- a/src/test/java/com/jcabi/xml/SaxonDocumentTest.java +++ b/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() { @@ -16,4 +49,4 @@ void findsXpathWithFunctionThatReturnsSeveralItems() { Matchers.hasItems("a|1", "b|2") ); } -} \ No newline at end of file +} diff --git a/src/test/java/com/jcabi/xml/XMLDocumentTest.java b/src/test/java/com/jcabi/xml/XMLDocumentTest.java index 65dc00f..b6e1bb0 100644 --- a/src/test/java/com/jcabi/xml/XMLDocumentTest.java +++ b/src/test/java/com/jcabi/xml/XMLDocumentTest.java @@ -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 { @@ -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( - "" - ).xpath("//o[@base and @ver]/concat(@base,'|',@ver)"), - Matchers.hasItems("a|1", "b|2") - ); - } }