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 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;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

public class XmlReportParser {

  private Element root;
  private Document doc;
  private DocumentBuilder builder;

  public XmlReportParser() {
    DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
    try {
      bf.setFeature("http://apache.org/xml/features/validation/schema", false);
      bf.setFeature("http://xml.org/sax/features/external-general-entities", false);
      bf.setFeature("http://xml.org/sax/features/validation", false);
      bf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
      bf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
      bf.setNamespaceAware(false);
      bf.setValidating(false);
      builder = bf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
      throw new XmlParserException("can not instance a XML parser", e);
    }
  }

  public void parse(File file) {
    try {
      doc = builder.parse(file);
    } catch (SAXException e) {
      throw new XmlParserException("can not parse the file " + file.getAbsolutePath(), e);
    } catch (IOException e) {
      throw new XmlParserException("can not parse the file " + file.getAbsolutePath(), e);
    }
  }

  public void parse(InputStream stream)  {
    try {
      doc = builder.parse(stream);
    } catch (SAXException e) {
      throw new XmlParserException("can not parse the input stream", e);
    } catch (IOException e) {
      throw new XmlParserException("can not parse the stream", e);
    }
  }

  public void parse(String xml)  {
    try {
      doc = builder.parse(new InputSource(new StringReader(xml)));
    } catch (SAXException e) {
      throw new XmlParserException("can not parse xml : " + xml, e);
    } catch (IOException e) {
      throw new XmlParserException("can not parse xml : " + xml, e);
    }
  }

  public Element getRoot() {
    if (root == null && doc != null) {
      root = doc.getDocumentElement();
    }
    return root;
  }

  public 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;
  }

  public Element getChildElement(String elementName) {
    NodeList childrens = getRoot().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;
  }

  public List getChildElements(String elementName) {
    List rtrVal = new ArrayList();
    NodeList childrens = getRoot().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;
  }

  public 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;
  }

  public 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;
  }

  public String getChildElementValue(String elementName) {
    NodeList childrens = getRoot().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;
  }

  public Object executeXPath(Node node, QName qname, String xPathExpression)
      throws MojoExecutionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr;
    try {
      expr = xpath.compile(xPathExpression);
      return expr.evaluate(node, qname);

    } catch (XPathExpressionException e) {
      throw new MojoExecutionException("Unable to evaluate xpath expression :" + xPathExpression, e);
    }
  }

  public String executeXPath(String xPathExpression)
      throws MojoExecutionException {
    return (String) executeXPath(doc, XPathConstants.STRING, xPathExpression);
  }

  public String executeXPath(Node node, String xPathExpression)
      throws MojoExecutionException {
    return (String) executeXPath(node, XPathConstants.STRING, xPathExpression);
  }

  public NodeList executeXPathNodeList(String xPathExpression) throws MojoExecutionException {
    return (NodeList) executeXPath(doc, XPathConstants.NODESET, xPathExpression);
  }

  public NodeList executeXPathNodeList(Node node, String xPathExpression) throws MojoExecutionException {
    return (NodeList) executeXPath(node, XPathConstants.NODESET, xPathExpression);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy