Skip to content

Commit

Permalink
feat(objectionary#889): proceed with XMLDocument removal
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Nov 29, 2024
1 parent 08c1050 commit 5d14e2e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
11 changes: 8 additions & 3 deletions src/main/java/org/eolang/jeo/Assembling.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -61,15 +67,14 @@ 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);
}

@Override
public byte[] transform() {
return new XmirRepresentation(this.from).toBytecode().bytes();
return this.repr.toBytecode().bytes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -41,6 +47,11 @@
*/
public final class XmirRepresentation {

/**
* XPath's factory.
*/
private static final XPathFactory FACTORY = XPathFactory.newInstance();

/**
* XML.
*/
Expand Down Expand Up @@ -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
);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 5d14e2e

Please sign in to comment.