KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.oddjob.ant;
2
3 import org.apache.tools.ant.ComponentHelper;
4 import org.apache.tools.ant.Location;
5 import org.apache.tools.ant.Project;
6 import org.apache.tools.ant.Task;
7 import org.oddjob.arooa.ArooaException;
8 import org.oddjob.arooa.ArooaHandler;
9 import org.oddjob.arooa.ArooaContext;
10 import org.oddjob.arooa.ArooaConstants;
11 import org.oddjob.arooa.ArooaRuntime;
12 import org.oddjob.arooa.Lifecycle;
13 import org.oddjob.arooa.RuntimeConfiguration;
14 import org.oddjob.arooa.reflect.IntrospectionHelper;
15 import org.xml.sax.Attributes JavaDoc;
16 import org.xml.sax.SAXParseException JavaDoc;
17
18 /**
19  * The handler for an Ant ProjectComponent such as a task.
20  * This uses an ant project to create a task.
21  *
22  * @see ArooaHandler
23  * @author Rob Gordon
24  */

25
26 public class AntJobComponentHandler extends ArooaHandler {
27
28     private Project project;
29     
30     /**
31      * Constructor.
32      *
33      */

34     public AntJobComponentHandler(Project project) {
35         this.project = project;
36     }
37
38     /**
39      * Handles the start of an element.
40      *
41      * @param uri the namespace URI for the tag
42      * @param tag The name of the element being started.
43      * Will not be <code>null</code>.
44      * @param qname The qualified name of the element.
45      * @param attrs Attributes of the element being started.
46      * Will not be <code>null</code>.
47      * @param context The context that this element is in.
48      *
49      * @exception SAXParseException if this method is not overridden, or in
50      * case of error in an overridden version
51      */

52     public void onStartElement(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname,
53                                Attributes JavaDoc attrs,
54                                ArooaContext context)
55             throws SAXParseException JavaDoc {
56
57         ArooaRuntime parentWrapper
58             = (ArooaRuntime) context.get(
59                     ArooaConstants.CURRENTLY_CONFIGURING);
60
61         ComponentHelper helper = ComponentHelper.getComponentHelper(
62             project);
63         Object JavaDoc o = helper.createComponent(tag);
64
65         if (o == null) {
66             throw new ArooaException("Failed to create ant task or type [" + tag + "]");
67         }
68
69         if (o instanceof Task) {
70             Task task = (Task)o;
71             task.setProject(project);
72             task.setTaskName(tag);
73             task.setTaskType(tag);
74             Location location = new Location(context.getLocator().getSystemId(),
75                     context.getLocator().getLineNumber(),
76                     context.getLocator().getColumnNumber());
77             task.setLocation(location);
78         }
79
80
81         AntJobRtc wrapper
82             = new AntJobRtc(project, o, tag);
83
84         for (int i = 0; i < attrs.getLength(); i++) {
85             String JavaDoc attrUri = attrs.getURI(i);
86             if (attrUri != null
87                 && !attrUri.equals("")
88                 && !attrUri.equals(uri)) {
89                 continue; // Ignore attributes from unknown uris
90
}
91             String JavaDoc name = attrs.getLocalName(i);
92             String JavaDoc value = attrs.getValue(i);
93             // a crude handling of references.
94
if (name.equals("id")) {
95                 project.addReference(value, o);
96             }
97             else {
98                 wrapper.setAttribute(name, value);
99             }
100         }
101
102         parentWrapper.link(wrapper);
103         context.set(ArooaConstants.CURRENTLY_CONFIGURING, wrapper);
104     }
105     
106     /**
107      * Adds text to the component, using the wrapper
108      *
109      * @param buf A character array of the text within the element.
110      * Will not be <code>null</code>.
111      * @param start The start element in the array.
112      * @param count The number of characters to read from the array.
113      * @param context The current context.
114      *
115      * @exception SAXParseException if the element doesn't support text
116      *
117      */

118     public void characters(char[] buf, int start, int count,
119                            ArooaContext context)
120             throws SAXParseException JavaDoc {
121         AntJobRtc wrapper
122             = (AntJobRtc) context.get(
123                     ArooaConstants.CURRENTLY_CONFIGURING);
124         wrapper.addText(buf, start, count);
125     }
126
127     /**
128      * Handle a child tag.
129      *
130      * @param uri The namespace uri.
131      * @param name The element tag.
132      * @param qname The element qualified name.
133      * @param attrs The attributes of the element.
134      * @param context The current context.
135      * @return The handler that handles this subelement.
136      * @exception SAXParseException if the qualified name is not "project".
137      */

138     public ArooaHandler onStartChild(String JavaDoc uri, String JavaDoc name, String JavaDoc qname,
139                                    Attributes JavaDoc attrs,
140                                    ArooaContext context)
141             throws SAXParseException JavaDoc {
142         return new AntElementHandler(project);
143     }
144     
145     /**
146      * Handles the end of the element.
147      *
148      * @param uri The namespace URI for the element.
149      * @param tag The name of the element.
150      * @param context The current context.
151      */

152     public void onEndElement(String JavaDoc uri, String JavaDoc tag, ArooaContext context) {
153         Object JavaDoc component = ((RuntimeConfiguration)context.get(
154                 ArooaConstants.CURRENTLY_CONFIGURING)).getWrappedObject();
155         Object JavaDoc parent = ((RuntimeConfiguration)context.getParent().get(
156                 ArooaConstants.CURRENTLY_CONFIGURING)).getWrappedObject();
157         
158         Lifecycle.init(component);
159         
160         if (parent != null) {
161             IntrospectionHelper ih = IntrospectionHelper.getHelper(parent.getClass());
162             ih.storeComponent(parent, component, "");
163         }
164     }
165
166 }
167
168  
Popular Tags