KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.oddjob.designer.arooa;
2
3 import org.oddjob.arooa.ArooaException;
4 import org.oddjob.arooa.ArooaHandler;
5 import org.oddjob.arooa.ArooaContext;
6 import org.oddjob.arooa.ArooaConstants;
7 import org.oddjob.arooa.reflect.IntrospectionHelper;
8 import org.oddjob.designer.model.DesignAdult;
9 import org.oddjob.designer.model.DesignElementType;
10 import org.xml.sax.Attributes JavaDoc;
11 import org.xml.sax.SAXParseException JavaDoc;
12
13
14 /**
15  * Handler for a nested element.
16  */

17 public class DesignElementHandler extends ArooaHandler {
18         
19     /*
20      * (non-Javadoc)
21      * @see org.oddjob.arooa.ArooaHandler#onStartElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes, org.oddjob.arooa.ArooaXMLContext)
22      */

23     public void onStartElement(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
24                                Attributes JavaDoc attrs,
25                                ArooaContext context)
26     throws SAXParseException JavaDoc {
27         
28         Object JavaDoc parent = context.getParent().get(
29                 ArooaConstants.CURRENTLY_CONFIGURING);
30         if (parent instanceof DesignAdult) {
31             Object JavaDoc element = ((DesignAdult)parent).createType(tag);
32             if (element == null) {
33                 throw new ArooaException("Class [" + parent.getClass()
34                         + "] doesn't support the element [" + tag + "]");
35             }
36             HandlerUtils.setAttributes(uri, attrs, element);
37             context.set(ArooaConstants.CURRENTLY_CONFIGURING, element);
38         }
39         else {
40             throw new ArooaException("Parent of element [" + tag
41                     + "], class [" + parent.getClass()
42                     + "] must be an instance of DesignAdult");
43         }
44     }
45
46     /*
47      * (non-Javadoc)
48      * @see org.oddjob.arooa.ArooaHandler#characters(char[], int, int, org.oddjob.arooa.ArooaXMLContext)
49      */

50     public void characters(char[] buf, int start, int count,
51                            ArooaContext context)
52     throws SAXParseException JavaDoc {
53         Object JavaDoc object = context.get(ArooaConstants.CURRENTLY_CONFIGURING);
54         IntrospectionHelper ih = IntrospectionHelper.getHelper(object.getClass());
55         ih.addText(object, new String JavaDoc(buf, start, count));
56     }
57
58     /*
59      * (non-Javadoc)
60      * @see org.oddjob.arooa.ArooaHandler#onStartChild(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes, org.oddjob.arooa.ArooaXMLContext)
61      */

62     public ArooaHandler onStartChild(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
63                                    Attributes JavaDoc attrs,
64                                    ArooaContext context)
65     throws SAXParseException JavaDoc {
66         Object JavaDoc object = context.get(ArooaConstants.CURRENTLY_CONFIGURING);
67         
68         IntrospectionHelper ih = IntrospectionHelper.getHelper(object.getClass());
69         return ih.provideHandler(object, tag, context);
70     }
71
72     /*
73      * (non-Javadoc)
74      * @see org.oddjob.arooa.ArooaHandler#onEndElement(java.lang.String, java.lang.String, org.oddjob.arooa.ArooaXMLContext)
75      */

76     public void onEndElement(String JavaDoc uri, String JavaDoc tag, ArooaContext context) {
77         DesignElementType object = (DesignElementType) context.get(
78                 ArooaConstants.CURRENTLY_CONFIGURING);
79         DesignAdult parent = (DesignAdult) context.getParent().get(
80                 ArooaConstants.CURRENTLY_CONFIGURING);
81
82         parent.addChild(object);
83     }
84 }
85
Popular Tags