Skip to content

Commit

Permalink
(#227) Refactor setting schema reference attribute
Browse files Browse the repository at this point in the history
Also apply stylistic fixes suggested in reviews to the pull request.
  • Loading branch information
gsnoff committed Apr 28, 2019
1 parent c3d214e commit b753f7f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 81 deletions.
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(
new Xembler(
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()
)
);
}

}
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.

2 comments on commit b753f7f

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on b753f7f May 1, 2019

Choose a reason for hiding this comment

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

Puzzle 135-1c80191e disappeared from src/main/java/org/jpeek/Report.java, that's why I closed #227. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on b753f7f May 1, 2019

Choose a reason for hiding this comment

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

Puzzle 227-fbf042c2 discovered in src/main/java/org/jpeek/Report.java and submitted as #327. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.