XMLVersionDetectorpublic class XMLVersionDetector extends Object This class scans the version of the document to determine
which scanner to use: XML 1.1 or XML 1.0.
The version is scanned using XML 1.1. scanner. |
Fields Summary |
---|
private static final char[] | XML11_VERSION | protected static final String | SYMBOL_TABLEProperty identifier: symbol table. | protected static final String | ERROR_REPORTERProperty identifier: error reporter. | protected static final String | ENTITY_MANAGERProperty identifier: entity manager. | protected static final String | fVersionSymbolSymbol: "version". | protected static final String | fXMLSymbol | protected org.apache.xerces.util.SymbolTable | fSymbolTableSymbol table. | protected XMLErrorReporter | fErrorReporterError reporter. | protected XMLEntityManager | fEntityManagerEntity manager. | protected String | fEncoding | private final char[] | fExpectedVersionString |
Methods Summary |
---|
public short | determineDocVersion(org.apache.xerces.xni.parser.XMLInputSource inputSource)This methods scans the XML declaration to find out the version
(and provisional encoding) of the document.
The scanning is doing using XML 1.1 scanner.
fEncoding = fEntityManager.setupCurrentEntity(fXMLSymbol, inputSource, false, true);
// Must use XML 1.0 scanner to handle whitespace correctly
// in the XML declaration.
fEntityManager.setScannerVersion(Constants.XML_VERSION_1_0);
XMLEntityScanner scanner = fEntityManager.getEntityScanner();
try {
if (!scanner.skipString("<?xml")) {
// definitely not a well-formed 1.1 doc!
return Constants.XML_VERSION_1_0;
}
if (!scanner.skipDeclSpaces()) {
fixupCurrentEntity(fEntityManager, fExpectedVersionString, 5);
return Constants.XML_VERSION_1_0;
}
if (!scanner.skipString("version")) {
fixupCurrentEntity(fEntityManager, fExpectedVersionString, 6);
return Constants.XML_VERSION_1_0;
}
scanner.skipDeclSpaces();
// Check if the next character is '='. If it is then consume it.
if (scanner.peekChar() != '=") {
fixupCurrentEntity(fEntityManager, fExpectedVersionString, 13);
return Constants.XML_VERSION_1_0;
}
scanner.scanChar();
scanner.skipDeclSpaces();
int quoteChar = scanner.scanChar();
fExpectedVersionString[14] = (char) quoteChar;
for (int versionPos = 0; versionPos < XML11_VERSION.length; versionPos++) {
fExpectedVersionString[15 + versionPos] = (char) scanner.scanChar();
}
// REVISIT: should we check whether this equals quoteChar?
fExpectedVersionString[18] = (char) scanner.scanChar();
fixupCurrentEntity(fEntityManager, fExpectedVersionString, 19);
int matched = 0;
for (; matched < XML11_VERSION.length; matched++) {
if (fExpectedVersionString[15 + matched] != XML11_VERSION[matched])
break;
}
return (matched == XML11_VERSION.length) ?
Constants.XML_VERSION_1_1 :
Constants.XML_VERSION_1_0;
}
// encoding errors
catch (MalformedByteSequenceException e) {
fErrorReporter.reportError(e.getDomain(), e.getKey(),
e.getArguments(), XMLErrorReporter.SEVERITY_FATAL_ERROR, e);
return Constants.XML_VERSION_ERROR;
}
catch (CharConversionException e) {
fErrorReporter.reportError(
XMLMessageFormatter.XML_DOMAIN,
"CharConversionFailure",
null,
XMLErrorReporter.SEVERITY_FATAL_ERROR, e);
return Constants.XML_VERSION_ERROR;
}
// premature end of file
catch (EOFException e) {
fErrorReporter.reportError(
XMLMessageFormatter.XML_DOMAIN,
"PrematureEOF",
null,
XMLErrorReporter.SEVERITY_FATAL_ERROR);
return Constants.XML_VERSION_ERROR;
}
| private void | fixupCurrentEntity(XMLEntityManager manager, char[] scannedChars, int length)
XMLEntityManager.ScannedEntity currentEntity = manager.getCurrentEntity();
if(currentEntity.count-currentEntity.position+length > currentEntity.ch.length) {
//resize array; this case is hard to imagine...
char[] tempCh = currentEntity.ch;
currentEntity.ch = new char[length+currentEntity.count-currentEntity.position+1];
System.arraycopy(tempCh, 0, currentEntity.ch, 0, tempCh.length);
}
if(currentEntity.position < length) {
// have to move sensitive stuff out of the way...
System.arraycopy(currentEntity.ch, currentEntity.position, currentEntity.ch, length, currentEntity.count-currentEntity.position);
currentEntity.count += length-currentEntity.position;
} else {
// have to reintroduce some whitespace so this parses:
for(int i=length; i<currentEntity.position; i++)
currentEntity.ch[i]=' ";
}
// prepend contents...
System.arraycopy(scannedChars, 0, currentEntity.ch, 0, length);
currentEntity.position = 0;
currentEntity.baseCharOffset = 0;
currentEntity.startPosition = 0;
currentEntity.columnNumber = currentEntity.lineNumber = 1;
| public void | reset(org.apache.xerces.xni.parser.XMLComponentManager componentManager)
// Xerces properties
fSymbolTable = (SymbolTable)componentManager.getProperty(SYMBOL_TABLE);
fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER);
fEntityManager = (XMLEntityManager)componentManager.getProperty(ENTITY_MANAGER);
for(int i=14; i<fExpectedVersionString.length; i++ )
fExpectedVersionString[i] = ' ";
| public void | startDocumentParsing(XMLEntityHandler scanner, short version)Reset the reference to the appropriate scanner given the version of the
document and start document scanning.
if (version == Constants.XML_VERSION_1_0){
fEntityManager.setScannerVersion(Constants.XML_VERSION_1_0);
}
else {
fEntityManager.setScannerVersion(Constants.XML_VERSION_1_1);
}
// Make sure the locator used by the error reporter is the current entity scanner.
fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner());
// Note: above we reset fEntityScanner in the entity manager, thus in startEntity
// in each scanner fEntityScanner field must be reset to reflect the change.
//
fEntityManager.setEntityHandler(scanner);
scanner.startEntity(fXMLSymbol, fEntityManager.getCurrentResourceIdentifier(), fEncoding, null);
|
|