diff --git a/src/main/java/org/eolang/jeo/representation/xmir/XmlNode.java b/src/main/java/org/eolang/jeo/representation/xmir/XmlNode.java index db430ff7b..6d78faba7 100644 --- a/src/main/java/org/eolang/jeo/representation/xmir/XmlNode.java +++ b/src/main/java/org/eolang/jeo/representation/xmir/XmlNode.java @@ -46,7 +46,7 @@ public final class XmlNode { /** * Parent node. */ - private final XML node; + private final Node node; /** * Constructor. @@ -56,19 +56,11 @@ public XmlNode(final String xml) { this(new XMLDocument(xml).node().getFirstChild()); } - /** - * Constructor. - * @param parent XML document - */ - public XmlNode(final Node parent) { - this(new XMLDocument(parent)); - } - /** * Constructor. * @param parent Parent node. */ - public XmlNode(final XML parent) { + public XmlNode(final Node parent) { this.node = parent; } @@ -76,7 +68,7 @@ public XmlNode(final XML parent) { public boolean equals(final Object obj) { final boolean res; if (obj instanceof XmlNode) { - res = this.node.equals(((XmlNode) obj).node); + res = new XMLDocument(this.node).equals(new XMLDocument(((XmlNode) obj).node)); } else { res = false; } @@ -106,7 +98,7 @@ public Stream children() { * @return Text content. */ public String text() { - return this.node.node().getTextContent(); + return this.node.getTextContent(); } /** @@ -116,7 +108,7 @@ public String text() { */ public Optional attribute(final String name) { final Optional result; - final NamedNodeMap attrs = this.node.node().getAttributes(); + final NamedNodeMap attrs = this.node.getAttributes(); if (attrs == null) { result = Optional.empty(); } else { @@ -140,7 +132,7 @@ XmlNode child(final String name) { * @return List of elements. */ List xpath(final String xpath) { - return this.node.xpath(xpath); + return new XMLDocument(this.node).xpath(xpath); } /** @@ -206,7 +198,7 @@ boolean hasAttribute(final String name, final String value) { * @return Class. */ XmlClass toClass() { - return new XmlClass(this.node.node()); + return new XmlClass(this.node); } /** @@ -233,9 +225,14 @@ XmlBytecodeEntry toEntry() { */ private Optional optchild(final String name) { Optional result = Optional.empty(); - final List nodes = this.node.nodes(name); - if (!nodes.isEmpty()) { - result = Optional.of(new XmlNode(nodes.get(0))); + final NodeList children = this.node.getChildNodes(); + final int length = children.getLength(); + for (int index = 0; index < length; ++index) { + final Node current = children.item(index); + if (current.getNodeName().equals(name)) { + result = Optional.of(new XmlNode(current)); + break; + } } return result; } @@ -260,7 +257,7 @@ private IllegalStateException notFound(final String name) { * @return Stream of class objects. */ private Stream objects() { - final NodeList children = this.node.node().getChildNodes(); + final NodeList children = this.node.getChildNodes(); final List res = new ArrayList<>(children.getLength()); for (int index = 0; index < children.getLength(); ++index) { final Node child = children.item(index); diff --git a/src/test/java/org/eolang/jeo/representation/XmirRepresentationTest.java b/src/test/java/org/eolang/jeo/representation/XmirRepresentationTest.java index 490c487da..4440303d7 100644 --- a/src/test/java/org/eolang/jeo/representation/XmirRepresentationTest.java +++ b/src/test/java/org/eolang/jeo/representation/XmirRepresentationTest.java @@ -160,7 +160,8 @@ void failsToOpenBrokenXmirRepresentationFromFile(@TempDir final Path dir) throws * This is a performance test, which is disabled by default. * It is used to measure the performance of the conversion of the EO object * into the bytecode representation and back. - * Timings before the optimization were: 21s, 23s, 21s, 21s (500 attempts) + * 1) Timings before the optimization were: 21s, 23s, 21s, 21s (500 attempts) + * 2) `XMLDocument` -> `org.w3c.Node` optimization: 11s, 10s, 11s, 10s (500 attempts) */ @Test @Disabled