Example usage for org.apache.commons.jxpath Pointer getValue

List of usage examples for org.apache.commons.jxpath Pointer getValue

Introduction

In this page you can find the example usage for org.apache.commons.jxpath Pointer getValue.

Prototype

Object getValue();

Source Link

Document

Returns the value of the object, property or collection element this pointer represents.

Usage

From source file:net.sf.ahtutils.report.jxpath.TestDataTypesJXpath.java

@Test
public void test() {
    TestDataStructure structure = new TestDataStructure();
    //structure.setIntegerValue(1);
    //structure.setLongValue(2);

    JXPathContext context = JXPathContext.newContext(structure);
    String queryInteger = "@integerValue";
    String queryLong = "@longValue";

    Iterator iteratorLong = context.iteratePointers(queryLong);
    while (iteratorLong.hasNext()) {
        Pointer pointerToItem = (Pointer) iteratorLong.next();
        System.out.println("Got pointer: " + pointerToItem.getValue().getClass() + " with value = "
                + pointerToItem.getValue().toString());
    }/*from   w  ww .jav  a 2s. com*/

    Iterator iteratorInteger = context.iteratePointers(queryInteger);
    while (iteratorInteger.hasNext()) {
        Pointer pointerToItem = (Pointer) iteratorInteger.next();
        System.out.println("Got pointer: " + pointerToItem.getValue().getClass() + " with value = "
                + pointerToItem.getValue().toString());
    }

}

From source file:blueprint.sdk.util.config.Config.java

/**
 * Get value from given XPath//  w w  w  . j a v a  2s.  c  o m
 *
 * @param xpath XPath to evaluate
 * @return values as String[]
 */
public String[] getStringArray(String xpath) {
    List<String> result = new ArrayList<>(10);

    Iterator<Pointer> nodeIter = JXPathHelper.evaluateIteratorPointers(xpath, context);
    while (nodeIter.hasNext()) {
        Pointer nodePtr = nodeIter.next();

        String value = (String) nodePtr.getValue();
        value = resolveProperty(value);
        result.add(value);
    }

    if (result.size() == 0) {
        warnEmptyValue(xpath, null);
    }

    return result.toArray(new String[result.size()]);
}

From source file:blueprint.sdk.util.config.Config.java

/**
 * Get value from given XPath/*  www  .  j a  v a 2s . co  m*/
 *
 * @param xpath XPath to evaluate
 * @return values as Boolean[]
 * @throws XPathExpressionException
 */
public Boolean[] getBooleanArray(String xpath) throws XPathExpressionException {
    List<Boolean> result = new ArrayList<>(10);

    Iterator<Pointer> nodeIter = JXPathHelper.evaluateIteratorPointers(xpath, context);
    while (nodeIter.hasNext()) {
        Pointer nodePtr = nodeIter.next();

        String value = (String) nodePtr.getValue();
        value = resolveProperty(value);
        try {
            result.add(Boolean.parseBoolean(value));
        } catch (NumberFormatException e) {
            throw new XPathExpressionException("evaluated result of '" + xpath + "' is not a Boolean");
        }
    }

    if (result.size() == 0) {
        warnEmptyValue(xpath, null);
    }

    return result.toArray(new Boolean[result.size()]);
}

From source file:blueprint.sdk.util.config.Config.java

/**
 * Get value from given XPath//from ww w  . ja  v  a 2 s .com
 *
 * @param xpath XPath to evaluate
 * @return values Integer[]
 * @throws XPathExpressionException
 */
public Integer[] getIntArray(String xpath) throws XPathExpressionException {
    List<Integer> result = new ArrayList<>(10);

    Iterator<Pointer> nodeIter = JXPathHelper.evaluateIteratorPointers(xpath, context);
    while (nodeIter.hasNext()) {
        Pointer nodePtr = nodeIter.next();

        String value = (String) nodePtr.getValue();
        value = resolveProperty(value);
        try {
            result.add(Integer.parseInt(value));
        } catch (NumberFormatException e) {
            throw new XPathExpressionException("evaluated result of '" + xpath + "' is not an Integer");
        }
    }

    if (result.size() == 0) {
        warnEmptyValue(xpath, null);
    }

    return result.toArray(new Integer[result.size()]);
}

From source file:jp.terasoluna.fw.beans.JXPathIndexedBeanWrapperImpl.java

/**
 * ???????//  w  ww.j ava2s  . co  m
 * @param propertyName ??
 * @return ??????Map??
 */
