Example usage for org.apache.commons.beanutils LazyDynaClass add

List of usage examples for org.apache.commons.beanutils LazyDynaClass add

Introduction

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

Prototype

public void add(String name, Class type) 

Source Link

Document

Add a new dynamic property with the specified data type, but with no restrictions on readability or writeability.

Usage

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

public DynaClass getDynaClass() {
    final LazyDynaClass dynaClass = new LazyDynaClass();
    for (final Map.Entry<String, Class<?>> entry : propertyTypes.entrySet()) {
        dynaClass.add(entry.getKey(), entry.getValue());
    }/*from   w w  w . j a  v a 2s  . c  o m*/
    return dynaClass;
}

From source file:org.apache.commons.validator.example.ValidateExample.java

/**
 * This is the main method that will be called to initialize the Validator, create some sample beans, and
 * run the Validator against them./*  ww  w .  ja  va  2s.  c o m*/
 */
public static void main(String[] args) throws ValidatorException, IOException, SAXException {

    InputStream in = null;
    ValidatorResources resources = null;

    try {

        // Create a new instance of a ValidatorResource, then get a stream
        // handle on the XML file with the actions in it, and initialize the
        // resources from it.  This would normally be done by a servlet
        // run during JSP initialization or some other application-startup
        // routine.
        in = ValidateExample.class.getResourceAsStream("validator-example.xml");
        resources = new ValidatorResources(in);

    } finally {
        // Make sure we close the input stream.
        if (in != null) {
            in.close();
        }
    }

    // Create a test bean to validate against.
    //ValidateBean bean = new ValidateBean();
    LazyDynaClass dynaClass = new LazyDynaClass();
    dynaClass.add("lastName", String.class);
    dynaClass.add("firstName", String.class);
    dynaClass.add("street1", String.class);
    dynaClass.add("city", String.class);
    dynaClass.add("state", String.class);
    dynaClass.add("postalCode", String.class);
    dynaClass.add("age", String.class);
    LazyDynaBean dynaBean = new LazyDynaBean(dynaClass);

    // Create a validator with the ValidateBean actions for the bean
    // we're interested in.
    Validator validator = new Validator(resources, "ValidateBean");

    // Tell the validator which bean to validate against.
    validator.setParameter(Validator.BEAN_PARAM, dynaBean);

    ValidatorResults results = null;

    // Run the validation actions against the bean.  Since all of the properties
    // are null, we expect them all to error out except for street2, which has
    // no validations (it's an optional property)

    results = validator.validate();
    printResults(dynaBean, results, resources);

    // Now set all the required properties, but make the age a non-integer.
    // You'll notice that age will pass the required test, but fail the int
    // test.
    dynaBean.set("lastName", "Tester");
    dynaBean.set("firstName", "John");
    dynaBean.set("street1", "1 Test Street");
    dynaBean.set("city", "Testville");
    dynaBean.set("state", "TE");
    dynaBean.set("postalCode", "12345");
    dynaBean.set("age", "Too Old");
    results = validator.validate();
    printResults(dynaBean, results, resources);

    // Now only report failed fields
    validator.setOnlyReturnErrors(true);
    results = validator.validate();
    printResults(dynaBean, results, resources);

    // Now everything should pass.
    validator.setOnlyReturnErrors(false);
    dynaBean.set("age", "123");
    results = validator.validate();
    printResults(dynaBean, results, resources);
}