KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > util > xml > sax > QueueContentHandler


1 package org.mr.core.util.xml.sax;
2
3 import org.xml.sax.Attributes JavaDoc;
4 import org.xml.sax.ContentHandler JavaDoc;
5 import org.xml.sax.Locator JavaDoc;
6 import org.xml.sax.SAXException JavaDoc;
7 import org.xml.sax.ext.LexicalHandler JavaDoc;
8 import org.xml.sax.helpers.AttributesImpl JavaDoc;
9
10 import java.io.Serializable JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.LinkedList JavaDoc;
13
14 /*
15  * Copyright 2002 by
16  * <a HREF="http://www.coridan.com">Coridan</a>
17  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
18  *
19  * The contents of this file are subject to the Mozilla Public License Version
20  * 1.1 (the "License"); you may not use this file except in compliance with the
21  * License. You may obtain a copy of the License at
22  * http://www.mozilla.org/MPL/
23  *
24  * Software distributed under the License is distributed on an "AS IS" basis,
25  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
26  * for the specific language governing rights and limitations under the
27  * License.
28  *
29  * The Original Code is "MantaRay" (TM).
30  *
31  * The Initial Developer of the Original Code is Coridan.
32  * Portions created by the Initial Developer are Copyright (C) 2006
33  * Coridan Inc. All Rights Reserved.
34  *
35  * Contributor(s): all the names of the contributors are added in the source
36  * code where applicable.
37  *
38  * Alternatively, the contents of this file may be used under the terms of the
39  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
40  * provisions of LGPL are applicable instead of those above. If you wish to
41  * allow use of your version of this file only under the terms of the LGPL
42  * License and not to allow others to use your version of this file under
43  * the MPL, indicate your decision by deleting the provisions above and
44  * replace them with the notice and other provisions required by the LGPL.
45  * If you do not delete the provisions above, a recipient may use your version
46  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
47  
48  *
49  * This library is free software; you can redistribute it and/or modify it
50  * under the terms of the MPL as stated above or under the terms of the GNU
51  * Lesser General Public License as published by the Free Software Foundation;
52  * either version 2.1 of the License, or any later version.
53  *
54  * This library is distributed in the hope that it will be useful, but WITHOUT
55  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
56  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
57  * License for more details.
58  */

59
60  /**
61  * User: Moti Tal
62  * Date: Apr 6, 2005
63  * Time: 9:18:21 AM
64   *
65  * Queue implementation of ContentHandler
66  */

