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

#135 Added schema location to metric.xml #196

Merged
merged 8 commits into from
Feb 28, 2018
35 changes: 33 additions & 2 deletions src/main/java/org/jpeek/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@
import java.util.HashMap;
import java.util.Map;
import org.cactoos.collection.CollectionOf;
import org.cactoos.io.InputOf;
import org.cactoos.io.LengthOf;
import org.cactoos.io.TeeInput;
import org.cactoos.map.MapEntry;
import org.cactoos.map.MapOf;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;

/**
* Single report.
Expand All @@ -63,11 +67,16 @@ final class Report {
*/
private static final double DEFAULT_SIGMA = 0.1d;

/**
* Location to the schema file.
*/
private static final String SCHEMA_FILE = "xsd/metric.xsd";

/**
* XSD schema.
*/
private static final XSD SCHEMA = XSDDocument.make(
Report.class.getResourceAsStream("xsd/metric.xsd")
Report.class.getResourceAsStream(Report.SCHEMA_FILE)
);

/**
Expand Down Expand Up @@ -130,15 +139,37 @@ final class Report {
* @param mean Mean
* @param sigma Sigma
* @checkstyle ParameterNumberCheck (10 lines)
* @todo #135:30min The current solution to add the reference to
* 'metric.xsd' in the generated 'metric.xml' is too complex for
* the task at hand. Refactor towards a simpler solution that
* ideally would just require one or two Xembly instructions to
* add the required attribute.
*/
@SuppressWarnings("unchecked")
Report(final XML xml, final String name,
final Map<String, Object> args,
final double mean, final double sigma) {
this.skeleton = xml;
this.metric = name;
this.params = args;
this.params = new MapOf<>(
args,
new MapEntry<>("schemaLocation", Report.SCHEMA_FILE)
);
this.post = new XSLChain(
new CollectionOf<>(
new XSLDocument(
new UncheckedText(
new TextOf(
new InputOf(
Report.class.getResourceAsStream(
"xsl/metric-post-schemaloc.xsl"
Copy link
Member

Choose a reason for hiding this comment

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

@llorllale looks weird. Can't we just add this single attribute to the skeleton.xml when we are building it? Why making this simple operation so complex?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yegor256 My line of reasoning is: Report creates metric.xml so it is responsible for properly building it.

Skeleton creates the skeleton.xml so I believe it is responsible for properly building that xml.

I see that App is currently creating both index.xml and matrix.xml and I think those two things should be extracted into their own classes who in turn will be responsible for properly building those xmls.

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yegor256 let's also remember that the value for xsi:noNamespaceSchemaLocation will be different among the files.

Copy link
Member

Choose a reason for hiding this comment

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

@llorllale why can't you set it via Xembly? Why using XSL?

Copy link
Contributor Author

@llorllale llorllale Feb 23, 2018

Choose a reason for hiding this comment

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

@yegor256

why can't you set it via Xembly?

Xembler works with nodes. Can't set the required attribute on the node returned by the XML resulting from transforming the skeleton into metric because it's a document node (throws error "Cannot cast ...DocumentImpl to ...Element"). I think I would have to bend over backwards in the code to make it work (appending child elements, etc)

Why using XSL?

We already have a post-processing stage in Report so I took advantage of that. Also, this way seems cleaner to me than the alternative I described above. The noise can be reduced a little if XSLDocument had a ctor that accepted InputStream and params together.

)
)
)
).asString(),
Sources.DUMMY,
this.params
),
new XSLDocument(
Report.class.getResourceAsStream(
"xsl/metric-post-colors.xsl"
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/org/jpeek/xsd/index.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ SOFTWARE.
@todo #134:30min Index.xsd: add `xsd:documentation` tags as per issue #134. We need to
document the schemas in src/resources/org/jpeek/xsd so that the semantics of
the generated XML documents is transparent for maintainers.
@todo #135:30min Index.xsd: add a reference to this schema from the generated
index.xml. Schemas from src/resources/org/jpeek/xsd must be referenced in
generated XMLs.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="index">
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/org/jpeek/xsd/matrix.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ SOFTWARE.
@todo #134:30min Matrix.xsd: add `xsd:documentation` tags as per issue #134. We need to
document the schemas in src/resources/org/jpeek/xsd so that the semantics of
the generated XML documents is transparent for maintainers.
@todo #135:30min Matrix.xsd: add a reference to this schema from the generated
matrix.xml. Schemas from src/resources/org/jpeek/xsd must be referenced in
generated XMLs.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="matrix">
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/org/jpeek/xsd/skeleton.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ 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.
-->
<!--
@todo #135:30min Skeleton.xsd: add a reference to this schema from the generated
skeleton.xml. Schemas from src/resources/org/jpeek/xsd must be referenced in
Copy link
Member

Choose a reason for hiding this comment

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

@llorllale what skeleton.xml has to do with this file? Typo?

Copy link
Contributor Author

@llorllale llorllale Feb 21, 2018

Choose a reason for hiding this comment

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

@yegor256 yes, typo actually, NO, this one was not a typo :)

generated XMLs.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="class">
<xs:all>
Expand Down
40 changes: 40 additions & 0 deletions src/main/resources/org/jpeek/xsl/metric-post-schemaloc.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0"?>
<!--
The MIT License (MIT)

Copyright (c) 2017-2018 Yegor Bugayenko

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.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:param name="schemaLocation"/>
<xsl:template match="metric">
<xsl:element name="metric">
<xsl:attribute name="xsi:noNamespaceSchemaLocation" namespace="http://www.w3.org/2001/XMLSchema-instance">
<xsl:value-of select="$schemaLocation"/>
</xsl:attribute>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
14 changes: 14 additions & 0 deletions src/test/java/org/jpeek/ReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,18 @@ public void createsFullXmlReport() throws IOException {
);
}

@Test
public void setsCorrectschemaLocation() throws IOException {
final Path output = Files.createTempDirectory("");
new Report(new Skeleton(new FakeBase()).xml(), "LCOM").save(output);
MatcherAssert.assertThat(
XhtmlMatchers.xhtml(
new TextOf(output.resolve("LCOM.xml")).asString()
),
XhtmlMatchers.hasXPaths(
// @checkstyle LineLength (1 line)
"/metric[@xsi:noNamespaceSchemaLocation = 'xsd/metric.xsd']"
)
);
}
}