Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/xerces/readers/DefaultReaderFactory.java


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.apache.xerces.readers;
59  
60  import org.apache.xerces.framework.XMLErrorReporter;
61  import org.apache.xerces.utils.ChunkyByteArray;
62  import org.apache.xerces.utils.StringPool;
63  import org.xml.sax.InputSource;
64  import java.io.InputStream;
65  import java.io.InputStreamReader;
66  import java.io.Reader;
67  import java.net.URL;
68  import java.util.Stack;
69  
70  public class DefaultReaderFactory implements XMLEntityReaderFactory {
71      //
72      // Constants
73      //
74      private static final boolean USE_CHAR_READER_FOR_UTF8 = false;
75      private static final boolean USE_BYTE_READER_FOR_UTF8 = true;
76  
77      //
78      // Instance variables
79      //
80      private boolean fSendCharDataAsCharArray = false;
81      private boolean fAllowJavaEncodingName = false;
82      private Stack fRecognizers = null;
83  
84      /**
85          * Constructor
86          */
87      public DefaultReaderFactory() {
88      }
89  
90      /**
91          * Adds a recognizer.
92          *
93          * @param recognizer The XML recognizer to add.
94          */
95      public void addRecognizer(XMLDeclRecognizer recognizer) {
96          if (fRecognizers == null) {
97              fRecognizers = new Stack();
98              XMLDeclRecognizer.registerDefaultRecognizers(fRecognizers);
99          }
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(XMLEntityHandler entityHandler,
128                                                             XMLErrorReporter errorReporter,
129                                                             InputSource source,
130                                                       String systemId, boolean xmlDecl, StringPool stringPool) throws Exception {
131 
132         // create reader from source's character stream
133         if (source.getCharacterStream() != null) {
134             return createCharReader(entityHandler, errorReporter, fSendCharDataAsCharArray, source.getCharacterStream(), stringPool);
135         }
136 
137         // create reader from source's byte stream
138         if (source.getEncoding() != null && source.getByteStream() != null) {
139             java.io.Reader reader = new InputStreamReader(source.getByteStream(), source.getEncoding());
140             return createCharReader(entityHandler, errorReporter, fSendCharDataAsCharArray, reader, stringPool);
141         }
142 
143         // create new input stream
144         InputStream is = source.getByteStream();
145         if (is == null) {
146 
147             // create url and open the stream
148             URL url = new URL(systemId);
149             is = url.openStream();
150         }
151 
152         // create array and find recognizer
153         ChunkyByteArray data = new ChunkyByteArray(is);
154         if (fRecognizers == null) {
155             fRecognizers = new Stack();
156             XMLDeclRecognizer.registerDefaultRecognizers(fRecognizers);
157         }
158         for (int i = fRecognizers.size() - 1; i >= 0; i--) {
159             XMLDeclRecognizer recognizer = (XMLDeclRecognizer)fRecognizers.elementAt(i);
160             XMLEntityHandler.EntityReader reader = recognizer.recognize(this, entityHandler, errorReporter, fSendCharDataAsCharArray, stringPool, data, xmlDecl, fAllowJavaEncodingName);
161             if (reader != null) {
162                 return reader;
163             }
164         }
165         return createUTF8Reader(entityHandler, errorReporter, fSendCharDataAsCharArray, data, stringPool);
166     }
167 
168     /**
169         * Create an entity reader for a character stream.
170         *
171         * @param enityHandler The entity handler.
172         * @param errorReporter The error reporter.
173         * @param sendCharDataAsCharArray true if char data should be reported using
174         *                                char arrays instead of string handles.
175         * @param reader The character stream.
176         * @param stringPool The string pool.
177         * @return The reader that will process the character data.
178         * @exception java.lang.Exception
179         */
180     public XMLEntityHandler.EntityReader createCharReader(XMLEntityHandler entityHandler,
181                                                             XMLErrorReporter errorReporter,
182                                                             boolean sendCharDataAsCharArray,
183                                                             Reader reader,
184                                                             StringPool stringPool) throws Exception
185     {
186         return new CharReader(entityHandler, errorReporter, sendCharDataAsCharArray, reader, stringPool);
187     }
188 
189     /**
190         * Create an entity reader for a byte stream encoded in UTF-8.
191         *
192         * @param enityHandler The entity handler.
193         * @param errorReporter The error reporter.
194         * @param sendCharDataAsCharArray true if char data should be reported using
195         *                                char arrays instead of string handles.
196         * @param data The byte stream.
197         * @param stringPool The string pool.
198         * @return The reader that will process the UTF-8 data.
199         * @exception java.lang.Exception
200         */
201     public XMLEntityHandler.EntityReader createUTF8Reader(XMLEntityHandler entityHandler,
202                                                             XMLErrorReporter errorReporter,
203                                                             boolean sendCharDataAsCharArray,
204                                                             InputStream data,
205                                                             StringPool stringPool) throws Exception
206     {
207         XMLEntityHandler.EntityReader reader;
208         if (USE_CHAR_READER_FOR_UTF8) {
209             reader = new CharReader(entityHandler, errorReporter, sendCharDataAsCharArray, new InputStreamReader(data, "UTF8"), stringPool);
210         } else if (USE_BYTE_READER_FOR_UTF8) {
211             reader = new UTF8Reader(entityHandler, errorReporter, sendCharDataAsCharArray, data, stringPool);
212         } else {
213             reader = new UTF8CharReader(entityHandler, errorReporter, sendCharDataAsCharArray, data, stringPool);
214         }
215         return reader;
216     }
217 
218     /**
219         * Create an entity reader for data from a String.
220         *
221         * @param entityHandler The current entity handler.
222         * @param errorReporter The current error reporter.
223         * @param sendCharDataAsCharArray true if char data should be reported using
224         *                                char arrays instead of string handles.
225         * @param lineNumber The line number to return as our position.
226         * @param columnNumber The column number to return as our position.
227         * @param stringHandle The StringPool handle for the data to process.
228         * @param stringPool The string pool.
229         * @param addEnclosingSpaces If true, treat the data to process as if
230         *                           there were a leading and trailing space
231         *                           character enclosing the string data.
232         * @return The reader that will process the string data.
233         * @exception java.lang.Exception
234         */
235     public XMLEntityHandler.EntityReader createStringReader(XMLEntityHandler entityHandler,
236                                                             XMLErrorReporter errorReporter,
237                                                             boolean sendCharDataAsCharArray,
238                                                             int lineNumber,
239                                                             int columnNumber,
240                                                             int stringHandle,
241                                                             StringPool stringPool,
242                                                             boolean addEnclosingSpaces) throws Exception
243     {
244         return StringReader.createStringReader(entityHandler, errorReporter, sendCharDataAsCharArray,
245                                                 lineNumber, columnNumber, stringHandle, stringPool, addEnclosingSpaces);
246     }
247 }