KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.oddjob.designer.arooa;
2
3 import org.oddjob.arooa.ArooaHandler;
4 import org.oddjob.arooa.ArooaContext;
5 import org.oddjob.arooa.ArooaConstants;
6 import org.oddjob.arooa.reflect.BeanUtilsHelper;
7 import org.oddjob.arooa.reflect.IntrospectionHelper;
8 import org.xml.sax.Attributes JavaDoc;
9 import org.xml.sax.SAXParseException JavaDoc;
10
11 /**
12  * The handler for an Arooa type. This will be used for
13  * inline attributes.
14  *
15  * @see ArooaHandler
16  * @author Rob Gordon
17  */

18
19 public class DesignValueHandler extends ArooaHandler {
20
21     /**
22      * Handles the start of an element.
23      *
24      * @param uri the namespace URI for the tag
25      * @param tag The name of the element being started.
26      * Will not be <code>null</code>.
27      * @param qname The qualified name of the element.
28      * @param attrs Attributes of the element being started.
29      * Will not be <code>null</code>.
30      * @param context The context that this element is in.
31      *
32      * @exception SAXParseException if this method is not overridden, or in
33      * case of error in an overridden version
34      */

35     public void onStartElement(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
36                                Attributes JavaDoc attrs,
37                                ArooaContext context)
38     throws SAXParseException JavaDoc {
39
40         Object JavaDoc valueObj = null;
41         
42         Object JavaDoc parent = context.get(ArooaConstants.CURRENTLY_CONFIGURING);
43         IntrospectionHelper pih = IntrospectionHelper.getHelper(parent.getClass());
44         Class JavaDoc attributeClass = pih.getAttributeType(tag);
45         try {
46             valueObj = attributeClass.newInstance();
47         } catch (Exception JavaDoc e) {
48             throw new SAXParseException JavaDoc("No way to create inline attribute class [" + attributeClass
49                 + "] at element [" + qname + "]", context.getLocator());
50         }
51
52         IntrospectionHelper ih = IntrospectionHelper.getHelper(valueObj.getClass());
53
54         HandlerUtils.setAttributes(uri, attrs, valueObj);
55         
56         context.set(ArooaConstants.CURRENTLY_CONFIGURING, valueObj);
57     }
58     
59     /**
60      * Adds text to the component, using the wrapper
61      *
62      * @param buf A character array of the text within the element.
63      * Will not be <code>null</code>.
64      * @param start The start element in the array.
65      * @param count The number of characters to read from the array.
66      * @param context The current context.
67      *
68      * @exception SAXParseException if the element doesn't support text
69      *
70      */

71     public void characters(char[] buf, int start, int count,
72                            ArooaContext context)
73         throws SAXParseException JavaDoc {
74         Object JavaDoc obj
75             = context.get(ArooaConstants.CURRENTLY_CONFIGURING);
76         IntrospectionHelper ih = IntrospectionHelper.getHelper(obj.getClass());
77         ih.addText(obj, new String JavaDoc(buf, start, count));
78     }
79
80     /**
81      * Handle a child tag.
82      *
83      * @param uri The namespace uri.
84      * @param name The element tag.
85      * @param qname The element qualified name.
86      * @param attrs The attributes of the element.
87      * @param context The current context.
88      * @return The handler that handles this subelement.
89      * @exception SAXParseException if the qualified name is not "project".
90      */

91     public ArooaHandler onStartChild(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
92                                    Attributes JavaDoc attrs,
93                                    ArooaContext context)
94     throws SAXParseException JavaDoc {
95         Object JavaDoc object = context.get(
96                     ArooaConstants.CURRENTLY_CONFIGURING);
97         
98         IntrospectionHelper ih = IntrospectionHelper.getHelper(object.getClass());
99         return ih.provideHandler(object, tag, context);
100     }
101         
102     public void onEndElement(String JavaDoc uri, String JavaDoc tag,
103             ArooaContext context) {
104
105         Object JavaDoc valueObj = context.get(
106                     ArooaConstants.CURRENTLY_CONFIGURING);
107         Object JavaDoc parent = context.getParent().get(
108                     ArooaConstants.CURRENTLY_CONFIGURING);
109
110         BeanUtilsHelper.setProperty(parent, tag, valueObj);
111         
112     }
113 }
114
115  
Popular Tags