KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.oddjob.arooa.handlers;
2
3 import org.oddjob.arooa.ArooaRuntime;
4 import org.oddjob.arooa.ArooaContext;
5 import org.oddjob.arooa.ArooaConstants;
6 import org.oddjob.arooa.Lifecycle;
7 import org.oddjob.arooa.registry.ComponentRegistry;
8 import org.xml.sax.Attributes JavaDoc;
9 import org.xml.sax.SAXParseException JavaDoc;
10
11 /**
12  * Handles the document element. This handler behaves just like
13  * a component handler except that it doesn't create a component -
14  * the root component must have been provided.
15  * <p>
16  * This handler doesn't attempt to add the component to any parent because there
17  * is none. The id attribute and other attributes
18  * are processed as per the ComponentHandler.
19  * <p>
20  * The RuntimeConfigurable wrapper will be set in the rootComponent or
21  * added to a parent.
22  *
23  * @see ArooaHandler.
24  * @author Rob Gordon.
25  */

26 public class DocumentStartHandler extends TypicalElementHandler {
27
28     /** The root object */
29     private final Object JavaDoc rootComponent;
30     /**
31     
32     /**
33      * Constructor.
34      *
35      * @param rootObject The root object which is to be built up.
36      */

37     public DocumentStartHandler(Object JavaDoc rootObject) {
38         if (rootObject == null) {
39             throw new NullPointerException JavaDoc("Root Object can not be null!");
40         }
41         this.rootComponent = rootObject;
42     }
43     
44     /**
45      * Handle the top level element.
46      *
47      * @param uri The namespace uri.
48      * @param name The element tag.
49      * @param qname The element qualified name.
50      * @param attrs The attributes of the element.
51      * @param context The current context.
52      * @return The handler that handles the top level elelement.
53      * @exception SAXParseException if the qualified name is not "project".
54      */

55     public void onStartElement(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
56                                    Attributes JavaDoc attrs,
57                                    ArooaContext context)
58             throws SAXParseException JavaDoc {
59
60         final ComponentRegistry cr = (ComponentRegistry) context.get(
61                 ArooaConstants.COMPONENT_REGISTRY);
62         
63         final ArooaRuntime wrapper = new ArooaRuntime(rootComponent, tag, context);
64             
65         AttributeHelper ah = new AttributeHelper(uri, attrs);
66         ah.process(new AttributeHelper.Processor() {
67             public void process(String JavaDoc name, String JavaDoc value) {
68                 if (name.equals("id") ) {
69                     cr.register(value, rootComponent);
70                     try {
71                         wrapper.setAttribute(name, value);
72                     }
73                     catch (Exception JavaDoc e) {
74                         // ignore - if a component doesn't
75
// want to know it's id it doens't
76
// have to.
77
}
78                 }
79                 else {
80                     wrapper.setAttribute(name, value);
81                 }
82             }
83         });
84         
85         context.set(ArooaConstants.CURRENTLY_CONFIGURING, wrapper);
86         
87         // no individual rtc? link it to it's parents.
88
if (!Lifecycle.setContext(rootComponent, context)) {
89             ArooaRuntime parentWrapper
90                 = (ArooaRuntime) context.getParent().get(
91                         ArooaConstants.CURRENTLY_CONFIGURING);
92             parentWrapper.link(wrapper);
93         }
94     }
95
96     /**
97      * Handles the end of the element.
98      *
99      * @param uri The namespace URI for the element.
100      * @param tag The name of the element.
101      * @param context The current context.
102      */

103     public void onEndElement(String JavaDoc uri, String JavaDoc tag, ArooaContext context) {
104         Lifecycle.init(rootComponent);
105     }
106         
107 }
108
109  
Popular Tags