KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > handlers > MainHandler


1 package org.oddjob.arooa.handlers;
2
3 import org.oddjob.arooa.ArooaHandler;
4 import org.oddjob.arooa.ArooaContext;
5 import org.xml.sax.Attributes JavaDoc;
6 import org.xml.sax.SAXParseException JavaDoc;
7
8 /**
9  * The first handler to be called. This will recieve
10  * only an onStartChild with the document root as the child
11  * element.
12  * <p>
13  * If the document tag is specified this handler will
14  * verify the name of the element
15  * root against the document tag.
16  * <p>
17  * The startHandler is provided to Arooa as the handler for
18  * dealing with this document element. Thus the startHandler
19  * can then process the docment element in it's onStartElement
20  * method as per the typical pattern of an ArooaHandler.
21  *
22  * @see ArooaHandler.
23  * @author Rob Gordon.
24  */

25 public class MainHandler extends ArooaHandler {
26
27     /** The handle to use for the top level element */
28     private final String JavaDoc documentTag;
29
30     /** The start handler */
31     private final ArooaHandler startHandler;
32     /**
33     
34     /**
35      * Constructor.
36      *
37      * @param handler The handler to use for the top level element.
38      */

39     public MainHandler(ArooaHandler startHandler) {
40         this.documentTag = null;
41         this.startHandler = startHandler;
42     }
43     
44     /**
45      * Constructor.
46      *
47      * @param documentTag The document tag which will be validated
48      * against the top level element if not null.
49      * @param handler The handler to use for the top level element.
50      */

51     public MainHandler(String JavaDoc documentTag, ArooaHandler startHandler) {
52         this.documentTag = documentTag;
53         this.startHandler = startHandler;
54     }
55     
56     /**
57      * Handle the top level element.
58      *
59      * @param uri The namespace uri.
60      * @param name The element tag.
61      * @param qname The element qualified name.
62      * @param attrs The attributes of the element.
63      * @param context The current context.
64      * @return The handler that handles the top level elelement.
65      * @exception SAXParseException if the qualified name is not "project".
66      */

67     public ArooaHandler onStartChild(String JavaDoc uri, String JavaDoc name, String JavaDoc qname,
68                                    Attributes JavaDoc attrs,
69                                    ArooaContext context)
70             throws SAXParseException JavaDoc {
71
72         // check the root tag is what we expect
73
if (documentTag != null && !name.equals(documentTag)) {
74             throw new SAXParseException JavaDoc("Unexpected element \"" + qname
75                     + "\", expected \"" + documentTag + "\"" , context.getLocator());
76         }
77         return startHandler;
78     }
79             
80 }
81
82  
Popular Tags