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

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

Introduction

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

Prototype

public Object getBean() 

Source Link

Document

Get the parent bean.

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 {//from   w w w  .ja va 2  s.com
        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);
    }
}