Example usage for javax.xml.xpath XPathConstants NUMBER

List of usage examples for javax.xml.xpath XPathConstants NUMBER

Introduction

In this page you can find the example usage for javax.xml.xpath XPathConstants NUMBER.

Prototype

QName NUMBER

To view the source code for javax.xml.xpath XPathConstants NUMBER.

Click Source Link

Document

The XPath 1.0 number data type.

Maps to Java Double .

Usage

From source file:Main.java

public static double getDouble(Object node, XPathExpression expression) throws XPathExpressionException {
    return ((Double) expression.evaluate(node, XPathConstants.NUMBER)).doubleValue();
}

From source file:Main.java

public static int evalXPathAsInt(Object item, String xpath, XPathFactory factory)
        throws XPathExpressionException {
    XPath path = factory.newXPath();
    XPathExpression expr = path.compile(xpath);
    Number result = (Number) expr.evaluate(item, XPathConstants.NUMBER);
    return result == null ? 0 : result.intValue();
}

From source file:Main.java

public static Double getNodesCount(String expression, Document document) {
    XPath xpath = xPathFactory.newXPath();
    try {/*from   w  w w .  j  a v a2 s. c o m*/
        Double count = (Double) xpath.evaluate("count(" + expression + ")", document, XPathConstants.NUMBER);
        return count;
    } catch (XPathExpressionException xpe) {
        throw new IllegalStateException(xpe);
    }
}

From source file:Main.java

public static Double xpathNumber(String expr, Document doc) {
    try {//from   w  w w .j  av  a2 s  .  c o m
        return (Double) xpath.evaluate(expr, doc,

                XPathConstants.NUMBER);
    } catch (Exception e) {
        throw new RuntimeException("Cannot evaluate XPath:", e);
    }
}

From source file:Main.java

/**
 * <p>//  www.ja v a  2  s  . c o  m
 * Evaluates the given XPath expression in the context of the given item, with
 * the expectation of a number (Java <code>double</code>) result.
 * </p>
 * 
 * @param xpe
 *          An XPath expression.
 * @param item
 *          A context item.
 * @return The result of the evaluation as a {@link Double} instance.
 * @throws XPathExpressionException
 *           if the evaluation fails
 * @since 1.67.5
 * @see XPathConstants#NUMBER
 */
public static Double evaluateNumber(XPathExpression xpe, Object item) throws XPathExpressionException {
    return (Double) xpe.evaluate(item, XPathConstants.NUMBER);
}

From source file:Main.java

public static Double getNumber(String xPathExpression, Node node) throws XPathExpressionException {
    return (Double) xPath.evaluate(xPathExpression, node, XPathConstants.NUMBER);
}

From source file:com.espertech.esper.regression.event.TestSchemaXMLEventReplace.java

