AdapterFactory produces Node adapters for Java object types. Adapter classes are generally instantiated dynamically via a no-args constructor and populated with their context information via the AdapterNode interface. This factory supports proxying of generic DOM Node trees, allowing arbitrary Node types to be mixed together. You may simply return a Document or Node type as an object property and it will appear as a sub-tree in the XML as you'd expect. See #proxyNode(). Customization of the result XML can be accomplished by providing alternate adapters for Java types. Adapters are associated with Java types through the registerAdapterType() method. For example, since there is no default Date adapter, Date objects will be rendered with the generic Bean introspecting adapter, producing output like:
1910784300105
By extending the StringAdapter and overriding its normal behavior we can create a custom Date formatter:
public static class CustomDateAdapter extends StringAdapter { protected String getStringValue() { Date date = (Date)getPropertyValue(); return DateFormat.getTimeInstance( DateFormat.FULL ).format( date ); } }
Producing output like:
12:02:54 AM CDT
The StringAdapter (which is normally invoked only to adapt String values) is a useful base for these kinds of customizations and can produce structured XML output as well as plain text by setting its parseStringAsXML() property to true. See provided examples.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.