|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objecteu.beesoft.gaia.util.ObjectBuilderFactory
public class ObjectBuilderFactory
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 |
---|
public ObjectBuilderFactory()
Method Detail |
---|
public void registerBuilderClass(java.lang.String tag, java.lang.Class<? extends ObjectBuilder<?>> builderClass)
tag
and
builder class
.
tag
- - a XML element tagbuilderClass
- - a class of builder, that should be instantiated when this
factory hits a XML element with given tagpublic java.util.Map<java.lang.String,java.lang.Class<? extends ObjectBuilder<?>>> getBuilderByTagMap()
registerBuilderClass(String, Class)
method.
public java.util.Map<java.lang.String,ObjectBuilder<?>> getBuilderByIdMap()
public ObjectBuilder<?> getRootBuilder()
public ObjectBuilder<?> getBuilder(java.lang.String id)
id
- - identifier of requested builder instance
java.lang.RuntimeException
- if there is no builder instance with given idpublic ObjectBuilder<?> build(java.io.InputStream inputStream)
inputStream
- - input stream with XML to parse
java.lang.RuntimeException
- if some error occurred
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |