Class AdapterFactory

java.lang.Object
org.apache.struts2.result.xslt.AdapterFactory

public class AdapterFactory extends Object

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:

     <date>
      <date>19</date>
      <day>1</day>
      <hours>0</hours>
      <minutes>7</minutes>
      <month>8</month>
      <seconds>4</seconds>
      <time>1127106424531</time>
      <timezoneOffset>300</timezoneOffset>
      <year>105</year>
     </date>
 

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:

    <date>12:02:54 AM CDT</date>
 

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.

  • Constructor Details

    • AdapterFactory

      public AdapterFactory()
  • Method Details

    • registerAdapterType

      public void registerAdapterType(Class<?> type, Class<?> adapterType)
      Register an adapter type for a Java class type.
      Parameters:
      type - the Java class type which is to be handled by the adapter.
      adapterType - The adapter class, which implements AdapterNode.
    • adaptDocument

      public Document adaptDocument(String propertyName, Object propertyValue)
      Create a top level Document adapter for the specified Java object. The document will have a root element with the specified property name and contain the specified Java object content.
      Parameters:
      propertyName - the name of the root document element
      propertyValue - the property value
      Returns:
      the document object
    • adaptNode

      public Node adaptNode(AdapterNode parent, String propertyName, Object value)
      Create an Node adapter for a child element. Note that the parent of the created node must be an AdapterNode, however the child node itself may be any type of Node.
      Parameters:
      parent - the parent adapter node
      propertyName - the name of th property
      value - the value
      Returns:
      a node
      See Also:
    • proxyNode

      public Node proxyNode(AdapterNode parent, Node node)

      Construct a proxy adapter for a value that is an existing DOM Node. This allows arbitrary DOM Node trees to be mixed in with our results. The proxied nodes are read-only and currently support only limited types of Nodes including Element, Text, and Attributes. (Other Node types may be ignored by the proxy and not appear in the result tree).

      NameSpaces are not yet supported.

      This method is primarily for use by the adapter node classes.

      Parameters:
      parent - parent adapter node
      node - node
      Returns:
      proxy node
    • proxyNamedNodeMap

      public NamedNodeMap proxyNamedNodeMap(AdapterNode parent, NamedNodeMap nnm)
    • adaptNullValue

      public Node adaptNullValue(AdapterNode parent, String propertyName)
      Create an appropriate adapter for a null value.
      Parameters:
      parent - parent adapter node
      propertyName - the property name
      Returns:
      the new node
    • getAdapterForValue

      public Class<?> getAdapterForValue(Object value)