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

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

Introduction

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

Prototype

public abstract Object getValue(String xpath);

Source Link

Document

Evaluates the xpath and returns the resulting object.

Usage

From source file:org.geotools.util.XmlXpathUtilites.java

/**
 * /*  w  w w.  j  a  v  a 2 s .c om*/
* @param ns namespaces
 * @param xpathString xpath to search on
 * @param doc xml to search
 * @return the (single) value matching the xpath in the xml supplied
 */
public static String getSingleXPathValue(NamespaceSupport ns, String xpathString, Document doc) {
    String id = null;
    JXPathContext context = initialiseContext(ns, doc);
    try {
        Object ob = context.getValue(xpathString);
        id = (String) ob;
    } catch (RuntimeException e) {
        throw new RuntimeException("Error reading xpath " + xpathString, e);
    }
    return id;
}

From source file:org.jboss.dashboard.ui.formatters.ForComparator.java

private Object getObjectProperty(Object object, String property) {
    JXPathContext ctx = JXPathContext.newContext(object);
    try {/*from w ww  .j  a va  2s  .c  om*/
        return ctx.getValue(property);
    } catch (Exception e) {
        if (log.isDebugEnabled())
            log.debug("Invalid property '" + property + "' ", e);
    }
    return null;
}

From source file:org.jboss.dashboard.ui.formatters.ForFormatter.java

public void service(HttpServletRequest request, HttpServletResponse response) throws FormatterException {
    log.debug("Servicing ForFormatter.");
    Object array = getParameter("array");
    if (array == null) {
        Object componentName = getParameter("factoryElement");
        Object propertyName = getParameter("property");
        if (componentName != null) {
            Object component = CDIBeanLocator.getBeanByNameOrType((String) componentName);
            array = component;// w  w  w  .j ava 2s .com
            if (propertyName != null) {
                JXPathContext ctx = JXPathContext.newContext(component);
                try {
                    array = ctx.getValue((String) propertyName);
                } catch (Exception e) {
                    log.debug("Error:", e);
                }
            }
        }
    }
    String sortProperties = (String) getParameter("sortProperties");

    Iterator iterator = null;
    if (array == null) {
        renderFragment("empty");
        return;
    }

    if (array instanceof Collection) {
        iterator = ((Collection) array).iterator();
    } else if (array.getClass().isArray()) {
        final Object theArray = array;
        iterator = new Iterator() {
            int index = 0;

            public void remove() {
                throw new UnsupportedOperationException();
            }

            public boolean hasNext() {
                return Array.getLength(theArray) > index;
            }

            public Object next() {
                return Array.get(theArray, index++);
            }
        };
    } else if (array instanceof Iterator) {
        iterator = (Iterator) array;
    } else if (array instanceof Enumeration) {
        List l = new ArrayList();
        while (((Enumeration) array).hasMoreElements()) {
            l.add(((Enumeration) array).nextElement());
        }
        iterator = l.iterator();
    }

    if (sortProperties != null) {
        iterator = getSortedIterator(iterator, sortProperties);
    }

    if (iterator != null && iterator.hasNext()) {
        renderFragment("outputStart");
        int i = 0;
        while (iterator.hasNext()) {
            Object o = iterator.next();
            setAttribute("index", new Integer(i));
            setAttribute("count", new Integer(++i));
            if (o != null)
                setAttribute("element", o);
            else
                setAttribute("element", getParameter("nullValue"));
            renderFragment("output");
        }
        renderFragment("outputEnd");
    } else {
        renderFragment("empty");
    }
}

From source file:org.jboss.dashboard.ui.taglib.factory.PropertyTag.java

protected Object getValue() {
    if (value != null)
        return value;

    Object beanObject = CDIBeanLocator.getBeanByNameOrType(getBeanName());
    if (beanObject != null) {
        JXPathContext ctx = JXPathContext.newContext(beanObject);
        try {/*  w ww.j  a  v a 2 s .  c o  m*/
            return value = ctx.getValue(getProperty());
        } catch (JXPathException jxpe) {
            log.warn("Can't read property " + getProperty() + " in " + getBeanName() + ": ", jxpe);
        }
    }
    return null;
}

