eu.beesoft.gaia.util
Class ObjectBuilderFactory

java.lang.Object
  extended by eu.beesoft.gaia.util.ObjectBuilderFactory

public class ObjectBuilderFactory
extends java.lang.Object

This factory is designed to reading a XML stream of descriptors and to create an appropriate ObjectBuilder instance for each parsed element. Then it manages initializing of builders and creating and initializing objects in builders. There is one or collection of objects created and initialized at the end of this process.

This implementation is abstract enough to let you free to design the form of XML (tags for elements, attributes, ...) and to program your own ObjectBuilder implementations. There is one implementation for building Swing components from XML file in package eu.beesoft.gaia.swing.builder.

Regardless of the high abstract level of the basic implementation, there are a few XML element attributes supported by ObjectBuilderFactory and ObjectBuilder:

The attributes above are reserved.

Let's take a look on some example. You can design this XML file:

 <person_collection>
        <person name="John" city="London" />
        <person name="Mary" city="New York" />
 </person_collection>
 

It describes data of two people. You have your class Person with properties "name" and "city". And you want to load and create from XML each instances of Person and get it in collection. To do this you need to program two builders: one for collection and one for Person. Here is the code for PersonCollectionBuilder:

 public class PersonCollectionBuilder extends ObjectBuilder<List<Person>> {
 
        protected List<Person> createObject () {
                return new ArrayList<Person> ();
        }
 
        protected void addChild (ObjectBuilder<?> builder) {
                Person person = (Person) builder.getObject ();
                List<Person> collection = getObject ();
                collection.add (person);
        }
 }
 

And here is the code for the PersonBuilder class:

 public class PersonBuilder extends ObjectBuilder<Person> {
 
        protected Person createObject () {
                return new Person ();
        }
 
        protected void initName (String name) {
                Person person = getObject ();
                person.setName (name);
        }
 
        protected void initCity (String name) {
                Person person = getObject ();
                person.setCity (name);
        }
 }
 

For example, you can design this XML file:

 ObjectBuilderFactory factory = new ObjectBuilderFactory ();
 factory.registerBuilderClass ("person_collection", PersonCollectionBuilder.class);
 factory.registerBuilderClass ("person", PersonBuilder.class);
 factory.build (new FileInputStream (".....")); // input stream from your XML
 PersonCollectionBuilder rootBuilder = (PersonCollectionBuilder) factory.getRootBuilder ();
 List<Person> objects = rootBuilder.getObject ();
 


Constructor Summary
ObjectBuilderFactory()
          Creates a new builder factory.
 
Method Summary
 ObjectBuilder<?> build(java.io.InputStream inputStream)
          Starts objects build process - loads given XML, creates appropriate builders and objects in them.
 ObjectBuilder<?> getBuilder(java.lang.String id)
          Returns builder instance with given id (identifier).
 java.util.Map<java.lang.String,ObjectBuilder<?>> getBuilderByIdMap()
          Returns map of all {id : builder instance} pairs.
 java.util.Map<java.lang.String,java.lang.Class<? extends ObjectBuilder<?>>> getBuilderByTagMap()
          Returns map of all {tag : builder class} pairs registered by registerBuilderClass(String, Class) method.
 ObjectBuilder<?> getRootBuilder()
          Returns root builder instance (top of builder hierarchy).
 void registerBuilderClass(java.lang.String tag, java.lang.Class<? extends ObjectBuilder<?>> builderClass)
          Stores a relationship between given tag and builder class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectBuilderFactory

public ObjectBuilderFactory()
Creates a new builder factory.

Method Detail

registerBuilderClass

public void registerBuilderClass(java.lang.String tag,
                                 java.lang.Class<? extends ObjectBuilder<?>> builderClass)
Stores a relationship between given tag and builder class.

Parameters:
tag - - a XML element tag
builderClass - - a class of builder, that should be instantiated when this factory hits a XML element with given tag

getBuilderByTagMap

public java.util.Map<java.lang.String,java.lang.Class<? extends ObjectBuilder<?>>> getBuilderByTagMap()
Returns map of all {tag : builder class} pairs registered by registerBuilderClass(String, Class) method.

Returns:
map of all relationships between builder classes and XML element tags.

getBuilderByIdMap

public java.util.Map<java.lang.String,ObjectBuilder<?>> getBuilderByIdMap()
Returns map of all {id : builder instance} pairs.

Returns:
map of all {id : builder instance} pairs.

getRootBuilder

public ObjectBuilder<?> getRootBuilder()
Returns root builder instance (top of builder hierarchy).

Returns:
root builder

getBuilder

public ObjectBuilder<?> getBuilder(java.lang.String id)
Returns builder instance with given id (identifier).

Parameters:
id - - identifier of requested builder instance
Returns:
builder with given id
Throws:
java.lang.RuntimeException - if there is no builder instance with given id

build

public ObjectBuilder<?> build(java.io.InputStream inputStream)
Starts objects build process - loads given XML, creates appropriate builders and objects in them. Returns root builder of the builders hierarchy (the builder according to the root XML element).

Parameters:
inputStream - - input stream with XML to parse
Returns:
root object builder
Throws:
java.lang.RuntimeException - if some error occurred