Example usage for org.apache.commons.jxpath.ri.model.beans PropertyPointer getPropertyName

List of usage examples for org.apache.commons.jxpath.ri.model.beans PropertyPointer getPropertyName

Introduction

In this page you can find the example usage for org.apache.commons.jxpath.ri.model.beans PropertyPointer getPropertyName.

Prototype

public abstract String getPropertyName();

Source Link

Document

Get the property name.

Usage

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 {/*w w w.  j a va2  s  .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);
    }
}