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:nl.nn.adapterframework.util.XmlUtils.java

public static Double evaluateXPathNumber(String input, String xpathExpr)
        throws DomBuilderException, XPathExpressionException {
    String msg = XmlUtils.removeNamespaces(input);

    Document doc = buildDomDocument(msg, true, true);
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression xPathExpression = xPath.compile(xpathExpr);
    Object result = xPathExpression.evaluate(doc, XPathConstants.NUMBER);
    return (Double) result;
}

From source file:org.apache.camel.builder.xml.XPathBuilder.java

/**
 * Sets the expression result type to boolean
 *
 * @return the current builder//from  w w w.j av a 2  s.  c  o m
 */
public XPathBuilder numberResult() {
    resultQName = XPathConstants.NUMBER;
    return this;
}

From source file:org.apache.ode.bpel.elang.xpath20.runtime.XPath20ExpressionRuntime.java

public Number evaluateAsNumber(OExpression cexp, EvaluationContext ctx)
        throws FaultException, EvaluationException {
    return (Number) evaluate(cexp, ctx, XPathConstants.NUMBER);
}

From source file:org.apache.ode.bpel.elang.xquery10.runtime.XQuery10ExpressionRuntime.java

/**
 * Evaluate expression and return a number
 *
 * @param cexp cexp// w  w w  .j  av a2s  .c  o m
 * @param ctx ctx
 *
 * @return type
 *
 * @throws FaultException FaultException
 * @throws EvaluationException EvaluationException
 */
public Number evaluateAsNumber(OExpression cexp, EvaluationContext ctx)
        throws FaultException, EvaluationException {
    return (Number) evaluate(cexp, ctx, XPathConstants.NUMBER);
}

From source file:org.apache.ode.bpel.elang.xquery10.runtime.XQuery10ExpressionRuntime.java

/**
 * Cast XQuery sequence into an opaque list
 *
 * @param type type//  w w w . j a v a  2 s.  c  om
 * @param result result
 *
 * @return value
 *
 * @throws XQException XQException
 */
private Object getResultValue(QName type, XQResultSequence result) throws XQException {
    Document document = DOMUtils.newDocument();
    Object resultValue = null;
    if (XPathConstants.NODESET.equals(type)) {
        List list = new ArrayList();

        while (result.next()) {
            Object itemValue = getItemValue(result.getItem());
            if (itemValue instanceof Document) {
                itemValue = DOMUtils.cloneNode(document, ((Document) itemValue).getDocumentElement());
            } else if (itemValue instanceof Node) {
                itemValue = DOMUtils.cloneNode(document, (Node) itemValue);
            }

            if (itemValue != null) {
                list.add(itemValue);
            }
        }

        resultValue = list;
    } else if (XPathConstants.NODE.equals(type)) {
        XQItem item = null;
        if (result.count() > 0) {
            result.first();
            if (result.isOnItem()) {
                item = result.getItem();
            }
        }
        if (item != null) {
            resultValue = getItemValue(item);
            if (resultValue instanceof Node) {
                resultValue = DOMUtils.cloneNode(document, (Node) resultValue);
            }
        }
    } else if (XPathConstants.STRING.equals(type)) {
        resultValue = result.getSequenceAsString(new Properties());
    } else if (XPathConstants.NUMBER.equals(type)) {
        resultValue = result.getSequenceAsString(new Properties());
        resultValue = Integer.parseInt((String) resultValue);
    } else if (XPathConstants.BOOLEAN.equals(type)) {
        resultValue = result.getSequenceAsString(new Properties());
        resultValue = Boolean.parseBoolean((String) resultValue);
    }
    return resultValue;
}

From source file:org.apache.ode.bpel.rtrep.v1.xpath10.jaxp.JaxpXPath10ExpressionRuntime.java

public Number evaluateAsNumber(OExpression cexp, EvaluationContext ctx) throws FaultException {
    return (Number) evaluate(cexp, ctx, XPathConstants.NUMBER);
}

From source file:org.apache.ode.bpel.rtrep.v2.xquery10.runtime.XQuery10ExpressionRuntime.java

/**
 * Evaluate expression and return a number
 *
 * @param cexp cexp //from  w  w  w.ja  v a  2s .  c o  m
 * @param ctx ctx 
 *
 * @return type
 *
 * @throws FaultException FaultException 
 */
public Number evaluateAsNumber(OExpression cexp, EvaluationContext ctx) throws FaultException {
    return (Number) evaluate(cexp, ctx, XPathConstants.NUMBER);
}

From source file:org.apache.ode.bpel.rtrep.v2.xquery10.runtime.XQuery10ExpressionRuntime.java

/**
 * Cast XQuery sequence into an opaque list 
 *
 * @param type type // ww w  .j a v  a 2 s  . c  o  m
 * @param result result 
 *
 * @return value
 *
 * @throws XQException XQException 
 */
