Search in sources :

Example 16 with XPathFactory

use of javax.xml.xpath.XPathFactory 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 17 with XPathFactory

use of javax.xml.xpath.XPathFactory in project gocd by gocd.

the class XpathUtils method nodeExists.

public static boolean nodeExists(InputSource inputSource, String xpath) throws XPathExpressionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPathExpression expression = factory.newXPath().compile(xpath);
    Boolean b = (Boolean) expression.evaluate(inputSource, XPathConstants.BOOLEAN);
    return b != null && b;
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory)

Example 18 with XPathFactory

use of javax.xml.xpath.XPathFactory in project jdbc-shards by wplatform.

the class XPathParser method commonConstructor.

private void commonConstructor(boolean validation, EntityResolver entityResolver) {
    this.validation = validation;
    this.entityResolver = entityResolver;
    XPathFactory factory = XPathFactory.newInstance();
    this.xpath = factory.newXPath();
}
Also used : XPathFactory(javax.xml.xpath.XPathFactory)

Example 19 with XPathFactory

use of javax.xml.xpath.XPathFactory in project jdk8u_jdk by JetBrains.

the class XPathExFuncTest method evaluate.

void evaluate(boolean enableExt) throws XPathFactoryConfigurationException, XPathExpressionException {
    Document document = getDocument();
    XPathFactory xPathFactory = XPathFactory.newInstance();
    /**
         * Use of the extension function 'http://exslt.org/strings:tokenize' is
         * not allowed when the secure processing feature is set to true.
         * Attempt to use the new property to enable extension function
         */
    if (enableExt) {
        boolean isExtensionSupported = enableExtensionFunction(xPathFactory);
    }
    xPathFactory.setXPathFunctionResolver(new MyXPathFunctionResolver());
    if (System.getSecurityManager() == null) {
        xPathFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);
    }
    XPath xPath = xPathFactory.newXPath();
    xPath.setNamespaceContext(new MyNamespaceContext());
    String xPathResult = xPath.evaluate(XPATH_EXPRESSION, document);
    System.out.println("XPath result (enableExtensionFunction == " + enableExt + ") = \"" + xPathResult + "\"");
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) Document(org.w3c.dom.Document)

Example 20 with XPathFactory

use of javax.xml.xpath.XPathFactory in project c4sg-services by Code4SocialGood.

the class CoordinatesUtil method getCoordinates.

/**@param{Object} Address- The physical address of a location
	  * This method returns Geocode coordinates of the Address object.Geocoding is the process of converting addresses (like "1600 Amphitheatre Parkway,
	  * Mountain View, CA") into geographic coordinates (like latitude 37.423021 and longitude -122.083739).Please give all the physical Address values 
	  * for a precise coordinate. setting none of the values will give a null value for the Latitude and Longitude. 
	  */
public static GeoCode getCoordinates(Address address) throws Exception {
    GeoCode geo = new GeoCode();
    String physicalAddress = (address.getAddress1() == null ? "" : address.getAddress1() + ",") + (address.getAddress2() == null ? "" : address.getAddress2() + ",") + (address.getCityName() == null ? "" : address.getCityName() + ",") + (address.getState() == null ? "" : address.getState() + "-") + (address.getZip() == null ? "" : address.getZip() + ",") + (address.getCountry() == null ? "" : address.getCountry());
    String api = GMAPADDRESS + URLEncoder.encode(physicalAddress, "UTF-8") + SENSOR;
    URL url = new URL(api);
    HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
    httpConnection.connect();
    int responseCode = httpConnection.getResponseCode();
    if (responseCode == 200) {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document document = builder.parse(httpConnection.getInputStream());
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        XPathExpression expr = xpath.compile(STATUS);
        String status = (String) expr.evaluate(document, XPathConstants.STRING);
        if (status.equals("OK")) {
            expr = xpath.compile(LATITUDE);
            String latitude = (String) expr.evaluate(document, XPathConstants.STRING);
            expr = xpath.compile(LONGITUDE);
            String longitude = (String) expr.evaluate(document, XPathConstants.STRING);
            geo.setLatitude(latitude);
            geo.setLongitude(longitude);
        }
    } else {
        throw new Exception("Fail to Convert to Geocode");
    }
    return geo;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) HttpURLConnection(java.net.HttpURLConnection) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Document(org.w3c.dom.Document) URL(java.net.URL)

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