KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > handlers > ValueHandler


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.arooa.handlers;
5
6 import org.oddjob.arooa.ArooaRuntime;
7 import org.oddjob.arooa.ArooaContext;
8 import org.oddjob.arooa.ArooaConstants;
9 import org.oddjob.arooa.ObjectFactory;
10 import org.oddjob.arooa.SimpleObjectFactory;
11 import org.xml.sax.Attributes JavaDoc;
12 import org.xml.sax.SAXParseException JavaDoc;
13
14 /**
15  * This handler handles nested values. The values are created either
16  * with the class attribute or from the tag name using the Constants.VALUE_FACTORY.
17  * <p>
18  * The value create is added to the object being configured with a fake element
19  * tag which can be specified in the constructor. Even though the element being
20  * processed is &lt;xyz&gt; the addConfigured'ElementTag'
21  * method of the object currently being configured will be called with the new value.
22  * <p>
23  * Note that a value handler will not attempt to call the add'ElementTag' method on
24  * value creation, so the value objects created with this handler are only available
25  * to the object being configured at runtime, not parse time.
26  *
27  * @author Rob Gordon.
28  */

29 public class ValueHandler extends TypicalElementHandler {
30
31     /** The default tag name */
32     public static final String JavaDoc DEFAULT_ELEMENT_TAG = "value";
33     
34     /** The tag the value is pretending to be added with. */
35     private final String JavaDoc elementTag;
36
37     /** Allows the value factory to be temporarily overriden for the
38      * processing of just this tag. */

39     private ObjectFactory valueFactory;
40     
41     /**
42      * The default consructor.
43      *
44      */

45     public ValueHandler() {
46         this(DEFAULT_ELEMENT_TAG);
47     }
48
49     /**
50      * Constructor for a hanler which
51      * @param elementTag
52      */

53     public ValueHandler(String JavaDoc elementTag) {
54         this.elementTag = elementTag;
55     }
56     
57     /**
58      * Set a value factory. This allows the value factory to
59      * be temporarily overriden for the processing of just this tag.
60      *
61      * @param valueFactory The ObjectFactory for creating the values.
62      */

63     public void setValueFactory(ObjectFactory valueFactory) {
64         this.valueFactory = valueFactory;
65     }
66     
67     /*
68      * (non-Javadoc)
69      * @see org.oddjob.arooa.ArooaHandler#onStartElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes, org.oddjob.arooa.ArooaXMLContext)
70      */

71     public void onStartElement(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
72                                Attributes JavaDoc attrs,
73                                ArooaContext context)
74             throws SAXParseException JavaDoc {
75         Object JavaDoc valueObj = null;
76         AttributeHelper ah = new AttributeHelper(uri, attrs);
77         String JavaDoc className = ah.remove("class");
78         if (className != null) {
79             try {
80                 valueObj = SimpleObjectFactory.createObjectFromClass(className);
81             }
82             catch (Exception JavaDoc e) {
83                 throw new SAXParseException JavaDoc("Could not create object of class"
84                     + className + " for type \"" + tag + "\" (" + e.getMessage() + ")",
85                     context.getLocator(), e);
86             }
87         }
88         if (valueObj == null) {
89             ObjectFactory factory = valueFactory;
90             if (factory == null) {
91                 factory = (ObjectFactory) context.get(ArooaConstants.VALUE_FACTORY);
92             }
93             if (factory == null) {
94                 throw new SAXParseException JavaDoc("No value factory for creating value of type \"" + qname
95                         + "\"", context.getLocator());
96             }
97             try {
98                 valueObj = factory.createObject(tag);
99             } catch (Exception JavaDoc e) {
100                 throw new SAXParseException JavaDoc("Failed to create value for \"" + qname
101                     + "\" (" + e.getMessage() + ")", context.getLocator(), e);
102             }
103         }
104         if (valueObj == null) {
105             throw new SAXParseException JavaDoc("Unexpected type \"" + qname
106                 + "\"", context.getLocator());
107         }
108
109         // create a wrapper for a value tag so that
110
// addConfiguredValue will be called when
111
// the RTC is configured.
112
final ArooaRuntime wrapper = new ArooaRuntime(valueObj, elementTag, context);
113         ah.process(new AttributeHelper.Processor() {
114             /* (non-Javadoc)
115              * @see org.oddjob.arooa.handlers.AttributeHelper.Processor#process(java.lang.String, java.lang.String)
116              */

117             public void process(String JavaDoc name, String JavaDoc value) {
118                 wrapper.setAttribute(name, value);
119             }
120         });
121         
122         ArooaRuntime parentWrapper
123             = (ArooaRuntime) context.get(
124                     ArooaConstants.CURRENTLY_CONFIGURING);
125         parentWrapper.addChild(wrapper);
126         context.set(ArooaConstants.CURRENTLY_CONFIGURING, wrapper);
127     }
128         
129 }
130
Popular Tags