KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.oddjob.arooa.handlers;
2
3 import org.oddjob.arooa.ArooaHandler;
4 import org.oddjob.arooa.ArooaContext;
5 import org.oddjob.arooa.ArooaConstants;
6 import org.oddjob.arooa.ArooaRuntime;
7 import org.oddjob.arooa.ObjectFactory;
8 import org.oddjob.arooa.PropertyProxyResolver;
9 import org.oddjob.arooa.RuntimeConfiguration;
10 import org.oddjob.arooa.SimpleObjectFactory;
11 import org.oddjob.arooa.reflect.IntrospectionHelper;
12 import org.xml.sax.Attributes JavaDoc;
13 import org.xml.sax.SAXParseException JavaDoc;
14
15 /**
16  * The handler for an <code>setXyz(String name, SomeType value)</code> methods.
17  * <p>
18  * If MyJob has a <code>setXyz(String name, String value)</code> method
19  * then for this xml:
20  *
21  * <pre>
22  * &lt;myjob&gt;
23  * &lt;xyz&gt;
24  * &lt;string name="fred" value="stuff"/&gt;
25  * &lt;/xyz&gt;
26  * &lt;/myjob&gt;
27  * </pre>
28  *
29  * This handler is called on the string tag and is responsible for creating the string
30  * value and processing it's attributes. It relies on an ELEMENT_NAME of xyz in the
31  * context to set the correct method on the parent myjob wrapper.
32  *
33  * @see ArooaHandler
34  * @author Rob Gordon
35  */

36
37 public class MappedPropertyHandler extends ArooaHandler {
38
39     /**
40      * Handles the start of an element.
41      *
42      * @param uri the namespace URI for the tag
43      * @param tag The name of the element being started.
44      * Will not be <code>null</code>.
45      * @param qname The qualified name of the element.
46      * @param attrs Attributes of the element being started.
47      * Will not be <code>null</code>.
48      * @param context The context that this element is in.
49      *
50      * @exception SAXParseException if this method is not overridden, or in
51      * case of error in an overridden version
52      */

53     public void onStartElement(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
54                                Attributes JavaDoc attrs,
55                                ArooaContext context)
56             throws SAXParseException JavaDoc {
57         Object JavaDoc valueObj = null;
58         AttributeHelper ah = new AttributeHelper(uri, attrs);
59         
60         String JavaDoc className = ah.remove("class");
61         
62         // if a class attribute is specified then use it to create the value.
63
if (className != null) {
64             try {
65                 valueObj = SimpleObjectFactory.createObjectFromClass(className);
66             }
67             catch (Exception JavaDoc e) {
68                 throw new SAXParseException JavaDoc("Could not create object of class"
69                         + className + " for type \"" + tag + "\" ("
70                         + e.getMessage() + ")", context.getLocator(), e);
71             }
72         }
73         
74         if (valueObj == null) {
75             ObjectFactory factory = (ObjectFactory)context.get(ArooaConstants.VALUE_FACTORY);
76             try {
77                     valueObj = factory.createObject(tag);
78             } catch (Exception JavaDoc e) {
79                 // ignore and keep going
80
}
81         }
82         
83         ArooaRuntime parentWrapper =
84             (ArooaRuntime) context.get(
85                     ArooaConstants.CURRENTLY_CONFIGURING);
86         Object JavaDoc parent = parentWrapper.getWrappedObject();
87         
88         if (valueObj == null) {
89             PropertyProxyResolver pr = (PropertyProxyResolver) context.get(ArooaConstants.PROPERTY_PROXY_RESOLVER);
90             if (pr == null) {
91                 throw new IllegalStateException JavaDoc("Type Manager is missing!");
92             }
93             try {
94                 valueObj = pr.proxyFor(parent,
95                         (String JavaDoc) context.getParent().get(ArooaConstants.ELEMENT_NAME));
96             } catch (Exception JavaDoc e) {
97                 throw new SAXParseException JavaDoc("Failed to create value for \"" + qname
98                         + "\" (" + e.getMessage() + ")", context.getLocator(), e);
99             }
100         }
101         
102         if (valueObj == null) {
103             throw new SAXParseException JavaDoc("Unexpected type \"" + qname
104                 + "\"", context.getLocator());
105         }
106
107         final ArooaRuntime wrapper = new ArooaRuntime(valueObj, tag, context);
108         String JavaDoc name = ah.remove("name");
109         if (name != null) {
110             try {
111                 wrapper.setAttribute("name", name);
112             }
113             catch (Exception JavaDoc e) {
114                 // knowing your own name is optional.
115
}
116         }
117         ah.process(new AttributeHelper.Processor() {
118             public void process(String JavaDoc name, String JavaDoc value) {
119                 wrapper.setAttribute(name, value);
120             }
121         });
122         
123         parentWrapper.setMappedProperty(
124                 (String JavaDoc)context.getParent().get(ArooaConstants.ELEMENT_NAME), name, wrapper);
125         
126         context.set(ArooaConstants.CURRENTLY_CONFIGURING, wrapper);
127     }
128     
129     /**
130      * Handle a child tag.
131      *
132      * @param uri The namespace uri.
133      * @param name The element tag.
134      * @param qname The element qualified name.
135      * @param attrs The attributes of the element.
136      * @param context The current context.
137      * @return The handler that handles this subelement.
138      * @exception SAXParseException if the qualified name is not "project".
139      */

140     public ArooaHandler onStartChild(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
141                                    Attributes JavaDoc attrs,
142                                    ArooaContext context)
143             throws SAXParseException JavaDoc {
144         RuntimeConfiguration currentWrapper
145             = (RuntimeConfiguration ) context.get(
146                     ArooaConstants.CURRENTLY_CONFIGURING);
147         Object JavaDoc object = currentWrapper.getWrappedObject();
148         
149         IntrospectionHelper ih = IntrospectionHelper.getHelper(object.getClass());
150         return ih.provideHandler(object, tag, context);
151     }
152         
153 }
154
155  
Popular Tags