KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > ant > AntElementHandler


1 package org.oddjob.ant;
2
3 import org.apache.tools.ant.IntrospectionHelper;
4 import org.apache.tools.ant.Project;
5 import org.oddjob.arooa.ArooaHandler;
6 import org.oddjob.arooa.ArooaContext;
7 import org.oddjob.arooa.ArooaConstants;
8 import org.xml.sax.Attributes JavaDoc;
9 import org.xml.sax.SAXParseException JavaDoc;
10
11
12 /**
13  * Handler for a nested element.
14  */

15 public class AntElementHandler extends ArooaHandler {
16         
17     private final Project project;
18     
19     /**
20      * Constructor.
21      */

22     public AntElementHandler(Project project) {
23         this.project = project;
24     }
25
26     /**
27      * Initialisation routine called after handler creation
28      * with the element name and attributes. This configures
29      * the element with its attributes and sets it up with
30      * its parent container (if any). Nested elements are then
31      * added later as the parser encounters them.
32      *
33      * @param uri The namespace URI for this element.
34      * @param tag Name of the element which caused this handler
35      * to be created. Must not be <code>null</code>.
36      * @param qname The qualified name for this element.
37      * @param attrs Attributes of the element which caused this
38      * handler to be created. Must not be <code>null</code>.
39      * @param context The current context.
40      *
41      * @exception SAXParseException in case of error (not thrown in
42      * this implementation)
43      */

44     public void onStartElement(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
45                                Attributes JavaDoc attrs,
46                                ArooaContext context)
47         throws SAXParseException JavaDoc {
48         AntJobRtc parentWrapper
49             = (AntJobRtc) context.getParent().get(
50                     ArooaConstants.CURRENTLY_CONFIGURING);
51         Object JavaDoc parent = parentWrapper.getWrappedObject();
52         IntrospectionHelper ih = IntrospectionHelper.getHelper(parent.getClass());
53
54         IntrospectionHelper.Creator creator =
55             ih.getElementCreator(
56                 project, uri, parent, tag, null);
57         Object JavaDoc object = creator.create();
58
59         AntJobRtc wrapper
60             = new AntJobRtc(project, object, tag);
61
62         for (int i = 0; i < attrs.getLength(); i++) {
63             String JavaDoc attrUri = attrs.getURI(i);
64             if (attrUri != null
65                 && !attrUri.equals("")
66                 && !attrUri.equals(uri)) {
67                 continue; // Ignore attributes from unknown uris
68
}
69             String JavaDoc name = attrs.getLocalName(i);
70             String JavaDoc value = attrs.getValue(i);
71             wrapper.setAttribute(name, value);
72         }
73
74         parentWrapper.addChild(wrapper);
75         context.set(ArooaConstants.CURRENTLY_CONFIGURING, wrapper);
76     }
77
78     /**
79      * Adds text to the task, using the wrapper
80      *
81      * @param buf A character array of the text within the element.
82      * Will not be <code>null</code>.
83      * @param start The start element in the array.
84      * @param count The number of characters to read from the array.
85      * @param context The current context.
86      *
87      * @exception SAXParseException if the element doesn't support text
88      *
89      */

90     public void characters(char[] buf, int start, int count,
91                            ArooaContext context)
92         throws SAXParseException JavaDoc {
93         AntJobRtc wrapper
94             = (AntJobRtc) context.get(
95                     ArooaConstants.CURRENTLY_CONFIGURING);
96         wrapper.addText(buf, start, count);
97     }
98
99     /**
100      * Handles the start of an element within a target. Task containers
101      * will always use another task handler, and all other tasks
102      * will always use a nested element handler.
103      *
104      * @param uri The namespace URI for this element.
105      * @param tag The name of the element being started.
106      * Will not be <code>null</code>.
107      * @param qname The qualified name for this element.
108      * @param attrs Attributes of the element being started.
109      * Will not be <code>null</code>.
110      * @param context The current context.
111      * @return The handler for elements.
112      *
113      * @exception SAXParseException if an error occurs when initialising
114      * the appropriate child handler
115      */

116     public ArooaHandler onStartChild(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
117                                    Attributes JavaDoc attrs,
118                                    ArooaContext context) {
119         return this;
120     }
121 }
122
Popular Tags