From 5d14e2e5d10a1c8ef4a2fa1ee26c0e633321a8f8 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Fri, 29 Nov 2024 17:22:08 +0300 Subject: [PATCH] feat(#889): proceed with XMLDocument removal --- src/main/java/org/eolang/jeo/Assembling.java | 11 +++-- .../representation/XmirRepresentation.java | 46 +++++++++++++++---- .../jeo/representation/xmir/XmlProgram.java | 2 +- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/eolang/jeo/Assembling.java b/src/main/java/org/eolang/jeo/Assembling.java index 2b10c07cf..8da16bc34 100644 --- a/src/main/java/org/eolang/jeo/Assembling.java +++ b/src/main/java/org/eolang/jeo/Assembling.java @@ -44,6 +44,11 @@ public final class Assembling implements Transformation { */ private final Path from; + /** + * XMIR representation. + */ + private final XmirRepresentation repr; + /** * Constructor. * @param target Target folder. @@ -52,6 +57,7 @@ public final class Assembling implements Transformation { Assembling(final Path target, final Path representation) { this.folder = target; this.from = representation; + this.repr = new XmirRepresentation(this.from); } @Override @@ -61,8 +67,7 @@ public Path source() { @Override public Path target() { - final XmirRepresentation repr = new XmirRepresentation(this.from); - final String name = new PrefixedName(repr.name()).decode(); + final String name = new PrefixedName(this.repr.name()).decode(); final String[] subpath = name.split("\\."); subpath[subpath.length - 1] = String.format("%s.class", subpath[subpath.length - 1]); return Paths.get(this.folder.toString(), subpath); @@ -70,6 +75,6 @@ public Path target() { @Override public byte[] transform() { - return new XmirRepresentation(this.from).toBytecode().bytes(); + return this.repr.toBytecode().bytes(); } } diff --git a/src/main/java/org/eolang/jeo/representation/XmirRepresentation.java b/src/main/java/org/eolang/jeo/representation/XmirRepresentation.java index d0b5be2e1..db7c56892 100644 --- a/src/main/java/org/eolang/jeo/representation/XmirRepresentation.java +++ b/src/main/java/org/eolang/jeo/representation/XmirRepresentation.java @@ -27,12 +27,18 @@ import com.jcabi.xml.XMLDocument; import java.io.FileNotFoundException; import java.nio.file.Path; +import java.util.Optional; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; import org.cactoos.scalar.Sticky; import org.cactoos.scalar.Synced; import org.cactoos.scalar.Unchecked; import org.eolang.jeo.representation.bytecode.Bytecode; import org.eolang.jeo.representation.xmir.XmlProgram; import org.eolang.parser.Schema; +import org.w3c.dom.Node; /** * Intermediate representation of a class files from XMIR. @@ -41,6 +47,11 @@ */ public final class XmirRepresentation { + /** + * XPath's factory. + */ + private static final XPathFactory FACTORY = XPathFactory.newInstance(); + /** * XML. */ @@ -94,17 +105,36 @@ private XmirRepresentation( /** * Retrieves class name from XMIR. + * This method intentionally uses classes from `org.w3c.dom` instead of `com.jcabi.xml` + * by performance reasons. * @return Class name. */ public String name() { - return new ClassName( - this.xml.value() - .xpath("/program/metas/meta/tail/text()") - .stream() - .findFirst() - .orElse(""), - this.xml.value().xpath("/program/@name").get(0) - ).full(); + final Node node = this.xml.value().node(); + final XPath xpath = XmirRepresentation.FACTORY.newXPath(); + try { + return new ClassName( + Optional.ofNullable( + ((Node) xpath.evaluate( + "/program/metas/meta/tail/text()", + node, + XPathConstants.NODE + )).getTextContent() + ).orElse(""), + String.valueOf( + xpath.evaluate( + "/program/@name", + node, + XPathConstants.STRING + ) + ) + ).full(); + } catch (final XPathExpressionException exception) { + throw new IllegalStateException( + String.format("Can't extract class name from the '%s' source", this.source), + exception + ); + } } /** diff --git a/src/main/java/org/eolang/jeo/representation/xmir/XmlProgram.java b/src/main/java/org/eolang/jeo/representation/xmir/XmlProgram.java index 88b3aff14..67db1c3e1 100644 --- a/src/main/java/org/eolang/jeo/representation/xmir/XmlProgram.java +++ b/src/main/java/org/eolang/jeo/representation/xmir/XmlProgram.java @@ -89,7 +89,7 @@ public XmlProgram(final XML xml) { * * @param root Root node. */ - private XmlProgram(final Node root) { + public XmlProgram(final Node root) { this.root = root; }