From source file:org.jboss.dashboard.ui.taglib.formatter.ForFormatter.java

public void service(HttpServletRequest request, HttpServletResponse response) throws FormatterException {
    log.debug("Servicing ForFormatter.");
    Object array = getParameter("array");
    if (array == null) {
        Object componentName = getParameter("factoryElement");
        Object propertyName = getParameter("property");
        if (componentName != null) {
            Object component = Factory.lookup((String) componentName);
            array = component;// w w  w. j  av  a2  s.  c o  m
            if (propertyName != null) {
                JXPathContext ctx = JXPathContext.newContext(component);
                try {
                    array = ctx.getValue((String) propertyName);
                } catch (Exception e) {
                    log.debug("Error:", e);
                }
            }
        }
    }
    String sortProperties = (String) getParameter("sortProperties");

    Iterator iterator = null;
    if (array == null) {
        renderFragment("empty");
        return;
    }

    if (array instanceof Collection) {
        iterator = ((Collection) array).iterator();
    } else if (array.getClass().isArray()) {
        final Object theArray = array;
        iterator = new Iterator() {
            int index = 0;

            public void remove() {
                throw new UnsupportedOperationException();
            }

            public boolean hasNext() {
                return Array.getLength(theArray) > index;
            }

            public Object next() {
                return Array.get(theArray, index++);
            }
        };
    } else if (array instanceof Iterator) {
        iterator = (Iterator) array;
    } else if (array instanceof Enumeration) {
        List l = new ArrayList();
        while (((Enumeration) array).hasMoreElements()) {
            l.add(((Enumeration) array).nextElement());
        }
        iterator = l.iterator();
    }

    if (sortProperties != null) {
        iterator = getSortedIterator(iterator, sortProperties);
    }

    if (iterator != null && iterator.hasNext()) {
        renderFragment("outputStart");
        int i = 0;
        while (iterator.hasNext()) {
            Object o = iterator.next();
            setAttribute("index", new Integer(i));
            setAttribute("count", new Integer(++i));
            if (o != null)
                setAttribute("element", o);
            else
                setAttribute("element", getParameter("nullValue"));
            renderFragment("output");
        }
        renderFragment("outputEnd");
    } else {
        renderFragment("empty");
    }
}

From source file:org.jboss.dashboard.ui.taglib.formatter.FragmentTag.java

public Object getParam(String name) {
    FormatterTag parent = (FormatterTag) getParent();
    Object value = parent.getFragmentParams().get(name);
    if (value == null && parent.getFormaterTagDynamicAttributesInterpreter() != null) {
        value = parent.getFormaterTagDynamicAttributesInterpreter().getValueForParameter(name);
    }//from   w w w . j a v a2  s  . c  om
    if (value == null && name.indexOf('/') != -1)
        try {
            log.debug("Attempt JXPath detection of param...");
            JXPathContext ctx = JXPathContext.newContext(parent.getFragmentParams());
            ctx.setLenient(false);
            value = ctx.getValue(name);
            if (value == null && parent.getFormaterTagDynamicAttributesInterpreter() != null) {
                String firstName = name.substring(0, name.indexOf('/'));
                Object firstValue = parent.getFormaterTagDynamicAttributesInterpreter()
                        .getValueForParameter(firstName);
                if (firstValue != null) {
                    ctx = JXPathContext.newContext(firstValue);
                    ctx.setLenient(false);
                    value = ctx.getValue(name.substring(name.indexOf('/') + 1));
                }
            }
        } catch (Exception e) {
            if (log.isDebugEnabled())
                log.warn("Error getting attribute " + value + " in params.");
        }
    return value;
}

From source file:org.jbpm.formModeler.core.processing.impl.FormProcessorImpl.java

protected Object getUnbindedFieldValue(String bindingExpression, Map<String, Object> bindingData) {
    if (bindingExpression.indexOf("/") != -1) {
        try {/*from  www.j a  v  a2s . c o  m*/
            String root = bindingExpression.substring(0, bindingExpression.indexOf("/"));
            String expression = bindingExpression.substring(root.length() + 1);

            Object object = bindingData.get(root);
            JXPathContext ctx = JXPathContext.newContext(object);
            return ctx.getValue(expression);
        } catch (Exception e) {
            log.warn("Error getting value for xpath xpression '{}': {}", bindingExpression, e);
        }
    }
    return bindingData.get(bindingExpression);
}

