KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > RootHandler


1 /*
2  * This source code is heavily based on source code from the Apache
3  * Ant project. As such the following is included:
4  * ------------------------------------------------------------------
5  *
6  * The Apache Software License, Version 1.1
7  *
8  * Copyright (c) 2000,2002-2003 The Apache Software Foundation. All rights
9  * reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution, if
24  * any, must include the following acknowlegement:
25  * "This product includes software developed by the
26  * Apache Software Foundation (http://www.apache.org/)."
27  * Alternately, this acknowlegement may appear in the software itself,
28  * if and wherever such third-party acknowlegements normally appear.
29  *
30  * 4. The names "Ant" and "Apache Software
31  * Foundation" must not be used to endorse or promote products derived
32  * from this software without prior written permission. For written
33  * permission, please contact apache@apache.org.
34  *
35  * 5. Products derived from this software may not be called "Apache"
36  * nor may "Apache" appear in their names without prior written
37  * permission of the Apache Group.
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This software consists of voluntary contributions made by many
54  * individuals on behalf of the Apache Software Foundation. For more
55  * information on the Apache Software Foundation, please see
56  * <http://www.apache.org/>.
57  */

58 package org.oddjob.arooa;
59
60 import java.util.Stack JavaDoc;
61
62 import org.apache.log4j.Logger;
63 import org.xml.sax.Attributes JavaDoc;
64 import org.xml.sax.Locator JavaDoc;
65 import org.xml.sax.SAXException JavaDoc;
66 import org.xml.sax.SAXParseException JavaDoc;
67 import org.xml.sax.helpers.DefaultHandler JavaDoc;
68
69 /**
70  * Handler for ant processing. Uses a stack of AntHandlers to
71  * implement each element ( the original parser used a recursive behavior,
72  * with the implicit execution stack )
73  */

74 public class RootHandler extends DefaultHandler JavaDoc {
75     private static final Logger logger = Logger.getLogger(RootHandler.class);
76     
77     private Stack JavaDoc antHandlers = new Stack JavaDoc();
78     private ArooaHandler currentHandler = null;
79     private ArooaContext context;
80
81     /**
82      * Creates a new RootHandler instance.
83      *
84      * @param context The context for the handler.
85      * @param rootHandler The handler for the root element.
86      */

87     public RootHandler(ArooaContext context, ArooaHandler rootHandler) {
88         currentHandler = rootHandler;
89         antHandlers.push(currentHandler);
90         this.context = context;
91     }
92
93     /**
94      * Handles the start of a project element. A project handler is created
95      * and initialised with the element name and attributes.
96      *
97      * @param uri The namespace uri for this element.
98      * @param tag The name of the element being started.
99      * Will not be <code>null</code>.
100      * @param qname The qualified name for this element.
101      * @param attrs Attributes of the element being started.
102      * Will not be <code>null</code>.
103      *
104      * @exception org.xml.sax.SAXParseException if the tag given is not
105      * <code>"project"</code>
106      */

107     public void startElement(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname, Attributes JavaDoc attrs)
108         throws SAXParseException JavaDoc {
109         ArooaHandler next
110             = currentHandler.onStartChild(uri, tag, qname, attrs, context);
111         if (next == null) {
112             throw new IllegalStateException JavaDoc("Handler [" + currentHandler + "] did not return a child handler.");
113         }
114         antHandlers.push(currentHandler);
115         context = new ArooaContext(context);
116         currentHandler = next;
117         logger.debug("onStartElement(" + qname + "), handler [" + currentHandler + "]");
118         currentHandler.onStartElement(uri, tag, qname, attrs, context);
119     }
120
121     /**
122      * Sets the locator in the project helper for future reference.
123      *
124      * @param locator The locator used by the parser.
125      * Will not be <code>null</code>.
126      */

127     public void setDocumentLocator(Locator JavaDoc locator) {
128         context.setLocator(locator);
129     }
130
131     /**
132      * Handles the end of an element. Any required clean-up is performed
133      * by the onEndElement() method and then the original handler
134      * is restored to the parser.
135      *
136      * @param uri The namespace URI for this element.
137      * @param name The name of the element which is ending.
138      * Will not be <code>null</code>.
139      * @param qName The qualified name for this element.
140      *
141      * @exception SAXException in case of error (not thrown in
142      * this implementation)
143      *
144      */

145     public void endElement(String JavaDoc uri, String JavaDoc name, String JavaDoc qName) throws SAXException JavaDoc {
146         logger.debug("onEndElement(" + name + "), handler [" + currentHandler + "]");
147         currentHandler.onEndElement(uri, name, context);
148         ArooaHandler prev = (ArooaHandler) antHandlers.pop();
149         currentHandler = prev;
150         context = context.getParent();
151         if (currentHandler != null) {
152             currentHandler.onEndChild(uri, name, qName, context);
153         }
154     }
155
156     /**
157      * Handle text within an element, calls currentHandler.characters.
158      *
159      * @param buf A character array of the test.
160      * @param start The start offset in the array.
161      * @param count The number of characters to read.
162      * @exception SAXParseException if an error occurs
163      */

164     public void characters(char[] buf, int start, int count)
165         throws SAXParseException JavaDoc {
166         currentHandler.characters(buf, start, count, context);
167     }
168
169     /**
170      * Start a namespace prefix to uri mapping
171      *
172      * @param prefix the namespace prefix
173      * @param uri the namespace uri
174      */

175     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) {
176         context.startPrefixMapping(prefix, uri);
177     }
178
179     /**
180      * End a namepace prefix to uri mapping
181      *
182      * @param prefix the prefix that is not mapped anymore
183      */

184     public void endPrefixMapping(String JavaDoc prefix) {
185         context.endPrefixMapping(prefix);
186     }
187 }
188
Popular Tags