net.beandocument
Class BeanDocumentBuilder

java.lang.Object
  extended bynet.beandocument.BeanDocumentBuilder

public class BeanDocumentBuilder
extends java.lang.Object

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:

Author:
Jens Scheffler

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

MAX_DEPTH_DEFAULT

public static final int MAX_DEPTH_DEFAULT
default depth for element generation of bean documents

See Also:
Constant Field Values
Constructor Detail

BeanDocumentBuilder

public BeanDocumentBuilder()
Creates a new DocumentBuilder instance

Method Detail

getDocument

public org.w3c.dom.Document getDocument(java.lang.Object bean)
Generates and retrieves a new DOM document instance from the provided bean

Parameters:
bean - Object from which the document should be created
Returns:
DOM Document instance of the bean
Throws:
java.lang.IllegalArgumentException - If bean is null

addElementHandler

public void addElementHandler(java.lang.Class type,
                              java.lang.Class handler)
Adds a special element handler class to the element factory

Parameters:
type - Class type which need to match to use the specified handler
handler - Handler class which should handle the element. This needs to be of type BeanElement

setMaxDepth

public void setMaxDepth(int max)
Sets the maximum evaluation depth for bean documents. If this option is not set, the default depth of MAX_DEPTH_DEFAULT will be applied for each document which is going to be generated. This setting is required to prevent infinite recursion in use with transformer engines.

Parameters:
max - Maximum element depth to generate.
See Also:
MAX_DEPTH_DEFAULT

addIncludeFilter

public void addIncludeFilter(BeanContentFilter filter)
Adds a filter for evaluating bean element content. Only elements which match to the given filter criteria will be visible from the created document

Parameters:
filter - Filter criteria to add

addExcludeFilter

public void addExcludeFilter(BeanContentFilter filter)
Adds a filter for evaluating bean element content. Only elements which do not match to the given filter criteria will be visible from the created document

Parameters:
filter - Filter criteria to add