diff --git a/src/main/java/org/jpeek/App.java b/src/main/java/org/jpeek/App.java index 2c794661..4c65e9b1 100644 --- a/src/main/java/org/jpeek/App.java +++ b/src/main/java/org/jpeek/App.java @@ -23,11 +23,6 @@ */ package org.jpeek; -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 java.io.IOException; import java.nio.file.Path; import org.cactoos.io.LengthOf; @@ -39,8 +34,6 @@ import org.jpeek.metrics.basic.TotalFiles; import org.jpeek.metrics.cohesion.CAMC; import org.jpeek.metrics.cohesion.LCOM; -import org.xembly.Directives; -import org.xembly.Xembler; /** * Application. @@ -54,13 +47,6 @@ */ public final class App { - /** - * XSD schema. - */ - private static final XSD SCHEMA = XSDDocument.make( - App.class.getResourceAsStream("jpeek.xsd") - ); - /** * Location of the project to analyze. */ @@ -96,19 +82,7 @@ public void analyze() throws IOException { new And( metrics, metric -> { - new LengthOf( - new TeeInput( - new StrictXML( - App.xml(metric), App.SCHEMA - ).toString(), - this.output.resolve( - String.format( - "%s.xml", - metric.getClass().getSimpleName() - ) - ) - ) - ).value(); + new Report(metric).save(this.output); } ) ).value(); @@ -120,22 +94,4 @@ public void analyze() throws IOException { ).value(); } - /** - * Make XML. - * @param metric The metric - * @return XML - * @throws IOException If fails - */ - private static XML xml(final Metric metric) throws IOException { - return new XMLDocument( - new Xembler( - new Directives() - .pi("xml-stylesheet", "href='jpeek.xsl' type='text/xsl'") - .append(metric.xembly()) - .xpath("/app") - .attr("title", metric.getClass().getName()) - ).xmlQuietly() - ); - } - } diff --git a/src/main/java/org/jpeek/Report.java b/src/main/java/org/jpeek/Report.java new file mode 100644 index 00000000..4b9c3d40 --- /dev/null +++ b/src/main/java/org/jpeek/Report.java @@ -0,0 +1,129 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 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. + */ +package org.jpeek; + +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.XSLDocument; +import java.io.IOException; +import java.nio.file.Path; +import org.cactoos.io.LengthOf; +import org.cactoos.io.TeeInput; +import org.xembly.Directives; +import org.xembly.Xembler; + +/** + * Single report. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.1 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +final class Report { + + /** + * XSD schema. + */ + private static final XSD SCHEMA = XSDDocument.make( + App.class.getResourceAsStream("jpeek.xsd") + ); + + /** + * XSL stylesheet. + */ + private static final XSL STYLESHEET = XSLDocument.make( + App.class.getResourceAsStream("jpeek.xsl") + ); + + /** + * The metric. + */ + private final Metric metric; + + /** + * Ctor. + * @param mtc Metric + */ + Report(final Metric mtc) { + this.metric = mtc; + } + + /** + * Save report. + * @param target Target dir + * @throws IOException If fails + */ + public void save(final Path target) throws IOException { + final XML xml = new StrictXML( + this.xml(), Report.SCHEMA + ); + new LengthOf( + new TeeInput( + xml.toString(), + target.resolve( + String.format( + "%s.xml", + this.metric.getClass().getSimpleName() + ) + ) + ) + ).value(); + new LengthOf( + new TeeInput( + Report.STYLESHEET.transform(xml).toString(), + target.resolve( + String.format( + "%s.html", + this.metric.getClass().getSimpleName() + ) + ) + ) + ).value(); + } + + /** + * Make XML. + * @return XML + * @throws IOException If fails + */ + private XML xml() throws IOException { + return new XMLDocument( + new Xembler( + new Directives() + .pi("xml-stylesheet", "href='jpeek.xsl' type='text/xsl'") + .append(this.metric.xembly()) + .xpath("/app") + .attr("title", this.metric.getClass().getSimpleName()) + ).xmlQuietly() + ); + } + +} diff --git a/src/test/java/org/jpeek/ReportTest.java b/src/test/java/org/jpeek/ReportTest.java new file mode 100644 index 00000000..d218276e --- /dev/null +++ b/src/test/java/org/jpeek/ReportTest.java @@ -0,0 +1,58 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 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. + */ +package org.jpeek; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.jpeek.metrics.FakeBase; +import org.jpeek.metrics.cohesion.LCOM; +import org.junit.Test; + +/** + * Test case for {@link Report}. + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.4 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class ReportTest { + + @Test + public void createsXmlReport() throws IOException { + final Path output = Files.createTempDirectory(""); + new Report(new LCOM(new FakeBase("Foo"))).save(output); + MatcherAssert.assertThat( + Files.exists(output.resolve("LCOM.xml")), + Matchers.equalTo(true) + ); + MatcherAssert.assertThat( + Files.exists(output.resolve("LCOM.html")), + Matchers.equalTo(true) + ); + } + +}