View Javadoc

1   package net.sf.mavenhist.extractor.jncss;
2   
3   import javax.xml.transform.TransformerException;
4   
5   import net.sf.mavenhist.extractor.AbstractValidatingXMLValueExtractor;
6   import net.sf.mavenhist.extractor.MultiMetric;
7   
8   import org.apache.xpath.XPathAPI;
9   import org.w3c.dom.Document;
10  import org.w3c.dom.Node;
11  import org.w3c.dom.NodeList;
12  
13  /**
14   * Extracts the function count per package of a measured entity from a JavaNCSS-XML file.
15   */
16  public class JNCSSFunctionsPerPackageExtractor extends AbstractValidatingXMLValueExtractor {
17    
18    /**
19     * {@inheritDoc}
20     */
21    public MultiMetric[] extractValidatedDocument(Document document) throws TransformerException {
22      String xPathFunctions = "/javancss/packages/package/functions/text()";
23      String xPathLocations = "/javancss/packages/package/name/text()";
24      NodeList functionsNodeList = XPathAPI.selectNodeList(document, xPathFunctions);
25      NodeList locationsNodeList = XPathAPI.selectNodeList(document, xPathLocations);
26      int nodeListLength = locationsNodeList.getLength();
27      MultiMetric[] metrics = new MultiMetric[nodeListLength];
28      for (int i = 0; i < nodeListLength; i++) {
29        Node locationNode = locationsNodeList.item(i);
30        Node valueNode = functionsNodeList.item(i);
31        metrics[i] = new MultiMetric(locationNode.getNodeValue(), valueNode.getNodeValue());
32      }
33      return metrics;
34    }
35  
36    /**
37     * {@inheritDoc}
38     */
39    protected String getValidityTestXPath() {
40      return "/javancss";
41    }
42    
43    /**
44     * {@inheritDoc}
45     */
46    public String getName() {
47      return "jncssfunctionsperpackage";
48    }
49  }