KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > embedding > tools > EasyGenerationContentHandlerProxy


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

17
18 /* $Id: EasyGenerationContentHandlerProxy.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package embedding.tools;
21
22 //SAX
23
import org.xml.sax.ContentHandler JavaDoc;
24 import org.xml.sax.Locator JavaDoc;
25 import org.xml.sax.Attributes JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27 import org.xml.sax.helpers.AttributesImpl JavaDoc;
28
29 /**
30  * This class is an implementation of ContentHandler which acts as a proxy to
31  * another ContentHandler and has the purpose to provide a few handy methods
32  * that make life easier when generating SAX events.
33  * <br>
34  * Note: This class is only useful for simple cases with no namespaces.
35  */

36
37 public class EasyGenerationContentHandlerProxy implements ContentHandler JavaDoc {
38
39     /** An empty Attributes object used when no attributes are needed. */
40     public static final Attributes JavaDoc EMPTY_ATTS = new AttributesImpl JavaDoc();
41
42     private ContentHandler JavaDoc target;
43
44
45     /**
46      * Main constructor.
47      * @param forwardTo ContentHandler to forward the SAX event to.
48      */

49     public EasyGenerationContentHandlerProxy(ContentHandler JavaDoc forwardTo) {
50         this.target = forwardTo;
51     }
52
53
54     /**
55      * Sends the notification of the beginning of an element.
56      * @param name Name for the element.
57      * @throws SAXException Any SAX exception, possibly wrapping another exception.
58      */

59     public void startElement(String JavaDoc name) throws SAXException JavaDoc {
60         startElement(name, EMPTY_ATTS);
61     }
62
63
64     /**
65      * Sends the notification of the beginning of an element.
66      * @param name Name for the element.
67      * @param atts The attributes attached to the element. If there are no
68      * attributes, it shall be an empty Attributes object.
69      * @throws SAXException Any SAX exception, possibly wrapping another exception.
70      */

71     public void startElement(String JavaDoc name, Attributes JavaDoc atts) throws SAXException JavaDoc {
72         startElement(null, name, name, atts);
73     }
74
75
76     /**
77      * Send a String of character data.
78      * @param s The content String
79      * @throws SAXException Any SAX exception, possibly wrapping another exception.
80      */

81     public void characters(String JavaDoc s) throws SAXException JavaDoc {
82         target.characters(s.toCharArray(), 0, s.length());
83     }
84
85
86     /**
87      * Send the notification of the end of an element.
88      * @param name Name for the element.
89      * @throws SAXException Any SAX exception, possibly wrapping another exception.
90      */

91     public void endElement(String JavaDoc name) throws SAXException JavaDoc {
92         endElement(null, name, name);
93     }
94
95
96     /**
97      * Sends notifications for a whole element with some String content.
98      * @param name Name for the element.
99      * @param value Content of the element.
100      * @throws SAXException Any SAX exception, possibly wrapping another exception.
101      */

102     public void element(String JavaDoc name, String JavaDoc value) throws SAXException JavaDoc {
103         element(name, value, EMPTY_ATTS);
104     }
105
106
107     /**
108      * Sends notifications for a whole element with some String content.
109      * @param name Name for the element.
110      * @param value Content of the element.
111      * @param atts The attributes attached to the element. If there are no
112      * attributes, it shall be an empty Attributes object.
113      * @throws SAXException Any SAX exception, possibly wrapping another exception.
114      */

115     public void element(String JavaDoc name, String JavaDoc value, Attributes JavaDoc atts) throws SAXException JavaDoc {
116         startElement(name, atts);
117         if (value != null) {
118             characters(value.toCharArray(), 0, value.length());
119         }
120         endElement(name);
121     }
122
123     /* =========== ContentHandler interface =========== */
124
125     /**
126      * @see org.xml.sax.ContentHandler#setDocumentLocator(Locator)
127      */

128     public void setDocumentLocator(Locator JavaDoc locator) {
129         target.setDocumentLocator(locator);
130     }
131
132
133     /**
134      * @see org.xml.sax.ContentHandler#startDocument()
135      */

136     public void startDocument() throws SAXException JavaDoc {
137         target.startDocument();
138     }
139
140
141     /**
142      * @see org.xml.sax.ContentHandler#endDocument()
143      */

144     public void endDocument() throws SAXException JavaDoc {
145         target.endDocument();
146     }
147
148
149     /**
150      * @see org.xml.sax.ContentHandler#startPrefixMapping(String, String)
151      */

152     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
153         target.startPrefixMapping(prefix, uri);
154     }
155
156
157     /**
158      * @see org.xml.sax.ContentHandler#endPrefixMapping(String)
159      */

160     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
161         target.endPrefixMapping(prefix);
162     }
163
164
165     /**
166      * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
167      */

168     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
169                         String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
170         target.startElement(namespaceURI, localName, qName, atts);
171     }
172
173
174     /**
175      * @see org.xml.sax.ContentHandler#endElement(String, String, String)
176      */

177     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
178                                                         throws SAXException JavaDoc {
179         target.endElement(namespaceURI, localName, qName);
180     }
181
182
183     /**
184      * @see org.xml.sax.ContentHandler#characters(char[], int, int)
185      */

186     public void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
187         target.characters(ch, start, length);
188     }
189
190
191     /**
192      * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
193      */

194     public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException JavaDoc {
195         target.ignorableWhitespace(ch, start, length);
196     }
197
198
199     /**
200      * @see org.xml.sax.ContentHandler#processingInstruction(String, String)
201      */

202     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
203         this.target.processingInstruction(target, data);
204     }
205
206
207     /**
208      * @see org.xml.sax.ContentHandler#skippedEntity(String)
209      */

210     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
211         target.skippedEntity(name);
212     }
213
214 }
Popular Tags