com.marringtons.object
Class ObjectScraper

java.lang.Object
  extended bycom.marringtons.object.ObjectScraper

public class ObjectScraper
extends Object

Objects can be scraped from or to a pojo/dao using name/value string pairs contained in a Properties objectas required for loading property files and HTML forms into the Java name space.

  // Normally loaded from a web post or similar user imput request
  class TestObject { integer anInteger; InnerObject innerObject; }
  TestObject testObject;
  properties.set( "anInteger", "19582003");
  properties.set( "innerObject.anotherString", "1of2");
  Messages problems = ObjectScraper.fromProperties( testObject, properties, null);
  assertTrue( problems.none());
  assertTrue( testObject.anInteger == 19582003);
  
  // and the reverse is loading a name/value pair map with values from a pojo or DAO
  properties = new Properties();
  problems = ObjectScraper.toProperties( properties, null, testObject, null);
  assertTrue( properties.get( "anInteger").equals( "19582003");
  assertTrue( properties.get( "innerObject.anotherString").equals( "1of2");
  
  // or extract a subset of the object by specifying inclusions
  problems = ObjectScraper.toProperties( properties, "anInteger,innerObject.anotherString", testObject, null);
  
 
When scraping name/value pairs into an Object, a Translator object can be provided to special translations of required fields or types. The same facility is available for getting name/value pairs from an object by subclassing Formatter.

Author:
Paul Marrington
See Also:
Formatter, Translator

Method Summary
static Messages fromProperties(DAO dao, Properties properties, Translator translator)
          Scrape string data in Properties format into the a DAO using an explicit translator.
static Messages fromProperties(Object pojo, Properties properties, Translator translator)
          Scrape string data in Properties format into the a POJO.
static Messages toProperties(Properties properties, String inclusions, DAO dao, Formatter formatter)
          Convert a Java Object to a name/value pair set of string properties.
static Messages toProperties(Properties properties, String inclusions, Object pojo, Formatter formatter)
          Convert a Java Object to a name/value pair set of string properties.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

fromProperties

public static Messages fromProperties(DAO dao,
                                      Properties properties,
                                      Translator translator)
Scrape string data in Properties format into the a DAO using an explicit translator. Keys in the properties are compared with names of public data in the DAO - matches are moved from the properties into the internal data. This version uses the translator inside the POJO or a default translator.

Parameters:
dao - object to hold data scraped from properties
properties - name/value pairs containing data to be transferred to the pojo.
translator - to use for object translation for text to internal format.
Returns:
a list of problems keyed on the property name (empty if there are no problems)

fromProperties

public static Messages fromProperties(Object pojo,
                                      Properties properties,
                                      Translator translator)
Scrape string data in Properties format into the a POJO. Keys in the properties are compared with names of public data in the DAO - matches are moved from the properties into the internal data.

Parameters:
pojo - object to hold data scraped from properties
properties - name/value pairs containing data to be transferred to the pojo.
translator - to translate 'difficult' properties to they DAO form
Returns:
a list of problems keyed on the property name (empty if there are no problems)

toProperties

public static Messages toProperties(Properties properties,
                                    String inclusions,
                                    Object pojo,
                                    Formatter formatter)
Convert a Java Object to a name/value pair set of string properties. Used to massage internal data for display to the user.

Parameters:
properties - pre-existing properties to add.
inclusions - CSV list of properties to include - or null for all.
pojo - Object to extract properties from.
formatter - for unusual formatting requirements (null for standard formatting)
Returns:
a list of problems keyed on the property name (empty if there are no problems)
See Also:
Formatter

toProperties

public static Messages toProperties(Properties properties,
                                    String inclusions,
                                    DAO dao,
                                    Formatter formatter)
Convert a Java Object to a name/value pair set of string properties. Used to massage internal data for display to the user.

Parameters:
properties - pre-existing properties to add.
inclusions - CSV list of properties to include - or null for all.
dao - Object to extract properties from.
formatter - for unusual formatting requirements (null for standard formatting)
Returns:
a list of problems keyed on the property name (empty if there are no problems)
See Also:
Formatter


Copyright © 2005 Paul Marrington http://library.marringtons.com