KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > util > MultiplexerDecorator


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 XQuark Group.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.util;
24
25 import java.util.ArrayList JavaDoc;
26
27 import org.xml.sax.*;
28 import org.xml.sax.ext.LexicalHandler JavaDoc;
29
30
31 public class MultiplexerDecorator extends HandlerDecorator {
32
33     private ContentHandler[] contentHandlers;
34     private LexicalHandler JavaDoc[] lexicalHandlers;
35     private ErrorHandler[] errorHandlers;
36     
37     public MultiplexerDecorator(ArrayList JavaDoc handlers) {
38         ArrayList JavaDoc chList = new ArrayList JavaDoc();
39         ArrayList JavaDoc lhList = new ArrayList JavaDoc();
40         ArrayList JavaDoc ehList = new ArrayList JavaDoc();
41         for (int i = 0; i < handlers.size(); i++) {
42             Object JavaDoc handler = handlers.get(i);
43             if (handler instanceof ContentHandler)
44                 chList.add(handler);
45             if (handler instanceof LexicalHandler JavaDoc)
46                 lhList.add(handler);
47             if (handler instanceof ErrorHandler)
48                 ehList.add(handler);
49         }
50         contentHandlers = (ContentHandler[]) chList.toArray(new ContentHandler[0]);
51         lexicalHandlers = (LexicalHandler JavaDoc[]) lhList.toArray(new LexicalHandler JavaDoc[0]);
52         errorHandlers = (ErrorHandler[]) ehList.toArray(new ErrorHandler[0]);
53     }
54     //////////////////////////////////////////////////////
55
// Implementation of ContentHandler interface.
56
//////////////////////////////////////////////////////
57
public void startDocument() throws SAXException
58     {
59         super.startDocument();
60         for (int i = 0; i < contentHandlers.length; i++)
61             contentHandlers[i].startDocument();
62     }
63     
64     public void startElement(String JavaDoc str, String JavaDoc str1, String JavaDoc str2, Attributes attributes)
65     throws SAXException
66     {
67         super.startElement(str, str1, str2, attributes);
68         for (int i = 0; i < contentHandlers.length; i++)
69             contentHandlers[i].startElement(str, str1, str2, attributes);
70     }
71     
72     public void setDocumentLocator(Locator locator)
73     {
74         super.setDocumentLocator(locator);
75         for (int i = 0; i < contentHandlers.length; i++)
76             contentHandlers[i].setDocumentLocator(locator);
77     }
78     
79     public void endDocument() throws SAXException
80     {
81         super.endDocument();
82         for (int i = 0; i < contentHandlers.length; i++)
83             contentHandlers[i].endDocument();
84     }
85     
86     public void characters(char[] values, int param, int param2)
87     throws SAXException
88     {
89         super.characters(values, param, param2);
90         for (int i = 0; i < contentHandlers.length; i++)
91             contentHandlers[i].characters(values, param, param2);
92     }
93     
94     public void processingInstruction(String JavaDoc str, String JavaDoc str1)
95     throws SAXException
96     {
97         super.processingInstruction(str, str1);
98         for (int i = 0; i < contentHandlers.length; i++)
99             contentHandlers[i].processingInstruction(str, str1);
100     }
101     
102     public void endElement(String JavaDoc str, String JavaDoc str1, String JavaDoc str2)
103     throws SAXException
104     {
105         super.endElement(str, str1, str2);
106         for (int i = 0; i < contentHandlers.length; i++)
107             contentHandlers[i].endElement(str, str1, str2);
108     }
109     
110     public void startPrefixMapping(String JavaDoc str, String JavaDoc str1) throws SAXException
111     {
112         super.startPrefixMapping(str, str1);
113         for (int i = 0; i < contentHandlers.length; i++)
114             contentHandlers[i].startPrefixMapping(str, str1);
115     }
116     
117     public void ignorableWhitespace(char[] values, int param, int param2)
118     throws SAXException
119     {
120         super.ignorableWhitespace(values, param, param2);
121         for (int i = 0; i < contentHandlers.length; i++)
122             contentHandlers[i].ignorableWhitespace(values, param, param2);
123     }
124     
125     public void endPrefixMapping(String JavaDoc str) throws SAXException
126     {
127         super.endPrefixMapping(str);
128         for (int i = 0; i < contentHandlers.length; i++)
129             contentHandlers[i].endPrefixMapping(str);
130     }
131     
132     public void skippedEntity(String JavaDoc str) throws SAXException
133     {
134         super.skippedEntity(str);
135         for (int i = 0; i < contentHandlers.length; i++)
136             contentHandlers[i].skippedEntity(str);
137     }
138     
139     //////////////////////////////////////////////////////
140
// Implementation of LexicalHandler interface.
141
//////////////////////////////////////////////////////
142
public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
143     throws SAXException
144     {
145         super.startDTD(name, publicId, systemId);
146         for (int i = 0; i < lexicalHandlers.length; i++)
147             lexicalHandlers[i].startDTD(name, publicId, systemId);
148     }
149     
150     public void endDTD() throws SAXException
151     {
152         super.endDTD();
153         for (int i = 0; i < lexicalHandlers.length; i++)
154             lexicalHandlers[i].endDTD();
155     }
156     
157     public void startEntity(String JavaDoc name) throws SAXException
158     {
159         super.startEntity(name);
160         for (int i = 0; i < lexicalHandlers.length; i++)
161             lexicalHandlers[i].startEntity(name);
162     }
163     
164     public void endEntity(String JavaDoc name) throws SAXException
165     {
166         super.endEntity(name);
167         for (int i = 0; i < lexicalHandlers.length; i++)
168             lexicalHandlers[i].endEntity(name);
169     }
170     
171     public void startCDATA() throws SAXException
172     {
173         super.startCDATA();
174         for (int i = 0; i < lexicalHandlers.length; i++)
175             lexicalHandlers[i].startCDATA();
176     }
177     
178     public void endCDATA() throws SAXException
179     {
180         super.endCDATA();
181         for (int i = 0; i < lexicalHandlers.length; i++)
182             lexicalHandlers[i].endCDATA();
183     }
184     
185     public void comment(char ch[], int start, int length) throws SAXException
186     {
187         super.comment(ch, start, length);
188         for (int i = 0; i < lexicalHandlers.length; i++)
189             lexicalHandlers[i].comment(ch, start, length);
190     }
191     
192     //////////////////////////////////////////////////////
193
// Implementation of EventHandler interface.
194
//////////////////////////////////////////////////////
195
/**
196      * Filter a warning event.
197      *
198      * @param e The warning as an exception.
199      * @exception org.xml.sax.SAXException The client may throw
200      * an exception during processing.
201      * @see org.xml.sax.ErrorHandler#warning
202      */

203     public void warning(SAXParseException e)
204     throws SAXException
205     {
206         super.warning(e);
207         for (int i = 0; i < errorHandlers.length; i++)
208             errorHandlers[i].warning(e);
209     }
210     
211     
212     /**
213      * Filter an error event.
214      *
215      * @param e The error as an exception.
216      * @exception org.xml.sax.SAXException The client may throw
217      * an exception during processing.
218      * @see org.xml.sax.ErrorHandler#error
219      */

220     public void error(SAXParseException e)
221     throws SAXException
222     {
223         super.error(e);
224         for (int i = 0; i < errorHandlers.length; i++)
225             errorHandlers[i].error(e);
226     }
227     
228     
229     /**
230      * Filter a fatal error event.
231      *
232      * @param e The error as an exception.
233      * @exception org.xml.sax.SAXException The client may throw
234      * an exception during processing.
235      * @see org.xml.sax.ErrorHandler#fatalError
236      */

237     public void fatalError(SAXParseException e)
238     throws SAXException
239     {
240         super.fatalError(e);
241         for (int i = 0; i < errorHandlers.length; i++)
242             errorHandlers[i].fatalError(e);
243     }
244
245 }
246
Popular Tags