KickJava   Java API By Example, From Geeks To Geeks.

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


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.PropertyProxyResolver;
8 import org.oddjob.arooa.SimpleObjectFactory;
9 import org.xml.sax.Attributes JavaDoc;
10 import org.xml.sax.SAXParseException JavaDoc;
11
12 /**
13  * The handler for a propert who's definition is inline. If a class MyJob
14  * has a <code>setXyz(MyType value)</code> method then for this xml
15  *
16  * <pre>
17  * &lt;myjob&gt;
18  * &lt;xyz&gt;
19  * &lt;extra value="stuff"/&gt;
20  * &lt;/xyz&gt;
21  * &lt;/myjob&gt;
22  * </pre>
23  * This handler will handle the xyz element. It will create an appropriate
24  * proxy type for MyType. The Proxy will provide a handler by introspection
25  * following the usual rules to process the extra element.
26  *
27  * @see ArooaHandler
28  * @author Rob Gordon
29  */

30
31 public class NestedPropertyHandler extends TypicalElementHandler {
32
33     /**
34      * Handles the start of an element.
35      *
36      * @param uri the namespace URI for the tag
37      * @param tag The name of the element being started.
38      * Will not be <code>null</code>.
39      * @param qname The qualified name of the element.
40      * @param attrs Attributes of the element being started.
41      * Will not be <code>null</code>.
42      * @param context The context that this element is in.
43      *
44      * @exception SAXParseException if this method is not overridden, or in
45      * case of error in an overridden version
46      */

47     public void onStartElement(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
48                                Attributes JavaDoc attrs,
49                                ArooaContext context)
50             throws SAXParseException JavaDoc {
51         Object JavaDoc valueObj = null;
52         AttributeHelper ah = new AttributeHelper(uri, attrs);
53         
54         String JavaDoc className = ah.remove("class");
55         // if a class attribute is specified then use it to create the value.
56
if (className != null) {
57             try {
58                 valueObj = SimpleObjectFactory.createObjectFromClass(className);
59             }
60             catch (Exception JavaDoc e) {
61                 throw new SAXParseException JavaDoc("Could not create object of class"
62                         + className + " for type \"" + tag + "\" ("
63                         + e.getMessage() + ")", context.getLocator(), e);
64             }
65         }
66         
67         ArooaRuntime parentWrapper =
68                 (ArooaRuntime) context.get(
69                         ArooaConstants.CURRENTLY_CONFIGURING);
70         Object JavaDoc parent = parentWrapper.getWrappedObject();
71         
72         if (valueObj == null) {
73             PropertyProxyResolver pr = (PropertyProxyResolver) context.get(ArooaConstants.PROPERTY_PROXY_RESOLVER);
74             if (pr == null) {
75                 throw new IllegalStateException JavaDoc("PropertyProxyResolver is missing!");
76             }
77             try {
78                 valueObj = pr.proxyFor(parent, tag);
79             } catch (Exception JavaDoc e) {
80                     throw new SAXParseException JavaDoc("Failed to create value for \"" + qname
81                             + "\" (" + e.getMessage() + ")", context.getLocator(), e);
82             }
83         }
84         if (valueObj == null) {
85             throw new SAXParseException JavaDoc("There is no way to configure the attribute \"" + qname
86                 + "\" as an inline element.", context.getLocator());
87         }
88
89         final ArooaRuntime wrapper = new ArooaRuntime(valueObj, tag, context);
90         ah.process(new AttributeHelper.Processor() {
91             public void process(String JavaDoc name, String JavaDoc value) {
92                 wrapper.setAttribute(name, value);
93             }
94         });
95         
96         parentWrapper.setAttribute(
97                 (String JavaDoc)context.getParent().get(ArooaConstants.ELEMENT_NAME), wrapper);
98         
99         context.set(ArooaConstants.CURRENTLY_CONFIGURING, wrapper);
100     }
101     
102         
103 }
104
105  
Popular Tags