Search in sources :

Example 61 with XPath

use of javax.xml.xpath.XPath 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("target/dependencies-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 62 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 63 with XPath

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

the class EipDocumentationEnricherMojo method runPlugin.

private void runPlugin() throws Exception {
    Document document = XmlHelper.buildNamespaceAwareDocument(inputCamelSchemaFile);
    XPath xPath = XmlHelper.buildXPath(new CamelSpringNamespace());
    DomFinder domFinder = new DomFinder(document, xPath);
    DocumentationEnricher documentationEnricher = new DocumentationEnricher(document);
    // include schema files from camel-core, camel-corem-xml and from camel-spring
    File rootDir = new File(camelCoreDir, pathToModelDir);
    Map<String, File> jsonFiles = PackageHelper.findJsonFiles(rootDir);
    File rootDir2 = new File(camelCoreXmlDir, pathToCoreXmlModelDir);
    Map<String, File> jsonFiles2 = PackageHelper.findJsonFiles(rootDir2);
    File rootDir3 = new File(camelSpringDir, pathToSpringModelDir);
    Map<String, File> jsonFiles3 = PackageHelper.findJsonFiles(rootDir3);
    // merge the json files together
    jsonFiles.putAll(jsonFiles2);
    jsonFiles.putAll(jsonFiles3);
    NodeList elementsAndTypes = domFinder.findElementsAndTypes();
    documentationEnricher.enrichTopLevelElementsDocumentation(elementsAndTypes, jsonFiles);
    Map<String, String> typeToNameMap = buildTypeToNameMap(elementsAndTypes);
    Set<String> injectedTypes = new LinkedHashSet<String>();
    getLog().info("Found " + typeToNameMap.size() + " models to use when enriching the XSD schema");
    for (Map.Entry<String, String> entry : typeToNameMap.entrySet()) {
        String elementType = entry.getKey();
        String elementName = entry.getValue();
        if (jsonFileExistsForElement(jsonFiles, elementName)) {
            getLog().debug("Enriching " + elementName);
            File file = jsonFiles.get(elementName);
            injectAttributesDocumentation(domFinder, documentationEnricher, file, elementType, injectedTypes);
        }
    }
    saveToFile(document, outputCamelSchemaFile, XmlHelper.buildTransformer());
}
Also used : XPath(javax.xml.xpath.XPath) LinkedHashSet(java.util.LinkedHashSet) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 64 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 65 with XPath

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

the class YandexTranslate method translate.

@Override
protected String translate(Language sLang, Language tLang, String text) throws Exception {
    String mvYandexKey = getCredential(PROPERTY_API_KEY);
    if (mvYandexKey.isEmpty()) {
        return "";
    }
    String lvSourceLang = sLang.getLanguageCode().substring(0, 2).toLowerCase(Locale.ENGLISH);
    String lvTargetLang = tLang.getLanguageCode().substring(0, 2).toLowerCase(Locale.ENGLISH);
    // U+2026 HORIZONTAL ELLIPSIS
    String lvShorText = text.length() > 10000 ? text.substring(0, 9999) + "\u2026" : text;
    String prev = getFromCache(sLang, tLang, lvShorText);
    if (prev != null) {
        return prev;
    }
    // ----------------------------------------------------------------------
    Map<String, String> p = new TreeMap<String, String>();
    p.put("key", mvYandexKey);
    p.put("lang", lvSourceLang + '-' + lvTargetLang);
    p.put("text", lvShorText);
    // WikiGet.post(GT_URL, p);
    YTPPostResponse response = requestTranslate(p);
    XPathFactory xPathFactory = XPathFactory.newInstance();
    switch(response.code) {
        case ERR_OK:
            break;
        case ERR_KEY_INVALID:
            return response.code + ": " + OStrings.getString("MT_ENGINE_YANDEX_INVALID_KEY");
        case ERR_KEY_BLOCKED:
            return response.code + ": " + OStrings.getString("MT_ENGINE_YANDEX_API_BLOCKED");
        case ERR_DAILY_REQ_LIMIT_EXCEEDED:
            return response.code + ": " + OStrings.getString("MT_ENGINE_YANDEX_DAILY_LIMIT_DETECT");
        case ERR_DAILY_CHAR_LIMIT_EXCEEDED:
            return response.code + ": " + OStrings.getString("MT_ENGINE_YANDEX_DAILY_LIMIT_VOLUME");
        case ERR_TEXT_TOO_LONG:
            return response.code + ": " + OStrings.getString("MT_ENGINE_YANDEX_MAZ_SIZE");
        case ERR_UNPROCESSABLE_TEXT:
            return response.code + ": " + OStrings.getString("MT_ENGINE_YANDEX_TRANSLATION_NOT_POSSIBLE");
        case ERR_LANG_NOT_SUPPORTED:
            return response.code + ": " + OStrings.getString("MT_ENGINE_YANDEX_DIRECTION_NOT_SUPPORTED");
        default:
            break;
    }
    XPath xPathText = xPathFactory.newXPath();
    String pathText = "/Translation[@code='200']/text";
    String result = xPathText.evaluate(pathText, new InputSource(new StringReader(response.response)));
    putToCache(sLang, tLang, lvShorText, result);
    return result;
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) TreeMap(java.util.TreeMap)

Aggregations

XPath (javax.xml.xpath.XPath)526 Document (org.w3c.dom.Document)254 NodeList (org.w3c.dom.NodeList)230 XPathFactory (javax.xml.xpath.XPathFactory)215 Node (org.w3c.dom.Node)171 XPathExpressionException (javax.xml.xpath.XPathExpressionException)159 XPathExpression (javax.xml.xpath.XPathExpression)142 DocumentBuilder (javax.xml.parsers.DocumentBuilder)125 Element (org.w3c.dom.Element)118 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)97 IOException (java.io.IOException)89 Test (org.junit.Test)80 InputSource (org.xml.sax.InputSource)59 File (java.io.File)57 SAXException (org.xml.sax.SAXException)57 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)51 ByteArrayInputStream (java.io.ByteArrayInputStream)44 ArrayList (java.util.ArrayList)44 InputStream (java.io.InputStream)39 DSNamespaceContext (org.apache.xml.security.test.dom.DSNamespaceContext)37