Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/#2764/add hash in xml #2781

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions eo-parser/src/main/java/org/eolang/parser/EoSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.yegor256.xsline.Xsline;
import java.io.IOException;
import java.util.List;
import org.antlr.v4.runtime.CommonTokenStream;
Expand Down Expand Up @@ -70,7 +71,7 @@ public EoSyntax(final String nme, final Input ipt) {
}

/**
* Compile it to XML and save.
* Compile it to XML.
*
* <p>No exception will be thrown if the syntax is invalid. In any case, XMIR will
* be generated and saved. Read it in order to find the errors,
Expand All @@ -92,10 +93,12 @@ public XML parsed() throws IOException {
parser.addErrorListener(spy);
final XeEoListener xel = new XeEoListener(this.name);
new ParseTreeWalker().walk(xel, parser.program());
final XML dom = new XMLDocument(
new Xembler(
new Directives(xel).append(spy)
).domQuietly()
final XML dom = new Xsline(new StHash()).pass(
new XMLDocument(
new Xembler(
new Directives(xel).append(spy)
).domQuietly()
)
);
new Schema(dom).check();
if (spy.size() == 0) {
Expand Down
11 changes: 7 additions & 4 deletions eo-parser/src/main/java/org/eolang/parser/PhiSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.yegor256.xsline.Xsline;
import java.io.IOException;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
Expand Down Expand Up @@ -84,10 +85,12 @@ public XML parsed() throws IOException {
parser.removeErrorListeners();
parser.addErrorListener(spy);
new ParseTreeWalker().walk(xel, parser.program());
final XML dom = new XMLDocument(
new Xembler(
new Directives(xel).append(spy)
).domQuietly()
final XML dom = new Xsline(new StHash()).pass(
new XMLDocument(
new Xembler(
new Directives(xel).append(spy)
).domQuietly()
)
);
new Schema(dom).check();
if (spy.size() == 0) {
Expand Down
102 changes: 102 additions & 0 deletions eo-parser/src/main/java/org/eolang/parser/StHash.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.parser;

import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.yegor256.xsline.StEnvelope;
import com.yegor256.xsline.StLambda;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.xembly.Directives;
import org.xembly.Xembler;

/**
* Add attribute 'hash' in node 'program' in XML.
* Returns already XML with hash.
*
* @since 0.35.0
*/
public final class StHash extends StEnvelope {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yanich96 I have some doubts about the StHash implementation and if we need it. I believe you can set the hash function directly in XeEoListener during the parsing. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@volodya-lombrozo Hash code computes from node 'objects'. There isn't this node in parsing stage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@volodya-lombrozo Xembly directives will have to be run 2 times: 1 - to calculate the hash, 2 - to completely parse, and during the parsing process there may be XSD schema errors and parsing errors, as a result of which the hash calculation ceases to make sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@volodya-lombrozo Hash code computes from node 'objects'. There isn't this node in parsing stage.

@Yanich96 Would exitProgram help us here? It looks like the final parsing stage.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@volodya-lombrozo Xembly directives will have to be run 2 times: 1 - to calculate the hash, 2 - to completely parse, and during the parsing process there may be XSD schema errors and parsing errors, as a result of which the hash calculation ceases to make sense.

We anyway will need to read XML twice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@volodya-lombrozo I will write todo for it.


/**
* Returns XML with attribute 'hash'.
*/
public StHash() {
super(
new StLambda((
position,
xml) ->
new XMLDocument(
new Xembler(
new Directives().xpath("//program").attr("hash", new Hash(xml).compute())
).apply(xml.node())
)
)
);
}

/**
* Hash is generated by MD5 using node '/program/objects'.
* Returns program hash.
*
* @since 0.35.0
*/
public static final class Hash {

/**
* XML for which the hash code will be made.
*/
private final XML xml;

/**
* The constructor.
*
* @param xml XML for which the hash code will be made.
*/
public Hash(final XML xml) {
this.xml = xml;
}

/**
* Return program hash using node "/program/objects".
*
* @return String hash of this XML.
* @throws NoSuchAlgorithmException If fails.
*/
public String compute() throws NoSuchAlgorithmException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yanich96 This method contains lots of redundant variables. https://www.yegor256.com/2015/09/01/redundant-variables-are-evil.html

Copy link
Member

@volodya-lombrozo volodya-lombrozo Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yanich96 The method name looks a bit misleading. The method returns a value, but you use verb here. Maybe it's better to use a noun?

final MessageDigest algorithm = MessageDigest.getInstance("MD5");
final String program = this.xml.nodes("/program/objects").toString();
final byte[] code = algorithm.digest(program.getBytes());
final BigInteger number = new BigInteger(1, code);
final StringBuilder hash = new StringBuilder(number.toString(16));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yanich96 Could you just use Hex.encodeHexString() from Apache?

https://stackoverflow.com/a/9655275/10423604

Seems, we have this library in the classpath.

while (hash.length() < 32) {
hash.insert(0, "0");
}
return hash.toString();
}
}
}

