KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > apache > xerces > readers > DefaultReaderFactory


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.enhydra.apache.xerces.readers;
59
60 import java.io.InputStream JavaDoc;
61 import java.io.InputStreamReader JavaDoc;
62 import java.io.Reader JavaDoc;
63 import java.net.URL JavaDoc;
64 import java.util.Stack JavaDoc;
65
66 import org.enhydra.apache.xerces.framework.XMLErrorReporter;
67 import org.enhydra.apache.xerces.utils.ChunkyByteArray;
68 import org.enhydra.apache.xerces.utils.StringPool;
69 import org.xml.sax.InputSource JavaDoc;
70
71 public class DefaultReaderFactory implements XMLEntityReaderFactory {
72     //
73
// Constants
74
//
75
private static final boolean USE_CHAR_READER_FOR_UTF8 = false;
76     private static final boolean USE_BYTE_READER_FOR_UTF8 = true;
77
78     //
79
// Instance variables
80
//
81
private boolean fSendCharDataAsCharArray = false;
82     private boolean fAllowJavaEncodingName = false;
83     private Stack JavaDoc fRecognizers = null;
84
85     /**
86         * Constructor
87         */

88     public DefaultReaderFactory() {
89     }
90
91     /**
92         * Adds a recognizer.
93         *
94         * @param recognizer The XML recognizer to add.
95         */

96     public void addRecognizer(XMLDeclRecognizer recognizer) {
97         if (fRecognizers == null) {
98             fRecognizers = new Stack JavaDoc();
99             XMLDeclRecognizer.registerDefaultRecognizers(fRecognizers);
100         }
101         fRecognizers.push(recognizer);
102     }
103
104     /**
105         * Set char data processing preference.
106         */

107     public void setSendCharDataAsCharArray(boolean flag) {
108         fSendCharDataAsCharArray = flag;
109     }
110
111     /**
112         *
113         */

114     public void setAllowJavaEncodingName(boolean flag) {
115         fAllowJavaEncodingName = flag;
116     }
117
118     /**
119         *
120         */

121     public boolean getAllowJavaEncodingName() {
122         return fAllowJavaEncodingName;
123     }
124
125     /**
126         * Create a reader
127         */

128     public XMLEntityHandler.EntityReader createReader(XMLEntityHandler entityHandler,
129                                                             XMLErrorReporter errorReporter,
130                                                             InputSource JavaDoc source,
131                                                       String JavaDoc systemId, boolean xmlDecl, StringPool stringPool) throws Exception JavaDoc {
132
133         // create reader from source's character stream
134
if (source.getCharacterStream() != null) {
135             return createCharReader(entityHandler, errorReporter, fSendCharDataAsCharArray, source.getCharacterStream(), stringPool);
136         }
137
138         // create reader from source's byte stream
139
if (source.getEncoding() != null && source.getByteStream() != null) {
140             java.io.Reader JavaDoc reader = new InputStreamReader JavaDoc(source.getByteStream(), source.getEncoding());
141             return createCharReader(entityHandler, errorReporter, fSendCharDataAsCharArray, reader, stringPool);
142         }
143
144         // create new input stream
145
InputStream JavaDoc is = source.getByteStream();
146         if (is == null) {
147
148             // create url and open the stream
149
URL JavaDoc url = new URL JavaDoc(systemId);
150             is = url.openStream();
151         }
152
153         // create array and find recognizer
154
ChunkyByteArray data = new ChunkyByteArray(is);
155         if (fRecognizers == null) {
156             fRecognizers = new Stack JavaDoc();
157             XMLDeclRecognizer.registerDefaultRecognizers(fRecognizers);
158         }
159         for (int i = fRecognizers.size() - 1; i >= 0; i--) {
160             XMLDeclRecognizer recognizer = (XMLDeclRecognizer)fRecognizers.elementAt(i);
161             XMLEntityHandler.EntityReader reader = recognizer.recognize(this, entityHandler, errorReporter, fSendCharDataAsCharArray, stringPool, data, xmlDecl, fAllowJavaEncodingName);
162             if (reader != null) {
163                 return reader;
164             }
165         }
166         return createUTF8Reader(entityHandler, errorReporter, fSendCharDataAsCharArray, data, stringPool);
167     }
168
169     /**
170         * Create an entity reader for a character stream.
171         *
172         * @param enityHandler The entity handler.
173         * @param errorReporter The error reporter.
174         * @param sendCharDataAsCharArray true if char data should be reported using
175         * char arrays instead of string handles.
176         * @param reader The character stream.
177         * @param stringPool The string pool.
178         * @return The reader that will process the character data.
179         * @exception java.lang.Exception
180         */

181     public XMLEntityHandler.EntityReader createCharReader(XMLEntityHandler entityHandler,
182                                                             XMLErrorReporter errorReporter,
183                                                             boolean sendCharDataAsCharArray,
184                                                             Reader JavaDoc reader,
185                                                             StringPool stringPool) throws Exception JavaDoc
186     {
187         return new CharReader(entityHandler, errorReporter, sendCharDataAsCharArray, reader, stringPool);
188     }
189
190     /**
191         * Create an entity reader for a byte stream encoded in UTF-8.
192         *
193         * @param enityHandler The entity handler.
194         * @param errorReporter The error reporter.
195         * @param sendCharDataAsCharArray true if char data should be reported using
196         * char arrays instead of string handles.
197         * @param data The byte stream.
198         * @param stringPool The string pool.
199         * @return The reader that will process the UTF-8 data.
200         * @exception java.lang.Exception
201         */

202     public XMLEntityHandler.EntityReader createUTF8Reader(XMLEntityHandler entityHandler,
203                                                             XMLErrorReporter errorReporter,
204                                                             boolean sendCharDataAsCharArray,
205                                                             InputStream JavaDoc data,
206                                                             StringPool stringPool) throws Exception JavaDoc
207     {
208         XMLEntityHandler.EntityReader reader;
209         if (USE_CHAR_READER_FOR_UTF8) {
210             reader = new CharReader(entityHandler, errorReporter, sendCharDataAsCharArray, new InputStreamReader JavaDoc(data, "UTF8"), stringPool);
211         } else if (USE_BYTE_READER_FOR_UTF8) {
212             reader = new UTF8Reader(entityHandler, errorReporter, sendCharDataAsCharArray, data, stringPool);
213         } else {
214             reader = new UTF8CharReader(entityHandler, errorReporter, sendCharDataAsCharArray, data, stringPool);
215         }
216         return reader;
217     }
218
219     /**
220         * Create an entity reader for data from a String.
221         *
222         * @param entityHandler The current entity handler.
223         * @param errorReporter The current error reporter.
224         * @param sendCharDataAsCharArray true if char data should be reported using
225         * char arrays instead of string handles.
226         * @param lineNumber The line number to return as our position.
227         * @param columnNumber The column number to return as our position.
228         * @param stringHandle The StringPool handle for the data to process.
229         * @param stringPool The string pool.
230         * @param addEnclosingSpaces If true, treat the data to process as if
231         * there were a leading and trailing space
232         * character enclosing the string data.
233         * @return The reader that will process the string data.
234         * @exception java.lang.Exception
235         */

236     public XMLEntityHandler.EntityReader createStringReader(XMLEntityHandler entityHandler,
237                                                             XMLErrorReporter errorReporter,
238                                                             boolean sendCharDataAsCharArray,
239                                                             int lineNumber,
240                                                             int columnNumber,
241                                                             int stringHandle,
242                                                             StringPool stringPool,
243                                                             boolean addEnclosingSpaces) throws Exception JavaDoc
244     {
245         return StringReader.createStringReader(entityHandler, errorReporter, sendCharDataAsCharArray,
246                                                 lineNumber, columnNumber, stringHandle, stringPool, addEnclosingSpaces);
247     }
248 }
249
Popular Tags