Description
Evaluates the specified XPath expression and returns the result as a Boolean.
License
Open Source License
Parameter
Parameter | Description |
---|
expression | The XPath expression to evaluate. |
node | The node to run the expression on. |
Exception
Parameter | Description |
---|
XPathExpressionException | If there was a problem processing the specified XPathexpression. |
Return
The Boolean result.
Declaration
public static Boolean asBoolean(String expression, Node node) throws XPathExpressionException
Method Source Code
//package com.java2s;
/*//from www .j a va 2 s . c o m
* Copyright 2010-2016 ksyun.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://ksyun.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Node;
public class Main {
/**
* Evaluates the specified XPath expression and returns the result as a
* Boolean.
* <p>
* This method can be expensive as a new xpath is instantiated per
* invocation. Consider passing in the xpath explicitly via {
* {@link #asDouble(String, Node, XPath)} instead. Note {@link XPath} is
* not thread-safe and not reentrant.
*
* @param expression
* The XPath expression to evaluate.
* @param node
* The node to run the expression on.
*
* @return The Boolean result.
*
* @throws XPathExpressionException
* If there was a problem processing the specified XPath
* expression.
*/
public static Boolean asBoolean(String expression, Node node) throws XPathExpressionException {
return asBoolean(expression, node, xpath());
}
/**
* Same as {@link #asBoolean(String, Node)} but allows an xpath to be passed
* in explicitly for reuse.
*/
public static Boolean asBoolean(String expression, Node node, XPath xpath) throws XPathExpressionException {
String booleanString = evaluateAsString(expression, node, xpath);
return (isEmptyString(booleanString)) ? null : Boolean.parseBoolean(booleanString);
}
/**
* Returns a new instance of XPath, which is not thread safe and not
* reentrant.
*/
public static XPath xpath() {
return XPathFactory.newInstance().newXPath();
}
/**
* Evaluates the specified expression on the specified node and returns the
* result as a String.
*
* @param expression
* The Xpath expression to evaluate.
* @param node
* The node on which to evaluate the expression.
*
* @return The result of evaluating the specified expression, or null if the
* evaluation didn't return any result.
*
* @throws XPathExpressionException
* If there are any problems evaluating the Xpath expression.
*/
private static String evaluateAsString(String expression, Node node, XPath xpath)
throws XPathExpressionException {
if (isEmpty(node))
return null;
if (!expression.equals(".")) {
/*
* If the expression being evaluated doesn't select a node, we want
* to return null to distinguish between cases where a node isn't
* present (which should be represented as null) and when a node is
* present, but empty (which should be represented as the empty
* string).
*
* We skip this test if the expression is "." since we've already
* checked that the node exists.
*/
if (asNode(expression, node, xpath) == null)
return null;
}
String s = xpath.evaluate(expression, node);
return s.trim();
}
/**
* Returns true if the specified string is null or empty.
*
* @param s
* The string to test.
* @return True if the specified string is null or empty.
*/
private static boolean isEmptyString(String s) {
return s == null || s.trim().length() == 0;
}
/**
* Returns true if the specified node is null or has no children.
*
* @param node
* The node to test.
*
* @return True if the specified node is null or has no children.
*/
public static boolean isEmpty(Node node) {
return (node == null);
}
/**
* Evaluates the specified XPath expression and returns the result as a
* Node.
*
* @param nodeName
* The XPath expression to evaluate.
* @param node
* The node to run the expression on.
*
* @return The Node result.
*
* @throws XPathExpressionException
* If there was a problem processing the specified XPath
* expression.
*/
public static Node asNode(String nodeName, Node node) throws XPathExpressionException {
return asNode(nodeName, node, xpath());
}
/**
* Same as {@link #asNode(String, Node)} but allows an xpath to be
* passed in explicitly for reuse.
*/
public static Node asNode(String nodeName, Node node, XPath xpath) throws XPathExpressionException {
if (node == null)
return null;
return (Node) xpath.evaluate(nodeName, node, XPathConstants.NODE);
}
}
Related
- asLong(String expression, Node node)
- compile(String expression)
- compile(String expression)
- compile(String path)