Example usage for org.apache.commons.beanutils PropertyUtils NESTED_DELIM

List of usage examples for org.apache.commons.beanutils PropertyUtils NESTED_DELIM

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils NESTED_DELIM.

Prototype

char NESTED_DELIM

To view the source code for org.apache.commons.beanutils PropertyUtils NESTED_DELIM.

Click Source Link

Document

The delimiter that separates the components of a nested reference.

Usage

From source file:net.sf.jasperreports.engine.data.JRHibernateAbstractDataSource.java

protected FieldReader getFieldReaderSingleReturn(Map<String, Integer> aliasesMap, JRField field,
        boolean useIndex) {
    FieldReader reader;//from   w  ww  . ja v a  2 s  .c om

    String fieldMapping = getFieldMapping(field);
    if (aliasesMap.containsKey(fieldMapping)) {
        if (useIndex) {
            reader = new IndexFieldReader(0);
        } else {
            reader = new IdentityFieldReader();
        }
    } else {
        @SuppressWarnings("deprecation")
        int firstNestedIdx = fieldMapping.indexOf(PropertyUtils.NESTED_DELIM);

        if (firstNestedIdx >= 0 && aliasesMap.containsKey(fieldMapping.substring(0, firstNestedIdx))) {
            fieldMapping = fieldMapping.substring(firstNestedIdx + 1);
        }

        if (useIndex) {
            reader = new IndexPropertyFieldReader(0, fieldMapping);
        } else {
            reader = new PropertyFieldReader(fieldMapping);
        }
    }

    return reader;
}

From source file:net.sf.jasperreports.engine.data.JRHibernateAbstractDataSource.java

protected FieldReader getFieldReader(Type[] returnTypes, Map<String, Integer> aliasesMap, JRField field) {
    FieldReader reader;//ww  w.java2  s.c o m

    String fieldMapping = getFieldMapping(field);
    Integer fieldIdx = aliasesMap.get(fieldMapping);
    if (fieldIdx == null) {
        @SuppressWarnings("deprecation")
        int firstNestedIdx = fieldMapping.indexOf(PropertyUtils.NESTED_DELIM);

        if (firstNestedIdx < 0) {
            throw new JRRuntimeException(EXCEPTION_MESSAGE_KEY_UNKNOWN_RETURN_ALIAS,
                    new Object[] { fieldMapping });
        }

        String fieldAlias = fieldMapping.substring(0, firstNestedIdx);
        String fieldProperty = fieldMapping.substring(firstNestedIdx + 1);

        fieldIdx = aliasesMap.get(fieldAlias);
        if (fieldIdx == null) {
            throw new JRRuntimeException(EXCEPTION_MESSAGE_KEY_NO_FIELD_ALIAS, new Object[] { fieldAlias });
        }

        Type type = returnTypes[fieldIdx];
        if (!type.isEntityType() && !type.isComponentType()) {
            throw new JRRuntimeException(EXCEPTION_MESSAGE_KEY_FIELD_ALIAS_TYPE_MISMATCH,
                    new Object[] { fieldAlias });
        }

        reader = new IndexPropertyFieldReader(fieldIdx, fieldProperty);
    } else {
        reader = new IndexFieldReader(fieldIdx);
    }

    return reader;
}

From source file:nl.strohalm.cyclos.utils.binding.CustomBeanUtilsBean.java

private int findLastNestedIndex(final String expression) {
    // walk back from the end to the start
    // and find the first index that
    int bracketCount = 0;
    for (int i = expression.length() - 1; i >= 0; i--) {
        final char at = expression.charAt(i);
        switch (at) {
        case PropertyUtils.NESTED_DELIM:
            if (bracketCount < 1) {
                return i;
            }//from w  ww.  j av  a2s. c om
            break;

        case PropertyUtils.MAPPED_DELIM:
        case PropertyUtils.INDEXED_DELIM:
            // not bothered which
            --bracketCount;
            break;

        case PropertyUtils.MAPPED_DELIM2:
        case PropertyUtils.INDEXED_DELIM2:
            // not bothered which
            ++bracketCount;
            break;
        }
    }
    // can't find any
    return -1;
}