From source file:org.jbpm.formModeler.service.bb.mvc.taglib.factory.PropertyTag.java

protected Object getValue() throws Exception {
    if (value != null)
        return value;
    Object beanObject = CDIBeanLocator.getBeanByNameOrType(getBean());
    if (beanObject != null) {
        JXPathContext ctx = JXPathContext.newContext(beanObject);
        try {//from  www . jav  a  2 s  .c o m
            return value = ctx.getValue(getProperty());
        } catch (JXPathException jxpe) {
            if (log.isDebugEnabled()) {
                log.debug("Can't read property " + getProperty() + " in " + getBean() + ": ", jxpe);
            }
        }
    }
    return null;
}

From source file:org.jbpm.formModeler.service.bb.mvc.taglib.formatter.ForFormatter.java

public void service(HttpServletRequest request, HttpServletResponse response) throws FormatterException {
    log.debug("Servicing ForFormatter.");
    Object array = getParameter("array");
    if (array == null) {
        Object componentName = getParameter("bean");
        Object propertyName = getParameter("property");
        if (componentName != null) {
            Object component = CDIBeanLocator.getBeanByNameOrType((String) componentName);
            array = component;/*w  ww  . j  a v a2  s. c  o m*/
            if (propertyName != null) {
                JXPathContext ctx = JXPathContext.newContext(component);
                try {
                    array = ctx.getValue((String) propertyName);
                } catch (Exception e) {
                    log.debug("Error:", e);
                }
            }
        }
    }
    String sortProperties = (String) getParameter("sortProperties");

    Iterator iterator = null;
    if (array == null) {
        renderFragment("empty");
        return;
    }

    if (array instanceof Collection) {
        iterator = ((Collection) array).iterator();
    } else if (array.getClass().isArray()) {
        final Object theArray = array;
        iterator = new Iterator() {
            int index = 0;

            public void remove() {
                throw new UnsupportedOperationException();
            }

            public boolean hasNext() {
                return Array.getLength(theArray) > index;
            }

            public Object next() {
                return Array.get(theArray, index++);
            }
        };
    } else if (array instanceof Iterator) {
        iterator = (Iterator) array;
    } else if (array instanceof Enumeration) {
        List l = new ArrayList();
        while (((Enumeration) array).hasMoreElements()) {
            l.add(((Enumeration) array).nextElement());
        }
        iterator = l.iterator();
    }

    if (sortProperties != null) {
        iterator = getSortedIterator(iterator, sortProperties);
    }

    if (iterator != null && iterator.hasNext()) {
        renderFragment("outputStart");
        int i = 0;
        while (iterator.hasNext()) {
            Object o = iterator.next();
            setAttribute("index", new Integer(i));
            setAttribute("count", new Integer(++i));
            if (o != null)
                setAttribute("element", o);
            else
                setAttribute("element", getParameter("nullValue"));
            renderFragment("output");
        }
        renderFragment("outputEnd");
    } else {
        renderFragment("empty");
    }
}

From source file:org.jkcsoft.java.xml.XMLUtil.java

/**
 * Get Child Element Text.  Good when you have the following:
 * <Object>//from  w ww .  ja v  a 2 s  .co  m
 * <Property1>value1</Property1>
 * <Property2>value2</Property2>
 * </Object>
 * In effect, obj is the node for an object or row of data and
 * property is the name of the property.
 */
public static String getCET(Node obj, String property, String ifnull) throws Exception {
    String retVal = null;
    JXPathContext jxp = JXPathContext.newContext(obj);
    Text text = (org.w3c.dom.Text) jxp.getValue(property);
    if (text == null) {
        //     throw new Exception("Property not found: " + property);
        retVal = ifnull;
    } else {
        retVal = text.getData();
    }
    if (retVal == null)
        retVal = ifnull;
    return retVal;
}