Search in sources :

Example 56 with XPathFactory

use of javax.xml.xpath.XPathFactory in project jangaroo-tools by CoreMedia.

the class PomConverter method removeExmlPlugin.

/**
   * Replaces exml-maven-plugin configuration by jangaroo-maven-plugin configuration within the given {@code plugins}
   * element.
   */
private static void removeExmlPlugin(Node pluginsNode) throws XPathException {
    if (pluginsNode == null) {
        return;
    }
    Document document = pluginsNode.getOwnerDocument();
    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xPath = xPathFactory.newXPath();
    Node exmlPluginNode = (Node) xPath.evaluate("plugin[artifactId='exml-maven-plugin']", pluginsNode, NODE);
    if (exmlPluginNode == null) {
        return;
    }
    Node jangarooPluginNode = (Node) xPath.evaluate("plugin[artifactId='jangaroo-maven-plugin']", pluginsNode, NODE);
    Node exmlVersionNode = (Node) xPath.evaluate("version", exmlPluginNode, NODE);
    Node exmlExtensionNode = (Node) xPath.evaluate("extensions", exmlPluginNode, NODE);
    Node configClassPackageNode = (Node) xPath.evaluate("configuration/configClassPackage", exmlPluginNode, NODE);
    if (jangarooPluginNode == null) {
        jangarooPluginNode = document.createElement("plugin");
        insertChildWithWhitespace(pluginsNode, jangarooPluginNode, exmlPluginNode);
        Node jangarooPluginGroupIdNode = document.createElement("groupId");
        jangarooPluginGroupIdNode.setTextContent("net.jangaroo");
        insertChildWithWhitespace(jangarooPluginNode, jangarooPluginGroupIdNode, null);
        Node jangarooPluginArtifactIdNode = document.createElement("artifactId");
        jangarooPluginArtifactIdNode.setTextContent("jangaroo-maven-plugin");
        insertChildWithWhitespace(jangarooPluginNode, jangarooPluginArtifactIdNode, null);
        if (exmlVersionNode != null) {
            Node jangarooPluginVersionNode = document.createElement("version");
            jangarooPluginVersionNode.setTextContent(exmlVersionNode.getTextContent());
            insertChildWithWhitespace(jangarooPluginNode, jangarooPluginVersionNode, null);
        }
    }
    Node jangarooConfigurationNode = (Node) xPath.evaluate("configuration", jangarooPluginNode, NODE);
    Node jangarooExtensionsNode = (Node) xPath.evaluate("extensions", jangarooPluginNode, NODE);
    removeChildWithWhitespace(pluginsNode, exmlPluginNode);
    if (exmlExtensionNode != null) {
        if (jangarooExtensionsNode == null) {
            insertChildWithWhitespace(jangarooPluginNode, exmlExtensionNode, jangarooConfigurationNode);
        } else {
            jangarooExtensionsNode.setTextContent(exmlExtensionNode.getTextContent());
        }
    }
    if (configClassPackageNode != null) {
        if (jangarooConfigurationNode == null) {
            jangarooConfigurationNode = document.createElement("configuration");
            insertChildWithWhitespace(jangarooPluginNode, jangarooConfigurationNode, null);
        }
        Node namespacesNode = document.createElement("namespaces");
        insertChildWithWhitespace(jangarooConfigurationNode, namespacesNode, null);
        Node namespaceNode = document.createElement("namespace");
        insertChildWithWhitespace(namespacesNode, namespaceNode, null);
        Node uriNode = document.createElement("uri");
        uriNode.setTextContent("exml:" + configClassPackageNode.getTextContent());
        insertChildWithWhitespace(namespaceNode, uriNode, null);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document)

Example 57 with XPathFactory

use of javax.xml.xpath.XPathFactory in project spring-boot by spring-projects.

the class GradleIT method evaluateExpression.

private static String evaluateExpression(String expression) {
    try {
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();
        XPathExpression expr = xpath.compile(expression);
        String version = expr.evaluate(new InputSource(new FileReader("pom.xml")));
        return version;
    } catch (Exception ex) {
        throw new IllegalStateException("Failed to evaluate expression", ex);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) InputSource(org.xml.sax.InputSource) FileReader(java.io.FileReader)

Example 58 with XPathFactory

use of javax.xml.xpath.XPathFactory in project spring-boot by spring-projects.

the class Versions method evaluateExpression.

private static String evaluateExpression(String expression) {
    try {
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();
        XPathExpression expr = xpath.compile(expression);
        String version = expr.evaluate(new InputSource(new FileReader("pom.xml")));
        return version;
    } catch (Exception ex) {
        throw new IllegalStateException("Failed to evaluate expression", ex);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) InputSource(org.xml.sax.InputSource) FileReader(java.io.FileReader)

Example 59 with XPathFactory

use of javax.xml.xpath.XPathFactory in project perun by CESNET.

the class MUStrategy method parseResponse.

/**
	 * Parse String response as XML document and retrieve Publications from it.
	 * @param xml XML response from MU Prezentator
	 * @return List of Publications
	 * @throws CabinetException If anything fails
	 */
protected List<Publication> parseResponse(String xml) throws CabinetException {
    assert xml != null;
    List<Publication> result = new ArrayList<Publication>();
    //hook for titles with &
    xml = xml.replace("&", "&amp;");
    //log.debug("RESPONSE: "+xml);
    //Create new document factory builder
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new CabinetException("Error when creating newDocumentBuilder.", ex);
    }
    Document doc;
    try {
        doc = builder.parse(new InputSource(new StringReader(xml)));
    } catch (SAXParseException ex) {
        throw new CabinetException("Error when parsing uri by document builder.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    } catch (SAXException ex) {
        throw new CabinetException("Problem with parsing is more complex, not only invalid characters.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    } catch (IOException ex) {
        throw new CabinetException("Error when parsing uri by document builder. Problem with input or output.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    }
    //Prepare xpath expression
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression publicationsQuery;
    try {
        publicationsQuery = xpath.compile("/P/UL/publication");
    } catch (XPathExpressionException ex) {
        throw new CabinetException("Error when compiling xpath query.", ex);
    }
    NodeList nodeList;
    try {
        nodeList = (NodeList) publicationsQuery.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException ex) {
        throw new CabinetException("Error when evaluate xpath query on document.", ex);
    }
    //Test if there is any nodeset in result
    if (nodeList.getLength() == 0) {
        //There is no results, return empty subjects
        return result;
    }
    //Iterate through nodes and convert them to Map<String,String>
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node singleNode = nodeList.item(i);
        // remove node from original structure in order to keep access time constant (otherwise is exp.)
        singleNode.getParentNode().removeChild(singleNode);
        try {
            Publication publication = convertNodeToPublication(singleNode);
            result.add(publication);
        } catch (InternalErrorException ex) {
            log.error("Unable to parse Publication:", ex);
        }
    }
    return result;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Publication(cz.metacentrum.perun.cabinet.model.Publication) IOException(java.io.IOException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 60 with XPathFactory

use of javax.xml.xpath.XPathFactory in project perun by CESNET.

the class OBD30Strategy method parseResponse.

/**
	 * Parse String response as XML document and retrieve Publications from it.
	 * @param xml XML response from OBD 3.0
	 * @return List of Publications
	 * @throws CabinetException If anything fails
	 */
protected List<Publication> parseResponse(String xml) throws CabinetException {
    assert xml != null;
    List<Publication> result = new ArrayList<Publication>();
    //hook for titles with &
    xml = xml.replace("&", "&amp;");
    //log.debug("RESPONSE: "+xml);
    //Create new document factory builder
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new CabinetException("Error when creating newDocumentBuilder.", ex);
    }
    Document doc;
    try {
        doc = builder.parse(new InputSource(new StringReader(xml)));
    } catch (SAXParseException ex) {
        throw new CabinetException("Error when parsing uri by document builder.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    } catch (SAXException ex) {
        throw new CabinetException("Problem with parsing is more complex, not only invalid characters.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    } catch (IOException ex) {
        throw new CabinetException("Error when parsing uri by document builder. Problem with input or output.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    }
    //Prepare xpath expression
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression publicationsQuery;
    try {
        publicationsQuery = xpath.compile("/zaznamy/zaznam");
    } catch (XPathExpressionException ex) {
        throw new CabinetException("Error when compiling xpath query.", ex);
    }
    NodeList nodeList;
    try {
        nodeList = (NodeList) publicationsQuery.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException ex) {
        throw new CabinetException("Error when evaluate xpath query on document.", ex);
    }
    //Test if there is any nodeset in result
    if (nodeList.getLength() == 0) {
        //There is no results, return empty subjects
        return result;
    }
    //Iterate through nodes and convert them to Map<String,String>
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node singleNode = nodeList.item(i);
        // remove node from original structure in order to keep access time constant (otherwise is exp.)
        singleNode.getParentNode().removeChild(singleNode);
        try {
            Publication publication = convertNodeToPublication(singleNode);
            result.add(publication);
        } catch (InternalErrorException ex) {
            log.error("Unable to parse Publication:", ex);
        }
    }
    return result;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Publication(cz.metacentrum.perun.cabinet.model.Publication) IOException(java.io.IOException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

XPathFactory (javax.xml.xpath.XPathFactory)75 XPath (javax.xml.xpath.XPath)59 XPathExpression (javax.xml.xpath.XPathExpression)40 Document (org.w3c.dom.Document)34 NodeList (org.w3c.dom.NodeList)34 XPathExpressionException (javax.xml.xpath.XPathExpressionException)26 DocumentBuilder (javax.xml.parsers.DocumentBuilder)24 Node (org.w3c.dom.Node)24 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)22 InputSource (org.xml.sax.InputSource)16 Test (org.junit.Test)15 IOException (java.io.IOException)13 PBXNativeTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget)11 PBXTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXTarget)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 Path (java.nio.file.Path)11 SAXException (org.xml.sax.SAXException)11 PBXFileReference (com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)7