Example usage for org.apache.commons.beanutils ConvertingWrapDynaBean ConvertingWrapDynaBean

List of usage examples for org.apache.commons.beanutils ConvertingWrapDynaBean ConvertingWrapDynaBean

Introduction

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

Prototype

public ConvertingWrapDynaBean(Object instance) 

Source Link

Document

Construct a new DynaBean associated with the specified JavaBean instance.

Usage

From source file:org.ejbca.ui.cli.FieldEditor.java

/** Lists, Gets or sets fields in a Bean.
 * /*  ww w . j  av  a2  s .  c om*/
 * @param listOnly if true, fields will be listed, and nothing more will happen.
 * @param getOnly if true (and listOnly is false), will get the value of a field and nothing else will happen
 * @param name the name of the Bean to be modified
 * @param field the field name to get or set
 * @param value the value to set, of we should set a new value
 * @param obj the Bean to list, get or set fields
 * @return true if we only listed or got a value, i.e. if nothing was modified, false is we set a value.
 * @throws FieldNotFoundException if field was not found. 
 */
public boolean listGetOrSet(boolean listOnly, boolean getOnly, final String name, final String field,
        final String value, final Object obj) throws FieldNotFoundException {
    if (listOnly) {
        listSetMethods(obj);
    } else if (getOnly) {
        getBeanValue(field, obj);
    } else {
        Object val = value;
        logger.info("Modifying '" + name + "'...");
        final ConvertingWrapDynaBean db = new ConvertingWrapDynaBean(obj);
        DynaProperty prop = db.getDynaClass().getDynaProperty(field);
        if (prop == null) {
            throw new FieldNotFoundException("Field '" + field
                    + "' does not exist. Did you use correct case for every character of the field?");
        }
        if (prop.getType().isInterface()) {
            logger.info("Converting value '" + value + "' to type '" + ArrayList.class + "', ");
            // If the value can be converted into an integer, we will use an ArrayList<Integer>
            // Our problem here is that the type of a collection (<Integer>, <String>) is only compile time, it can not be determined in runtime.
            List<Object> arr = new ArrayList<Object>();
            if (StringUtils.isNumeric(value)) {
                logger.info("using Integer value.");
                arr.add(Integer.valueOf(value));
            } else {
                // Make it into an array of String
                logger.info("using String value.");
                arr.add(value);
            }
            val = arr;
        }
        final Object gotValue = db.get(field);
        logger.info("Current value of " + field + " is '" + gotValue + "'.");
        db.set(field, val);
    }
    // return true of we only listed
    return listOnly || getOnly;
}

From source file:org.kohsuke.stapler.jelly.groovy.JellyBuilder.java

private void configureTag(Tag tag, Map attributes) throws JellyException {
    if (tag instanceof DynaTag) {
        DynaTag dynaTag = (DynaTag) tag;

        for (Object o : attributes.entrySet()) {
            Entry entry = (Entry) o;/*from  ww w  .j a va  2  s  . c  o  m*/
            String name = (String) entry.getKey();
            if (name.equals("xmlns"))
                continue; // we'll process this by ourselves

            Object value = getValue(entry, dynaTag.getAttributeType(name));
            dynaTag.setAttribute(name, value);
        }
    } else {
        // treat the tag as a bean
        DynaBean dynaBean = new ConvertingWrapDynaBean(tag);
        for (Object o : attributes.entrySet()) {
            Entry entry = (Entry) o;
            String name = (String) entry.getKey();
            if (name.equals("xmlns"))
                continue; // we'll process this by ourselves

            DynaProperty property = dynaBean.getDynaClass().getDynaProperty(name);
            if (property == null) {
                throw new JellyException("This tag does not understand the '" + name + "' attribute");
            }

            dynaBean.set(name, getValue(entry, property.getType()));
        }
    }
}

From source file:org.xsystem.bpmn2.formats.xml.XMLParser3.java

void parseAttributes(Object base, Element rootElement) {

    ConvertingWrapDynaBean wrap = new ConvertingWrapDynaBean(base);
    DynaClass danaClass = wrap.getDynaClass();
    NamedNodeMap atts = rootElement.getAttributes();
    String defNamespace = rootElement.getNamespaceURI();
    for (int i = 0; i < atts.getLength(); i++) {
        Node currentAttribute = atts.item(i);
        String ns = currentAttribute.getNamespaceURI();
        if (ns == null) {
            ns = defNamespace;/* w  w w. j a  v  a2 s.  c o m*/
        }
        String attrName = currentAttribute.getLocalName();
        //  getNodeName();
        switch (ns) {
        case BPMN2_NS: {
            DynaProperty dynaProperty = danaClass.getDynaProperty(attrName);
            if (dynaProperty != null) {
                Class clazz = dynaProperty.getType();
                if (clazz.isAssignableFrom(Reference.class)) {
                    String value = currentAttribute.getNodeValue();
                    Reference ref = createReference(value);
                    wrap.set(attrName, ref);
                } else if (clazz.isAssignableFrom(QName.class)) {
                    String value = currentAttribute.getNodeValue();
                    QName ret = createQName(value);
                    wrap.set(attrName, ret);
                } else if (clazz.isAssignableFrom(MultiInstanceBehavior.class)) {
                    String value = currentAttribute.getNodeValue();
                    wrap.set(attrName, MultiInstanceBehavior.valueOf(value));
                } else {
                    String value = currentAttribute.getNodeValue();
                    wrap.set(attrName, value);
                }
            }
            break;
        }
        case BPMNDI_NS:
        case DC_NS:
        case DI_NS: {
            DynaProperty dynaProperty = danaClass.getDynaProperty(attrName);
            if (dynaProperty != null) {
                String value = currentAttribute.getNodeValue();
                wrap.set(attrName, value);
            }
            break;
        }
        }
    }
}