67 public class QueueContentHandler implements ContentHandler JavaDoc, LexicalHandler JavaDoc, Serializable JavaDoc {
68
69     private LinkedList JavaDoc m_q = new LinkedList JavaDoc();
70
71     /**
72      * Fires the events to a different content handler.
73      */

74     public void fireInto(ContentHandler JavaDoc i_handler)
75             throws SAXException JavaDoc {
76         Iterator JavaDoc events = m_q.iterator();
77         while (events.hasNext()) {
78             SaxEvent event = (SaxEvent) events.next();
79             event.fire(i_handler);
80
81         }
82     }
83
84     public int getSize(){
85         return m_q.size();
86     }
87
88     /**
89      * Fires the events into another stack.
90      */

91     public void fireInto(QueueContentHandler i_handler) {
92         i_handler.m_q.addAll(m_q);
93     }
94
95     /**
96      * Clears the stack of events.
97      */

98     public void clear() {
99         m_q.clear();
100     }
101
102     public final boolean hasEvents() {
103         return m_q.size() > 0;
104     }
105
106     ///////////////////////////////////////////////////////////////////////////
107
//
108
// Implementation of the content handler
109
//
110
///////////////////////////////////////////////////////////////////////////
111

112     public void setDocumentLocator(Locator JavaDoc i_locator) {
113         m_q.add(new SetDocumentLocator(i_locator));
114     }
115
116     public void startDocument() throws SAXException JavaDoc {
117         m_q.add(new StartDocument());
118     }
119
120     public void endDocument() throws SAXException JavaDoc {
121         m_q.add(new EndDocument());
122     }
123
124     public void startPrefixMapping(String JavaDoc i_prefix, String JavaDoc i_uri) throws SAXException JavaDoc {
125         m_q.add(new StartPrefixMapping(i_prefix, i_uri));
126     }
127
128     public void endPrefixMapping(String JavaDoc i_prefix) throws SAXException JavaDoc {
129         m_q.add(new EndPrefixMapping(i_prefix));
130     }
131
132     public void startElement(String JavaDoc i_namespaceURI, String JavaDoc i_localName, String JavaDoc i_qName, Attributes JavaDoc i_atts) throws SAXException JavaDoc {
133         m_q.add(new StartElement(i_namespaceURI, i_localName, i_qName, i_atts));
134     }
135
136     public void endElement(String JavaDoc i_namespaceURI, String JavaDoc i_localName, String JavaDoc i_qName) throws SAXException JavaDoc {
137         m_q.add(new EndElement(i_namespaceURI, i_localName, i_qName));
138     }
139
140     public void ignorableWhitespace(char[] i_chars, int i_start, int i_length) throws SAXException JavaDoc {
141         m_q.add(new IgnorableWhitespace(i_chars, i_start, i_length));
142     }
143
144     public void processingInstruction(String JavaDoc i_target, String JavaDoc i_data) throws SAXException JavaDoc {
145         m_q.add(new ProcessingInstruction(i_target, i_data));
146     }
147
148     public void skippedEntity(String JavaDoc i_name) throws SAXException JavaDoc {
149         m_q.add(new SkippedEntity(i_name));
150     }
151
152     public void characters(char[] i_chars, int i_start, int i_length) throws SAXException JavaDoc {
153         m_q.add(new Characters(i_chars, i_start, i_length));
154     }
155
156     ///////////////////////////////////////////////////////////////////////////
157
//
158
// Implementation of LexicalHandler
159
//
160
///////////////////////////////////////////////////////////////////////////
161
public void startDTD(String JavaDoc s, String JavaDoc s1, String JavaDoc s2) throws SAXException JavaDoc {
162         m_q.add(new StartDTD(s, s1, s2));
163     }
164
165     public void endDTD() throws SAXException JavaDoc {
166         m_q.add(new EndDTD());
167     }
168
169     public void startEntity(String JavaDoc s) throws SAXException JavaDoc {
170         m_q.add(new StartEntity(s));
171     }
172
173     public void endEntity(String JavaDoc s) throws SAXException JavaDoc {
174         m_q.add(new EndEntity(s));
175     }
176
177     public void startCDATA() throws SAXException JavaDoc {
178         m_q.add(new StartCDATA());
179     }
180
181     public void endCDATA() throws SAXException JavaDoc {
182         m_q.add(new EndCDATA());
183     }
184
185     public void comment(char[] i_chars, int i, int i1) throws SAXException JavaDoc {
186         m_q.add(new Comment(i_chars, i, i1));
187     }
188
189     ///////////////////////////////////////////////////////////////////////////
190
//
191
// inner classes to keep the events
192
//
193
///////////////////////////////////////////////////////////////////////////
194

195     /**
196      * A Sax Event
197      */

198     private static interface SaxEvent extends Serializable JavaDoc {
199
200         /**
201          * The function that writes the event to the handler.
202          *
203          * @param i_handler The handler to flush the event to
204          */

205         public void fire(ContentHandler JavaDoc i_handler)
206                 throws SAXException JavaDoc;
207     }
208
209     protected static abstract class SaxLexicalEvent implements SaxEvent {
210         public void fire(ContentHandler JavaDoc i_handler)
211                 throws SAXException JavaDoc {
212             if (i_handler instanceof LexicalHandler JavaDoc) {
213                 fire((LexicalHandler JavaDoc)i_handler);
214             }
215         }
216
217         public abstract void fire(LexicalHandler JavaDoc i_handler) throws SAXException JavaDoc;
218     }
219
220     public static class SetDocumentLocator implements SaxEvent {
221         private Locator JavaDoc m_docLocator;
222
223         public SetDocumentLocator(Locator JavaDoc i_docLocator) {
224             m_docLocator = i_docLocator;
225         }
226
227         public void fire(ContentHandler JavaDoc i_handler) throws SAXException JavaDoc {
228             i_handler.setDocumentLocator(m_docLocator);
229         }
230     }
231
232     public static class StartDocument implements SaxEvent {
233         public void fire(ContentHandler JavaDoc i_handler) throws SAXException JavaDoc {
234             i_handler.startDocument();
235         }
236     }
237
238     public static class EndDocument implements SaxEvent {
239         public void fire(ContentHandler JavaDoc i_handler) throws SAXException JavaDoc {
240             i_handler.endDocument();
241         }
242     }
243
244     public static class StartPrefixMapping implements SaxEvent {
245         private String JavaDoc m_prefix;
246         private String JavaDoc m_uri;
247
248         public StartPrefixMapping(String JavaDoc i_prefix, String JavaDoc i_uri) {
249             m_prefix = i_prefix;
250             m_uri = i_uri;
251         }
252
253         public void fire(ContentHandler JavaDoc i_handler) throws SAXException JavaDoc {
254             i_handler.startPrefixMapping(m_prefix, m_uri);
255         }
256     }
257
258     public static class EndPrefixMapping implements SaxEvent {
259         private String JavaDoc m_prefix;
260
261         public EndPrefixMapping(String JavaDoc i_prefix) {
262             m_prefix = i_prefix;
263         }
264
265         public void fire(ContentHandler JavaDoc i_handler) throws SAXException JavaDoc {
266             i_handler.endPrefixMapping(m_prefix);
267         }
268     }
269
270     public static class StartElement implements SaxEvent {
271         private String JavaDoc m_namespaceURI;
272         private String JavaDoc m_localName;
273         private String JavaDoc m_qName;
274         private Attributes JavaDoc m_atts;
275
276         public StartElement(String JavaDoc i_namespaceURI, String JavaDoc i_localName, String JavaDoc i_qName, Attributes JavaDoc i_atts) {
277             m_namespaceURI = i_namespaceURI;
278             m_localName = i_localName;
279             m_qName = i_qName;
280             m_atts = new AttributesImpl JavaDoc(i_atts);
281         }
282
283         public void fire(ContentHandler JavaDoc i_handler) throws SAXException JavaDoc {
284             i_handler.startElement(m_namespaceURI, m_localName, m_qName, m_atts);
285         }
286     }
287
288     public static class EndElement implements SaxEvent {
289         private String JavaDoc m_namespaceURI;
290         private String JavaDoc m_localName;
291         private String JavaDoc m_qName;
292
293         public EndElement(String JavaDoc i_namespaceURI, String JavaDoc i_localName, String JavaDoc i_qName) {
294             m_namespaceURI = i_namespaceURI;
295             m_localName = i_localName;
296             m_qName = i_qName;
297         }
298
299         public void fire(ContentHandler JavaDoc i_handler) throws SAXException JavaDoc {
300             i_handler.endElement(m_namespaceURI, m_localName, m_qName);
301         }
302     }
303
304     public static class IgnorableWhitespace implements SaxEvent {
305         private String JavaDoc m_chars;
306
307         public IgnorableWhitespace(char[] i_chars, int i_start, int i_length) {
308             m_chars = new String JavaDoc(i_chars, i_start, i_length);
309         }
310
311         public void fire(ContentHandler JavaDoc i_handler)
312                 throws SAXException JavaDoc {
313             i_handler.ignorableWhitespace(m_chars.toCharArray(), 0, m_chars.length());
314         }
315     }
316
317     public static class ProcessingInstruction implements SaxEvent {
318         private String JavaDoc m_target;
319         private String JavaDoc m_data;
320
321         public ProcessingInstruction(String JavaDoc i_target, String JavaDoc i_data) {
322             m_target = i_target;
323             m_data = i_data;
324         }
325
326         public void fire(ContentHandler JavaDoc i_handler) throws SAXException JavaDoc {
327             i_handler.processingInstruction(m_target, m_data);
328         }
329     }
330
331     public static class SkippedEntity implements SaxEvent {
332         private String JavaDoc m_name;
333
334         public SkippedEntity(String JavaDoc i_name) {
335             m_name = i_name;
336         }
337
338         public void fire(ContentHandler JavaDoc i_handler) throws SAXException JavaDoc {
339             i_handler.skippedEntity(m_name);
340         }
341     }
342
343     public static class Characters implements SaxEvent {
344         private String JavaDoc m_chars;
345
346         public Characters(char[] i_chars, int i_start, int i_length) {
347             m_chars = new String JavaDoc(i_chars, i_start, i_length);
348         }
349
350         public void fire(ContentHandler JavaDoc i_handler)
351                 throws SAXException JavaDoc {
352             i_handler.characters(m_chars.toCharArray(), 0, m_chars.length());
353         }
354     }
355
356     public static class Comment extends SaxLexicalEvent {
357         private String JavaDoc m_chars;
358
359         public Comment(char[] i_chars, int i_start, int i_length) {
360             m_chars = new String JavaDoc(i_chars, i_start, i_length);
361         }
362
363         public void fire(LexicalHandler JavaDoc i_handler)
364                 throws SAXException JavaDoc {
365             i_handler.comment(m_chars.toCharArray(), 0, m_chars.length());
366         }
367     }
368
369     public static class StartDTD extends SaxLexicalEvent {
370         private String JavaDoc m_name;
371         private String JavaDoc m_publicId;
372         private String JavaDoc m_systemId;
373
374         public StartDTD(String JavaDoc i_name, String JavaDoc i_publicId, String JavaDoc i_systemId) {
375             m_name = i_name;
376             m_publicId = i_publicId;
377             m_systemId = i_systemId;
378         }
379
380         public void fire(LexicalHandler JavaDoc i_handler) throws SAXException JavaDoc {
381             i_handler.startDTD(m_name, m_publicId, m_systemId);
382         }
383     }
384
385     public static class EndDTD extends SaxLexicalEvent {
386         public void fire(LexicalHandler JavaDoc i_handler) throws SAXException JavaDoc {
387             i_handler.endDTD();
388         }
389     }
390
391     public static class StartCDATA extends SaxLexicalEvent {
392         public void fire(LexicalHandler JavaDoc i_handler) throws SAXException JavaDoc {
393             i_handler.startCDATA();
394         }
395     }
396
397     public static class EndCDATA extends SaxLexicalEvent {
398         public void fire(LexicalHandler JavaDoc i_handler) throws SAXException JavaDoc {
399             i_handler.endCDATA();
400         }
401     }
402
403     public static class StartEntity extends SaxLexicalEvent {
404         private String JavaDoc m_name;
405
406         public StartEntity(String JavaDoc i_name) {
407             m_name = i_name;
408         }
409
410         public void fire(LexicalHandler JavaDoc i_handler) throws SAXException JavaDoc {
411             i_handler.startEntity(m_name);
412         }
413     }
414
415     public static class EndEntity extends SaxLexicalEvent {
416         private String JavaDoc m_name;
417
418         public EndEntity(String JavaDoc i_name) {
419             m_name = i_name;
420         }
421
422         public void fire(LexicalHandler JavaDoc i_handler) throws SAXException JavaDoc {
423             i_handler.endEntity(m_name);
424         }
425     }
426
427 }
428
Popular Tags