All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.hortis.sonar.mvn.mc.XmlReportParser Maven / Gradle / Ivy

/*
 * This program is copyright (c) 2007 Hortis-GRC SA.
 * 
 * This file is part of Sonar.
 * Sonar is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 * 
 * Sonar is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License 
 * along with Sonar; if not, write to the Free Software 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package ch.hortis.sonar.mvn.mc;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.namespace.QName;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.maven.plugin.MojoExecutionException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Util class used to parse reports in XML format
 */
public class XmlReportParser extends BaseMetricCollector {

    protected Element root;

    private Document doc;

    protected XmlReportParser() {
    }

    public XmlReportParser(File report) throws MojoExecutionException {
        parseDocument(report);
    }

    public XmlReportParser(InputStream report) throws MojoExecutionException {
        parseDocument(report);
    }

    protected void parseDocument(File report) throws MojoExecutionException {
        try {
            parseDocument(new BufferedInputStream(new FileInputStream(report)));
        } catch (FileNotFoundException e) {
            throw new MojoExecutionException("Unable to find file '"
                    + report.getAbsolutePath() + "'", e);
        }
    }

    protected void parseDocument(InputStream report)
            throws MojoExecutionException {
        try {
            DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = bf.newDocumentBuilder();
            doc = builder.parse(report);
            root = doc.getDocumentElement();
        } catch (Exception ex) {
            throw new MojoExecutionException("XML parsing error", ex);
        }
    }

    protected Element getChildElement(Element base, String elementName) {
        NodeList childrens = base.getElementsByTagName(elementName);
        for (int i = 0; i < childrens.getLength(); i++) {
            Node nde = childrens.item(i);
            if (nde.getNodeType() == Node.ELEMENT_NODE) {
                return (Element) nde;
            }
        }
        return null;
    }

    protected Element getChildElement(String elementName) {
        NodeList childrens = root.getElementsByTagName(elementName);
        for (int i = 0; i < childrens.getLength(); i++) {
            Node nde = childrens.item(i);
            if (nde.getNodeType() == Node.ELEMENT_NODE) {
                return (Element) nde;
            }
        }
        return null;
    }

    protected List getChildElements(String elementName) {
        List rtrVal = new ArrayList();
        NodeList childrens = root.getElementsByTagName(elementName);
        for (int i = 0; i < childrens.getLength(); i++) {
            Node nde = childrens.item(i);
            if (nde.getNodeType() == Node.ELEMENT_NODE) {
                rtrVal.add((Element) nde);
            }
        }
        return rtrVal;
    }

    protected List getChildElements(Element base, String elementName) {
        List rtrVal = new ArrayList();
        NodeList childrens = base.getElementsByTagName(elementName);
        for (int i = 0; i < childrens.getLength(); i++) {
            Node nde = childrens.item(i);
            if (nde.getNodeType() == Node.ELEMENT_NODE) {
                rtrVal.add((Element) nde);
            }
        }
        return rtrVal;
    }

    protected String getChildElementValue(Element base, String elementName) {
        NodeList childrens = base.getElementsByTagName(elementName);
        for (int i = 0; i < childrens.getLength(); i++) {
            if (childrens.item(i).getNodeType() == Node.ELEMENT_NODE) {
                return childrens.item(i).getFirstChild().getNodeValue();
            }
        }
        return null;
    }

    protected String getChildElementValue(String elementName) {
        NodeList childrens = root.getElementsByTagName(elementName);
        for (int i = 0; i < childrens.getLength(); i++) {
            if (childrens.item(i).getNodeType() == Node.ELEMENT_NODE) {
                return childrens.item(i).getFirstChild().getNodeValue();
            }
        }
        return null;
    }

    protected Element getRootElement() {
        return root;
    }

    public String executeXPath(String xPathExpression)
            throws MojoExecutionException {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr;
        try {
            expr = xpath.compile(xPathExpression);
            return expr.evaluate(doc);
        } catch (XPathExpressionException e) {
            throw new MojoExecutionException(
                    "Unable to evaluate xpath expression '" + xPathExpression
                            + "'", e);
        }
    }

    public Object executeXPath(String xPathExpression, QName namespace)
            throws MojoExecutionException {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr;
        try {
            expr = xpath.compile(xPathExpression);
            return expr.evaluate(doc, namespace);
        } catch (XPathExpressionException e) {
            throw new MojoExecutionException(
                    "Unable to evaluate xpath expression '" + xPathExpression
                            + "'", e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy