Skip to content

Commit

Permalink
moved cmm stuff into ccm class
Browse files Browse the repository at this point in the history
  • Loading branch information
starkda committed Mar 21, 2024
1 parent aebacaa commit c9293ab
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 169 deletions.
120 changes: 112 additions & 8 deletions src/main/java/org/jpeek/calculus/java/Ccm.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,26 @@
*/
package org.jpeek.calculus.java;

import com.jcabi.xml.Sources;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.jcabi.xml.XSLDocument;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.cactoos.io.ResourceOf;
import org.cactoos.io.UncheckedInput;
import org.cactoos.text.FormattedText;
import org.cactoos.text.Joined;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;
import org.jpeek.calculus.Calculus;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

/**
* CCM metric Java calculus.
Expand All @@ -52,14 +63,19 @@ public XML node(
).toString()
);
}
return Ccm.withFixedNcc(
new XSLDocument(
new UncheckedInput(
new ResourceOf("org/jpeek/metrics/CCM.xsl")
).stream()
).transform(skeleton),
skeleton
final XSLDocument doc = new XSLDocument(
new UncheckedText(
new TextOf(
new ResourceOf(
new FormattedText("org/jpeek/metrics/%s.xsl", metric)
)
)
).asString(),
Sources.DUMMY,
params
);
final XML meta = addMetaTag(skeleton);
return doc.transform(meta);
}

/**
Expand All @@ -68,6 +84,7 @@ public XML node(
* @param skeleton XML Skeleton
* @return XML with fixed NCC.
*/
@SuppressWarnings("PMD.UnusedPrivateMethod")
private static XML withFixedNcc(final XML transformed, final XML skeleton) {
final List<XML> packages = transformed.nodes("//package");
for (final XML elt : packages) {
Expand Down Expand Up @@ -105,4 +122,91 @@ private static void updateNcc(
).toString()
);
}

/**
* Adds a meta tag to the skeleton XML.
* @param skeleton The skeleton XML to which the meta tag will be added
* @return The modified XML with the meta tag added
*/
private static XML addMetaTag(final XML skeleton) {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
final Document doc;
try {
builder = factory.newDocumentBuilder();
doc = builder.newDocument();
final Element meta = doc.createElement("meta");
Node packages = skeleton.node().getFirstChild().getFirstChild().getNextSibling()
.getFirstChild();
while (Objects.nonNull(packages)) {
if (packages.getTextContent().matches("\\s*")) {
packages = packages.getNextSibling();
continue;
}
final Node child = createPackageTag(doc, packages);
meta.appendChild(child);
packages = packages.getNextSibling();
}
doc.appendChild(meta);
} catch (final ParserConfigurationException ex) {
throw new IllegalStateException(ex);
}
final Node repr = skeleton.node().getFirstChild();
final Node node = repr.getOwnerDocument()
.importNode(doc.getDocumentElement(), true);
repr.appendChild(node);
return new XMLDocument(repr);
}

/**
* Creates a package tag element in the XML document.
* @param doc The XML document to which the package tag will be added
* @param packages The node representing the package
* @return The package tag element created in the XML document
*/
private static Node createPackageTag(final Document doc, final Node packages) {
final NamedNodeMap map = packages.getAttributes();
final Element root = doc.createElement("package");
final Node id = map.getNamedItem("id");
root.setAttribute("id", id.getNodeValue());
Node classes = packages.getFirstChild();
while (Objects.nonNull(classes)) {
if (classes.getTextContent().matches("\\s*")) {
classes = classes.getNextSibling();
continue;
}
final Node child = createClassTag(doc, classes);
root.appendChild(child);
classes = classes.getNextSibling();
}
return root;
}

/**
* Creates a class tag element in the XML document.
*
* This method constructs a class tag element in the XML document based on the
* information provided by the given node representing a class. It traverses
* through the attributes of the class node to extract necessary information
* and constructs the class tag element accordingly.
*
* Additionally, this method adds a child element representing a metric called
* "ncc" to the class tag.
*
* @param doc The XML document to which the class tag will be added
* @param classes The node representing the class
* @return The class tag element created in the XML document
* @todo Implement method that would return connected components
*/
private static Node createClassTag(final Document doc, final Node classes) {
final NamedNodeMap map = classes.getAttributes();
final Element root = doc.createElement("class");
final Node id = map.getNamedItem("id");
root.setAttribute("id", id.getNodeValue());
final Integer value = 1;
final Element ncc = doc.createElement("ncc");
ncc.setTextContent(value.toString());
root.appendChild(ncc);
return root;
}
}
157 changes: 0 additions & 157 deletions src/main/java/org/jpeek/calculus/xsl/CcmXslCalculus.java

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/resources/org/jpeek/metrics/CCM.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ SOFTWARE.
</xsl:variable>
<xsl:copy>
<xsl:variable name="nc" select="count($edges/edge)"/>
<!--
@todo add capture ncc value from skeleton
-->
<xsl:variable name="ncc" select="count(distinct-values($edges/edge/method/text()))"/>
<xsl:variable name="nmp" select="(count($methods) * (count($methods) - 1)) div 2"/>
<xsl:attribute name="value">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.io.IOException;
import java.util.HashMap;
import org.jpeek.FakeBase;
import org.jpeek.calculus.xsl.CcmXslCalculus;
import org.jpeek.skeleton.Skeleton;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;
Expand All @@ -38,10 +37,10 @@
* Test class for CCM calculus.
* @since 0.30.9
*/
final class CcmCalculusTest {
final class CcmTest {
@Test
void createsSkeletonWithMeta() throws IOException {
final XML result = new CcmXslCalculus().node(
final XML result = new Ccm().node(
"CCM", new HashMap<>(0), new Skeleton(
new FakeBase(
"Foo", "Bar"
Expand All @@ -59,6 +58,6 @@ void createsSkeletonWithMeta() throws IOException {
"Must have 2 ncc vars",
result.xpath("/metric/app/package/class/vars/var[@id=\'ncc\']/text()").size() == 2,
new IsTrue()
);
).affirm();
}
}

0 comments on commit c9293ab

Please sign in to comment.