-
Notifications
You must be signed in to change notification settings - Fork 140
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* 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 | ||
* | ||
* @todo #2764:30min Replace the hash code entry | ||
* from the XML file node attribute to the file attribute. | ||
* This is necessary to increase the speed of searching for a cached file. | ||
* The hash code entry should be deleted from EoSyntax.java and PhiSyntax.java. | ||
* Hash code should save to the file attribute in ParseMojo.java and UnphiMojo. | ||
* Delete tests containsHash() from EoSyntaxTest.java and PhiSyntaxTest.java. | ||
* @todo #2764:30min Is it possible set the hash function directly | ||
* in XeEoListener during the parsing? Now hash code is generated in class StHash.java. | ||
* Maybe it will be better generating hash code in class XeEoListener. | ||
*/ | ||
public final class StHash extends StEnvelope { | ||
|
||
/** | ||
* 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 BigInteger number = new BigInteger( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yanich96 |
||
1, | ||
MessageDigest | ||
.getInstance("MD5") | ||
.digest( | ||
this.xml.nodes( | ||
"/program/objects" | ||
) | ||
.toString().getBytes() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yanich96 Here you apply There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @volodya-lombrozo I use '/program/objects' for creating the hash code. If 'xml.toString' is used, hash code will be created from all xml. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @volodya-lombrozo Using the entire xml will cause the hash code to be inconsistent. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yanich96 Why? |
||
) | ||
); | ||
final StringBuilder hash = new StringBuilder(number.toString(16)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yanich96 Could you just use 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(); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"/> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yanich96 Returning to the discussion. Maybe we don't need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @volodya-lombrozo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've found this solution: https://stackoverflow.com/questions/18955475/add-custom-attribute-or-metadata-to-file-java There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yanich96 You can always calculate the hash on the fly, like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"/> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ | |
* | ||
* @since 0.1 | ||
*/ | ||
@SuppressWarnings("PMD.TooManyMethods") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
I did it myself and the linter said nothing about methods number. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yanich96 Ok. Let's leave it as is. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -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( | ||
|
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() | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
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 thehash
function directly inXeEoListener
during the parsing. What do you think?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Yanich96 Would
exitProgram
help us here? It looks like the final parsing stage.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We anyway will need to read XML twice
There was a problem hiding this comment.
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.