KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > conf > AbstractHandler


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.conf;
21
22 import org.xml.sax.Attributes JavaDoc;
23 import org.xml.sax.ContentHandler JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25 import org.xml.sax.XMLReader JavaDoc;
26 import org.xml.sax.helpers.DefaultHandler JavaDoc;
27
28 /**
29  * The common superclass for all SAX event handlers used to parse the configuration file.
30  * Each method just throws an exception, so subclasses should override what they can
31  * handle. Each type of XML element (map, node, etc.) has a specific subclass. In the
32  * constructor, this class takes over the handling of SAX events from the parent handler
33  * and returns control back to the parent in the endElement method.
34  * </p>
35  *
36  * @since 1.2
37  * @author Andrus Adamchik
38  */

39 class AbstractHandler extends DefaultHandler JavaDoc {
40
41     /** Current parser. */
42     protected XMLReader JavaDoc parser;
43
44     /**
45      * Previous handler for the document. When the next element is finished, control
46      * returns to this handler.
47      */

48     protected ContentHandler JavaDoc parentHandler;
49
50     /**
51      * Creates a handler and sets the parser to use it for the current element.
52      *
53      * @param parser Currently used XML parser. Must not be <code>null</code>.
54      * @param parentHandler The handler which should be restored to the parser at the end
55      * of the element. Must not be <code>null</code>.
56      */

57     public AbstractHandler(XMLReader JavaDoc parser, ContentHandler JavaDoc parentHandler) {
58         this.parentHandler = parentHandler;
59         this.parser = parser;
60
61         // Start handling SAX events
62
parser.setContentHandler(this);
63     }
64
65     /** Returns currently used XMLReader. */
66     public XMLReader JavaDoc getParser() {
67         return parser;
68     }
69
70     /**
71      * Handles the start of an element. This base implementation just throws an exception.
72      *
73      * @exception SAXException if this method is not overridden, or in case of error in an
74      * overridden version
75      */

76     public void startElement(
77             String JavaDoc namespaceURI,
78             String JavaDoc localName,
79             String JavaDoc qName,
80             Attributes JavaDoc atts) throws SAXException JavaDoc {
81         throw new SAXException JavaDoc(this.getClass().getName()
82                 + ": unexpected element \""
83                 + localName
84                 + "\"");
85     }
86
87     /**
88      * Handles text within an element. This base implementation just throws an exception.
89      *
90      * @param buf A character array of the text within the element. Will not be
91      * <code>null</code>.
92      * @param start The start element in the array.
93      * @param count The number of characters to read from the array.
94      * @exception SAXException if this method is not overridden, or in case of error in an
95      * overridden version
96      */

97     public void characters(char[] buf, int start, int count) throws SAXException JavaDoc {
98         String JavaDoc s = new String JavaDoc(buf, start, count).trim();
99
100         if (s.length() > 0) {
101             throw new SAXException JavaDoc(this.getClass().getName()
102                     + ": unexpected text \""
103                     + s
104                     + "\"");
105         }
106     }
107
108     /**
109      * Called when this element and all elements nested into it have been handled.
110      */

111     protected void finished() {
112     }
113
114     /**
115      * Handles the end of an element. Any required clean-up is performed by the finished()
116      * method and then the original handler is restored to the parser.
117      *
118      * @see #finished()
119      */

120     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
121             throws SAXException JavaDoc {
122         finished();
123         // Let parent resume handling SAX events
124
parser.setContentHandler(parentHandler);
125     }
126 }
127
Popular Tags