public void testSchemaReplace() throws Exception {
    ConfigurationEventTypeXMLDOM eventTypeMeta = new ConfigurationEventTypeXMLDOM();
    eventTypeMeta.setRootElementName("simpleEvent");
    String schemaUri = TestSchemaXMLEventReplace.class.getClassLoader().getResource(CLASSLOADER_SCHEMA_URI)
            .toString();/*from www.j a  v a  2s.  c  o  m*/
    eventTypeMeta.setSchemaResource(schemaUri);
    eventTypeMeta.addNamespacePrefix("ss", "samples:schemas:simpleSchema");
    eventTypeMeta.addXPathProperty("customProp", "count(/ss:simpleEvent/ss:nested3/ss:nested4)",
            XPathConstants.NUMBER);

    Configuration configuration = SupportConfigFactory.getConfiguration();
    configuration.addEventType("TestXMLSchemaType", eventTypeMeta);

    epService = EPServiceProviderManager.getProvider("TestSchemaXML", configuration);
    epService.initialize();

    String stmtSelectWild = "select * from TestXMLSchemaType";
    EPStatement wildStmt = epService.getEPAdministrator().createEPL(stmtSelectWild);
    EventType type = wildStmt.getEventType();
    EventTypeAssertionUtil.assertConsistency(type);

    EPAssertionUtil.assertEqualsAnyOrder(new Object[] {
            new EventPropertyDescriptor("nested1", Node.class, null, false, false, false, false, true),
            new EventPropertyDescriptor("prop4", String.class, null, false, false, false, false, false),
            new EventPropertyDescriptor("nested3", Node.class, null, false, false, false, false, true),
            new EventPropertyDescriptor("customProp", Double.class, null, false, false, false, false, false), },
            type.getPropertyDescriptors());

    // update type and replace
    schemaUri = TestSchemaXMLEventReplace.class.getClassLoader().getResource(CLASSLOADER_SCHEMA_VERSION2_URI)
            .toString();
    eventTypeMeta.setSchemaResource(schemaUri);
    eventTypeMeta.addXPathProperty("countProp", "count(/ss:simpleEvent/ss:nested3/ss:nested4)",
            XPathConstants.NUMBER);
    epService.getEPAdministrator().getConfiguration().replaceXMLEventType("TestXMLSchemaType", eventTypeMeta);

    wildStmt = epService.getEPAdministrator().createEPL(stmtSelectWild);
    type = wildStmt.getEventType();
    EventTypeAssertionUtil.assertConsistency(type);

    EPAssertionUtil.assertEqualsAnyOrder(new Object[] {
            new EventPropertyDescriptor("nested1", Node.class, null, false, false, false, false, true),
            new EventPropertyDescriptor("prop4", String.class, null, false, false, false, false, false),
            new EventPropertyDescriptor("prop5", String.class, null, false, false, false, false, false),
            new EventPropertyDescriptor("nested3", Node.class, null, false, false, false, false, true),
            new EventPropertyDescriptor("customProp", Double.class, null, false, false, false, false, false),
            new EventPropertyDescriptor("countProp", Double.class, null, false, false, false, false, false), },
            type.getPropertyDescriptors());
}

From source file:Main.java

/** Evaluates an XPath returning null if not found. <br>
 *  <br>//from   www. j av  a 2 s  . c om
 *  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);
}

From source file:edu.sabanciuniv.sentilab.sare.controllers.opinion.OpinionDocumentFactory.java

private OpinionDocument create(OpinionDocument document, OpinionCorpus corpus, Node node)
        throws XPathException {

    try {/*from   w ww.jav  a2 s  .  c  om*/
        Validate.notNull(node, CannedMessages.NULL_ARGUMENT, "node");
    } catch (NullPointerException e) {
        throw new IllegalFactoryOptionsException(e);
    }

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    Double polarity = (Double) xpath.compile("./@polarity").evaluate(node, XPathConstants.NUMBER);

    return this.create(document, corpus, node.getTextContent().trim(), polarity);
}

From source file:com.espertech.esperio.representation.axiom.AxiomXPathPropertyGetter.java

public Object get(EventBean eventBean) throws PropertyAccessException {
    Object und = eventBean.getUnderlying();
    if (und == null) {
        throw new PropertyAccessException(
                "Unexpected null underlying event encountered, expecting org.w3c.dom.Node instance as underlying");
    }//from  ww w.j  a v a 2  s.c  om
    if (!(und instanceof OMNode)) {
        throw new PropertyAccessException("Unexpected underlying event of type '" + und.getClass()
                + "' encountered, expecting org.w3c.dom.Node as underlying");
    }
    try {
        // if there is no parser, return xpath expression type
        if (optionalCastToType == null) {
            if (resultType.equals(XPathConstants.BOOLEAN)) {
                return expression.booleanValueOf(und);
            } else if (resultType.equals(XPathConstants.NUMBER)) {
                Number n = expression.numberValueOf(und);
                return n.doubleValue();
            } else {
                String result = expression.stringValueOf(und);
                return result;
            }
        }

        // obtain result as string and parse
        String result = expression.stringValueOf(und);
        if (result == null) {
            return null;
        }

        try {
            return simpleTypeParser.parse(result.toString());
        } catch (RuntimeException ex) {
            log.warn("Error parsing XPath property named '" + property + "' expression result '" + result
                    + " as type " + optionalCastToType.getName());
            return null;
        }
    } catch (JaxenException e) {
        throw new PropertyAccessException("Error getting property '" + property + "' : " + e.getMessage(), e);
    }
}