1 57 58 package com.sun.org.apache.xerces.internal.impl; 59 60 import java.io.EOFException ; 61 import java.io.IOException ; 62 63 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 64 import com.sun.org.apache.xerces.internal.util.SymbolTable; 65 import com.sun.org.apache.xerces.internal.xni.XMLString; 66 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 67 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 68 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 69 70 79 80 public class XMLVersionDetector { 81 82 86 private final static char[] XML11_VERSION = new char[]{'1', '.', '1'}; 87 88 89 91 92 protected static final String SYMBOL_TABLE = 93 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 94 95 96 protected static final String ERROR_REPORTER = 97 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 98 99 100 protected static final String ENTITY_MANAGER = 101 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; 102 103 107 108 protected final static String fVersionSymbol = "version".intern(); 109 110 protected static final String fXMLSymbol = "[xml]".intern(); 112 113 114 protected SymbolTable fSymbolTable; 115 116 117 protected XMLErrorReporter fErrorReporter; 118 119 120 protected XMLEntityManager fEntityManager; 121 122 protected String fEncoding = null; 123 124 private XMLString fVersionNum = new XMLString(); 125 126 private final char [] fExpectedVersionString = {'<', '?', 'x', 'm', 'l', ' ', 'v', 'e', 'r', 's', 127 'i', 'o', 'n', '=', ' ', ' ', ' ', ' ', ' '}; 128 129 137 public void reset(XMLComponentManager componentManager) 138 throws XMLConfigurationException { 139 140 fSymbolTable = (SymbolTable)componentManager.getProperty(SYMBOL_TABLE); 142 fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER); 143 fEntityManager = (XMLEntityManager)componentManager.getProperty(ENTITY_MANAGER); 144 for(int i=14; i<fExpectedVersionString.length; i++ ) 145 fExpectedVersionString[i] = ' '; 146 } 148 154 public void startDocumentParsing(XMLEntityHandler scanner, short version){ 155 156 if (version == Constants.XML_VERSION_1_0){ 157 fEntityManager.setScannerVersion(Constants.XML_VERSION_1_0); 158 } 159 else { 160 fEntityManager.setScannerVersion(Constants.XML_VERSION_1_1); 161 } 162 fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner()); 164 165 fEntityManager.setEntityHandler(scanner); 169 170 scanner.startEntity(fXMLSymbol, fEntityManager.getCurrentResourceIdentifier(), fEncoding, null); 171 } 172 173 174 183 public short determineDocVersion(XMLInputSource inputSource) throws IOException { 184 fEncoding = fEntityManager.setupCurrentEntity(fXMLSymbol, inputSource, false, true); 185 186 fEntityManager.setScannerVersion(Constants.XML_VERSION_1_0); 189 XMLEntityScanner scanner = fEntityManager.getEntityScanner(); 190 try { 191 if (!scanner.skipString("<?xml")) { 192 return Constants.XML_VERSION_1_0; 194 } 195 if (!scanner.skipDeclSpaces()) { 196 fixupCurrentEntity(fEntityManager, fExpectedVersionString, 5); 197 return Constants.XML_VERSION_1_0; 198 } 199 if (!scanner.skipString("version")) { 200 fixupCurrentEntity(fEntityManager, fExpectedVersionString, 6); 201 return Constants.XML_VERSION_1_0; 202 } 203 scanner.skipDeclSpaces(); 204 if (scanner.peekChar() != '=') { 206 fixupCurrentEntity(fEntityManager, fExpectedVersionString, 13); 207 return Constants.XML_VERSION_1_0; 208 } 209 scanner.scanChar(); 210 scanner.skipDeclSpaces(); 211 int quoteChar = scanner.scanChar(); 212 fExpectedVersionString[14] = (char) quoteChar; 213 for (int versionPos = 0; versionPos < XML11_VERSION.length; versionPos++) { 214 fExpectedVersionString[15 + versionPos] = (char) scanner.scanChar(); 215 } 216 fExpectedVersionString[18] = (char) scanner.scanChar(); 218 fixupCurrentEntity(fEntityManager, fExpectedVersionString, 19); 219 int matched = 0; 220 for (; matched < XML11_VERSION.length; matched++) { 221 if (fExpectedVersionString[15 + matched] != XML11_VERSION[matched]) 222 break; 223 } 224 if (matched == XML11_VERSION.length) 225 return Constants.XML_VERSION_1_1; 226 return Constants.XML_VERSION_1_0; 227 } 229 catch (EOFException e) { 230 fErrorReporter.reportError( 231 XMLMessageFormatter.XML_DOMAIN, 232 "PrematureEOF", 233 null, 234 XMLErrorReporter.SEVERITY_FATAL_ERROR); 235 return Constants.XML_VERSION_1_0; 236 237 } 238 239 } 240 241 private void fixupCurrentEntity(XMLEntityManager manager, 244 char [] scannedChars, int length) { 245 XMLEntityManager.ScannedEntity currentEntity = manager.getCurrentEntity(); 246 if(currentEntity.count-currentEntity.position+length > currentEntity.ch.length) { 247 char[] tempCh = currentEntity.ch; 249 currentEntity.ch = new char[length+currentEntity.count-currentEntity.position+1]; 250 System.arraycopy(tempCh, 0, currentEntity.ch, 0, tempCh.length); 251 } 252 if(currentEntity.position < length) { 253 System.arraycopy(currentEntity.ch, currentEntity.position, currentEntity.ch, length, currentEntity.count-currentEntity.position); 255 currentEntity.count += length-currentEntity.position; 256 } else { 257 for(int i=length; i<currentEntity.position; i++) 259 currentEntity.ch[i]=' '; 260 } 261 System.arraycopy(scannedChars, 0, currentEntity.ch, 0, length); 263 currentEntity.position = 0; 264 currentEntity.columnNumber = currentEntity.lineNumber = 1; 265 } 266 267 } 269 | Popular Tags |