1 16 17 package org.apache.xerces.impl; 18 19 import java.io.EOFException ; 20 import java.io.IOException ; 21 22 import org.apache.xerces.impl.msg.XMLMessageFormatter; 23 import org.apache.xerces.util.SymbolTable; 24 import org.apache.xerces.xni.XMLString; 25 import org.apache.xerces.xni.parser.XMLComponentManager; 26 import org.apache.xerces.xni.parser.XMLConfigurationException; 27 import org.apache.xerces.xni.parser.XMLInputSource; 28 29 40 public class XMLVersionDetector { 41 42 46 private final static char[] XML11_VERSION = new char[]{'1', '.', '1'}; 47 48 49 51 52 protected static final String SYMBOL_TABLE = 53 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 54 55 56 protected static final String ERROR_REPORTER = 57 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 58 59 60 protected static final String ENTITY_MANAGER = 61 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; 62 63 67 68 protected final static String fVersionSymbol = "version".intern(); 69 70 protected static final String fXMLSymbol = "[xml]".intern(); 72 73 74 protected SymbolTable fSymbolTable; 75 76 77 protected XMLErrorReporter fErrorReporter; 78 79 80 protected XMLEntityManager fEntityManager; 81 82 protected String fEncoding = null; 83 84 private XMLString fVersionNum = new XMLString(); 85 86 private final char [] fExpectedVersionString = {'<', '?', 'x', 'm', 'l', ' ', 'v', 'e', 'r', 's', 87 'i', 'o', 'n', '=', ' ', ' ', ' ', ' ', ' '}; 88 89 97 public void reset(XMLComponentManager componentManager) 98 throws XMLConfigurationException { 99 100 fSymbolTable = (SymbolTable)componentManager.getProperty(SYMBOL_TABLE); 102 fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER); 103 fEntityManager = (XMLEntityManager)componentManager.getProperty(ENTITY_MANAGER); 104 for(int i=14; i<fExpectedVersionString.length; i++ ) 105 fExpectedVersionString[i] = ' '; 106 } 108 114 public void startDocumentParsing(XMLEntityHandler scanner, short version){ 115 116 if (version == Constants.XML_VERSION_1_0){ 117 fEntityManager.setScannerVersion(Constants.XML_VERSION_1_0); 118 } 119 else { 120 fEntityManager.setScannerVersion(Constants.XML_VERSION_1_1); 121 } 122 fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner()); 124 125 fEntityManager.setEntityHandler(scanner); 129 130 scanner.startEntity(fXMLSymbol, fEntityManager.getCurrentResourceIdentifier(), fEncoding, null); 131 } 132 133 134 143 public short determineDocVersion(XMLInputSource inputSource) throws IOException { 144 fEncoding = fEntityManager.setupCurrentEntity(fXMLSymbol, inputSource, false, true); 145 146 fEntityManager.setScannerVersion(Constants.XML_VERSION_1_0); 149 XMLEntityScanner scanner = fEntityManager.getEntityScanner(); 150 try { 151 if (!scanner.skipString("<?xml")) { 152 return Constants.XML_VERSION_1_0; 154 } 155 if (!scanner.skipDeclSpaces()) { 156 fixupCurrentEntity(fEntityManager, fExpectedVersionString, 5); 157 return Constants.XML_VERSION_1_0; 158 } 159 if (!scanner.skipString("version")) { 160 fixupCurrentEntity(fEntityManager, fExpectedVersionString, 6); 161 return Constants.XML_VERSION_1_0; 162 } 163 scanner.skipDeclSpaces(); 164 if (scanner.peekChar() != '=') { 166 fixupCurrentEntity(fEntityManager, fExpectedVersionString, 13); 167 return Constants.XML_VERSION_1_0; 168 } 169 scanner.scanChar(); 170 scanner.skipDeclSpaces(); 171 int quoteChar = scanner.scanChar(); 172 fExpectedVersionString[14] = (char) quoteChar; 173 for (int versionPos = 0; versionPos < XML11_VERSION.length; versionPos++) { 174 fExpectedVersionString[15 + versionPos] = (char) scanner.scanChar(); 175 } 176 fExpectedVersionString[18] = (char) scanner.scanChar(); 178 fixupCurrentEntity(fEntityManager, fExpectedVersionString, 19); 179 int matched = 0; 180 for (; matched < XML11_VERSION.length; matched++) { 181 if (fExpectedVersionString[15 + matched] != XML11_VERSION[matched]) 182 break; 183 } 184 if (matched == XML11_VERSION.length) 185 return Constants.XML_VERSION_1_1; 186 return Constants.XML_VERSION_1_0; 187 } 189 catch (EOFException e) { 190 fErrorReporter.reportError( 191 XMLMessageFormatter.XML_DOMAIN, 192 "PrematureEOF", 193 null, 194 XMLErrorReporter.SEVERITY_FATAL_ERROR); 195 return Constants.XML_VERSION_1_0; 196 197 } 198 199 } 200 201 private void fixupCurrentEntity(XMLEntityManager manager, 204 char [] scannedChars, int length) { 205 XMLEntityManager.ScannedEntity currentEntity = manager.getCurrentEntity(); 206 if(currentEntity.count-currentEntity.position+length > currentEntity.ch.length) { 207 char[] tempCh = currentEntity.ch; 209 currentEntity.ch = new char[length+currentEntity.count-currentEntity.position+1]; 210 System.arraycopy(tempCh, 0, currentEntity.ch, 0, tempCh.length); 211 } 212 if(currentEntity.position < length) { 213 System.arraycopy(currentEntity.ch, currentEntity.position, currentEntity.ch, length, currentEntity.count-currentEntity.position); 215 currentEntity.count += length-currentEntity.position; 216 } else { 217 for(int i=length; i<currentEntity.position; i++) 219 currentEntity.ch[i]=' '; 220 } 221 System.arraycopy(scannedChars, 0, currentEntity.ch, 0, length); 223 currentEntity.position = 0; 224 currentEntity.baseCharOffset = 0; 225 currentEntity.startPosition = 0; 226 currentEntity.columnNumber = currentEntity.lineNumber = 1; 227 } 228 229 } 231 | Popular Tags |