001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 1999 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Xerces" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 1999, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.xerces.readers;
059:
060: import org.apache.xerces.framework.XMLErrorReporter;
061: import org.apache.xerces.utils.ChunkyByteArray;
062: import org.apache.xerces.utils.StringPool;
063: import org.xml.sax.InputSource;
064: import java.io.InputStream;
065: import java.io.InputStreamReader;
066: import java.io.Reader;
067: import java.net.URL;
068: import java.util.Stack;
069:
070: public class DefaultReaderFactory implements XMLEntityReaderFactory {
071: //
072: // Constants
073: //
074: private static final boolean USE_CHAR_READER_FOR_UTF8 = false;
075: private static final boolean USE_BYTE_READER_FOR_UTF8 = true;
076:
077: //
078: // Instance variables
079: //
080: private boolean fSendCharDataAsCharArray = false;
081: private boolean fAllowJavaEncodingName = false;
082: private Stack fRecognizers = null;
083:
084: /**
085: * Constructor
086: */
087: public DefaultReaderFactory() {
088: }
089:
090: /**
091: * Adds a recognizer.
092: *
093: * @param recognizer The XML recognizer to add.
094: */
095: public void addRecognizer(XMLDeclRecognizer recognizer) {
096: if (fRecognizers == null) {
097: fRecognizers = new Stack();
098: XMLDeclRecognizer.registerDefaultRecognizers(fRecognizers);
099: }
100: fRecognizers.push(recognizer);
101: }
102:
103: /**
104: * Set char data processing preference.
105: */
106: public void setSendCharDataAsCharArray(boolean flag) {
107: fSendCharDataAsCharArray = flag;
108: }
109:
110: /**
111: *
112: */
113: public void setAllowJavaEncodingName(boolean flag) {
114: fAllowJavaEncodingName = flag;
115: }
116:
117: /**
118: *
119: */
120: public boolean getAllowJavaEncodingName() {
121: return fAllowJavaEncodingName;
122: }
123:
124: /**
125: * Create a reader
126: */
127: public XMLEntityHandler.EntityReader createReader(
128: XMLEntityHandler entityHandler,
129: XMLErrorReporter errorReporter, InputSource source,
130: String systemId, boolean xmlDecl, StringPool stringPool)
131: throws Exception {
132:
133: // create reader from source's character stream
134: if (source.getCharacterStream() != null) {
135: return createCharReader(entityHandler, errorReporter,
136: fSendCharDataAsCharArray, source
137: .getCharacterStream(), stringPool);
138: }
139:
140: // create reader from source's byte stream
141: if (source.getEncoding() != null
142: && source.getByteStream() != null) {
143: java.io.Reader reader = new InputStreamReader(source
144: .getByteStream(), source.getEncoding());
145: return createCharReader(entityHandler, errorReporter,
146: fSendCharDataAsCharArray, reader, stringPool);
147: }
148:
149: // create new input stream
150: InputStream is = source.getByteStream();
151: if (is == null) {
152:
153: // create url and open the stream
154: URL url = new URL(systemId);
155: is = url.openStream();
156: }
157:
158: // create array and find recognizer
159: ChunkyByteArray data = new ChunkyByteArray(is);
160: if (fRecognizers == null) {
161: fRecognizers = new Stack();
162: XMLDeclRecognizer.registerDefaultRecognizers(fRecognizers);
163: }
164: for (int i = fRecognizers.size() - 1; i >= 0; i--) {
165: XMLDeclRecognizer recognizer = (XMLDeclRecognizer) fRecognizers
166: .elementAt(i);
167: XMLEntityHandler.EntityReader reader = recognizer
168: .recognize(this , entityHandler, errorReporter,
169: fSendCharDataAsCharArray, stringPool, data,
170: xmlDecl, fAllowJavaEncodingName);
171: if (reader != null) {
172: return reader;
173: }
174: }
175: return createUTF8Reader(entityHandler, errorReporter,
176: fSendCharDataAsCharArray, data, stringPool);
177: }
178:
179: /**
180: * Create an entity reader for a character stream.
181: *
182: * @param enityHandler The entity handler.
183: * @param errorReporter The error reporter.
184: * @param sendCharDataAsCharArray true if char data should be reported using
185: * char arrays instead of string handles.
186: * @param reader The character stream.
187: * @param stringPool The string pool.
188: * @return The reader that will process the character data.
189: * @exception java.lang.Exception
190: */
191: public XMLEntityHandler.EntityReader createCharReader(
192: XMLEntityHandler entityHandler,
193: XMLErrorReporter errorReporter,
194: boolean sendCharDataAsCharArray, Reader reader,
195: StringPool stringPool) throws Exception {
196: return new CharReader(entityHandler, errorReporter,
197: sendCharDataAsCharArray, reader, stringPool);
198: }
199:
200: /**
201: * Create an entity reader for a byte stream encoded in UTF-8.
202: *
203: * @param enityHandler The entity handler.
204: * @param errorReporter The error reporter.
205: * @param sendCharDataAsCharArray true if char data should be reported using
206: * char arrays instead of string handles.
207: * @param data The byte stream.
208: * @param stringPool The string pool.
209: * @return The reader that will process the UTF-8 data.
210: * @exception java.lang.Exception
211: */
212: public XMLEntityHandler.EntityReader createUTF8Reader(
213: XMLEntityHandler entityHandler,
214: XMLErrorReporter errorReporter,
215: boolean sendCharDataAsCharArray, InputStream data,
216: StringPool stringPool) throws Exception {
217: XMLEntityHandler.EntityReader reader;
218: if (USE_CHAR_READER_FOR_UTF8) {
219: reader = new CharReader(entityHandler, errorReporter,
220: sendCharDataAsCharArray, new InputStreamReader(
221: data, "UTF8"), stringPool);
222: } else if (USE_BYTE_READER_FOR_UTF8) {
223: reader = new UTF8Reader(entityHandler, errorReporter,
224: sendCharDataAsCharArray, data, stringPool);
225: } else {
226: reader = new UTF8CharReader(entityHandler, errorReporter,
227: sendCharDataAsCharArray, data, stringPool);
228: }
229: return reader;
230: }
231:
232: /**
233: * Create an entity reader for data from a String.
234: *
235: * @param entityHandler The current entity handler.
236: * @param errorReporter The current error reporter.
237: * @param sendCharDataAsCharArray true if char data should be reported using
238: * char arrays instead of string handles.
239: * @param lineNumber The line number to return as our position.
240: * @param columnNumber The column number to return as our position.
241: * @param stringHandle The StringPool handle for the data to process.
242: * @param stringPool The string pool.
243: * @param addEnclosingSpaces If true, treat the data to process as if
244: * there were a leading and trailing space
245: * character enclosing the string data.
246: * @return The reader that will process the string data.
247: * @exception java.lang.Exception
248: */
249: public XMLEntityHandler.EntityReader createStringReader(
250: XMLEntityHandler entityHandler,
251: XMLErrorReporter errorReporter,
252: boolean sendCharDataAsCharArray, int lineNumber,
253: int columnNumber, int stringHandle, StringPool stringPool,
254: boolean addEnclosingSpaces) throws Exception {
255: return StringReader.createStringReader(entityHandler,
256: errorReporter, sendCharDataAsCharArray, lineNumber,
257: columnNumber, stringHandle, stringPool,
258: addEnclosingSpaces);
259: }
260: }
|