private Object getResultValue(QName type, XQResultSequence result) throws XQException {
    Document document = DOMUtils.newDocument();
    Object resultValue = null;
    if (XPathConstants.NODESET.equals(type)) {
        List list = new ArrayList();

        while (result.next()) {
            Object itemValue = getItemValue(result.getItem());
            if (itemValue instanceof Node) {
                itemValue = DOMUtils.cloneNode(document, (Node) itemValue);
            }

            if (itemValue != null) {
                list.add(itemValue);
            }
        }

        resultValue = list;
    } else if (XPathConstants.NODE.equals(type)) {
        XQItem item = null;
        if (result.count() > 0) {
            result.first();
            if (result.isOnItem()) {
                item = result.getItem();
            }
        }
        if (item != null) {
            resultValue = getItemValue(item);
            if (resultValue instanceof Node) {
                resultValue = DOMUtils.cloneNode(document, (Node) resultValue);
            }
        }
    } else if (XPathConstants.STRING.equals(type)) {
        resultValue = result.getSequenceAsString(new Properties());
    } else if (XPathConstants.NUMBER.equals(type)) {
        resultValue = result.getSequenceAsString(new Properties());
        resultValue = Integer.parseInt((String) resultValue);
    } else if (XPathConstants.BOOLEAN.equals(type)) {
        resultValue = result.getSequenceAsString(new Properties());
        resultValue = Boolean.parseBoolean((String) resultValue);
    }
    return resultValue;
}

From source file:org.apereo.portal.portlets.marketplace.PortletMarketplaceController.java

private List<PortletTab> getPortletTabInfo(DistributedUserLayout layout, String fname) {
    final String XPATH_TAB = "/layout/folder/folder[@hidden = 'false' and @type = 'regular']";
    final String XPATH_COUNT_COLUMNS = "count(./folder[@hidden = \"false\"])";
    final String XPATH_COUNT_NON_EDITABLE_COLUMNS = "count(./folder[@hidden = \"false\" and @*[local-name() = \"editAllowed\"] = \"false\"])";
    final String XPATH_GET_TAB_PORTLET_FMT = ".//channel[@hidden = \"false\" and @fname = \"%s\"]";

    Document doc = layout.getLayout();

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();

    try {/*  www.j  ava2  s . co  m*/
        XPathExpression tabExpr = xpath.compile(XPATH_TAB);
        NodeList list = (NodeList) tabExpr.evaluate(doc, XPathConstants.NODESET);

        // Count columns and non-editable columns...
        XPathExpression columnCountExpr = xpath.compile(XPATH_COUNT_COLUMNS);
        XPathExpression nonEditableCountExpr = xpath.compile(XPATH_COUNT_NON_EDITABLE_COLUMNS);

        // get the list of tabs...
        String xpathStr = format(XPATH_GET_TAB_PORTLET_FMT, fname);
        XPathExpression portletExpr = xpath.compile(xpathStr);

        List<PortletTab> tabs = new ArrayList<>();
        for (int i = 0; i < list.getLength(); i++) {
            Node tab = list.item(i);
            String tabName = ((Element) tab).getAttribute("name");
            String tabId = ((Element) tab).getAttribute("ID");

            // check if tab is editable...
            Number columns = (Number) columnCountExpr.evaluate(tab, XPathConstants.NUMBER);
            Number nonEditColumns = (Number) nonEditableCountExpr.evaluate(tab, XPathConstants.NUMBER);
            // tab is not editable...  skip it...
            if (columns.intValue() > 0 && columns.intValue() == nonEditColumns.intValue()) {
                continue;
            }

            // get all instances of this portlet on this tab...
            List<String> layoutIds = new ArrayList<>();
            NodeList fnameListPerTab = (NodeList) portletExpr.evaluate(tab, XPathConstants.NODESET);
            for (int j = 0; j < fnameListPerTab.getLength(); j++) {
                Node channel = fnameListPerTab.item(j);

                String layoutId = ((Element) channel).getAttribute("ID");
                layoutIds.add(layoutId);
            }

            PortletTab tabInfo = new PortletTab(tabName, tabId, layoutIds);
            tabs.add(tabInfo);
        }

        return tabs;

    } catch (XPathExpressionException e) {
        logger.error("Error evaluating xpath", e);
    }

    return null;
}

From source file:org.geoserver.kml.KMLReflectorTest.java

@Test
public void testExternalImageSize() throws Exception {
    GetMapRequest req = createGetMapRequest(MockData.STREAMS);
    req.setWidth(256);/*w ww.  j  av  a2 s.co m*/
    req.setHeight(256);

    WMSMapContent mapContent = new WMSMapContent(req);
    mapContent.addLayer(createMapLayer(MockData.STREAMS, "big-local-image"));

    mapContent.getViewport().setBounds(new ReferencedEnvelope(-180, 0, -90, 90, DefaultGeographicCRS.WGS84));
    mapContent.setMapHeight(256);
    mapContent.setMapWidth(256);

    KMLMapOutputFormat of = new KMLMapOutputFormat(getWMS());
    KMLMap map = of.produceMap(mapContent);

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    new KMLEncoder().encode(map.getKml(), bout);

    Document document = dom(new ByteArrayInputStream(bout.toByteArray()));

    assertEquals("kml", document.getDocumentElement().getNodeName());
    assertEquals(1, document.getElementsByTagName("Style").getLength());

    XMLAssert.assertXpathExists("//kml:IconStyle/kml:scale", document);

    XPath xPath = XPathFactory.newInstance().newXPath();
    initXPath(xPath);

    Double scale = (Double) xPath.evaluate("//kml:IconStyle/kml:scale", document.getDocumentElement(),
            XPathConstants.NUMBER);
    assertEquals(42d / 16d, scale, 0.01);
}