Listing 4: HashtableDocumentBuilder

package mypackage;

import java.util.Hashtable;

/**
 * Implementation of a DocumentBuilder that extracts the data
 * needed to support the getValue method from the Hashtable
 * provided in the constructor.  The field code it accepts
 * must match a key in the Hashtable provided.  Otherwise, 
 * <code>null</code> is used.
 **/
public class HashtableDocumentBuilder extends DocumentBuilder {
    
    Hashtable model;
    
    /**
     * Creates a new instance using the Hashtable parameter as the "data"
     * source, and the template as the document template.
     **/
    public HashtableDocumentBuilder(Hashtable model, String template) {
        super(template);
      this.model = model;
    }    
    
    /**
     * returns String value representing data associated with field code.
     * in this implementation, the field code is the key of an object stored
     * in the Hashtable provided. If no value is returned from the Hashtable
     * from the <code>get()</code> method (using the field code as the key),
     * then <code>null</code> is returned.
     **/
    public String getValue(String fieldCode) {
        String ret = null;
        
        try{
            // attempt to get value from Hashtable
            ret = model.get(fieldCode).toString();
        }
        catch(Exception e){
            // could not get value from Hashtable, use null
            ret = null;   
        }
        finally{
            return ret;   
        }
    }
}
— End of Listing —