1 change: 1 addition & 0 deletions eo-parser/src/main/resources/XMIR.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ SOFTWARE.
<xs:attribute name="version" type="xs:string" use="required"/>
<xs:attribute name="revision" type="xs:string" use="required"/>
<xs:attribute name="dob" type="xs:dateTime" use="required"/>
<xs:attribute name="hash" type="xs:string" use="required"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yanich96 Returning to the discussion. Maybe we don't need the hash attribute? Especially if we will save hash as a file attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@volodya-lombrozo
I don't understand how to save hash as a file attribute.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@volodya-lombrozo In OptCahed.java the method 'contains(final XML xml)' get XML, but not file. We should get hash code of program, because we can't have file attributes. This reason is saving hash code in XML.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yanich96 You can always calculate the hash on the fly, like hash(xml); and compare it with the cache file attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@Yanich96 Yanich96 Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@volodya-lombrozo I have done todo. This line will be deleted after it.

<xs:attribute name="source" type="xs:string"/>
</xs:complexType>
<xs:element name="program" type="program"/>
Expand Down
13 changes: 13 additions & 0 deletions eo-parser/src/test/java/org/eolang/parser/EoSyntaxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
*
* @since 0.1
*/
@SuppressWarnings("PMD.TooManyMethods")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yanich96 I believe, you don't need that line. Could you please, remove the line and run the following command:

mvn qulice:check -Pqulice

I did it myself and the linter said nothing about methods number.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@volodya-lombrozo Test failed : PMD: eo-parser/src/test/java/org/eolang/parser/EoSyntaxTest.java[50-250]: This class has too many methods, consider refactoring it. (TooManyMethods)
I used this command.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yanich96 Ok. Let's leave it as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@volodya-lombrozo This line will be deleted in the next pr, when I will transfer hash code in file attributes.

final class EoSyntaxTest {
@Test
void parsesSimpleCode() throws Exception {
Expand Down Expand Up @@ -175,6 +176,18 @@ void parsesDefinition() throws IOException {
);
}

@Test
void containsHash() throws IOException {
MatcherAssert.assertThat(
"XML file should have 'hash' attribute on program node, but didn't",
new EoSyntax(
"test-it-5",
new InputOf("[v] > p\n f.write > @\n")
).parsed(),
XhtmlMatchers.hasXPath("/program/@hash")
);
}

@Test
void parsesMethodCalls() throws IOException {
MatcherAssert.assertThat(
Expand Down
11 changes: 11 additions & 0 deletions eo-parser/src/test/java/org/eolang/parser/PhiSyntaxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,15 @@ void addsError() throws IOException {
)
);
}

@Test
void containsHash() throws IOException {
MatcherAssert.assertThat(
"Result XML must contain hash",
new PhiSyntax(
"empty ↦ Φ.org.eolang.bytes"
).parsed(),
XhtmlMatchers.hasXPath("/program/@hash")
);
}
}
77 changes: 77 additions & 0 deletions eo-parser/src/test/java/org/eolang/parser/StHashTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.parser;

import com.jcabi.matchers.XhtmlMatchers;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.yegor256.xsline.Xsline;
import java.security.NoSuchAlgorithmException;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import org.xembly.Directives;
import org.xembly.Xembler;

/**
* Test case for {@link StHash}.
*
* @since 0.35.0
*/
final class StHashTest {

@Test
void isHashInXml() {
MatcherAssert.assertThat(
"We should get XML, which has new attribute 'hash', but didn't",
new Xsline(new StHash()).pass(program()),
XhtmlMatchers.hasXPath("/program/@hash")
);
}

@Test
void checksHash() throws NoSuchAlgorithmException {
MatcherAssert.assertThat(
"We should get the same hash code, but didn't",
new Xsline(new StHash()).pass(program()),
XhtmlMatchers.hasXPath("/program/@hash", new StHash.Hash(program()).compute())
);
}

/**
* Generates EO program for tests with specified time and context.
* @return XML representation of program.
*/
private static XML program() {
return new XMLDocument(
new Xembler(
new Directives()
.add("program")
.attr("name", "main")
.add("objects")
.attr("object", "10")
.up()
).xmlQuietly()
);
}
}
Loading