Search in sources :

Example 21 with XPathExpression

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

the class DefaultXmlSignature2Message method getOutputNodeViaXPath.

protected Node getOutputNodeViaXPath(Input input) throws Exception {
    //NOPMD
    checkSearchValueNotNull(input);
    checkSearchValueOfType(XPathFilterParameterSpec.class, input);
    XPathFilterParameterSpec xpathFilter = (XPathFilterParameterSpec) input.getOutputNodeSearch();
    XPathExpression expr = XmlSignatureHelper.getXPathExpression(xpathFilter);
    NodeList nodes = (NodeList) expr.evaluate(input.getMessageBodyDocument(), XPathConstants.NODESET);
    if (nodes == null || nodes.getLength() == 0) {
        throw new XmlSignatureException(String.format("Cannot extract root node for the output document from the XML signature document. No node found for XPATH %s as specified in the output node search.", xpathFilter.getXPath()));
    }
    if (nodes.getLength() > 1) {
        throw new XmlSignatureException(String.format("Cannot extract root node for the output document from the XML signature document. XPATH %s as specified in the output node search results into more than one child.", xpathFilter.getXPath()));
    }
    Node result = nodes.item(0);
    if (Node.ELEMENT_NODE == result.getNodeType() || Node.TEXT_NODE == result.getNodeType() || Node.DOCUMENT_NODE == result.getNodeType()) {
        return result;
    }
    throw new XmlSignatureException(String.format("Cannot extract root node for the output document from the XML signature document. " + "XPATH %s as specified in the output node search results into a node which has the wrong type.", xpathFilter.getXPath()));
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) XPathFilterParameterSpec(javax.xml.crypto.dsig.spec.XPathFilterParameterSpec)

Example 22 with XPathExpression

use of javax.xml.xpath.XPathExpression 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 23 with XPathExpression

use of javax.xml.xpath.XPathExpression in project lwjgl by LWJGL.

the class StandalonePublisher method xpath.

protected String xpath(Node node, String path) {
    try {
        XPathExpression expression = xpath.compile(path);
        NodeList nodes = (NodeList) expression.evaluate(node, XPathConstants.NODESET);
        String s = nodes.item(0).getNodeValue();
        s = s.replaceAll("^\\s+", "");
        s = s.replaceAll("\\s+$", "");
        return s;
    } catch (Exception ex) {
        System.err.println(ex);
        return null;
    }
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) NodeList(org.w3c.dom.NodeList) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 24 with XPathExpression

use of javax.xml.xpath.XPathExpression 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)

Example 25 with XPathExpression

use of javax.xml.xpath.XPathExpression in project azure-tools-for-java by Microsoft.

the class XMLHelper method main.

public static void main(String[] argv) {
    if (argv.length < 4) {
        System.out.println("[Failed] Please specify the arguments $XML_FILE $XPATH $NEW_VALUE $JOIN_OR_REPLACE.");
        System.exit(1);
    }
    String FILEPATH = argv[0];
    String XPATH = argv[1];
    String NEW_VALUE = argv[2];
    String CHANGETYPE = argv[3];
    boolean DISPLAY_LOG = false;
    if (argv.length > 4 && argv[4].toLowerCase().equals("true")) {
        DISPLAY_LOG = true;
    }
    try {
        System.out.println("Starting to modify XML " + FILEPATH);
        // Read the content from xml file
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(FILEPATH);
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        XPathExpression expression = xpath.compile(XPATH);
        Node targetNode = (Node) expression.evaluate(doc, XPathConstants.NODE);
        String oldValue = targetNode.getNodeValue();
        String newValue = "";
        if (CHANGETYPE.equals("JOIN")) {
            newValue = oldValue + NEW_VALUE;
        }
        if (DISPLAY_LOG) {
            System.out.println("The old value is " + oldValue);
            System.out.println("The new value is " + newValue);
        }
        targetNode.setNodeValue(newValue);
        // Write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(FILEPATH));
        transformer.transform(source, result);
        System.out.println("Modify XML Finished.");
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
    System.exit(0);
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) IOException(java.io.IOException) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File)

Aggregations

XPathExpression (javax.xml.xpath.XPathExpression)98 XPath (javax.xml.xpath.XPath)69 NodeList (org.w3c.dom.NodeList)56 Document (org.w3c.dom.Document)48 XPathExpressionException (javax.xml.xpath.XPathExpressionException)40 XPathFactory (javax.xml.xpath.XPathFactory)40 Node (org.w3c.dom.Node)38 DocumentBuilder (javax.xml.parsers.DocumentBuilder)24 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)19 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 Element (org.w3c.dom.Element)12 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 IOException (java.io.IOException)11 Path (java.nio.file.Path)11 PBXFileReference (com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)10 InputSource (org.xml.sax.InputSource)9