/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.plugins.gradle.util;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.StringReader;

/**
 * {@link XmlXpathHelper} utility class for query of XML using XPath expressions.
 *
 * @author Vladislav.Soroka
 * @since 2/20/14
 */
public class XmlXpathHelper {
  private final XPath xpath;
  private Document xmlDocument;

  /**
   * Parses the given XML string as a DOM document, using the JDK parser. The parser does not
   * validate, and is namespace aware.
   *
   * @param xml            the XML content to be parsed (must be well formed)
   * @param namespaceAware whether the parser is namespace aware
   * @throws XmlParserException
   */
  public XmlXpathHelper(String xml, boolean namespaceAware) throws XmlParserException {
    xpath = XPathFactory.newInstance().newXPath();
    try {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      InputSource is = new InputSource(new StringReader(xml));
      factory.setNamespaceAware(namespaceAware);
      factory.setValidating(false);
      xmlDocument = factory.newDocumentBuilder().parse(is);
    }
    catch (Exception ex) {
      throw new XmlParserException(ex);
    }
  }

  public XmlXpathHelper(String xml) throws XmlParserException {
    this(xml, false);
  }

  public String queryXml(final String xpathExpr) throws XmlParserException {
    try {
      return xmlDocument == null ? "" : xpath.evaluate(xpathExpr, xmlDocument);
    }
    catch (XPathExpressionException ex) {
      throw new XmlParserException(ex);
    }
  }

  /**
   * Extracts the String value for the given expression.
   *
   * @param node
   * @param expr
   * @return
   */
  public String getValue(Node node, String expr) throws XmlParserException {
    try {
      return xpath.compile(expr).evaluate(node);
    }
    catch (XPathExpressionException ex) {
      throw new XmlParserException(ex);
    }
  }

  /**
   * {@link XmlParserException} indicates errors during xml processing.
   */
  public static class XmlParserException extends Exception {
    public XmlParserException(final Throwable cause) {
      super(cause);
    }

    public XmlParserException(final String message, final Throwable cause) {
      super(message, cause);
    }

    public XmlParserException(final String message) {
      super(message);
    }
  }
}

@@ -671,33 +671,8 @@
 nt;%0A
-import org.w3c.dom.Node;%0A
 impo
@@ -1352,73 +1352,8 @@
 ed)%0A
-   * @param namespaceAware whether the parser is namespace aware%0A
    *
@@ -1424,32 +1424,8 @@
  xml
-, boolean namespaceAware
 ) th
@@ -1687,21 +1687,12 @@
 are(
-namespaceAwar
+fals
 e);%0A
@@ -1869,98 +1869,8 @@
    %7D
-%0A  %7D%0A%0A  public XmlXpathHelper(String xml) throws XmlParserException %7B%0A    this(xml, false)
 ;%0A  
@@ -2142,364 +2142,8 @@
  %7D%0A%0A
-  /**%0A   * Extracts the String value for the given expression.%0A   *%0A   * @param node%0A   * @param expr%0A   * @return%0A   */%0A  public String getValue(Node node, String expr) throws XmlParserException %7B%0A    try %7B%0A      return xpath.compile(expr).evaluate(node);%0A    %7D%0A    catch (XPathExpressionException ex) %7B%0A      throw new XmlParserException(ex);%0A    %7D%0A  %7D%0A%0A
   /*

drop unused code, simplify