From source file:org.displaytag.util.LookupUtil.java

/**
 * <p>/*  w  ww.  j  a v a2 s .  c o m*/
 * Returns the value of a property in the given bean.
 * </p>
 * <p>
 * This method is a modificated version from commons-beanutils PropertyUtils.getProperty(). It allows intermediate
 * nulls in expression without throwing exception (es. it doesn't throw an exception for the property
 * <code>object.date.time</code> if <code>date</code> is null)
 * </p>
 * @param bean javabean
 * @param name name of the property to read from the javabean
 * @return Object
 * @throws ObjectLookupException for errors while retrieving a property in the bean
 */
public static Object getBeanProperty(Object bean, String name) throws ObjectLookupException {

    if (log.isDebugEnabled()) {
        log.debug("getProperty [" + name + "] on bean " + bean);
    }

    if (bean == null) {
        throw new IllegalArgumentException("No bean specified");
    }
    if (name == null) {
        throw new IllegalArgumentException("No name specified");
    }

    Object evalBean = bean;
    String evalName = name;

    try {

        int indexOfINDEXEDDELIM;
        int indexOfMAPPEDDELIM;
        int indexOfMAPPEDDELIM2;
        int indexOfNESTEDDELIM;
        while (true) {

            indexOfNESTEDDELIM = evalName.indexOf(PropertyUtils.NESTED_DELIM);
            indexOfMAPPEDDELIM = evalName.indexOf(PropertyUtils.MAPPED_DELIM);
            indexOfMAPPEDDELIM2 = evalName.indexOf(PropertyUtils.MAPPED_DELIM2);
            if (indexOfMAPPEDDELIM2 >= 0 && indexOfMAPPEDDELIM >= 0
                    && (indexOfNESTEDDELIM < 0 || indexOfNESTEDDELIM > indexOfMAPPEDDELIM)) {
                indexOfNESTEDDELIM = evalName.indexOf(PropertyUtils.NESTED_DELIM, indexOfMAPPEDDELIM2);
            } else {
                indexOfNESTEDDELIM = evalName.indexOf(PropertyUtils.NESTED_DELIM);
            }
            if (indexOfNESTEDDELIM < 0) {
                break;
            }
            String next = evalName.substring(0, indexOfNESTEDDELIM);
            indexOfINDEXEDDELIM = next.indexOf(PropertyUtils.INDEXED_DELIM);
            indexOfMAPPEDDELIM = next.indexOf(PropertyUtils.MAPPED_DELIM);
            if (evalBean instanceof Map) {
                evalBean = ((Map) evalBean).get(next);
            } else if (indexOfMAPPEDDELIM >= 0) {

                evalBean = PropertyUtils.getMappedProperty(evalBean, next);

            } else if (indexOfINDEXEDDELIM >= 0) {
                evalBean = PropertyUtils.getIndexedProperty(evalBean, next);
            } else {
                evalBean = PropertyUtils.getSimpleProperty(evalBean, next);
            }

            if (evalBean == null) {
                log.debug("Null property value for '" + evalName.substring(0, indexOfNESTEDDELIM) + "'");
                return null;
            }
            evalName = evalName.substring(indexOfNESTEDDELIM + 1);

        }

        indexOfINDEXEDDELIM = evalName.indexOf(PropertyUtils.INDEXED_DELIM);
        indexOfMAPPEDDELIM = evalName.indexOf(PropertyUtils.MAPPED_DELIM);

        if (evalBean instanceof Map) {
            evalBean = ((Map) evalBean).get(evalName);
        } else if (indexOfMAPPEDDELIM >= 0) {
            evalBean = PropertyUtils.getMappedProperty(evalBean, evalName);
        } else if (indexOfINDEXEDDELIM >= 0) {
            evalBean = PropertyUtils.getIndexedProperty(evalBean, evalName);
        } else {
            evalBean = PropertyUtils.getSimpleProperty(evalBean, evalName);
        }
    } catch (IllegalAccessException e) {
        throw new ObjectLookupException(LookupUtil.class, evalBean, evalName, e);
    }

    catch (InvocationTargetException e) {
        throw new ObjectLookupException(LookupUtil.class, evalBean, evalName, e);
    } catch (NoSuchMethodException e) {
        throw new ObjectLookupException(LookupUtil.class, evalBean, evalName, e);
    }

    return evalBean;

}

