KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > arooa > DesignComponentHandler


1 package org.oddjob.designer.arooa;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4
5 import org.apache.log4j.Logger;
6 import org.oddjob.arooa.ArooaException;
7 import org.oddjob.arooa.ArooaHandler;
8 import org.oddjob.arooa.ArooaContext;
9 import org.oddjob.arooa.ArooaConstants;
10 import org.oddjob.arooa.Lifecycle;
11 import org.oddjob.arooa.ObjectFactory;
12 import org.oddjob.arooa.handlers.XmlHandler;
13 import org.oddjob.arooa.reflect.BeanUtilsHelper;
14 import org.oddjob.arooa.reflect.IntrospectionHelper;
15 import org.oddjob.designer.components.UnknownDC;
16 import org.xml.sax.Attributes JavaDoc;
17 import org.xml.sax.SAXParseException JavaDoc;
18
19 /**
20  * The handler for a DesignComponent. These are the
21  * main nodes in the designer tree.
22  *
23  * @see ArooaHandler
24  * @author Rob Gordon
25  */

26
27 public class DesignComponentHandler extends ArooaHandler {
28     private static final Logger logger = Logger.getLogger(DesignComponentHandler.class);
29
30     private static final String JavaDoc UNKNOWN = "unknown";
31     
32     private static final String JavaDoc NAME_ATTRIBUTE = "nameattribute";
33     
34     private ByteArrayOutputStream JavaDoc xmlOut = new ByteArrayOutputStream JavaDoc();
35     private XmlHandler xmlHandler = new XmlHandler(xmlOut);
36     
37     private DualHandler dualHandler;
38     
39
40     /**
41      * Handles the start of an element.
42      *
43      * @param uri the namespace URI for the tag
44      * @param tag The name of the element being started.
45      * Will not be <code>null</code>.
46      * @param qname The qualified name of the element.
47      * @param attrs Attributes of the element being started.
48      * Will not be <code>null</code>.
49      * @param context The context that this element is in.
50      *
51      * @exception SAXParseException if this method is not overridden, or in
52      * case of error in an overridden version
53      */

54     public void onStartElement(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
55                                Attributes JavaDoc attrs,
56                                ArooaContext context)
57             throws SAXParseException JavaDoc {
58         xmlHandler.onStartElement(uri, tag, qname, attrs, context);
59         context.set(NAME_ATTRIBUTE, attrs.getValue("name"));
60
61         ObjectFactory factory = (ObjectFactory) context.get(ArooaConstants.COMPONENT_FACTORY);
62         Object JavaDoc component = null;
63         try {
64             component = factory.createObject(tag);
65         } catch (Exception JavaDoc e) {
66             throw new SAXParseException JavaDoc("Failed to create object - \"" + qname
67                 + "\"", context.getLocator(), e);
68         }
69         if (component == null) {
70             component = factory.createObject("other");
71         }
72         // components like Ant are also unknown...
73
if (component instanceof UnknownDC) {
74             context.set(UNKNOWN, "true");
75         }
76         
77         IntrospectionHelper ih = IntrospectionHelper.getHelper(component.getClass());
78         
79         if (context.get(UNKNOWN) == null) {
80             try {
81                 HandlerUtils.setAttributes(uri, attrs, component);
82             } catch (Exception JavaDoc e) {
83                 logger.debug("Tag [" + tag + "], failed to set attribute ("
84                         + e.getMessage() + "), switching to unknown.");
85                 context.set(UNKNOWN, "true");
86             }
87         }
88         
89         context.set(ArooaConstants.CURRENTLY_CONFIGURING, component);
90     }
91     
92     /*
93      * (non-Javadoc)
94      * @see org.oddjob.arooa.ArooaHandler#characters(char[], int, int, org.oddjob.arooa.ArooaXMLContext)
95      */

96     public void characters(char[] buf, int start, int count,
97                            ArooaContext context)
98     throws SAXParseException JavaDoc {
99         xmlHandler.characters(buf, start, count, context);
100         
101         Object JavaDoc component = context.get(ArooaConstants.CURRENTLY_CONFIGURING);
102         IntrospectionHelper ih = IntrospectionHelper.getHelper(component.getClass());
103         try {
104             ih.addText(component, new String JavaDoc(buf, start, count));
105         } catch (ArooaException e) {
106             context.set(UNKNOWN, "true");
107         }
108     }
109
110     /*
111      * (non-Javadoc)
112      * @see org.oddjob.arooa.ArooaHandler#onStartChild(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes, org.oddjob.arooa.ArooaXMLContext)
113      */

114     public ArooaHandler onStartChild(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
115                                    Attributes JavaDoc attrs,
116                                    ArooaContext context)
117     throws SAXParseException JavaDoc {
118         ArooaHandler nextXmlHandler = xmlHandler.onStartChild(uri, tag, qname, attrs, context);
119         context.set(ArooaConstants.COMPONENT_HANDLER, new DesignComponentHandler());
120         if (context.getLocal(UNKNOWN) == null) {
121             Object JavaDoc component = context.get(ArooaConstants.CURRENTLY_CONFIGURING);
122             IntrospectionHelper ih = IntrospectionHelper.getHelper(component.getClass());
123             ArooaHandler nextOtherHandler = ih.provideHandler(component, tag, context);
124             dualHandler = new DualHandler(nextXmlHandler, nextOtherHandler);
125             return dualHandler;
126         }
127         else {
128             return nextXmlHandler;
129         }
130     }
131         
132     public void onEndChild(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
133             ArooaContext context)
134     throws SAXParseException JavaDoc {
135         xmlHandler.onEndChild(uri, tag, qname, context);
136         if (dualHandler != null && !dualHandler.isHandler2OK()) {
137             context.set(UNKNOWN, "true");
138         }
139     }
140
141     /*
142      * (non-Javadoc)
143      * @see org.oddjob.arooa.ArooaHandler#onEndElement(java.lang.String, java.lang.String, org.oddjob.arooa.ArooaXMLContext)
144      */

145     public void onEndElement(String JavaDoc uri, String JavaDoc tag, ArooaContext context) {
146         xmlHandler.onEndElement(uri, tag, context);
147         
148         Object JavaDoc component = context.get(ArooaConstants.CURRENTLY_CONFIGURING);
149         if (context.get(UNKNOWN) != null) {
150             DesignIH dih = DesignIH.getHelper(component.getClass());
151             String JavaDoc xml = xmlOut.toString();
152             try {
153                 dih.unknown(component, xml);
154             }
155             catch (NullPointerException JavaDoc e) {
156                 ObjectFactory factory = (ObjectFactory) context.get(ArooaConstants.COMPONENT_FACTORY);
157                 component = factory.createObject("other");
158                 dih = DesignIH.getHelper(component.getClass());
159                 dih.unknown(component, xml);
160             }
161             // assume we have have UnkonwnDC at this point or similar
162
IntrospectionHelper ih = IntrospectionHelper.getHelper(component.getClass());
163             String JavaDoc name = (String JavaDoc) context.getLocal(NAME_ATTRIBUTE);
164             if (name != null) {
165                     BeanUtilsHelper.setProperty(component, "name", name);
166             }
167             BeanUtilsHelper.setProperty(component, "tag", tag);
168         }
169         Lifecycle.init(component);
170         
171         Object JavaDoc parent = context.getParent().get(ArooaConstants.CURRENTLY_CONFIGURING);
172         IntrospectionHelper pih = IntrospectionHelper.getHelper(parent.getClass());
173         pih.storeComponent(parent, component,
174                 (String JavaDoc) context.getParent().get(ArooaConstants.ELEMENT_NAME));
175     }
176 }
177
Popular Tags