@Override
public Map<String, Object> getIndexedPropertyValues(String propertyName) {

    // ???Null
    if (StringUtils.isEmpty(propertyName)) {
        String message = "PropertyName is empty!";
        log.error(message);
        throw new IllegalArgumentException(message);
    }

    // ?????
    if (StringUtils.indexOfAny(propertyName, new char[] { '/', '"', '\'' }) != -1) {
        String message = "Invalid character has found within property name." + " '" + propertyName + "' "
                + "Cannot use [ / \" ' ]";
        log.error(message);
        throw new IllegalArgumentException(message);
    }

    // ??[]?[]????
    String stringIndex = extractIndex(propertyName);
    if (stringIndex.length() > 0) {
        try {
            Integer.parseInt(stringIndex);
        } catch (NumberFormatException e) {
            String message = "Invalid character has found within property name." + " '" + propertyName + "' "
                    + "Cannot use [ [] ]";
            log.error(message);
            throw new IllegalArgumentException(message);
        }
    }

    Map<String, Object> result = new LinkedHashMap<String, Object>();
    String requestXpath = toXPath(propertyName);

    // JXPath??
    Iterator<?> ite = null;
    try {
        ite = context.iteratePointers(requestXpath);
    } catch (JXPathException e) {
        // ????
        String message = "Invalid property name. " + "PropertyName: '" + propertyName + "'" + "XPath: '"
                + requestXpath + "'";
        log.error(message, e);
        throw new IllegalArgumentException(message, e);
    }

    // XPath  Java property
    while (ite.hasNext()) {
        Pointer p = (Pointer) ite.next();
        result.put(this.toPropertyName(p.asPath()), p.getValue());
    }
    return result;
}

From source file:com.opera.core.systems.scope.stp.services.ScopeEcmascriptService.java

protected Runtime findRuntime(int windowId) {
    createAllRuntimes();/* w  w  w . j a  v a 2  s.  c  o  m*/
    Pointer pointer = xpathPointer(runtimesList.values(),
            String.format("/.[htmlFramePath='%s' and windowID='%d']", currentFramePath, windowId));
    if (pointer != null) {
        return (Runtime) pointer.getValue();
    }
    return null;
}

From source file:net.sf.oval.ogn.ObjectGraphNavigatorJXPathImpl.java

public ObjectGraphNavigationResult navigateTo(final Object root, final String xpath)
        throws InvalidConfigurationException {
    Assert.argumentNotNull("root", root);
    Assert.argumentNotNull("xpath", xpath);

    try {//from w ww .jav a  2s  . c o m
        final JXPathContext ctx = JXPathContext.newContext(root);
        ctx.setLenient(true); // do not throw an exception if object graph is incomplete, e.g. contains null-values

        Pointer pointer = ctx.getPointer(xpath);

        // no match found or invalid xpath
        if (pointer instanceof NullPropertyPointer || pointer instanceof NullPointer)
            return null;

        if (pointer instanceof BeanPointer) {
            final Pointer parent = ((BeanPointer) pointer).getImmediateParentPointer();
            if (parent instanceof PropertyPointer) {
                pointer = parent;
            }
        }

        if (pointer instanceof PropertyPointer) {
            final PropertyPointer pp = (PropertyPointer) pointer;
            final Class<?> beanClass = pp.getBean().getClass();
            AccessibleObject accessor = ReflectionUtils.getField(beanClass, pp.getPropertyName());
            if (accessor == null) {
                accessor = ReflectionUtils.getGetter(beanClass, pp.getPropertyName());
            }
            return new ObjectGraphNavigationResult(root, xpath, pp.getBean(), accessor, pointer.getValue());
        }

        throw new InvalidConfigurationException("Don't know how to handle pointer [" + pointer + "] of type ["
                + pointer.getClass().getName() + "] for xpath [" + xpath + "]");
    } catch (final JXPathNotFoundException ex) {
        // thrown if the xpath is invalid
        throw new InvalidConfigurationException(ex);
    }
}

From source file:org.apache.cocoon.forms.datatype.FlowJXPathSelectionList.java

public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException {
    JXPathContext ctx = null;/*from   w  w w.  j a v  a  2  s. c  o  m*/
    Iterator iter = null;
    if (model == null) {
        Object flowData = FlowHelper.getContextObject(ContextHelper.getObjectModel(this.context));
        if (flowData == null) {
            throw new SAXException("No flow data to produce selection list");
        }

        // Move to the list location
        ctx = JXPathContext.newContext(flowData);

        // Iterate on all elements of the list
        iter = ctx.iteratePointers(this.listPath);
    } else {
        // Move to the list location
        ctx = JXPathContext.newContext(model);

        // Iterate on all elements of the list
        iter = ctx.iteratePointers(".");
    }

    // Start the selection-list
    contentHandler.startElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL,
            FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL, XMLUtils.EMPTY_ATTRIBUTES);
    if (this.nullable) {
        final AttributesImpl voidAttrs = new AttributesImpl();
        voidAttrs.addCDATAAttribute("value", "");
        contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, voidAttrs);

        if (this.nullText != null) {
            contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL,
                    FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES);

            if (this.nullTextIsI18nKey) {
                if ((this.i18nCatalog != null) && (this.i18nCatalog.trim().length() > 0)) {
                    new I18nMessage(this.nullText, this.i18nCatalog).toSAX(contentHandler);
                } else {
                    new I18nMessage(this.nullText).toSAX(contentHandler);
                }
            } else {
                contentHandler.characters(this.nullText.toCharArray(), 0, this.nullText.length());
            }

            contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL,
                    FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL);
        }

        contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
    }

    while (iter.hasNext()) {
        String stringValue = "";
        Object label = null;

        // Get a context on the current item
        Pointer ptr = (Pointer) iter.next();
        if (ptr.getValue() != null) {
            JXPathContext itemCtx = ctx.getRelativeContext(ptr);

            // Get the value as a string
            Object value = itemCtx.getValue(this.valuePath);

            // List may contain null value, and (per contract with convertors),
            // convertors are not invoked on nulls.
            if (value != null) {
                stringValue = this.datatype.convertToString(value, locale);
            }

            // Get the label (can be ommitted)
            itemCtx.setLenient(true);
            label = itemCtx.getValue(this.labelPath);
            if (label == null) {
                label = stringValue;
            }
        }

        // Output this item
        AttributesImpl itemAttrs = new AttributesImpl();
        itemAttrs.addCDATAAttribute("value", stringValue);
        contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, itemAttrs);
        if (label != null) {
            contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL,
                    FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES);
            if (label instanceof XMLizable) {
                ((XMLizable) label).toSAX(contentHandler);
            } else if (this.labelIsI18nKey) {
                String stringLabel = label.toString();

                if ((this.i18nCatalog != null) && (this.i18nCatalog.trim().length() > 0)) {
                    new I18nMessage(stringLabel, this.i18nCatalog).toSAX(contentHandler);
                } else {
                    new I18nMessage(stringLabel).toSAX(contentHandler);
                }
            } else {
                String stringLabel = label.toString();
                contentHandler.characters(stringLabel.toCharArray(), 0, stringLabel.length());
            }
            contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL,
                    FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL);
        }
        contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
    }

    // End the selection-list
    contentHandler.endElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL,
            FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL);
}

