KickJava   Java API By Example, From Geeks To Geeks.

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


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.ComponentRestorer;
8 import org.oddjob.arooa.Lifecycle;
9 import org.oddjob.arooa.registry.ComponentRegistry;
10 import org.xml.sax.Attributes JavaDoc;
11 import org.xml.sax.SAXParseException JavaDoc;
12
13 /**
14  * This handler attempts to load components as serialised objects
15  * from the file system. If the component has an id, this handler
16  * will check the file system, otherwise it delegates to a
17  * delegate handler to handle the component.
18  * <p>
19  * Once the component has been created, runtime attributes and
20  * child elements are set as normal.
21  *
22  * @see ArooaHandler
23  * @author Rob Gordon
24  */

25
26 public class SerializedComponentHandler extends ArooaHandler {
27
28     /** To load the components. */
29     private final ComponentRestorer restorer;
30
31     /** The handler for nodes which aren't persisted and
32      * handling everything but the creation of the component. */

33     private final ArooaHandler delegate;
34
35     /**
36      * Constructor.
37      *
38      * @param logger Logger to use.
39      * @param delegeate The delegate handler.
40      * @param directory The directory to look for files in.
41      *
42      */

43     public SerializedComponentHandler(ArooaHandler delegate,
44             ComponentRestorer componentRestorer) {
45         this.delegate = delegate;
46         this.restorer = componentRestorer;
47     }
48
49     /**
50      * Handles the start of an element.
51      *
52      * @param uri the namespace URI for the tag
53      * @param tag The name of the element being started.
54      * Will not be <code>null</code>.
55      * @param qname The qualified name of the element.
56      * @param attrs Attributes of the element being started.
57      * Will not be <code>null</code>.
58      * @param context The context that this element is in.
59      *
60      * @exception SAXParseException if this method is not overridden, or in
61      * case of error in an overridden version
62      */

63     public void onStartElement(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
64                                Attributes JavaDoc attrs,
65                                ArooaContext context)
66             throws SAXParseException JavaDoc {
67
68         AttributeHelper ah = new AttributeHelper(uri, attrs);
69         String JavaDoc id = ah.remove("id");
70
71         Object JavaDoc component = null;
72         try {
73             if (id == null
74                  || (component = restorer.restore(id))
75                         == null)
76             {
77                 delegate.onStartElement(uri, tag, qname,
78                            attrs, context);
79                 return;
80             }
81         } catch (Exception JavaDoc e) {
82             throw new SAXParseException JavaDoc("Failed to load component for id \""
83                     + id + "\" " + tag, context.getLocator(), e);
84         }
85
86         if (component==null) {
87             throw new SAXParseException JavaDoc("Unexpected element \"" + qname
88                 + "\" " + tag, context.getLocator());
89         }
90         
91         ComponentRegistry cr = (ComponentRegistry) context.get(
92                 ArooaConstants.COMPONENT_REGISTRY);
93         if (id != null) {
94             cr.register(id, component);
95         }
96         
97         final ArooaRuntime wrapper = new ArooaRuntime(component, tag, context);
98         wrapper.setConfigured(true);
99         ah.remove("class");
100         ah.process(new AttributeHelper.Processor() {
101             public void process(String JavaDoc name, String JavaDoc value) {
102                 wrapper.setAttribute(name, value);
103             }
104         });
105
106         context.set(ArooaConstants.CURRENTLY_CONFIGURING, wrapper);
107         context.set(DefaultComponentHandler.CURRENT_COMPONENT, component);
108         
109         // no individual rtc? link it to it's parents.
110
if (!Lifecycle.setContext(component, context)) {
111             ArooaRuntime parentWrapper
112                 = (ArooaRuntime) context.get(
113                         ArooaConstants.CURRENTLY_CONFIGURING);
114             parentWrapper.link(wrapper);
115         }
116     }
117     
118     /**
119      * Adds text to the component, using the wrapper
120      *
121      * @param buf A character array of the text within the element.
122      * Will not be <code>null</code>.
123      * @param start The start element in the array.
124      * @param count The number of characters to read from the array.
125      * @param context The current context.
126      *
127      * @exception SAXParseException if the element doesn't support text
128      *
129      */

130     public void characters(char[] buf, int start, int count,
131                            ArooaContext context)
132     throws SAXParseException JavaDoc {
133         delegate.characters(buf, start, count, context);
134     }
135
136     /**
137      * Handle a child tag.
138      *
139      * @param uri The namespace uri.
140      * @param name The element tag.
141      * @param qname The element qualified name.
142      * @param attrs The attributes of the element.
143      * @param context The current context.
144      * @return The handler that handles this subelement.
145      * @exception SAXParseException if the qualified name is not "project".
146      */

147     public ArooaHandler onStartChild(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
148                                    Attributes JavaDoc attrs,
149                                    ArooaContext context)
150     throws SAXParseException JavaDoc {
151         return delegate.onStartChild(uri, tag, qname, attrs, context);
152     }
153     
154     /**
155      * Handle the end of a child.
156      *
157      * @param uri the namespace uri of the element
158      * @param tag the tag of the element
159      * @param qname the qualified name of the element
160      * @param context the current context
161      * @exception SAXParseException if an error occurs
162      */

163     public void onEndChild(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
164                                  ArooaContext context)
165     throws SAXParseException JavaDoc {
166         delegate.onEndChild(uri, tag, qname, context);
167     }
168     
169     /**
170      * Handles the end of the element.
171      *
172      * @param uri The namespace URI for the element.
173      * @param tag The name of the element.
174      * @param context The current context.
175      */

176     public void onEndElement(String JavaDoc uri, String JavaDoc tag, ArooaContext context) {
177         delegate.onEndElement(uri, tag, context);
178     }
179
180 }
181
182  
Popular Tags