Search in sources :

Example 1 with XPathException

use of org.springframework.xml.xpath.XPathException in project spring-integration by spring-projects.

the class XPathUtils method evaluate.

/**
 * Utility method to evaluate an xpath on the provided object.
 * Delegates evaluation to an {@link XPathExpression}.
 * Note this method provides the {@code #xpath()} SpEL function.
 *
 * @param o         the xml Object for evaluaton.
 * @param xpath     an 'xpath' expression String.
 * @param resultArg an optional parameter to represent the result type of the xpath evaluation.
 *                  Only one argument is allowed, which can be an instance of {@link org.springframework.xml.xpath.NodeMapper} or
 *                  one of these String constants: "string", "boolean", "number", "node" or "node_list".
 * @param <T>       The required return type.
 * @return the result of the xpath expression evaluation.
 * @throws IllegalArgumentException - if the provided arguments aren't appropriate types or values;
 * @throws MessagingException - if the provided object can't be converted to a {@link Node};
 * @throws XPathException - if the xpath expression can't be evaluated.
 */
@SuppressWarnings({ "unchecked" })
public static <T> T evaluate(Object o, String xpath, Object... resultArg) {
    Object resultType = null;
    if (resultArg != null && resultArg.length > 0) {
        Assert.isTrue(resultArg.length == 1, "'resultArg' can contains only one element.");
        Assert.noNullElements(resultArg, "'resultArg' can't contains 'null' elements.");
        resultType = resultArg[0];
    }
    XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpath);
    Node node = converter.convertToNode(o);
    if (resultType == null) {
        return (T) expression.evaluateAsString(node);
    } else if (resultType instanceof NodeMapper<?>) {
        return (T) expression.evaluateAsObject(node, (NodeMapper<?>) resultType);
    } else if (resultType instanceof String && RESULT_TYPES.contains(resultType)) {
        String resType = (String) resultType;
        if (DOCUMENT_LIST.equals(resType)) {
            List<Node> nodeList = (List<Node>) XPathEvaluationType.NODE_LIST_RESULT.evaluateXPath(expression, node);
            try {
                DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
                List<Node> documents = new ArrayList<Node>(nodeList.size());
                for (Node n : nodeList) {
                    Document document = documentBuilder.newDocument();
                    document.appendChild(document.importNode(n, true));
                    documents.add(document);
                }
                return (T) documents;
            } catch (ParserConfigurationException e) {
                throw new XPathException("Unable to create 'documentBuilder'.", e);
            }
        } else {
            XPathEvaluationType evaluationType = XPathEvaluationType.valueOf(resType.toUpperCase() + "_RESULT");
            return (T) evaluationType.evaluateXPath(expression, node);
        }
    } else {
        throw new IllegalArgumentException("'resultArg[0]' can be an instance of 'NodeMapper<?>' " + "or one of supported String constants: " + RESULT_TYPES);
    }
}
Also used : XPathExpression(org.springframework.xml.xpath.XPathExpression) XPathException(org.springframework.xml.xpath.XPathException) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XPathException (org.springframework.xml.xpath.XPathException)1 XPathExpression (org.springframework.xml.xpath.XPathExpression)1 Document (org.w3c.dom.Document)1 Node (org.w3c.dom.Node)1