Search in sources :

Example 26 with XPath

use of javax.xml.xpath.XPath in project camel by apache.

the class SpringBootStarterMojo method fixAdditionalRepositories.

private void fixAdditionalRepositories(Document pom) throws Exception {
    if (project.getFile() != null) {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document originalPom = builder.parse(project.getFile());
        XPath xpath = XPathFactory.newInstance().newXPath();
        Node repositories = (Node) xpath.compile("/project/repositories").evaluate(originalPom, XPathConstants.NODE);
        if (repositories != null) {
            pom.getDocumentElement().appendChild(pom.createComment(GENERATED_SECTION_START));
            pom.getDocumentElement().appendChild(pom.importNode(repositories, true));
            pom.getDocumentElement().appendChild(pom.createComment(GENERATED_SECTION_END));
        }
    } else {
        getLog().warn("Cannot access the project pom file to retrieve repositories");
    }
}
Also used : XPath(javax.xml.xpath.XPath) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Node(org.w3c.dom.Node) DependencyNode(org.apache.maven.shared.dependency.tree.DependencyNode) Document(org.w3c.dom.Document)

Example 27 with XPath

use of javax.xml.xpath.XPath in project camel by apache.

the class BaseNexusRepository method indexNexus.

/**
     * Runs the task to index nexus for new artifacts
     */
protected void indexNexus() throws Exception {
    // must have q parameter so use component to find all component
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setIgnoringElementContentWhitespace(true);
    factory.setIgnoringComments(true);
    DocumentBuilder documentBuilder = factory.newDocumentBuilder();
    URL url = createNexusUrl();
    InputStream is = url.openStream();
    try {
        Document dom = documentBuilder.parse(is);
        XPathFactory xpFactory = XPathFactory.newInstance();
        XPath exp = xpFactory.newXPath();
        NodeList list = (NodeList) exp.evaluate("//data/artifact", dom, XPathConstants.NODESET);
        Set<NexusArtifactDto> newArtifacts = new LinkedHashSet<>();
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            String g = getNodeText(node.getChildNodes(), "groupId");
            String a = getNodeText(node.getChildNodes(), "artifactId");
            String v = getNodeText(node.getChildNodes(), "version");
            String l = getNodeText(node.getChildNodes(), "artifactLink");
            if (g != null & a != null & v != null & l != null) {
                NexusArtifactDto dto = new NexusArtifactDto();
                dto.setGroupId(g);
                dto.setArtifactId(a);
                dto.setVersion(v);
                dto.setArtifactLink(l);
                log.debug("Found: {}:{}:{}", dto.getGroupId(), dto.getArtifactId(), dto.getVersion());
                // is it a new artifact
                boolean newArtifact = true;
                for (NexusArtifactDto existing : indexedArtifacts) {
                    if (existing.getGroupId().equals(dto.getGroupId()) && existing.getArtifactId().equals(dto.getArtifactId()) && existing.getVersion().equals(dto.getVersion())) {
                        newArtifact = false;
                        break;
                    }
                }
                if (newArtifact) {
                    newArtifacts.add(dto);
                }
            }
        }
        // if there is any new artifacts then process them
        if (!newArtifacts.isEmpty()) {
            onNewArtifacts(newArtifacts);
        }
    } finally {
        close(is);
    }
}
Also used : XPath(javax.xml.xpath.XPath) LinkedHashSet(java.util.LinkedHashSet) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) URL(java.net.URL) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 28 with XPath

use of javax.xml.xpath.XPath in project languagetool by languagetool-org.

the class AtDEvaluator method getRuleMatches.

private List<RuleMatch> getRuleMatches(String resultXml, AnnotatedText text) throws XPathExpressionException {
    List<RuleMatch> matches = new ArrayList<>();
    Document document = getDocument(resultXml);
    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList errors = (NodeList) xPath.evaluate("//error", document, XPathConstants.NODESET);
    for (int i = 0; i < errors.getLength(); i++) {
        Node error = errors.item(i);
        String string = xPath.evaluate("string", error);
        String description = xPath.evaluate("description", error);
        String preContext = xPath.evaluate("precontext", error);
        String errorText = preContext + " " + string;
        int fromPos = text.getPlainText().indexOf(errorText) + preContext.length() + 1;
        int toPos = fromPos + string.length();
        NodeList suggestions = (NodeList) xPath.evaluate("suggestions", error, XPathConstants.NODESET);
        RuleMatch ruleMatch = new RuleMatch(new AtdRule(), text.getOriginalTextPositionFor(fromPos), text.getOriginalTextPositionFor(toPos), description);
        for (int j = 0; j < suggestions.getLength(); j++) {
            Node option = suggestions.item(j);
            String optionStr = xPath.evaluate("option", option);
            ruleMatch.setSuggestedReplacement(optionStr);
        }
        matches.add(ruleMatch);
    }
    return matches;
}
Also used : XPath(javax.xml.xpath.XPath) RuleMatch(org.languagetool.rules.RuleMatch) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document)

Example 29 with XPath

use of javax.xml.xpath.XPath in project languagetool by languagetool-org.

the class PatternRuleXmlCreator method toXML.

/**
   * Return the given pattern rule as an indented XML string.
   * @since 2.3
   */
public final String toXML(PatternRuleId ruleId, Language language) {
    List<String> filenames = language.getRuleFileNames();
    XPath xpath = XPathFactory.newInstance().newXPath();
    for (String filename : filenames) {
        try (InputStream is = this.getClass().getResourceAsStream(filename)) {
            Document doc = getDocument(is);
            Node ruleNode = (Node) xpath.evaluate("/rules/category/rule[@id='" + ruleId.getId() + "']", doc, XPathConstants.NODE);
            if (ruleNode != null) {
                return nodeToString(ruleNode);
            }
            Node ruleNodeInGroup = (Node) xpath.evaluate("/rules/category/rulegroup/rule[@id='" + ruleId.getId() + "']", doc, XPathConstants.NODE);
            if (ruleNodeInGroup != null) {
                return nodeToString(ruleNodeInGroup);
            }
            if (ruleId.getSubId() != null) {
                NodeList ruleGroupNodes = (NodeList) xpath.evaluate("/rules/category/rulegroup[@id='" + ruleId.getId() + "']/rule", doc, XPathConstants.NODESET);
                if (ruleGroupNodes != null) {
                    for (int i = 0; i < ruleGroupNodes.getLength(); i++) {
                        if (Integer.toString(i + 1).equals(ruleId.getSubId())) {
                            return nodeToString(ruleGroupNodes.item(i));
                        }
                    }
                }
            } else {
                Node ruleGroupNode = (Node) xpath.evaluate("/rules/category/rulegroup[@id='" + ruleId.getId() + "']", doc, XPathConstants.NODE);
                if (ruleGroupNode != null) {
                    return nodeToString(ruleGroupNode);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("Could not turn rule '" + ruleId + "' for language " + language + " into a string", e);
        }
    }
    throw new RuntimeException("Could not find rule '" + ruleId + "' for language " + language + " in files: " + filenames);
}
Also used : XPath(javax.xml.xpath.XPath) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException)

Example 30 with XPath

use of javax.xml.xpath.XPath in project flyway by flyway.

the class GradleLargeTest method getPomVersion.

/**
     * Retrieves the version embedded in the project pom. Useful for running these tests in IntelliJ.
     *
     * @return The POM version.
     */
private String getPomVersion() {
    try {
        File pom = new File("pom.xml");
        if (!pom.exists()) {
            return "unknown";
        }
        XPath xPath = XPathFactory.newInstance().newXPath();
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(false);
        Document document = documentBuilderFactory.newDocumentBuilder().parse(pom);
        return xPath.evaluate("/project/version", document);
    } catch (Exception e) {
        throw new IllegalStateException("Unable to read POM version", e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Document(org.w3c.dom.Document) File(java.io.File)

Aggregations

XPath (javax.xml.xpath.XPath)197 Document (org.w3c.dom.Document)107 NodeList (org.w3c.dom.NodeList)99 XPathExpressionException (javax.xml.xpath.XPathExpressionException)70 Node (org.w3c.dom.Node)70 XPathExpression (javax.xml.xpath.XPathExpression)69 XPathFactory (javax.xml.xpath.XPathFactory)59 DocumentBuilder (javax.xml.parsers.DocumentBuilder)45 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)37 IOException (java.io.IOException)32 Element (org.w3c.dom.Element)32 Test (org.junit.Test)25 InputSource (org.xml.sax.InputSource)23 SAXException (org.xml.sax.SAXException)21 File (java.io.File)19 ArrayList (java.util.ArrayList)19 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)18 HashMap (java.util.HashMap)16 InputStream (java.io.InputStream)12 PBXNativeTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget)11