From source file:org.gameye.psp.image.utils.TBeanUtilsBean.java

public void copyProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException {

    // Trace logging (if enabled)
    if (log.isTraceEnabled()) {
        StringBuffer sb = new StringBuffer("  copyProperty(");
        sb.append(bean);//from  ww  w  . jav  a  2  s .co  m
        sb.append(", ");
        sb.append(name);
        sb.append(", ");
        if (value == null) {
            sb.append("<NULL>");
        } else if (value instanceof String) {
            sb.append((String) value);
        } else if (value instanceof String[]) {
            String values[] = (String[]) value;
            sb.append('[');
            for (int i = 0; i < values.length; i++) {
                if (i > 0) {
                    sb.append(',');
                }
                sb.append(values[i]);
            }
            sb.append(']');
        } else {
            sb.append(value.toString());
        }
        sb.append(')');
        log.trace(sb.toString());
    }

    // Resolve any nested expression to get the actual target bean
    Object target = bean;
    int delim = name.lastIndexOf(PropertyUtils.NESTED_DELIM);
    if (delim >= 0) {
        try {
            target = getPropertyUtils().getProperty(bean, name.substring(0, delim));
        } catch (NoSuchMethodException e) {
            return; // Skip this property setter
        }
        name = name.substring(delim + 1);
        if (log.isTraceEnabled()) {
            log.trace("    Target bean = " + target);
            log.trace("    Target name = " + name);
        }
    }

    // Declare local variables we will require
    String propName = null; // Simple name of target property
    Class type = null; // Java type of target property
    int index = -1; // Indexed subscript value (if any)
    String key = null; // Mapped key value (if any)

    // Calculate the target property name, index, and key values
    propName = name;
    int i = propName.indexOf(PropertyUtils.INDEXED_DELIM);
    if (i >= 0) {
        int k = propName.indexOf(PropertyUtils.INDEXED_DELIM2);
        try {
            index = Integer.parseInt(propName.substring(i + 1, k));
        } catch (NumberFormatException e) {
            ;
        }
        propName = propName.substring(0, i);
    }
    int j = propName.indexOf(PropertyUtils.MAPPED_DELIM);
    if (j >= 0) {
        int k = propName.indexOf(PropertyUtils.MAPPED_DELIM2);
        try {
            key = propName.substring(j + 1, k);
        } catch (IndexOutOfBoundsException e) {
            ;
        }
        propName = propName.substring(0, j);
    }

    // Calculate the target property type
    if (target instanceof DynaBean) {
        DynaClass dynaClass = ((DynaBean) target).getDynaClass();
        DynaProperty dynaProperty = dynaClass.getDynaProperty(propName);
        if (dynaProperty == null) {
            return; // Skip this property setter
        }
        type = dynaProperty.getType();
    } else {
        PropertyDescriptor descriptor = null;
        try {
            descriptor = getPropertyUtils().getPropertyDescriptor(target, name);
            if (descriptor == null) {
                return; // Skip this property setter
            }
        } catch (NoSuchMethodException e) {
            return; // Skip this property setter
        }
        type = descriptor.getPropertyType();
        if (type == null) {
            // Most likely an indexed setter on a POJB only
            if (log.isTraceEnabled()) {
                log.trace("    target type for property '" + propName + "' is null, so skipping ths setter");
            }
            return;
        }
    }
    if (log.isTraceEnabled()) {
        log.trace("    target propName=" + propName + ", type=" + type + ", index=" + index + ", key=" + key);
    }

    // Convert the specified value to the required type and store it
    if (index >= 0) { // Destination must be indexed
        Converter converter = getConvertUtils().lookup(type.getComponentType());
        if (converter != null) {
            log.trace("        USING CONVERTER " + converter);
            value = converter.convert(type, value);
        }
        try {
            getPropertyUtils().setIndexedProperty(target, propName, index, value);
        } catch (NoSuchMethodException e) {
            throw new InvocationTargetException(e, "Cannot set " + propName);
        }
    } else if (key != null) { // Destination must be mapped
        // Maps do not know what the preferred data type is,
        // so perform no conversions at all
        // FIXME - should we create or support a TypedMap?
        try {
            getPropertyUtils().setMappedProperty(target, propName, key, value);
        } catch (NoSuchMethodException e) {
            throw new InvocationTargetException(e, "Cannot set " + propName);
        }
    } else { // Destination must be simple
        Converter converter = getConvertUtils().lookup(type);
        if (converter != null) {
            log.trace("        USING CONVERTER " + converter);
            value = converter.convert(type, value);
        }
        try {
            getPropertyUtils().setSimpleProperty(target, propName, value);
        } catch (NoSuchMethodException e) {
            throw new InvocationTargetException(e, "Cannot set " + propName);
        }
    }

}

