Skip to content

Commit

Permalink
feat(objectionary#889): fix all the code offences
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Dec 1, 2024
1 parent c676d99 commit 032e367
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
51 changes: 38 additions & 13 deletions src/main/java/org/eolang/jeo/representation/XmirRepresentation.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;
Expand All @@ -39,12 +37,12 @@
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.cactoos.io.ResourceOf;
import org.cactoos.io.UncheckedInput;
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.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

Expand Down Expand Up @@ -185,19 +183,21 @@ private static Unchecked<Node> fromFile(final Path path) {
* Convert a path to XML.
* @param path Path to XML file.
* @return XML.
* @checkstyle IllegalCatchCheck (20 lines)
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private static Node open(final Path path) {
try {
DocumentBuilder builder = XmirRepresentation.DOC_FACTORY.newDocumentBuilder();
Document document = builder.parse(new java.io.FileInputStream(path.toFile()));
return document.getDocumentElement();
return XmirRepresentation.DOC_FACTORY
.newDocumentBuilder()
.parse(path.toFile())
.getDocumentElement();
} catch (final FileNotFoundException exception) {
throw new IllegalStateException(
String.format("Can't find file '%s'", path),
exception
);
} catch (final IllegalArgumentException | ParserConfigurationException | IOException |
SAXException broken) {
} catch (final Exception broken) {
throw new IllegalStateException(
String.format(
"Can't parse XML from the file '%s'",
Expand All @@ -208,22 +208,48 @@ private static Node open(final Path path) {
}
}


/**
* Optimized schema for XMIR.
* @since 0.6
*/
private static class OptimizedSchema {
/**
* Node.
*/
private final Node node;

/**
* Schema factory.
*/
private final SchemaFactory factory;

/**
* Constructor.
* @param node Node.
*/
OptimizedSchema(final Node node) {
this(node, SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"));
}

/**
* Constructor.
* @param node Node.
* @param factory Schema factory.
*/
private OptimizedSchema(final Node node, final SchemaFactory factory) {
this.node = node;
this.factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
this.factory = factory;
}

/**
* Check the node.
*/
void check() {
try {
this.factory.newSchema(
new StreamSource(new ResourceOf("XMIR.xsd").stream())
new StreamSource(new UncheckedInput(new ResourceOf("XMIR.xsd")).stream())
).newValidator().validate(new DOMSource(this.node));
} catch (final Exception exception) {
} catch (final IOException | SAXException exception) {
throw new IllegalStateException(
String.format(
"There are XSD violations, see the log",
Expand All @@ -232,7 +258,6 @@ void check() {
exception
);
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@
* @since 0.1
*/
public final class XmlProgram {
/**
* Program node name.
*/
private static final String PROGRAM = "program";

/**
* Root node.
Expand Down Expand Up @@ -123,7 +119,6 @@ public BytecodeProgram bytecode() {
*/
private XmlClass top() {
return new XmlNode(this.root)
// .child(XmlProgram.PROGRAM)
.child("objects")
.child("o")
.toClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import com.jcabi.log.Logger;
import com.jcabi.matchers.XhtmlMatchers;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -164,7 +163,7 @@ void failsToOpenBrokenXmirRepresentationFromFile(@TempDir final Path dir) throws
* into the bytecode representation and back.
* 1) Timings before the optimization were: 21s, 23s, 21s, 21s (500 attempts)
* 2) `XMLDocument` -> `org.w3c.Node` optimization: 11s, 10s, 11s, 10s (500 attempts)
* 3) Remove `XMLDocument` from the `XmirRepresentation` constructor: 10s, 9s, 9s, 9s (500 attempts)
* 3) Remove `XMLDocument` usage: 10s, 9s, 9s, 9s (500 attempts)
*/
@Test
@Disabled
Expand Down

0 comments on commit 032e367

Please sign in to comment.