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

(#227) Refactor setting schema reference attribute #326

Merged
merged 1 commit into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
79 changes: 39 additions & 40 deletions src/main/java/org/jpeek/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@
import com.jcabi.xml.Sources;
import com.jcabi.xml.StrictXML;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.jcabi.xml.XSD;
import com.jcabi.xml.XSDDocument;
import com.jcabi.xml.XSL;
import com.jcabi.xml.XSLChain;
import com.jcabi.xml.XSLDocument;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
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.ResourceOf;
import org.cactoos.io.TeeInput;
import org.cactoos.map.MapEntry;
import org.cactoos.map.MapOf;
import org.cactoos.text.FormattedText;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;
import org.xembly.Directives;
import org.xembly.Xembler;

/**
* Single report.
Expand Down Expand Up @@ -139,37 +139,15 @@ 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 = new MapOf<>(
args,
new MapEntry<>("schemaLocation", Report.SCHEMA_FILE)
);
this.params = args;
this.post = new XSLChain(
new CollectionOf<>(
new XSLDocument(
new UncheckedText(
new TextOf(
new InputOf(
Report.class.getResourceAsStream(
"xsl/metric-post-schemaloc.xsl"
)
)
)
).asString(),
Sources.DUMMY,
this.params
),
new XSLDocument(
Report.class.getResourceAsStream(
"xsl/metric-post-colors.xsl"
Expand Down Expand Up @@ -228,20 +206,41 @@ public void save(final Path target) throws IOException {
* Make XML.
* @return XML
* @throws IOException If fails
* @todo #227:30min Add a test to check whether passing params to
* XSLDocument really works. Currently only C3 metric template
* is known to use parameter named 'ctors'. However C3.xsl is a
* work in progress and has impediments, see #175. In case the
* parameter becomes obsolete, consider simplifying construction
* of XSLDocument without params (see reviews to #326).
*/
private XML xml() throws IOException {
final String name = String.format("metrics/%s.xsl", this.metric);
final URL res = this.getClass().getResource(name);
if (res == null) {
throw new IllegalArgumentException(
String.format("XSL not found: %s", name)
);
}
return new XSLDocument(
new TextOf(res).asString(),
Sources.DUMMY,
this.params
).transform(this.skeleton);
return new XMLDocument(
gsnoff marked this conversation as resolved.
Show resolved Hide resolved
new Xembler(
Copy link
Contributor

Choose a reason for hiding this comment

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

In this class, sometimes resources are accessed viathis.getClass().getResource() sometimes via Report.class.getResource(). Need to decide which way you want to use and stick to it, so it would be uniform across the whole class (either one way or another).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ilyakharlamov Fixed. Turns out the this.getClass() form isn’t used anywhere else in the project.

new Directives()
.xpath("/metric")
.attr(
"xmlns:xsi",
"http://www.w3.org/2001/XMLSchema-instance"
)
.attr(
"xsi:noNamespaceSchemaLocation",
Report.SCHEMA_FILE
)
).applyQuietly(
new XSLDocument(
new TextOf(
new ResourceOf(
new FormattedText(
"org/jpeek/metrics/%s.xsl",
this.metric
)
)
).asString(),
Sources.DUMMY,
this.params
).transform(this.skeleton).node()
)
Copy link
Contributor

Choose a reason for hiding this comment

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

If we replace that with just applyQuietly( new XSLDocument( this.getClass().getResourceAsStream(name) ).transform(this.skeleton).node() ) all the unit test pass just fine. This means that either this.params is not needed or isn't tested. So we need to either remove this.params at all, or add a unit test that checks whether those params really work.

Copy link
Contributor Author

@gsnoff gsnoff Apr 28, 2019

Choose a reason for hiding this comment

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

@ilyakharlamov Currently only C3.xsl and OCC.xsl metric templates in src/main/resources/org/jpeek/metrics have a <xsl:param/> tag defining a parameter labeled ctors, and it seems that OCC.xsl doesn’t even use the parameter. C3.xsl is a work in progress, see #175.

Maybe leave a puzzle here or in ReportTest.java describing the impediment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ilyakharlamov FYI, added the puzzle.

);
}

}
1 change: 0 additions & 1 deletion src/main/resources/org/jpeek/metrics/OCC.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ 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="ctors" select="0"/>
<xsl:template match="skeleton">
<metric>
<xsl:apply-templates select="@*"/>
Expand Down
40 changes: 0 additions & 40 deletions src/main/resources/org/jpeek/xsl/metric-post-schemaloc.xsl

This file was deleted.