Skip to content

Commit

Permalink
#22 Report with HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Oct 25, 2017
1 parent 311f4c3 commit ed752d0
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 45 deletions.
46 changes: 1 addition & 45 deletions src/main/java/org/jpeek/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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();
Expand All @@ -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()
);
}

}
129 changes: 129 additions & 0 deletions src/main/java/org/jpeek/Report.java
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>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()
);
}

}
58 changes: 58 additions & 0 deletions src/test/java/org/jpeek/ReportTest.java
Original file line number Diff line number Diff line change
@@ -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)
);
}

}

0 comments on commit ed752d0

Please sign in to comment.