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()));
}
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;
}
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;
}
}
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;
}
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);
}
Aggregations