|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.beandocument.BeanDocumentBuilder
Builds bean based documents.
This class is used as starting point when constructing new DOM documents from any Java bean. The following example
illustrates the use of the class and provides all system properties to the new document:
Object xmlSource = System.getProperties(); try { BeanDocumentBuilder beanBuilder = new BeanDocumentBuilder(); beanBuilder.setMaxDepth(2); Document xml = beanBuilder.getDocument(xmlSource); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.transform(new DOMSource(xml), new StreamResult(System.out)); } catch (Exception e) { System.err.println("Error during transformation: " + e); e.printStackTrace(); }The result document as serialized XML looks like:
<?xml version="1.0" encoding="UTF-8"?> <Properties className="java.util.Properties"> <propertyNames className="java.util.Hashtable$Enumerator"> <item className="java.lang.String">java.runtime.name</item> </propertyNames> <hashCode className="java.lang.Integer">1436952335</hashCode> <toString className="java.lang.String">{java.runtime.name=Java(TM) 2 Runtime Environment (...)</toString> <size className="java.lang.Integer">54</size> <values className="java.util.Collections$SynchronizedCollection"> <item className="java.lang.String">Java(TM) 2 Runtime Environment, Standard Edition</item> </values> <elements className="java.util.Hashtable$Enumerator"> <item className="java.lang.String">Java(TM) 2 Runtime Environment, Standard Edition</item> </elements> <keys className="java.util.Hashtable$Enumerator"> <item className="java.lang.String">java.runtime.name</item> </keys> <entrySet className="java.util.Collections$SynchronizedSet"> <item className="java.util.Hashtable$Entry"/> </entrySet> <isEmpty className="java.lang.Boolean">false</isEmpty> <keySet className="java.util.Collections$SynchronizedSet"> <item className="java.lang.String">java.runtime.name</item> </keySet> <getClass className="java.lang.Class"/> <element key="java.runtime.name" className="java.lang.String">Java(TM) 2 Runtime Environment, Standard Edition</element> <element key="vendor-url" className="java.lang.String">http://xml.apache.org/xalan-j</element> <element key="sun.boot.library.path" className="java.lang.String">C:\Program Files\Java\j2re1.4.2_05\bin</element> <element key="java.vm.version" className="java.lang.String">1.4.2_05-b04</element> (...) </Properties>Additional mechanisms can be used to influence the behavior of the created documents:
MAX_DEPTH_DEFAULT
. To override this
setting, call the method setMaxDepth(int)
.addIncludeFilter(BeanContentFilter)
and
addExcludeFilter(BeanContentFilter)
addElementHandler(Class, Class)
method to set a custom handler.
This handler class will be instantiated for the defined type. Currently basic handlers for
Number, String, Collections and Maps are implemented.
Field Summary | |
static int |
MAX_DEPTH_DEFAULT
default depth for element generation of bean documents |
Constructor Summary | |
BeanDocumentBuilder()
Creates a new DocumentBuilder instance |
Method Summary | |
void |
addElementHandler(java.lang.Class type,
java.lang.Class handler)
Adds a special element handler class to the element factory |
void |
addExcludeFilter(BeanContentFilter filter)
Adds a filter for evaluating bean element content. |
void |
addIncludeFilter(BeanContentFilter filter)
Adds a filter for evaluating bean element content. |
org.w3c.dom.Document |
getDocument(java.lang.Object bean)
Generates and retrieves a new DOM document instance from the provided bean |
void |
setMaxDepth(int max)
Sets the maximum evaluation depth for bean documents. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int MAX_DEPTH_DEFAULT
Constructor Detail |
public BeanDocumentBuilder()
Method Detail |
public org.w3c.dom.Document getDocument(java.lang.Object bean)
bean
- Object from which the document should be created
java.lang.IllegalArgumentException
- If bean is nullpublic void addElementHandler(java.lang.Class type, java.lang.Class handler)
type
- Class type which need to match to use the specified handlerhandler
- Handler class which should handle the element. This needs to be of type BeanElementpublic void setMaxDepth(int max)
max
- Maximum element depth to generate.MAX_DEPTH_DEFAULT
public void addIncludeFilter(BeanContentFilter filter)
filter
- Filter criteria to addpublic void addExcludeFilter(BeanContentFilter filter)
filter
- Filter criteria to add
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |