org.xembly.Xembler Java Examples

The following examples show how to use org.xembly.Xembler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XslReport.java    From jpeek with MIT License 6 votes vote down vote up
/**
 * 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 {
    return new XMLDocument(
        new Xembler(
            new Directives()
                .xpath("/metric")
                .attr(
                    "xmlns:xsi",
                    "http://www.w3.org/2001/XMLSchema-instance"
                )
                .attr(
                    "xsi:noNamespaceSchemaLocation",
                    XslReport.SCHEMA_FILE
                )
        ).applyQuietly(
            this.calculus.node(
                this.metric, this.params, this.skeleton
            ).node()
        )
    );
}
 
Example #2
Source File: Skeleton.java    From jpeek with MIT License 6 votes vote down vote up
/**
 * Calculate Xembly for a single .class file.
 * @param ctc The class
 * @return Metrics
 */
private static Map.Entry<String, Directives> xembly(final CtClass ctc) {
    ctc.defrost();
    String pkg = ctc.getPackageName();
    if (pkg == null) {
        pkg = "";
    }
    return new MapEntry<>(
        pkg,
        new Directives()
            .add("class")
            .comment(
                Xembler.escape(
                    String.format(
                        "Package: %s; name: %s; file: %s",
                        ctc.getPackageName(),
                        ctc.getName(),
                        ctc.getClassFile().getName()
                    )
                )
            )
            .attr("id", ctc.getSimpleName())
            .append(new XmlClass(ctc))
            .up()
    );
}
 
Example #3
Source File: MistakesTest.java    From jpeek with MIT License 6 votes vote down vote up
@Test
@Disabled
public void acceptsAndRenders(@TempDir final Path output) throws Exception {
    final Path input = Paths.get(".");
    new App(input, output).analyze();
    final Mistakes mistakes = new Mistakes();
    mistakes.add(output);
    new Assertion<>(
        "Must accept and render",
        XhtmlMatchers.xhtml(
            new Xembler(
                new Directives().add("metrics").append(
                    new Joined<>(mistakes.worst())
                )
            ).xmlQuietly()
        ),
        XhtmlMatchers.hasXPath("/metrics/metric[@id='LCOM']/avg")
    ).affirm();
}
 
Example #4
Source File: ResultsTest.java    From jpeek with MIT License 6 votes vote down vote up
@Test
@Disabled
public void acceptsAndRenders(@TempDir final Path output) throws Exception {
    final Path input = Paths.get(".");
    new App(input, output).analyze();
    final Results results = new Results();
    results.add("org.takes:takes", output);
    new Assertion<>(
        "Must exists repos tag",
        XhtmlMatchers.xhtml(
            new Xembler(
                new Directives().add("repos").append(
                    new Joined<>(results.recent())
                )
            ).xmlQuietly()
        ),
        XhtmlMatchers.hasXPath("/repos")
    ).affirm();
}
 
Example #5
Source File: TypesOfTest.java    From jpeek with MIT License 6 votes vote down vote up
@Test
public void parsesSignature() {
    new Assertion<>(
        "Must parse signature",
        XhtmlMatchers.xhtml(
            new Xembler(
                new Directives().add("method").append(
                    new TypesOf("(Ljava/lang/String;Lorg/jpeek/Test;)Z")
                )
            ).xmlQuietly()
        ),
        XhtmlMatchers.hasXPaths(
            "/method/args[count(arg) = 2]",
            "/method/args/arg[@type = 'Ljava/lang/String']",
            "/method[return='Z']"
        )
    ).affirm();
}
 
Example #6
Source File: XmlAttributeTest.java    From eo with MIT License 6 votes vote down vote up
@Test
public void compileToXml() throws Exception {
    MatcherAssert.assertThat(
        XhtmlMatchers.xhtml(
            new Xembler(
                new XmlAttribute(
                    "count",
                    "Number"
                )
            ).xml()
        ),
        XhtmlMatchers.hasXPath(
            "/attribute[@name = 'count']/type[@name = 'Number']"
        )
    );
}
 
Example #7
Source File: RsXembly.java    From takes with MIT License 6 votes vote down vote up
/**
 * Render source as XML.
 * @param dom DOM node to build upon
 * @param src Source
 * @return XML
 * @throws IOException If fails
 */
private static InputStream render(final Node dom,
    final XeSource src) throws IOException {
    final Node copy = cloneNode(dom);
    final ByteArrayOutputStream baos =
        new ByteArrayOutputStream();
    final Node node = new Xembler(src.toXembly()).applyQuietly(copy);
    try {
        TransformerFactory.newInstance().newTransformer().transform(
            new DOMSource(node),
            new StreamResult(
                new WriterTo(baos)
            )
        );
    } catch (final TransformerException ex) {
        throw new IllegalStateException(ex);
    }
    return new ByteArrayInputStream(baos.toByteArray());
}
 
Example #8
Source File: ClassAsXml.java    From jpeek with MIT License 5 votes vote down vote up
@Override
public String value() {
    return new Xembler(
        new Directives().add("class").append(
            new XmlClass(
                new Classes(
                    new FakeBase(this.name)
                ).iterator().next()
            )
        )
    ).xmlQuietly();
}
 
Example #9
Source File: XslReportTest.java    From jpeek with MIT License 5 votes vote down vote up
@Test
public void createsFullXmlReport(@TempDir final Path output) throws IOException {
    new XslReport(
        new XMLDocument(
            new Xembler(
                new Directives()
                    .add("skeleton")
                    .append(new Header())
                    .add("app").attr("id", ".")
                    .add("package").attr("id", ".")
                    .add("class").attr("id", "A").attr("value", "0.1").up()
                    .add("class").attr("id", "B").attr("value", "0.5").up()
                    .add("class").attr("id", "C").attr("value", "0.6").up()
                    .add("class").attr("id", "D").attr("value", "0.7").up()
                    .add("class").attr("id", "E").attr("value", "NaN").up()
            ).xmlQuietly()
        ), new XslCalculus(), new ReportData("LCOM")
    ).save(output);
    new Assertion<>(
        "Must create full report",
        XhtmlMatchers.xhtml(
            new TextOf(output.resolve("LCOM.xml")).asString()
        ),
        XhtmlMatchers.hasXPaths(
            "/metric[min='0.5' and max='0.6']",
            "//class[@id='B' and @element='true']",
            "//class[@id='D' and @element='false']",
            "//class[@id='E' and @element='false']",
            "//statistics[total='5']",
            "//statistics[elements='2']",
            "//statistics[mean='0.55']"
        )
    ).affirm();
}
 
Example #10
Source File: DyUsage.java    From jare with MIT License 5 votes vote down vote up
@Override
public void add(final Date date, final long bytes) throws IOException {
    final int day = DyUsage.asNumber(date);
    final XML xml = this.xml();
    final String xpath = String.format("/usage/day[@id='%d']/text()", day);
    final List<String> items = xml.xpath(xpath);
    final long before;
    if (items.isEmpty()) {
        before = 0L;
    } else {
        before = Long.parseLong(items.get(0));
    }
    final Node node = xml.node();
    new Xembler(
        new Directives()
            .xpath(xpath).up().remove()
            .xpath("/usage").add("day").attr("id", day).set(before + bytes)
    ).applyQuietly(node);
    new Xembler(
        new Directives().xpath(
            String.format(
                "/usage/day[number(@id) < %d]",
                DyUsage.asNumber(DateUtils.addDays(new Date(), -Tv.THIRTY))
            )
        ).remove()
    ).applyQuietly(node);
    final XML after = new XMLDocument(node);
    this.save(after.toString());
    this.save(Long.parseLong(after.xpath("sum(/usage/day)").get(0)));
}
 
Example #11
Source File: XmlMethodDefTest.java    From eo with MIT License 5 votes vote down vote up
@Test
public void compileToXml() throws Exception {
    MatcherAssert.assertThat(
        XhtmlMatchers.xhtml(
            new Xembler(
                new XmlMethodDef(
                    "printf",
                    "Text",
                    new ListOf<>(
                        new Parameter(
                            "format",
                            "String"
                        ),
                        new Parameter(
                            "args",
                            "List"
                        )
                    )
                )
            ).xml()
        ),
        Matchers.allOf(
            XhtmlMatchers.hasXPath("/method[@name = 'printf']"),
            XhtmlMatchers.hasXPath("/method/type[@name = 'Text']"),
            XhtmlMatchers.hasXPath("/method/params[count(param) = 2]")
        )
    );
}
 
Example #12
Source File: XmlTypeDefTest.java    From eo with MIT License 5 votes vote down vote up
@Test
public void compileToXml() throws Exception {
    MatcherAssert.assertThat(
        XhtmlMatchers.xhtml(
            new Xembler(
                new XmlTypeDef(
                    "Book",
                    new ListOf<>(
                        "Text"
                    ),
                    new ListOf<>(
                        new Method(
                            "page",
                            Collections.emptyList(),
                            "Page"
                        )
                    )
                )
            ).xml()
        ),
        Matchers.allOf(
            XhtmlMatchers.hasXPath("/type[@name = 'Book']"),
            XhtmlMatchers.hasXPath("/type/super/type[@name = 'Text']"),
            XhtmlMatchers.hasXPath("/type/methods[count(method) = 1]")
        )
    );
}
 
Example #13
Source File: XmlParamTest.java    From eo with MIT License 5 votes vote down vote up
@Test
public void compileToXml() throws Exception {
    MatcherAssert.assertThat(
        XhtmlMatchers.xhtml(
            new Xembler(
                new XmlParam("value", "Int")
            ).xml()
        ),
        Matchers.allOf(
            XhtmlMatchers.hasXPath("/param/name[text() = 'value']"),
            XhtmlMatchers.hasXPath("/param/type[@name = 'Int']")
        )
    );
}
 
Example #14
Source File: ReportWithStatistics.java    From jpeek with MIT License 4 votes vote down vote up
/**
 * Ctor.
 * @param xml The XML
 */
ReportWithStatistics(final XML xml) {
    this.output = new Unchecked<>(
        new Solid<XML>(
            () -> {
                final Collection<Double> values = new Mapped<>(
                    Double::parseDouble,
                    xml.xpath(
                        // @checkstyle LineLength (1 line)
                        "//class[@value<=/metric/max and @value>=/metric/min]/@value"
                    )
                );
                final double total = (double) values.size();
                double sum = 0.0d;
                for (final Double value : values) {
                    sum += value;
                }
                final double mean = sum / total;
                double squares = 0.0d;
                for (final Double value : values) {
                    squares += Math.pow(value - mean, 2.0d);
                }
                final double variance = squares / total;
                final double sigma = Math.sqrt(variance);
                double defects = 0.0d;
                for (final Double value : values) {
                    if (value < mean - sigma || value > mean + sigma) {
                        ++defects;
                    }
                }
                return new XMLDocument(
                    new Xembler(
                        new Directives()
                            .xpath("/metric")
                            .add("statistics")
                            .add("total").set(xml.nodes("//class").size())
                            .up()
                            .add("elements").set((long) total).up()
                            .add("mean").set(Double.toString(mean)).up()
                            .add("sigma").set(Double.toString(sigma)).up()
                            .add("variance").set(Double.toString(variance))
                            .up()
                            .add("defects")
                            .set(Double.toString(defects / total)).up()
                    ).applyQuietly(xml.node())
                );
            }
        )
    );
}