From source file:org.gameye.psp.image.utils.TBeanUtilsBean.java

private int findLastNestedIndex(String expression) {
    // walk back from the end to the start 
    // and find the first index that 
    int bracketCount = 0;
    for (int i = expression.length() - 1; i >= 0; i--) {
        char at = expression.charAt(i);
        switch (at) {
        case PropertyUtils.NESTED_DELIM:
            if (bracketCount < 1) {
                return i;
            }//from  www. j a  va 2  s  .co  m
            break;

        case PropertyUtils.MAPPED_DELIM:
        case PropertyUtils.INDEXED_DELIM:
            // not bothered which
            --bracketCount;
            break;

        case PropertyUtils.MAPPED_DELIM2:
        case PropertyUtils.INDEXED_DELIM2:
            // not bothered which
            ++bracketCount;
            break;
        }
    }
    // can't find any
    return -1;
}

From source file:org.kuali.ext.mm.ObjectUtil.java

/**
 * get the types of the nested attributes starting at the given class
 *
 * @param clazz the given class//from w ww  .j  a va  2 s.  c om
 * @param nestedAttribute the nested attributes of the given class
 * @return a map that contains the types of the nested attributes and the attribute names
 */
public static Map<Class<?>, String> getNestedAttributeTypes(Class<?> clazz, String nestedAttribute) {
    List<String> attributes = Arrays.asList(StringUtils.split(nestedAttribute, PropertyUtils.NESTED_DELIM));
    Map<Class<?>, String> nestedAttributes = new HashMap<Class<?>, String>();

    Class<?> currentClass = clazz;
    for (String propertyName : attributes) {
        String methodName = "get" + StringUtils.capitalize(propertyName);
        try {
            Method method = currentClass.getMethod(methodName);
            currentClass = method.getReturnType();
            nestedAttributes.put(currentClass, propertyName);
        } catch (Exception e) {
            LOG.info(e);
            break;
        }
    }
    return nestedAttributes;
}

From source file:org.kuali.kfs.sys.document.datadictionary.TotalDefinition.java

/**
 * get the actual property name if the property is nested; otherwise, return the given property name
 * //w  ww . ja va 2s .  c o m
 * @param containingPropertyName the given containing property name
 * @param propertyName the given peropety name
 * @return the actual property name if the property is nested; otherwise, return the given property name
 */
