KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.oddjob.arooa.handlers;
2
3 import org.oddjob.arooa.ArooaConstants;
4 import org.oddjob.arooa.ArooaContext;
5 import org.oddjob.arooa.ArooaHandler;
6 import org.oddjob.arooa.ArooaRuntime;
7 import org.oddjob.arooa.ComponentProxyResolver;
8 import org.oddjob.arooa.Lifecycle;
9 import org.oddjob.arooa.ObjectFactory;
10 import org.oddjob.arooa.SimpleObjectFactory;
11 import org.oddjob.arooa.reflect.IntrospectionHelper;
12 import org.oddjob.arooa.registry.ComponentRegistry;
13 import org.xml.sax.Attributes JavaDoc;
14 import org.xml.sax.SAXParseException JavaDoc;
15
16 /**
17  * The handler for an Arooa component. These are the
18  * main nodes in the hierarchy.
19  *
20  * @see ArooaHandler
21  * @author Rob Gordon
22  */

23
24 public class DefaultComponentHandler extends TypicalElementHandler {
25
26     public static final String JavaDoc CURRENT_COMPONENT = "curruentcomponent";
27     
28     /**
29      * Handles the start of an element.
30      *
31      * @param uri the namespace URI for the tag
32      * @param tag The name of the element being started.
33      * Will not be <code>null</code>.
34      * @param qname The qualified name of the element.
35      * @param attrs Attributes of the element being started.
36      * Will not be <code>null</code>.
37      * @param context The context that this element is in.
38      *
39      * @exception SAXParseException if this method is not overridden, or in
40      * case of error in an overridden version
41      */

42     public void onStartElement(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
43                                Attributes JavaDoc attrs,
44                                ArooaContext context)
45             throws SAXParseException JavaDoc {
46         Object JavaDoc component = null;
47         AttributeHelper ah = new AttributeHelper(uri, attrs);
48
49         String JavaDoc className = ah.remove("class");
50
51         if (className != null) {
52             try {
53                 component = SimpleObjectFactory.createObjectFromClass(className);
54             }
55             catch (Exception JavaDoc e) {
56                 throw new SAXParseException JavaDoc("Could not create object of class "
57                         + className + " for tag " + tag + " - ", context.getLocator(), e);
58             }
59         }
60         if (component == null) {
61             ObjectFactory factory = (ObjectFactory)context.get(ArooaConstants.COMPONENT_FACTORY);
62             try {
63                 component = factory.createObject(tag);
64             } catch (Exception JavaDoc e) {
65                 throw new SAXParseException JavaDoc("Failed to create component for \"" + qname
66                     + "\" (" + e.getMessage() + ")", context.getLocator(), e);
67             }
68         }
69         if (component==null) {
70             throw new SAXParseException JavaDoc("Unexpected element \"" + qname
71                 + "\" " + tag, context.getLocator());
72         }
73         
74         final ComponentRegistry cr = (ComponentRegistry) context.get(
75                 ArooaConstants.COMPONENT_REGISTRY);
76         final ArooaRuntime wrapper = new ArooaRuntime(component, tag, context);
77         ComponentProxyResolver resolver = (ComponentProxyResolver) context.get(
78                 ArooaConstants.COMPONENT_PROXY_RESOLVER);
79         final Object JavaDoc theComponent = resolver == null ? component
80                 : resolver.proxyFor(component);
81         
82         ah.process(new AttributeHelper.Processor() {
83             public void process(String JavaDoc name, String JavaDoc value) {
84                 if (name.equals("id") ) {
85                     cr.register(value, theComponent);
86                     try {
87                         wrapper.setAttribute(name, value);
88                     }
89                     catch (Exception JavaDoc e) {
90                         // ignore - if a component doesn't
91
// want to know it's id it doens't
92
// have to.
93
}
94                 }
95                 else {
96                     wrapper.setAttribute(name, value);
97                 }
98             }
99         });
100
101         context.set(ArooaConstants.CURRENTLY_CONFIGURING, wrapper);
102         context.set(CURRENT_COMPONENT, theComponent);
103         
104         // no individual rtc? link it to it's parents.
105
if (!Lifecycle.setContext(theComponent, context)) {
106             ArooaContext parentContext = context.getParent();
107             if (parentContext != null) {
108                 ArooaRuntime parentWrapper
109                     = (ArooaRuntime) parentContext.get(
110                         ArooaConstants.CURRENTLY_CONFIGURING);
111                 parentWrapper.link(wrapper);
112             }
113         }
114     }
115     
116         
117     /**
118      * Handles the end of the element.
119      *
120      * @param uri The namespace URI for the element.
121      * @param tag The name of the element.
122      * @param context The current context.
123      */

124     public void onEndElement(String JavaDoc uri, String JavaDoc tag, ArooaContext context) {
125         ArooaContext parentContext = context.getParent();
126         if (parentContext == null) {
127             return;
128         }
129         Object JavaDoc component =
130             context.get(CURRENT_COMPONENT);
131         Object JavaDoc parent = ((ArooaRuntime) parentContext
132                 .get(ArooaConstants.CURRENTLY_CONFIGURING)).getWrappedObject();
133
134         Lifecycle.init(component);
135         
136         if (parent != null) {
137             IntrospectionHelper ih = IntrospectionHelper.getHelper(parent.getClass());
138             ih.storeComponent(parent, component,
139                     (String JavaDoc) context.getParent().get(ArooaConstants.ELEMENT_NAME));
140         }
141     }
142 }
143
144  
Popular Tags