List of usage examples for org.apache.commons.beanutils LazyDynaClass LazyDynaClass
public LazyDynaClass()
From source file:kenh.xscript.database.beans.ResultSetBean.java
/** * Use result set to initial a bean./*from w w w. j av a2 s .c o m*/ * * @param rs * @param includeFieldName * @throws SQLException * @throws IllegalAccessException * @throws InstantiationException */ public ResultSetBean(ResultSet rs, boolean includeFieldName) throws SQLException, IllegalAccessException, InstantiationException { include_field_name = includeFieldName; LazyDynaClass beanClass = new LazyDynaClass(); ResultSetMetaData m = rs.getMetaData(); for (int i = 1; i <= m.getColumnCount(); i++) { Column c = new Column(); try { c.catalogName = m.getCatalogName(i); } catch (SQLException e) { } try { c.className = m.getColumnClassName(i); } catch (SQLException e) { } try { c.displaySize = m.getColumnDisplaySize(i); } catch (SQLException e) { } try { c.label = m.getColumnLabel(i); } catch (SQLException e) { } try { c.name = m.getColumnName(i); } catch (SQLException e) { } try { c.type = m.getColumnType(i); } catch (SQLException e) { } try { c.typeName = m.getColumnTypeName(i); } catch (SQLException e) { } try { c.precision = m.getPrecision(i); } catch (SQLException e) { } try { c.scale = m.getScale(i); } catch (SQLException e) { } try { c.schemaName = m.getSchemaName(i); } catch (SQLException e) { } try { c.tableName = m.getTableName(i); } catch (SQLException e) { } beanClass.add(m.getColumnLabel(i).toLowerCase()); beanClass.add("" + i); cols.add(c); } DynaBean colBean = beanClass.newInstance(); int i = 1; for (Column col : cols) { String field = col.getLabel().toLowerCase(); colBean.set(field, col.getLabel()); colBean.set("" + i, col.getLabel()); i++; } if (include_field_name) rows.add(colBean); while (rs.next()) { DynaBean bean = beanClass.newInstance(); i = 1; for (Column c : cols) { String field = c.getLabel().toLowerCase(); Object obj = rs.getObject(field); bean.set(field, obj); bean.set("" + i, obj); i++; } rows.add(bean); } }
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. jav a 2 s. 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.//from w w w. j ava 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); }