1 57 58 package org.enhydra.apache.xerces.readers; 59 60 import java.io.InputStream ; 61 import java.io.InputStreamReader ; 62 import java.io.Reader ; 63 import java.net.URL ; 64 import java.util.Stack ; 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 ; 70 71 public class DefaultReaderFactory implements XMLEntityReaderFactory { 72 private static final boolean USE_CHAR_READER_FOR_UTF8 = false; 76 private static final boolean USE_BYTE_READER_FOR_UTF8 = true; 77 78 private boolean fSendCharDataAsCharArray = false; 82 private boolean fAllowJavaEncodingName = false; 83 private Stack fRecognizers = null; 84 85 88 public DefaultReaderFactory() { 89 } 90 91 96 public void addRecognizer(XMLDeclRecognizer recognizer) { 97 if (fRecognizers == null) { 98 fRecognizers = new Stack (); 99 XMLDeclRecognizer.registerDefaultRecognizers(fRecognizers); 100 } 101 fRecognizers.push(recognizer); 102 } 103 104 107 public void setSendCharDataAsCharArray(boolean flag) { 108 fSendCharDataAsCharArray = flag; 109 } 110 111 114 public void setAllowJavaEncodingName(boolean flag) { 115 fAllowJavaEncodingName = flag; 116 } 117 118 121 public boolean getAllowJavaEncodingName() { 122 return fAllowJavaEncodingName; 123 } 124 125 128 public XMLEntityHandler.EntityReader createReader(XMLEntityHandler entityHandler, 129 XMLErrorReporter errorReporter, 130 InputSource source, 131 String systemId, boolean xmlDecl, StringPool stringPool) throws Exception { 132 133 if (source.getCharacterStream() != null) { 135 return createCharReader(entityHandler, errorReporter, fSendCharDataAsCharArray, source.getCharacterStream(), stringPool); 136 } 137 138 if (source.getEncoding() != null && source.getByteStream() != null) { 140 java.io.Reader reader = new InputStreamReader (source.getByteStream(), source.getEncoding()); 141 return createCharReader(entityHandler, errorReporter, fSendCharDataAsCharArray, reader, stringPool); 142 } 143 144 InputStream is = source.getByteStream(); 146 if (is == null) { 147 148 URL url = new URL (systemId); 150 is = url.openStream(); 151 } 152 153 ChunkyByteArray data = new ChunkyByteArray(is); 155 if (fRecognizers == null) { 156 fRecognizers = new Stack (); 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 181 public XMLEntityHandler.EntityReader createCharReader(XMLEntityHandler entityHandler, 182 XMLErrorReporter errorReporter, 183 boolean sendCharDataAsCharArray, 184 Reader reader, 185 StringPool stringPool) throws Exception 186 { 187 return new CharReader(entityHandler, errorReporter, sendCharDataAsCharArray, reader, stringPool); 188 } 189 190 202 public XMLEntityHandler.EntityReader createUTF8Reader(XMLEntityHandler entityHandler, 203 XMLErrorReporter errorReporter, 204 boolean sendCharDataAsCharArray, 205 InputStream data, 206 StringPool stringPool) throws Exception 207 { 208 XMLEntityHandler.EntityReader reader; 209 if (USE_CHAR_READER_FOR_UTF8) { 210 reader = new CharReader(entityHandler, errorReporter, sendCharDataAsCharArray, new InputStreamReader (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 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 244 { 245 return StringReader.createStringReader(entityHandler, errorReporter, sendCharDataAsCharArray, 246 lineNumber, columnNumber, stringHandle, stringPool, addEnclosingSpaces); 247 } 248 } 249 | Popular Tags |