Skip to content

Commit

Permalink
feat(#277): remove #node() method usages
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Dec 5, 2024
1 parent befa879 commit 4520d07
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/jcabi/xml/StrictXML.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public XML merge(final NamespaceContext context) {

@Override
public Node node() {
return this.origin.node();
return this.origin.deepCopy();
}

@Override
Expand Down Expand Up @@ -270,7 +270,7 @@ private static Collection<SAXParseException> validate(
validator.setErrorHandler(
new XMLDocument.ValidationHandler(errors)
);
final DOMSource dom = new DOMSource(xml.node());
final DOMSource dom = new DOMSource(xml.inner());
for (int retry = 1; retry <= max; ++retry) {
try {
validator.validate(dom);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/jcabi/xml/XML.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
* // ...
* }</pre>
*
* <p>You can always get DOM node out of this abstraction using {@link #node()}
* method.
* <p>You can always get DOM node out of this abstraction using {@link #inner()}
* or {@link #deepCopy()} methods.
*
* <p>{@code toString()} must produce a full XML.
*
Expand Down Expand Up @@ -165,7 +165,7 @@ public interface XML {
* This method works exactly the same as {@link #deepCopy()}.
* @return DOM node
*/
// @Deprecated
@Deprecated
Node node();

/**
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/jcabi/xml/XMLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,7 @@ public int hashCode() {

@Override
public Node node() {
final Node casted = this.cache;
final Node answer;
if (casted instanceof Document) {
answer = casted.cloneNode(true);
} else {
answer = XMLDocument.createImportedNode(casted);
}
return answer;
return this.deepCopy();
}

@Override
Expand All @@ -327,7 +320,14 @@ public Node inner() {

@Override
public Node deepCopy() {
return this.node();
final Node casted = this.cache;
final Node answer;
if (casted instanceof Document) {
answer = casted.cloneNode(true);
} else {
answer = XMLDocument.createImportedNode(casted);
}
return answer;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jcabi/xml/XSLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ private void transformInto(final XML xml, final Result result) {
trans.setErrorListener(errors);
final long start = System.nanoTime();
try {
trans.transform(new DOMSource(xml.node()), result);
trans.transform(new DOMSource(xml.inner()), result);
} catch (final TransformerException ex) {
final StringBuilder summary = new StringBuilder(
String.join("; ", errors.summary())
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/com/jcabi/xml/XMLDocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void printsWithNamespace() {
new XMLDocument(
new XMLDocument(
"<z xmlns:a='hey'><f a:boom='test'/></z>"
).node()
).inner()
).toString(),
Matchers.containsString("a:boom")
);
Expand All @@ -202,11 +202,11 @@ void retrievesDomNode() throws Exception {
this.getClass().getResource("simple.xml")
);
MatcherAssert.assertThat(
doc.nodes("/root/simple").get(0).node().getNodeName(),
doc.nodes("/root/simple").get(0).inner().getNodeName(),
Matchers.equalTo("simple")
);
MatcherAssert.assertThat(
doc.nodes("//simple").get(0).node().getNodeType(),
doc.nodes("//simple").get(0).inner().getNodeType(),
Matchers.equalTo(Node.ELEMENT_NODE)
);
}
Expand Down Expand Up @@ -392,7 +392,7 @@ void takesNodeInMultipleThreads() throws Exception {
final Runnable runnable = () -> {
try {
MatcherAssert.assertThat(
new XMLDocument(xml.node()).toString(),
new XMLDocument(xml.inner()).toString(),
Matchers.containsString(">5555<")
);
done.incrementAndGet();
Expand Down Expand Up @@ -434,11 +434,11 @@ void performsXpathCalculations() {
void buildsDomNode() {
final XML doc = new XMLDocument("<?xml version='1.0'?><f/>");
MatcherAssert.assertThat(
doc.node(),
doc.inner(),
Matchers.instanceOf(Document.class)
);
MatcherAssert.assertThat(
doc.nodes("/f").get(0).node(),
doc.nodes("/f").get(0).inner(),
Matchers.instanceOf(Element.class)
);
}
Expand Down Expand Up @@ -486,7 +486,7 @@ void preservesXmlNamespaces() {
@Test
void preservesImmutability() {
final XML xml = new XMLDocument("<r1><a/></r1>");
final Node node = xml.nodes("/r1/a").get(0).node();
final Node node = xml.nodes("/r1/a").get(0).deepCopy();
node.appendChild(node.getOwnerDocument().createElement("h9"));
MatcherAssert.assertThat(
xml,
Expand Down

0 comments on commit 4520d07

Please sign in to comment.