1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999-2003 The Apache Software Foundation.
  6. * All rights 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) 2003, 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. package com.sun.org.apache.xerces.internal.impl;
  58. import java.io.EOFException;
  59. import java.io.IOException;
  60. import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
  61. import com.sun.org.apache.xerces.internal.util.SymbolTable;
  62. import com.sun.org.apache.xerces.internal.xni.XMLString;
  63. import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
  64. import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
  65. import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
  66. /**
  67. * This class scans the version of the document to determine
  68. * which scanner to use: XML 1.1 or XML 1.0.
  69. * The version is scanned using XML 1.1. scanner.
  70. *
  71. * @author Neil Graham, IBM
  72. * @author Elena Litani, IBM
  73. * @version $Id: XMLVersionDetector.java,v 1.12 2004/02/27 20:36:07 mrglavas Exp $
  74. */
  75. public class XMLVersionDetector {
  76. //
  77. // Constants
  78. //
  79. private final static char[] XML11_VERSION = new char[]{'1', '.', '1'};
  80. // property identifiers
  81. /** Property identifier: symbol table. */
  82. protected static final String SYMBOL_TABLE =
  83. Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
  84. /** Property identifier: error reporter. */
  85. protected static final String ERROR_REPORTER =
  86. Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
  87. /** Property identifier: entity manager. */
  88. protected static final String ENTITY_MANAGER =
  89. Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
  90. //
  91. // Data
  92. //
  93. /** Symbol: "version". */
  94. protected final static String fVersionSymbol = "version".intern();
  95. // symbol: [xml]:
  96. protected static final String fXMLSymbol = "[xml]".intern();
  97. /** Symbol table. */
  98. protected SymbolTable fSymbolTable;
  99. /** Error reporter. */
  100. protected XMLErrorReporter fErrorReporter;
  101. /** Entity manager. */
  102. protected XMLEntityManager fEntityManager;
  103. protected String fEncoding = null;
  104. private XMLString fVersionNum = new XMLString();
  105. private final char [] fExpectedVersionString = {'<', '?', 'x', 'm', 'l', ' ', 'v', 'e', 'r', 's',
  106. 'i', 'o', 'n', '=', ' ', ' ', ' ', ' ', ' '};
  107. /**
  108. *
  109. *
  110. * @param componentManager The component manager.
  111. *
  112. * @throws SAXException Throws exception if required features and
  113. * properties cannot be found.
  114. */
  115. public void reset(XMLComponentManager componentManager)
  116. throws XMLConfigurationException {
  117. // Xerces properties
  118. fSymbolTable = (SymbolTable)componentManager.getProperty(SYMBOL_TABLE);
  119. fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER);
  120. fEntityManager = (XMLEntityManager)componentManager.getProperty(ENTITY_MANAGER);
  121. for(int i=14; i<fExpectedVersionString.length; i++ )
  122. fExpectedVersionString[i] = ' ';
  123. } // reset(XMLComponentManager)
  124. /**
  125. * Reset the reference to the appropriate scanner given the version of the
  126. * document and start document scanning.
  127. * @param scanner - the scanner to use
  128. * @param version - the version of the document (XML 1.1 or XML 1.0).
  129. */
  130. public void startDocumentParsing(XMLEntityHandler scanner, short version){
  131. if (version == Constants.XML_VERSION_1_0){
  132. fEntityManager.setScannerVersion(Constants.XML_VERSION_1_0);
  133. }
  134. else {
  135. fEntityManager.setScannerVersion(Constants.XML_VERSION_1_1);
  136. }
  137. // Make sure the locator used by the error reporter is the current entity scanner.
  138. fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner());
  139. // Note: above we reset fEntityScanner in the entity manager, thus in startEntity
  140. // in each scanner fEntityScanner field must be reset to reflect the change.
  141. //
  142. fEntityManager.setEntityHandler(scanner);
  143. scanner.startEntity(fXMLSymbol, fEntityManager.getCurrentResourceIdentifier(), fEncoding, null);
  144. }
  145. /**
  146. * This methods scans the XML declaration to find out the version
  147. * (and provisional encoding) of the document.
  148. * The scanning is doing using XML 1.1 scanner.
  149. * @param inputSource
  150. * @return short - Constants.XML_VERSION_1_1 if document version 1.1,
  151. * otherwise Constants.XML_VERSION_1_0
  152. * @throws IOException
  153. */
  154. public short determineDocVersion(XMLInputSource inputSource) throws IOException {
  155. fEncoding = fEntityManager.setupCurrentEntity(fXMLSymbol, inputSource, false, true);
  156. // Must use XML 1.0 scanner to handle whitespace correctly
  157. // in the XML declaration.
  158. fEntityManager.setScannerVersion(Constants.XML_VERSION_1_0);
  159. XMLEntityScanner scanner = fEntityManager.getEntityScanner();
  160. try {
  161. if (!scanner.skipString("<?xml")) {
  162. // definitely not a well-formed 1.1 doc!
  163. return Constants.XML_VERSION_1_0;
  164. }
  165. if (!scanner.skipDeclSpaces()) {
  166. fixupCurrentEntity(fEntityManager, fExpectedVersionString, 5);
  167. return Constants.XML_VERSION_1_0;
  168. }
  169. if (!scanner.skipString("version")) {
  170. fixupCurrentEntity(fEntityManager, fExpectedVersionString, 6);
  171. return Constants.XML_VERSION_1_0;
  172. }
  173. scanner.skipDeclSpaces();
  174. // Check if the next character is '='. If it is then consume it.
  175. if (scanner.peekChar() != '=') {
  176. fixupCurrentEntity(fEntityManager, fExpectedVersionString, 13);
  177. return Constants.XML_VERSION_1_0;
  178. }
  179. scanner.scanChar();
  180. scanner.skipDeclSpaces();
  181. int quoteChar = scanner.scanChar();
  182. fExpectedVersionString[14] = (char) quoteChar;
  183. for (int versionPos = 0; versionPos < XML11_VERSION.length; versionPos++) {
  184. fExpectedVersionString[15 + versionPos] = (char) scanner.scanChar();
  185. }
  186. // REVISIT: should we check whether this equals quoteChar?
  187. fExpectedVersionString[18] = (char) scanner.scanChar();
  188. fixupCurrentEntity(fEntityManager, fExpectedVersionString, 19);
  189. int matched = 0;
  190. for (; matched < XML11_VERSION.length; matched++) {
  191. if (fExpectedVersionString[15 + matched] != XML11_VERSION[matched])
  192. break;
  193. }
  194. if (matched == XML11_VERSION.length)
  195. return Constants.XML_VERSION_1_1;
  196. return Constants.XML_VERSION_1_0;
  197. // premature end of file
  198. }
  199. catch (EOFException e) {
  200. fErrorReporter.reportError(
  201. XMLMessageFormatter.XML_DOMAIN,
  202. "PrematureEOF",
  203. null,
  204. XMLErrorReporter.SEVERITY_FATAL_ERROR);
  205. return Constants.XML_VERSION_1_0;
  206. }
  207. }
  208. // This method prepends "length" chars from the char array,
  209. // from offset 0, to the manager's fCurrentEntity.ch.
  210. private void fixupCurrentEntity(XMLEntityManager manager,
  211. char [] scannedChars, int length) {
  212. XMLEntityManager.ScannedEntity currentEntity = manager.getCurrentEntity();
  213. if(currentEntity.count-currentEntity.position+length > currentEntity.ch.length) {
  214. //resize array; this case is hard to imagine...
  215. char[] tempCh = currentEntity.ch;
  216. currentEntity.ch = new char[length+currentEntity.count-currentEntity.position+1];
  217. System.arraycopy(tempCh, 0, currentEntity.ch, 0, tempCh.length);
  218. }
  219. if(currentEntity.position < length) {
  220. // have to move sensitive stuff out of the way...
  221. System.arraycopy(currentEntity.ch, currentEntity.position, currentEntity.ch, length, currentEntity.count-currentEntity.position);
  222. currentEntity.count += length-currentEntity.position;
  223. } else {
  224. // have to reintroduce some whitespace so this parses:
  225. for(int i=length; i<currentEntity.position; i++)
  226. currentEntity.ch[i]=' ';
  227. }
  228. // prepend contents...
  229. System.arraycopy(scannedChars, 0, currentEntity.ch, 0, length);
  230. currentEntity.position = 0;
  231. currentEntity.columnNumber = currentEntity.lineNumber = 1;
  232. }
  233. } // class XMLVersionDetector