Java XPath Evaluate evalXPath(String expression, Object item, Class type)

Here you can find the source of evalXPath(String expression, Object item, Class type)

Description

Evaluates an XPath returning null if not found.

License

Open Source License

Parameter

Parameter Description
expression The XPath expression to evaluate.
item The Node or other item to evaluate the XPath on.
type The type to return, this must be one of the following: String , CharSequence , Boolean , Node , NodeList , Double , or Number .

Exception

Parameter Description
AssertionError If the nested call to <tt>XPath.newInstance(path)</tt>throws an XPathExpressionException.

Declaration

public static <T> T evalXPath(String expression, Object item, Class<T> type) 

Method Source Code

//package com.java2s;
/**//from  ww  w. j  ava 2 s  .c o  m
 * This file is protected by Copyright. Please refer to the COPYRIGHT file
 * distributed with this source distribution.
 *
 * This file is part of REDHAWK.
 *
 * REDHAWK is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * REDHAWK is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/.
 */

import javax.xml.namespace.QName;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    private static XPath xPath = null;

    /** Evaluates an XPath. */
    private static synchronized Object evalXPath(String expression, Object item, QName type) {
        if (xPath == null)
            xPath = XPathFactory.newInstance().newXPath();

        try {
            XPathExpression exp = xPath.compile(expression);
            Object node = exp.evaluate(item, XPathConstants.NODE);

            return (node == null) ? null : exp.evaluate(item, type);
        } catch (XPathExpressionException e) {
            throw new AssertionError("Error initializing XPath instance for '" + expression + "': " + e);
        }
    }

    /** Evaluates an XPath returning null if not found. <br>
     *  <br>
     *  This is intended for use with pre-defined XPaths stored as constants,
     *  where runtime exceptions should not be possible.
     *  @param expression The XPath expression to evaluate.
     *  @param item       The {@link Node} or other item to evaluate the XPath on.
     *  @param type       The type to return, this must be one of the following:
     *                    {@link String}, {@link CharSequence}, {@link Boolean},
     *                    {@link Node}, {@link NodeList}, {@link Double}, or
     *                    {@link Number}.
     *  @throws AssertionError If the nested call to <tt>XPath.newInstance(path)</tt>
     *                         throws an {@link XPathExpressionException}.
     */
    public static <T> T evalXPath(String expression, Object item, Class<T> type) {
        Object val;

        if (type == String.class)
            val = evalXPath(expression, item, XPathConstants.STRING);
        else if (type == CharSequence.class)
            val = evalXPath(expression, item, XPathConstants.STRING);
        else if (type == Boolean.class)
            val = evalXPath(expression, item, XPathConstants.BOOLEAN);
        else if (type == Boolean.TYPE)
            val = evalXPath(expression, item, XPathConstants.BOOLEAN);
        else if (type == Node.class)
            val = evalXPath(expression, item, XPathConstants.NODE);
        else if (type == NodeList.class)
            val = evalXPath(expression, item, XPathConstants.NODESET);
        else if (type == Double.class)
            val = evalXPath(expression, item, XPathConstants.NUMBER);
        else if (type == Double.TYPE)
            val = evalXPath(expression, item, XPathConstants.NUMBER);
        else if (type == Number.class)
            val = evalXPath(expression, item, XPathConstants.NUMBER);
        else
            throw new IllegalArgumentException("Invalid type given " + type);

        return type.cast(val);
    }

    /** Evaluates an XPath returning a default value if not found. <br>
     *  <br>
     *  This is intended for use with pre-defined XPaths stored as constants,
     *  where runtime exceptions should not be possible.
     *  @param expression The XPath expression to evaluate.
     *  @param item       The {@link Node} or other item to evaluate the XPath on.
     *  @param def        The default value to return if the evaluation of the
     *                    expression returns null. The default value must be an
     *                    instance of one of the following: {@link String},
     *                    {@link CharSequence}, {@link Boolean}, {@link Node},
     *                    {@link NodeList}, {@link Double}, or {@link Number}.
     *  @throws NullPointerException If <tt>def</tt> is null.
     *  @throws AssertionError If the nested call to <tt>XPath.newInstance(path)</tt>
     *                         throws an {@link XPathExpressionException}.
     */
    @SuppressWarnings("unchecked")
    public static <T> T evalXPath(String expression, Object item, T def) {
        T val = (T) evalXPath(expression, item, def.getClass());
        return (val == null) ? def : val;
    }
}

Related

  1. evaluateXPathExpressionAndReturnNode(String expression, Node node)
  2. evaluateXPathExpressionAndReturnNodeList(String expression, Node node)
  3. evaluateXPathQuery(Document doc, NamespaceContext context, String xPathQuery)
  4. evalXPath(Node d, String expr, QName returnType)
  5. evalXPath(Node node, String xPath)
  6. evalXPath(String path, Document doc)
  7. evalXpath(String xpath, Object item)
  8. evalXPathAsString(Object item, String xpath, XPathFactory factory)
  9. evalXPathAsStringList(Object item, String xpath, XPathFactory factory, boolean includeDuplicates)