|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.xml.sax.helpers.DefaultHandler
com.acsinet_solutions.util.xml.XMLObjectFactory
public final class XMLObjectFactory
An XML-based Object Factory implementation. Feeds a BlockBuilder instance, and provides an easy way to create hierarchies of objects from an XML file ( there are many approaches that perform this type of task, but this implementation is really simple and convenient for a lot of cases ).
Hierarchies in an XML document are mapped to a Hierarchy of objects. The root object instance ( or class ) is passed to this factory as a parameter in the constructer. Starting from there, reflection and property names are used in the following way:
All attributes of a tag are mapped to standard properties in a bean class. The BlockBuilder.setValue() method is used for this mapping.
All sub elements ( and sub-subelements... ) are mapped to nested objects. A non-standard addElement( Object instance ) method on th current bean is called for these cases. The name of the method is matched to the XML element name, for example <book> element matches addBook( ... ) method. The addBook() method receives an instance which is the type of a new object that will be created and used within the builder for new attributes or subelements to be created there. The type of this object being created may be overriden using the "class" attribute at any level of the XML structure.
Check the source code of BlockBuilderTest for a working example of this.
WARNING! This class is not-thread safe and should be used only once
BlockBuilder,
BlockBuilderTest| Field Summary | |
|---|---|
static java.lang.String |
_TEXT
Text attribute |
| Constructor Summary | |
|---|---|
XMLObjectFactory()
Empty constructor |
|
XMLObjectFactory(Builder builder)
Creata an object builder with an user-provided builder instance |
|
XMLObjectFactory(java.lang.Class defaultRootClass)
|
|
XMLObjectFactory(java.lang.Object rootObject)
|
|
| Method Summary | |
|---|---|
void |
characters(char[] ch,
int start,
int length)
If not empty, they are reported to the builder as an special "_text" attribute ( mapped to a set_text() method ). |
void |
endElement(java.lang.String uri,
java.lang.String local,
java.lang.String raw)
Closes the current element level, and returns the current object context to the parent object. |
java.lang.Object |
getObject(org.xml.sax.InputSource source)
|
java.lang.Object |
getObject(java.io.Reader reader)
|
java.lang.Object |
getObject(java.lang.String file)
|
void |
startElement(java.lang.String uri,
java.lang.String local,
java.lang.String raw,
org.xml.sax.Attributes attrs)
If an element is found in the input XML document, a new level is added to the object structure; the instance of the new object is determined by reflection using the datatype of the argument of the addXXX() method in the current object, or the "class" attribute of the currently found element is used as the FQCN of the object being created. |
| Methods inherited from class org.xml.sax.helpers.DefaultHandler |
|---|
endDocument, endPrefixMapping, error, fatalError, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, setDocumentLocator, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl, warning |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final java.lang.String _TEXT
| Constructor Detail |
|---|
public XMLObjectFactory(Builder builder)
public XMLObjectFactory(java.lang.Object rootObject)
defaultRootClass - A maybe-null instance valuepublic XMLObjectFactory(java.lang.Class defaultRootClass)
defaultRootClass - A maybe-null Class valuepublic XMLObjectFactory()
| Method Detail |
|---|
public final java.lang.Object getObject(java.lang.String file)
throws java.io.IOException,
BuildException
file - A not-null not-empty valid-file String instance
com.acsinet_solutions.cetia4.build.BuildException - Thrown if the class instance could not be obtained
java.io.IOException
BuildException
public final java.lang.Object getObject(java.io.Reader reader)
throws java.io.IOException,
BuildException
reader - A not-null Reader instance
com.acsinet_solutions.cetia4.build.BuildException - Thrown if the class instance could not be obtained
java.io.IOException
BuildException
public final java.lang.Object getObject(org.xml.sax.InputSource source)
throws java.io.IOException,
BuildException
inputSource - A not-null InputSource instance
com.acsinet_solutions.cetia4.build.BuildException - Thrown if the class instance could not be obtained
java.io.IOException
BuildException
public final void startElement(java.lang.String uri,
java.lang.String local,
java.lang.String raw,
org.xml.sax.Attributes attrs)
throws org.xml.sax.SAXException
Any name in the XML file that contains '-' characters is changed to a name in which '-' characters are replaced with '_' to work according to java naming restrictions.
startElement in interface org.xml.sax.ContentHandlerstartElement in class org.xml.sax.helpers.DefaultHandlerorg.xml.sax.SAXExceptionBlockBuilder.enterObject(String,String),
BlockBuilder.setValue(String,Object)
public void characters(char[] ch,
int start,
int length)
throws org.xml.sax.SAXException
characters in interface org.xml.sax.ContentHandlercharacters in class org.xml.sax.helpers.DefaultHandlerorg.xml.sax.SAXExceptionBlockBuilder.setValue(String,Object)
public final void endElement(java.lang.String uri,
java.lang.String local,
java.lang.String raw)
throws org.xml.sax.SAXException
endElement in interface org.xml.sax.ContentHandlerendElement in class org.xml.sax.helpers.DefaultHandlerorg.xml.sax.SAXExceptionBlockBuilder.exitObject()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||