public String getActualPropertyName(String containingPropertyName, String propertyName) {
    if (this.isNestedProperty() && StringUtils.isNotBlank(containingPropertyName)) {
        return containingPropertyName + PropertyUtils.NESTED_DELIM + propertyName;
    }

    return propertyName;
}

From source file:org.kuali.kfs.sys.document.web.RenderableAccountingLineContainer.java

/**
 * @see org.kuali.kfs.sys.document.web.AccountingLineRenderingContext#getAccountingLineContainingObjectPropertyName()
 *///from ww  w.j  av a2  s  .com
public String getAccountingLineContainingObjectPropertyName() {
    return StringUtils.substringBeforeLast(this.getAccountingLinePropertyPath(),
            String.valueOf(PropertyUtils.NESTED_DELIM));
}

From source file:org.kuali.rice.kns.web.struts.form.pojo.PojoPropertyUtilsBean.java

/**
 * begin Kuali Foundation modification/*from  ww  w.  j  a  v  a 2 s .  c o  m*/
 * Set the value of the (possibly nested) property of the specified name, for the specified bean, with no type conversions.
 *
 * @param bean Bean whose property is to be modified
 * @param name Possibly nested name of the property to be modified
 * @param value Value to which the property is to be set
 *
 * @exception IllegalAccessException if the caller does not have access to the property accessor method
 * @exception IllegalArgumentException if <code>bean</code> or <code>name</code> is null
 * @exception IllegalArgumentException if a nested reference to a property returns null
 * @exception InvocationTargetException if the property accessor method throws an exception
 * @exception NoSuchMethodException if an accessor method for this propety cannot be found
 * end Kuali Foundation modification
 */
public void setNestedProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    if (bean == null) {
        if (LOG.isDebugEnabled())
            LOG.debug("No bean specified, name = " + name + ", value = " + value);
        return;
    }
    if (name == null) {
        throw new IllegalArgumentException("No name specified");
    }

    Object propBean = null;
    int indexOfINDEXED_DELIM = -1;
    int indexOfMAPPED_DELIM = -1;
    while (true) {
        int delim = name.indexOf(PropertyUtils.NESTED_DELIM);
        if (delim < 0) {
            break;
        }
        String next = name.substring(0, delim);
        indexOfINDEXED_DELIM = next.indexOf(PropertyUtils.INDEXED_DELIM);
        indexOfMAPPED_DELIM = next.indexOf(PropertyUtils.MAPPED_DELIM);
        if (bean instanceof Map) {
            propBean = ((Map) bean).get(next);
        } else if (indexOfMAPPED_DELIM >= 0) {
            propBean = getMappedProperty(bean, next);
        } else if (indexOfINDEXED_DELIM >= 0) {
            propBean = getIndexedProperty(bean, next);
        } else {
            propBean = getSimpleProperty(bean, next);
        }
        if (ObjectUtils.isNull(propBean)) {
            Class propertyType = getPropertyType(bean, next);
            if (propertyType != null) {
                Object newInstance = ObjectUtils.createNewObjectFromClass(propertyType);
                setSimpleProperty(bean, next, newInstance);
                propBean = getSimpleProperty(bean, next);
            }
        }
        bean = propBean;
        name = name.substring(delim + 1);
    }

    indexOfINDEXED_DELIM = name.indexOf(PropertyUtils.INDEXED_DELIM);
    indexOfMAPPED_DELIM = name.indexOf(PropertyUtils.MAPPED_DELIM);

    if (bean instanceof Map) {
        // check to see if the class has a standard property
        PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
        if (descriptor == null) {
            // no - then put the value into the map
            ((Map) bean).put(name, value);
        } else {
            // yes - use that instead
            setSimpleProperty(bean, name, value);
        }
    } else if (indexOfMAPPED_DELIM >= 0) {
        setMappedProperty(bean, name, value);
    } else if (indexOfINDEXED_DELIM >= 0) {
        setIndexedProperty(bean, name, value);
    } else {
        setSimpleProperty(bean, name, value);
    }
}