From source file:org.apache.cocoon.woody.datatype.FlowJXPathSelectionList.java

public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException {
    JXPathContext ctx = null;//from  ww  w .j a  v a 2  s  .co m
    Iterator iter = null;
    if (model == null) {
        Object flowData = FlowHelper.getContextObject(ContextHelper.getObjectModel(this.context));
        if (flowData == null) {
            throw new SAXException("No flow data to produce selection list");
        }

        // Move to the list location
        ctx = JXPathContext.newContext(flowData);

        // Iterate on all elements of the list
        iter = ctx.iteratePointers(this.listPath);
    } else {
        // Move to the list location
        ctx = JXPathContext.newContext(model);

        // Iterate on all elements of the list
        iter = ctx.iteratePointers(".");
    }

    // Start the selection-list
    contentHandler.startElement(Constants.WI_NS, SELECTION_LIST_EL,
            Constants.WI_PREFIX_COLON + SELECTION_LIST_EL, Constants.EMPTY_ATTRS);

    while (iter.hasNext()) {
        String stringValue = "";
        Object label = null;

        // Get a context on the current item
        Pointer ptr = (Pointer) iter.next();
        if (ptr.getValue() != null) {
            JXPathContext itemCtx = ctx.getRelativeContext(ptr);

            // Get the value as a string
            Object value = itemCtx.getValue(this.valuePath);

            // List may contain null value, and (per contract with convertors),
            // convertors are not invoked on nulls.
            if (value != null) {
                stringValue = this.datatype.convertToString(value, locale);
            }

            // Get the label (can be ommitted)
            itemCtx.setLenient(true);
            label = itemCtx.getValue(this.labelPath);
            if (label == null) {
                label = stringValue;
            }
        }

        // Output this item
        AttributesImpl itemAttrs = new AttributesImpl();
        itemAttrs.addCDATAAttribute("value", stringValue);
        contentHandler.startElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL, itemAttrs);
        if (label != null) {
            contentHandler.startElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL,
                    Constants.EMPTY_ATTRS);
            if (label instanceof XMLizable) {
                ((XMLizable) label).toSAX(contentHandler);
            } else {
                String stringLabel = label.toString();
                contentHandler.characters(stringLabel.toCharArray(), 0, stringLabel.length());
            }
            contentHandler.endElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL);
        }
        contentHandler.endElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL);
    }

    // End the selection-list
    contentHandler.endElement(Constants.WI_NS, SELECTION_LIST_EL,
            Constants.WI_PREFIX_COLON + SELECTION_LIST_EL);
}

From source file:org.chiba.xml.xforms.connector.context.ContextResolver.java

/**
 * Performs a lookup in Chiba's context map and returns the object denoted
 * by the scheme specific part of the resolver's URI.
 *
 * @return the object denoted by the scheme specific part of the
 * resolver's URI.//w  w  w  .  j  a  va  2  s  . c om
 * @throws XFormsException if any error occurred during link traversal.
 */
public Object resolve() throws XFormsException {
    try {
        String xpath = new URI(getURI()).getSchemeSpecificPart();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("resolve: " + xpath);
        }

        JXPathContext contextContext = JXPathContext.newContext(getContext());
        Pointer pointer = contextContext.getPointer(xpath);
        if (pointer.getValue() == null) {
            throw new JXPathException("No pointer for xpath: " + xpath);
        }

        return pointer.getNode();
    } catch (Exception e) {
        throw new XFormsException(e);
    }
}