0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: package org.apache.xerces.parsers;
0019:
0020: import java.io.IOException;
0021: import java.util.ArrayList;
0022: import java.util.HashMap;
0023: import java.util.Locale;
0024:
0025: import org.apache.xerces.impl.Constants;
0026: import org.apache.xerces.impl.XML11DTDScannerImpl;
0027: import org.apache.xerces.impl.XML11DocumentScannerImpl;
0028: import org.apache.xerces.impl.XML11NSDocumentScannerImpl;
0029: import org.apache.xerces.impl.XMLDTDScannerImpl;
0030: import org.apache.xerces.impl.XMLDocumentScannerImpl;
0031: import org.apache.xerces.impl.XMLEntityHandler;
0032: import org.apache.xerces.impl.XMLEntityManager;
0033: import org.apache.xerces.impl.XMLErrorReporter;
0034: import org.apache.xerces.impl.XMLNSDocumentScannerImpl;
0035: import org.apache.xerces.impl.XMLVersionDetector;
0036: import org.apache.xerces.impl.dtd.XML11DTDProcessor;
0037: import org.apache.xerces.impl.dtd.XML11DTDValidator;
0038: import org.apache.xerces.impl.dtd.XML11NSDTDValidator;
0039: import org.apache.xerces.impl.dtd.XMLDTDProcessor;
0040: import org.apache.xerces.impl.dtd.XMLDTDValidator;
0041: import org.apache.xerces.impl.dtd.XMLNSDTDValidator;
0042: import org.apache.xerces.impl.dv.DTDDVFactory;
0043: import org.apache.xerces.impl.msg.XMLMessageFormatter;
0044: import org.apache.xerces.impl.validation.ValidationManager;
0045: import org.apache.xerces.impl.xs.XMLSchemaValidator;
0046: import org.apache.xerces.impl.xs.XSMessageFormatter;
0047: import org.apache.xerces.util.ParserConfigurationSettings;
0048: import org.apache.xerces.util.SymbolTable;
0049: import org.apache.xerces.xni.XMLDTDContentModelHandler;
0050: import org.apache.xerces.xni.XMLDTDHandler;
0051: import org.apache.xerces.xni.XMLDocumentHandler;
0052: import org.apache.xerces.xni.XMLLocator;
0053: import org.apache.xerces.xni.XNIException;
0054: import org.apache.xerces.xni.grammars.XMLGrammarPool;
0055: import org.apache.xerces.xni.parser.XMLComponent;
0056: import org.apache.xerces.xni.parser.XMLComponentManager;
0057: import org.apache.xerces.xni.parser.XMLConfigurationException;
0058: import org.apache.xerces.xni.parser.XMLDTDScanner;
0059: import org.apache.xerces.xni.parser.XMLDocumentScanner;
0060: import org.apache.xerces.xni.parser.XMLDocumentSource;
0061: import org.apache.xerces.xni.parser.XMLEntityResolver;
0062: import org.apache.xerces.xni.parser.XMLErrorHandler;
0063: import org.apache.xerces.xni.parser.XMLInputSource;
0064: import org.apache.xerces.xni.parser.XMLPullParserConfiguration;
0065:
0066: /**
0067: * This class is the configuration used to parse XML 1.0 and XML 1.1 documents.
0068: *
0069: * @author Elena Litani, IBM
0070: * @author Neil Graham, IBM
0071: * @author Michael Glavassevich, IBM
0072: *
0073: * @version $Id: XML11Configuration.java 548192 2007-06-18 03:34:19Z mrglavas $
0074: */
0075: public class XML11Configuration extends ParserConfigurationSettings
0076: implements XMLPullParserConfiguration, XML11Configurable {
0077:
0078: //
0079: // Constants
0080: //
0081: protected final static String XML11_DATATYPE_VALIDATOR_FACTORY = "org.apache.xerces.impl.dv.dtd.XML11DTDDVFactoryImpl";
0082:
0083: // feature identifiers
0084:
0085: /** Feature identifier: warn on duplicate attribute definition. */
0086: protected static final String WARN_ON_DUPLICATE_ATTDEF = Constants.XERCES_FEATURE_PREFIX
0087: + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE;
0088:
0089: /** Feature identifier: warn on duplicate entity definition. */
0090: protected static final String WARN_ON_DUPLICATE_ENTITYDEF = Constants.XERCES_FEATURE_PREFIX
0091: + Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE;
0092:
0093: /** Feature identifier: warn on undeclared element definition. */
0094: protected static final String WARN_ON_UNDECLARED_ELEMDEF = Constants.XERCES_FEATURE_PREFIX
0095: + Constants.WARN_ON_UNDECLARED_ELEMDEF_FEATURE;
0096:
0097: /** Feature identifier: allow Java encodings. */
0098: protected static final String ALLOW_JAVA_ENCODINGS = Constants.XERCES_FEATURE_PREFIX
0099: + Constants.ALLOW_JAVA_ENCODINGS_FEATURE;
0100:
0101: /** Feature identifier: continue after fatal error. */
0102: protected static final String CONTINUE_AFTER_FATAL_ERROR = Constants.XERCES_FEATURE_PREFIX
0103: + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE;
0104:
0105: /** Feature identifier: load external DTD. */
0106: protected static final String LOAD_EXTERNAL_DTD = Constants.XERCES_FEATURE_PREFIX
0107: + Constants.LOAD_EXTERNAL_DTD_FEATURE;
0108:
0109: /** Feature identifier: notify built-in refereces. */
0110: protected static final String NOTIFY_BUILTIN_REFS = Constants.XERCES_FEATURE_PREFIX
0111: + Constants.NOTIFY_BUILTIN_REFS_FEATURE;
0112:
0113: /** Feature identifier: notify character refereces. */
0114: protected static final String NOTIFY_CHAR_REFS = Constants.XERCES_FEATURE_PREFIX
0115: + Constants.NOTIFY_CHAR_REFS_FEATURE;
0116:
0117: /** Feature identifier: expose schema normalized value */
0118: protected static final String NORMALIZE_DATA = Constants.XERCES_FEATURE_PREFIX
0119: + Constants.SCHEMA_NORMALIZED_VALUE;
0120:
0121: /** Feature identifier: send element default value via characters() */
0122: protected static final String SCHEMA_ELEMENT_DEFAULT = Constants.XERCES_FEATURE_PREFIX
0123: + Constants.SCHEMA_ELEMENT_DEFAULT;
0124:
0125: /** Feature identifier: augment PSVI */
0126: protected static final String SCHEMA_AUGMENT_PSVI = Constants.XERCES_FEATURE_PREFIX
0127: + Constants.SCHEMA_AUGMENT_PSVI;
0128:
0129: /** feature identifier: XML Schema validation */
0130: protected static final String XMLSCHEMA_VALIDATION = Constants.XERCES_FEATURE_PREFIX
0131: + Constants.SCHEMA_VALIDATION_FEATURE;
0132:
0133: /** feature identifier: XML Schema validation -- full checking */
0134: protected static final String XMLSCHEMA_FULL_CHECKING = Constants.XERCES_FEATURE_PREFIX
0135: + Constants.SCHEMA_FULL_CHECKING;
0136:
0137: /** Feature: generate synthetic annotations */
0138: protected static final String GENERATE_SYNTHETIC_ANNOTATIONS = Constants.XERCES_FEATURE_PREFIX
0139: + Constants.GENERATE_SYNTHETIC_ANNOTATIONS_FEATURE;
0140:
0141: /** Feature identifier: validate annotations */
0142: protected static final String VALIDATE_ANNOTATIONS = Constants.XERCES_FEATURE_PREFIX
0143: + Constants.VALIDATE_ANNOTATIONS_FEATURE;
0144:
0145: /** Feature identifier: honour all schemaLocations */
0146: protected static final String HONOUR_ALL_SCHEMALOCATIONS = Constants.XERCES_FEATURE_PREFIX
0147: + Constants.HONOUR_ALL_SCHEMALOCATIONS_FEATURE;
0148:
0149: /** Feature identifier: use grammar pool only */
0150: protected static final String USE_GRAMMAR_POOL_ONLY = Constants.XERCES_FEATURE_PREFIX
0151: + Constants.USE_GRAMMAR_POOL_ONLY_FEATURE;
0152:
0153: // feature identifiers
0154:
0155: /** Feature identifier: validation. */
0156: protected static final String VALIDATION = Constants.SAX_FEATURE_PREFIX
0157: + Constants.VALIDATION_FEATURE;
0158:
0159: /** Feature identifier: namespaces. */
0160: protected static final String NAMESPACES = Constants.SAX_FEATURE_PREFIX
0161: + Constants.NAMESPACES_FEATURE;
0162:
0163: /** Feature identifier: external general entities. */
0164: protected static final String EXTERNAL_GENERAL_ENTITIES = Constants.SAX_FEATURE_PREFIX
0165: + Constants.EXTERNAL_GENERAL_ENTITIES_FEATURE;
0166:
0167: /** Feature identifier: external parameter entities. */
0168: protected static final String EXTERNAL_PARAMETER_ENTITIES = Constants.SAX_FEATURE_PREFIX
0169: + Constants.EXTERNAL_PARAMETER_ENTITIES_FEATURE;
0170:
0171: /** Feature identifier: whether to ignore xsi:type attributes until a global element declaration is encountered */
0172: protected static final String IGNORE_XSI_TYPE = Constants.XERCES_FEATURE_PREFIX
0173: + Constants.IGNORE_XSI_TYPE_FEATURE;
0174:
0175: /** Feature identifier: whether to ignore ID/IDREF errors */
0176: protected static final String ID_IDREF_CHECKING = Constants.XERCES_FEATURE_PREFIX
0177: + Constants.ID_IDREF_CHECKING_FEATURE;
0178:
0179: /** Feature identifier: whether to ignore unparsed entity errors */
0180: protected static final String UNPARSED_ENTITY_CHECKING = Constants.XERCES_FEATURE_PREFIX
0181: + Constants.UNPARSED_ENTITY_CHECKING_FEATURE;
0182:
0183: /** Feature identifier: whether to ignore identity constraint errors */
0184: protected static final String IDENTITY_CONSTRAINT_CHECKING = Constants.XERCES_FEATURE_PREFIX
0185: + Constants.IDC_CHECKING_FEATURE;
0186:
0187: // property identifiers
0188:
0189: /** Property identifier: xml string. */
0190: protected static final String XML_STRING = Constants.SAX_PROPERTY_PREFIX
0191: + Constants.XML_STRING_PROPERTY;
0192:
0193: /** Property identifier: symbol table. */
0194: protected static final String SYMBOL_TABLE = Constants.XERCES_PROPERTY_PREFIX
0195: + Constants.SYMBOL_TABLE_PROPERTY;
0196:
0197: /** Property identifier: error handler. */
0198: protected static final String ERROR_HANDLER = Constants.XERCES_PROPERTY_PREFIX
0199: + Constants.ERROR_HANDLER_PROPERTY;
0200:
0201: /** Property identifier: entity resolver. */
0202: protected static final String ENTITY_RESOLVER = Constants.XERCES_PROPERTY_PREFIX
0203: + Constants.ENTITY_RESOLVER_PROPERTY;
0204:
0205: /** Property identifier: XML Schema validator. */
0206: protected static final String SCHEMA_VALIDATOR = Constants.XERCES_PROPERTY_PREFIX
0207: + Constants.SCHEMA_VALIDATOR_PROPERTY;
0208:
0209: /** Property identifier: schema location. */
0210: protected static final String SCHEMA_LOCATION = Constants.XERCES_PROPERTY_PREFIX
0211: + Constants.SCHEMA_LOCATION;
0212:
0213: /** Property identifier: no namespace schema location. */
0214: protected static final String SCHEMA_NONS_LOCATION = Constants.XERCES_PROPERTY_PREFIX
0215: + Constants.SCHEMA_NONS_LOCATION;
0216:
0217: // property identifiers
0218:
0219: /** Property identifier: error reporter. */
0220: protected static final String ERROR_REPORTER = Constants.XERCES_PROPERTY_PREFIX
0221: + Constants.ERROR_REPORTER_PROPERTY;
0222:
0223: /** Property identifier: entity manager. */
0224: protected static final String ENTITY_MANAGER = Constants.XERCES_PROPERTY_PREFIX
0225: + Constants.ENTITY_MANAGER_PROPERTY;
0226:
0227: /** Property identifier document scanner: */
0228: protected static final String DOCUMENT_SCANNER = Constants.XERCES_PROPERTY_PREFIX
0229: + Constants.DOCUMENT_SCANNER_PROPERTY;
0230:
0231: /** Property identifier: DTD scanner. */
0232: protected static final String DTD_SCANNER = Constants.XERCES_PROPERTY_PREFIX
0233: + Constants.DTD_SCANNER_PROPERTY;
0234:
0235: /** Property identifier: grammar pool. */
0236: protected static final String XMLGRAMMAR_POOL = Constants.XERCES_PROPERTY_PREFIX
0237: + Constants.XMLGRAMMAR_POOL_PROPERTY;
0238:
0239: /** Property identifier: DTD loader. */
0240: protected static final String DTD_PROCESSOR = Constants.XERCES_PROPERTY_PREFIX
0241: + Constants.DTD_PROCESSOR_PROPERTY;
0242:
0243: /** Property identifier: DTD validator. */
0244: protected static final String DTD_VALIDATOR = Constants.XERCES_PROPERTY_PREFIX
0245: + Constants.DTD_VALIDATOR_PROPERTY;
0246:
0247: /** Property identifier: namespace binder. */
0248: protected static final String NAMESPACE_BINDER = Constants.XERCES_PROPERTY_PREFIX
0249: + Constants.NAMESPACE_BINDER_PROPERTY;
0250:
0251: /** Property identifier: datatype validator factory. */
0252: protected static final String DATATYPE_VALIDATOR_FACTORY = Constants.XERCES_PROPERTY_PREFIX
0253: + Constants.DATATYPE_VALIDATOR_FACTORY_PROPERTY;
0254:
0255: protected static final String VALIDATION_MANAGER = Constants.XERCES_PROPERTY_PREFIX
0256: + Constants.VALIDATION_MANAGER_PROPERTY;
0257:
0258: /** Property identifier: JAXP schema language / DOM schema-type. */
0259: protected static final String JAXP_SCHEMA_LANGUAGE = Constants.JAXP_PROPERTY_PREFIX
0260: + Constants.SCHEMA_LANGUAGE;
0261:
0262: /** Property identifier: JAXP schema source/ DOM schema-location. */
0263: protected static final String JAXP_SCHEMA_SOURCE = Constants.JAXP_PROPERTY_PREFIX
0264: + Constants.SCHEMA_SOURCE;
0265:
0266: /** Property identifier: root type definition. */
0267: protected static final String ROOT_TYPE_DEF = Constants.XERCES_PROPERTY_PREFIX
0268: + Constants.ROOT_TYPE_DEFINITION_PROPERTY;
0269:
0270: // debugging
0271:
0272: /** Set to true and recompile to print exception stack trace. */
0273: protected static final boolean PRINT_EXCEPTION_STACK_TRACE = false;
0274:
0275: //
0276: // Data
0277: //
0278:
0279: protected SymbolTable fSymbolTable;
0280: protected XMLInputSource fInputSource;
0281: protected ValidationManager fValidationManager;
0282: protected XMLVersionDetector fVersionDetector;
0283: protected XMLLocator fLocator;
0284: protected Locale fLocale;
0285:
0286: /** XML 1.0 Components. */
0287: protected ArrayList fComponents;
0288:
0289: /** XML 1.1. Components. */
0290: protected ArrayList fXML11Components = null;
0291:
0292: /** Common components: XMLEntityManager, XMLErrorReporter, XMLSchemaValidator */
0293: protected ArrayList fCommonComponents = null;
0294:
0295: /** The document handler. */
0296: protected XMLDocumentHandler fDocumentHandler;
0297:
0298: /** The DTD handler. */
0299: protected XMLDTDHandler fDTDHandler;
0300:
0301: /** The DTD content model handler. */
0302: protected XMLDTDContentModelHandler fDTDContentModelHandler;
0303:
0304: /** Last component in the document pipeline */
0305: protected XMLDocumentSource fLastComponent;
0306:
0307: /**
0308: * True if a parse is in progress. This state is needed because
0309: * some features/properties cannot be set while parsing (e.g.
0310: * validation and namespaces).
0311: */
0312: protected boolean fParseInProgress = false;
0313:
0314: /** fConfigUpdated is set to true if there has been any change to the configuration settings,
0315: * i.e a feature or a property was changed.
0316: */
0317: protected boolean fConfigUpdated = false;
0318:
0319: //
0320: // XML 1.0 components
0321: //
0322:
0323: /** The XML 1.0 Datatype validator factory. */
0324: protected DTDDVFactory fDatatypeValidatorFactory;
0325:
0326: /** The XML 1.0 Document scanner that does namespace binding. */
0327: protected XMLNSDocumentScannerImpl fNamespaceScanner;
0328: /** The XML 1.0 Non-namespace implementation of scanner */
0329: protected XMLDocumentScannerImpl fNonNSScanner;
0330: /** The XML 1.0 DTD Validator: binds namespaces */
0331: protected XMLDTDValidator fDTDValidator;
0332: /** The XML 1.0 DTD Validator that does not bind namespaces */
0333: protected XMLDTDValidator fNonNSDTDValidator;
0334: /** The XML 1.0 DTD scanner. */
0335: protected XMLDTDScanner fDTDScanner;
0336: /** The XML 1.0 DTD Processor . */
0337: protected XMLDTDProcessor fDTDProcessor;
0338:
0339: //
0340: // XML 1.1 components
0341: //
0342:
0343: /** The XML 1.1 datatype factory. **/
0344: protected DTDDVFactory fXML11DatatypeFactory = null;
0345:
0346: /** The XML 1.1 document scanner that does namespace binding. **/
0347: protected XML11NSDocumentScannerImpl fXML11NSDocScanner = null;
0348:
0349: /** The XML 1.1 document scanner that does not do namespace binding. **/
0350: protected XML11DocumentScannerImpl fXML11DocScanner = null;
0351:
0352: /** The XML 1.1 DTD validator that does namespace binding. **/
0353: protected XML11NSDTDValidator fXML11NSDTDValidator = null;
0354:
0355: /** The XML 1.1 DTD validator that does not do namespace binding. **/
0356: protected XML11DTDValidator fXML11DTDValidator = null;
0357:
0358: /** The XML 1.1 DTD scanner. **/
0359: protected XML11DTDScannerImpl fXML11DTDScanner = null;
0360: /** The XML 1.1 DTD processor. **/
0361: protected XML11DTDProcessor fXML11DTDProcessor = null;
0362:
0363: //
0364: // Common components
0365: //
0366:
0367: /** Grammar pool. */
0368: protected XMLGrammarPool fGrammarPool;
0369:
0370: /** Error reporter. */
0371: protected XMLErrorReporter fErrorReporter;
0372:
0373: /** Entity manager. */
0374: protected XMLEntityManager fEntityManager;
0375:
0376: /** XML Schema Validator. */
0377: protected XMLSchemaValidator fSchemaValidator;
0378:
0379: /** Current scanner */
0380: protected XMLDocumentScanner fCurrentScanner;
0381: /** Current Datatype validator factory. */
0382: protected DTDDVFactory fCurrentDVFactory;
0383: /** Current DTD scanner. */
0384: protected XMLDTDScanner fCurrentDTDScanner;
0385:
0386: /** Flag indiciating whether XML11 components have been initialized. */
0387: private boolean f11Initialized = false;
0388:
0389: //
0390: // Constructors
0391: //
0392:
0393: /** Default constructor. */
0394: public XML11Configuration() {
0395: this (null, null, null);
0396: } // <init>()
0397:
0398: /**
0399: * Constructs a parser configuration using the specified symbol table.
0400: *
0401: * @param symbolTable The symbol table to use.
0402: */
0403: public XML11Configuration(SymbolTable symbolTable) {
0404: this (symbolTable, null, null);
0405: } // <init>(SymbolTable)
0406:
0407: /**
0408: * Constructs a parser configuration using the specified symbol table and
0409: * grammar pool.
0410: * <p>
0411: * <strong>REVISIT:</strong>
0412: * Grammar pool will be updated when the new validation engine is
0413: * implemented.
0414: *
0415: * @param symbolTable The symbol table to use.
0416: * @param grammarPool The grammar pool to use.
0417: */
0418: public XML11Configuration(SymbolTable symbolTable,
0419: XMLGrammarPool grammarPool) {
0420: this (symbolTable, grammarPool, null);
0421: } // <init>(SymbolTable,XMLGrammarPool)
0422:
0423: /**
0424: * Constructs a parser configuration using the specified symbol table,
0425: * grammar pool, and parent settings.
0426: * <p>
0427: * <strong>REVISIT:</strong>
0428: * Grammar pool will be updated when the new validation engine is
0429: * implemented.
0430: *
0431: * @param symbolTable The symbol table to use.
0432: * @param grammarPool The grammar pool to use.
0433: * @param parentSettings The parent settings.
0434: */
0435: public XML11Configuration(SymbolTable symbolTable,
0436: XMLGrammarPool grammarPool,
0437: XMLComponentManager parentSettings) {
0438:
0439: super (parentSettings);
0440:
0441: // create a vector to hold all the components in use
0442: // XML 1.0 specialized components
0443: fComponents = new ArrayList();
0444: // XML 1.1 specialized components
0445: fXML11Components = new ArrayList();
0446: // Common components for XML 1.1. and XML 1.0
0447: fCommonComponents = new ArrayList();
0448:
0449: // create storage for recognized features and properties
0450: fRecognizedFeatures = new ArrayList();
0451: fRecognizedProperties = new ArrayList();
0452:
0453: // create table for features and properties
0454: fFeatures = new HashMap();
0455: fProperties = new HashMap();
0456:
0457: // add default recognized features
0458: final String[] recognizedFeatures = {
0459: CONTINUE_AFTER_FATAL_ERROR,
0460: LOAD_EXTERNAL_DTD, // from XMLDTDScannerImpl
0461: VALIDATION,
0462: NAMESPACES,
0463: NORMALIZE_DATA,
0464: SCHEMA_ELEMENT_DEFAULT,
0465: SCHEMA_AUGMENT_PSVI,
0466: GENERATE_SYNTHETIC_ANNOTATIONS,
0467: VALIDATE_ANNOTATIONS,
0468: HONOUR_ALL_SCHEMALOCATIONS,
0469: IGNORE_XSI_TYPE,
0470: ID_IDREF_CHECKING,
0471: IDENTITY_CONSTRAINT_CHECKING,
0472: UNPARSED_ENTITY_CHECKING,
0473: USE_GRAMMAR_POOL_ONLY,
0474: // NOTE: These shouldn't really be here but since the XML Schema
0475: // validator is constructed dynamically, its recognized
0476: // features might not have been set and it would cause a
0477: // not-recognized exception to be thrown. -Ac
0478: XMLSCHEMA_VALIDATION, XMLSCHEMA_FULL_CHECKING,
0479: EXTERNAL_GENERAL_ENTITIES, EXTERNAL_PARAMETER_ENTITIES,
0480: PARSER_SETTINGS,
0481:
0482: };
0483: addRecognizedFeatures(recognizedFeatures);
0484: // set state for default features
0485: fFeatures.put(VALIDATION, Boolean.FALSE);
0486: fFeatures.put(NAMESPACES, Boolean.TRUE);
0487: fFeatures.put(EXTERNAL_GENERAL_ENTITIES, Boolean.TRUE);
0488: fFeatures.put(EXTERNAL_PARAMETER_ENTITIES, Boolean.TRUE);
0489: fFeatures.put(CONTINUE_AFTER_FATAL_ERROR, Boolean.FALSE);
0490: fFeatures.put(LOAD_EXTERNAL_DTD, Boolean.TRUE);
0491: fFeatures.put(SCHEMA_ELEMENT_DEFAULT, Boolean.TRUE);
0492: fFeatures.put(NORMALIZE_DATA, Boolean.TRUE);
0493: fFeatures.put(SCHEMA_AUGMENT_PSVI, Boolean.TRUE);
0494: fFeatures.put(GENERATE_SYNTHETIC_ANNOTATIONS, Boolean.FALSE);
0495: fFeatures.put(VALIDATE_ANNOTATIONS, Boolean.FALSE);
0496: fFeatures.put(HONOUR_ALL_SCHEMALOCATIONS, Boolean.FALSE);
0497: fFeatures.put(IGNORE_XSI_TYPE, Boolean.FALSE);
0498: fFeatures.put(ID_IDREF_CHECKING, Boolean.TRUE);
0499: fFeatures.put(IDENTITY_CONSTRAINT_CHECKING, Boolean.TRUE);
0500: fFeatures.put(UNPARSED_ENTITY_CHECKING, Boolean.TRUE);
0501: fFeatures.put(USE_GRAMMAR_POOL_ONLY, Boolean.FALSE);
0502: fFeatures.put(PARSER_SETTINGS, Boolean.TRUE);
0503:
0504: // add default recognized properties
0505: final String[] recognizedProperties = { SYMBOL_TABLE,
0506: ERROR_HANDLER, ENTITY_RESOLVER, ERROR_REPORTER,
0507: ENTITY_MANAGER, DOCUMENT_SCANNER, DTD_SCANNER,
0508: DTD_PROCESSOR, DTD_VALIDATOR,
0509: DATATYPE_VALIDATOR_FACTORY, VALIDATION_MANAGER,
0510: SCHEMA_VALIDATOR, XML_STRING, XMLGRAMMAR_POOL,
0511: JAXP_SCHEMA_SOURCE, JAXP_SCHEMA_LANGUAGE,
0512: // NOTE: These shouldn't really be here but since the XML Schema
0513: // validator is constructed dynamically, its recognized
0514: // properties might not have been set and it would cause a
0515: // not-recognized exception to be thrown. -Ac
0516: SCHEMA_LOCATION, SCHEMA_NONS_LOCATION, ROOT_TYPE_DEF, };
0517: addRecognizedProperties(recognizedProperties);
0518:
0519: if (symbolTable == null) {
0520: symbolTable = new SymbolTable();
0521: }
0522: fSymbolTable = symbolTable;
0523: fProperties.put(SYMBOL_TABLE, fSymbolTable);
0524:
0525: fGrammarPool = grammarPool;
0526: if (fGrammarPool != null) {
0527: fProperties.put(XMLGRAMMAR_POOL, fGrammarPool);
0528: }
0529:
0530: fEntityManager = new XMLEntityManager();
0531: fProperties.put(ENTITY_MANAGER, fEntityManager);
0532: addCommonComponent(fEntityManager);
0533:
0534: fErrorReporter = new XMLErrorReporter();
0535: fErrorReporter.setDocumentLocator(fEntityManager
0536: .getEntityScanner());
0537: fProperties.put(ERROR_REPORTER, fErrorReporter);
0538: addCommonComponent(fErrorReporter);
0539:
0540: fNamespaceScanner = new XMLNSDocumentScannerImpl();
0541: fProperties.put(DOCUMENT_SCANNER, fNamespaceScanner);
0542: addComponent((XMLComponent) fNamespaceScanner);
0543:
0544: fDTDScanner = new XMLDTDScannerImpl();
0545: fProperties.put(DTD_SCANNER, fDTDScanner);
0546: addComponent((XMLComponent) fDTDScanner);
0547:
0548: fDTDProcessor = new XMLDTDProcessor();
0549: fProperties.put(DTD_PROCESSOR, fDTDProcessor);
0550: addComponent(fDTDProcessor);
0551:
0552: fDTDValidator = new XMLNSDTDValidator();
0553: fProperties.put(DTD_VALIDATOR, fDTDValidator);
0554: addComponent(fDTDValidator);
0555:
0556: fDatatypeValidatorFactory = DTDDVFactory.getInstance();
0557: fProperties.put(DATATYPE_VALIDATOR_FACTORY,
0558: fDatatypeValidatorFactory);
0559:
0560: fValidationManager = new ValidationManager();
0561: fProperties.put(VALIDATION_MANAGER, fValidationManager);
0562:
0563: fVersionDetector = new XMLVersionDetector();
0564:
0565: // add message formatters
0566: if (fErrorReporter
0567: .getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) {
0568: XMLMessageFormatter xmft = new XMLMessageFormatter();
0569: fErrorReporter.putMessageFormatter(
0570: XMLMessageFormatter.XML_DOMAIN, xmft);
0571: fErrorReporter.putMessageFormatter(
0572: XMLMessageFormatter.XMLNS_DOMAIN, xmft);
0573: }
0574:
0575: // set locale
0576: try {
0577: setLocale(Locale.getDefault());
0578: } catch (XNIException e) {
0579: // do nothing
0580: // REVISIT: What is the right thing to do? -Ac
0581: }
0582:
0583: fConfigUpdated = false;
0584:
0585: } // <init>(SymbolTable,XMLGrammarPool)
0586:
0587: //
0588: // Public methods
0589: //
0590: /**
0591: * Sets the input source for the document to parse.
0592: *
0593: * @param inputSource The document's input source.
0594: *
0595: * @exception XMLConfigurationException Thrown if there is a
0596: * configuration error when initializing the
0597: * parser.
0598: * @exception IOException Thrown on I/O error.
0599: *
0600: * @see #parse(boolean)
0601: */
0602: public void setInputSource(XMLInputSource inputSource)
0603: throws XMLConfigurationException, IOException {
0604:
0605: // REVISIT: this method used to reset all the components and
0606: // construct the pipeline. Now reset() is called
0607: // in parse (boolean) just before we parse the document
0608: // Should this method still throw exceptions..?
0609:
0610: fInputSource = inputSource;
0611:
0612: } // setInputSource(XMLInputSource)
0613:
0614: /**
0615: * Set the locale to use for messages.
0616: *
0617: * @param locale The locale object to use for localization of messages.
0618: *
0619: * @exception XNIException Thrown if the parser does not support the
0620: * specified locale.
0621: */
0622: public void setLocale(Locale locale) throws XNIException {
0623: fLocale = locale;
0624: fErrorReporter.setLocale(locale);
0625: } // setLocale(Locale)
0626:
0627: /**
0628: * Sets the document handler on the last component in the pipeline
0629: * to receive information about the document.
0630: *
0631: * @param documentHandler The document handler.
0632: */
0633: public void setDocumentHandler(XMLDocumentHandler documentHandler) {
0634: fDocumentHandler = documentHandler;
0635: if (fLastComponent != null) {
0636: fLastComponent.setDocumentHandler(fDocumentHandler);
0637: if (fDocumentHandler != null) {
0638: fDocumentHandler.setDocumentSource(fLastComponent);
0639: }
0640: }
0641: } // setDocumentHandler(XMLDocumentHandler)
0642:
0643: /** Returns the registered document handler. */
0644: public XMLDocumentHandler getDocumentHandler() {
0645: return fDocumentHandler;
0646: } // getDocumentHandler():XMLDocumentHandler
0647:
0648: /**
0649: * Sets the DTD handler.
0650: *
0651: * @param dtdHandler The DTD handler.
0652: */
0653: public void setDTDHandler(XMLDTDHandler dtdHandler) {
0654: fDTDHandler = dtdHandler;
0655: } // setDTDHandler(XMLDTDHandler)
0656:
0657: /** Returns the registered DTD handler. */
0658: public XMLDTDHandler getDTDHandler() {
0659: return fDTDHandler;
0660: } // getDTDHandler():XMLDTDHandler
0661:
0662: /**
0663: * Sets the DTD content model handler.
0664: *
0665: * @param handler The DTD content model handler.
0666: */
0667: public void setDTDContentModelHandler(
0668: XMLDTDContentModelHandler handler) {
0669: fDTDContentModelHandler = handler;
0670: } // setDTDContentModelHandler(XMLDTDContentModelHandler)
0671:
0672: /** Returns the registered DTD content model handler. */
0673: public XMLDTDContentModelHandler getDTDContentModelHandler() {
0674: return fDTDContentModelHandler;
0675: } // getDTDContentModelHandler():XMLDTDContentModelHandler
0676:
0677: /**
0678: * Sets the resolver used to resolve external entities. The EntityResolver
0679: * interface supports resolution of public and system identifiers.
0680: *
0681: * @param resolver The new entity resolver. Passing a null value will
0682: * uninstall the currently installed resolver.
0683: */
0684: public void setEntityResolver(XMLEntityResolver resolver) {
0685: fProperties.put(ENTITY_RESOLVER, resolver);
0686: } // setEntityResolver(XMLEntityResolver)
0687:
0688: /**
0689: * Return the current entity resolver.
0690: *
0691: * @return The current entity resolver, or null if none
0692: * has been registered.
0693: * @see #setEntityResolver
0694: */
0695: public XMLEntityResolver getEntityResolver() {
0696: return (XMLEntityResolver) fProperties.get(ENTITY_RESOLVER);
0697: } // getEntityResolver():XMLEntityResolver
0698:
0699: /**
0700: * Allow an application to register an error event handler.
0701: *
0702: * <p>If the application does not register an error handler, all
0703: * error events reported by the SAX parser will be silently
0704: * ignored; however, normal processing may not continue. It is
0705: * highly recommended that all SAX applications implement an
0706: * error handler to avoid unexpected bugs.</p>
0707: *
0708: * <p>Applications may register a new or different handler in the
0709: * middle of a parse, and the SAX parser must begin using the new
0710: * handler immediately.</p>
0711: *
0712: * @param errorHandler The error handler.
0713: * @exception java.lang.NullPointerException If the handler
0714: * argument is null.
0715: * @see #getErrorHandler
0716: */
0717: public void setErrorHandler(XMLErrorHandler errorHandler) {
0718: fProperties.put(ERROR_HANDLER, errorHandler);
0719: } // setErrorHandler(XMLErrorHandler)
0720:
0721: /**
0722: * Return the current error handler.
0723: *
0724: * @return The current error handler, or null if none
0725: * has been registered.
0726: * @see #setErrorHandler
0727: */
0728: public XMLErrorHandler getErrorHandler() {
0729: // REVISIT: Should this be a property?
0730: return (XMLErrorHandler) fProperties.get(ERROR_HANDLER);
0731: } // getErrorHandler():XMLErrorHandler
0732:
0733: /**
0734: * If the application decides to terminate parsing before the xml document
0735: * is fully parsed, the application should call this method to free any
0736: * resource allocated during parsing. For example, close all opened streams.
0737: */
0738: public void cleanup() {
0739: fEntityManager.closeReaders();
0740: }
0741:
0742: /**
0743: * Parses the specified input source.
0744: *
0745: * @param source The input source.
0746: *
0747: * @exception XNIException Throws exception on XNI error.
0748: * @exception java.io.IOException Throws exception on i/o error.
0749: */
0750: public void parse(XMLInputSource source) throws XNIException,
0751: IOException {
0752:
0753: if (fParseInProgress) {
0754: // REVISIT - need to add new error message
0755: throw new XNIException(
0756: "FWK005 parse may not be called while parsing.");
0757: }
0758: fParseInProgress = true;
0759:
0760: try {
0761: setInputSource(source);
0762: parse(true);
0763: } catch (XNIException ex) {
0764: if (PRINT_EXCEPTION_STACK_TRACE)
0765: ex.printStackTrace();
0766: throw ex;
0767: } catch (IOException ex) {
0768: if (PRINT_EXCEPTION_STACK_TRACE)
0769: ex.printStackTrace();
0770: throw ex;
0771: } catch (RuntimeException ex) {
0772: if (PRINT_EXCEPTION_STACK_TRACE)
0773: ex.printStackTrace();
0774: throw ex;
0775: } catch (Exception ex) {
0776: if (PRINT_EXCEPTION_STACK_TRACE)
0777: ex.printStackTrace();
0778: throw new XNIException(ex);
0779: } finally {
0780: fParseInProgress = false;
0781: // close all streams opened by xerces
0782: this .cleanup();
0783: }
0784:
0785: } // parse(InputSource)
0786:
0787: public boolean parse(boolean complete) throws XNIException,
0788: IOException {
0789: //
0790: // reset and configure pipeline and set InputSource.
0791: if (fInputSource != null) {
0792: try {
0793: fValidationManager.reset();
0794: fVersionDetector.reset(this );
0795: resetCommon();
0796:
0797: short version = fVersionDetector
0798: .determineDocVersion(fInputSource);
0799: // XML 1.0
0800: if (version == Constants.XML_VERSION_1_0) {
0801: configurePipeline();
0802: reset();
0803: }
0804: // XML 1.1
0805: else if (version == Constants.XML_VERSION_1_1) {
0806: initXML11Components();
0807: configureXML11Pipeline();
0808: resetXML11();
0809: }
0810: // Unrecoverable error reported during version detection
0811: else {
0812: return false;
0813: }
0814:
0815: // mark configuration as fixed
0816: fConfigUpdated = false;
0817:
0818: // resets and sets the pipeline.
0819: fVersionDetector.startDocumentParsing(
0820: (XMLEntityHandler) fCurrentScanner, version);
0821: fInputSource = null;
0822: } catch (XNIException ex) {
0823: if (PRINT_EXCEPTION_STACK_TRACE)
0824: ex.printStackTrace();
0825: throw ex;
0826: } catch (IOException ex) {
0827: if (PRINT_EXCEPTION_STACK_TRACE)
0828: ex.printStackTrace();
0829: throw ex;
0830: } catch (RuntimeException ex) {
0831: if (PRINT_EXCEPTION_STACK_TRACE)
0832: ex.printStackTrace();
0833: throw ex;
0834: } catch (Exception ex) {
0835: if (PRINT_EXCEPTION_STACK_TRACE)
0836: ex.printStackTrace();
0837: throw new XNIException(ex);
0838: }
0839: }
0840:
0841: try {
0842: return fCurrentScanner.scanDocument(complete);
0843: } catch (XNIException ex) {
0844: if (PRINT_EXCEPTION_STACK_TRACE)
0845: ex.printStackTrace();
0846: throw ex;
0847: } catch (IOException ex) {
0848: if (PRINT_EXCEPTION_STACK_TRACE)
0849: ex.printStackTrace();
0850: throw ex;
0851: } catch (RuntimeException ex) {
0852: if (PRINT_EXCEPTION_STACK_TRACE)
0853: ex.printStackTrace();
0854: throw ex;
0855: } catch (Exception ex) {
0856: if (PRINT_EXCEPTION_STACK_TRACE)
0857: ex.printStackTrace();
0858: throw new XNIException(ex);
0859: }
0860:
0861: } // parse(boolean):boolean
0862:
0863: /**
0864: * Returns the state of a feature.
0865: *
0866: * @param featureId The feature identifier.
0867: * @return true if the feature is supported
0868: *
0869: * @throws XMLConfigurationException Thrown for configuration error.
0870: * In general, components should
0871: * only throw this exception if
0872: * it is <strong>really</strong>
0873: * a critical error.
0874: */
0875: public boolean getFeature(String featureId)
0876: throws XMLConfigurationException {
0877: // make this feature special
0878: if (featureId.equals(PARSER_SETTINGS)) {
0879: return fConfigUpdated;
0880: }
0881: return super .getFeature(featureId);
0882:
0883: } // getFeature(String):boolean
0884:
0885: /**
0886: * Set the state of a feature.
0887: *
0888: * Set the state of any feature in a SAX2 parser. The parser
0889: * might not recognize the feature, and if it does recognize
0890: * it, it might not be able to fulfill the request.
0891: *
0892: * @param featureId The unique identifier (URI) of the feature.
0893: * @param state The requested state of the feature (true or false).
0894: *
0895: * @exception org.apache.xerces.xni.parser.XMLConfigurationException If the
0896: * requested feature is not known.
0897: */
0898: public void setFeature(String featureId, boolean state)
0899: throws XMLConfigurationException {
0900: fConfigUpdated = true;
0901: // forward to every XML 1.0 component
0902: int count = fComponents.size();
0903: for (int i = 0; i < count; i++) {
0904: XMLComponent c = (XMLComponent) fComponents.get(i);
0905: c.setFeature(featureId, state);
0906: }
0907: // forward it to common components
0908: count = fCommonComponents.size();
0909: for (int i = 0; i < count; i++) {
0910: XMLComponent c = (XMLComponent) fCommonComponents.get(i);
0911: c.setFeature(featureId, state);
0912: }
0913:
0914: // forward to every XML 1.1 component
0915: count = fXML11Components.size();
0916: for (int i = 0; i < count; i++) {
0917: XMLComponent c = (XMLComponent) fXML11Components.get(i);
0918: try {
0919: c.setFeature(featureId, state);
0920: } catch (Exception e) {
0921: // no op
0922: }
0923: }
0924: // save state if noone "objects"
0925: super .setFeature(featureId, state);
0926:
0927: } // setFeature(String,boolean)
0928:
0929: /**
0930: * setProperty
0931: *
0932: * @param propertyId
0933: * @param value
0934: */
0935: public void setProperty(String propertyId, Object value)
0936: throws XMLConfigurationException {
0937: fConfigUpdated = true;
0938: // forward to every XML 1.0 component
0939: int count = fComponents.size();
0940: for (int i = 0; i < count; i++) {
0941: XMLComponent c = (XMLComponent) fComponents.get(i);
0942: c.setProperty(propertyId, value);
0943: }
0944: // forward it to every common Component
0945: count = fCommonComponents.size();
0946: for (int i = 0; i < count; i++) {
0947: XMLComponent c = (XMLComponent) fCommonComponents.get(i);
0948: c.setProperty(propertyId, value);
0949: }
0950: // forward it to every XML 1.1 component
0951: count = fXML11Components.size();
0952: for (int i = 0; i < count; i++) {
0953: XMLComponent c = (XMLComponent) fXML11Components.get(i);
0954: try {
0955: c.setProperty(propertyId, value);
0956: } catch (Exception e) {
0957: // ignore it
0958: }
0959: }
0960:
0961: // store value if noone "objects"
0962: super .setProperty(propertyId, value);
0963:
0964: } // setProperty(String,Object)
0965:
0966: /** Returns the locale. */
0967: public Locale getLocale() {
0968: return fLocale;
0969: } // getLocale():Locale
0970:
0971: /**
0972: * reset all XML 1.0 components before parsing and namespace context
0973: */
0974: protected void reset() throws XNIException {
0975: int count = fComponents.size();
0976: for (int i = 0; i < count; i++) {
0977: XMLComponent c = (XMLComponent) fComponents.get(i);
0978: c.reset(this );
0979: }
0980:
0981: } // reset()
0982:
0983: /**
0984: * reset all common components before parsing
0985: */
0986: protected void resetCommon() throws XNIException {
0987: // reset common components
0988: int count = fCommonComponents.size();
0989: for (int i = 0; i < count; i++) {
0990: XMLComponent c = (XMLComponent) fCommonComponents.get(i);
0991: c.reset(this );
0992: }
0993:
0994: } // resetCommon()
0995:
0996: /**
0997: * reset all components before parsing and namespace context
0998: */
0999: protected void resetXML11() throws XNIException {
1000: // reset every component
1001: int count = fXML11Components.size();
1002: for (int i = 0; i < count; i++) {
1003: XMLComponent c = (XMLComponent) fXML11Components.get(i);
1004: c.reset(this );
1005: }
1006:
1007: } // resetXML11()
1008:
1009: /**
1010: * Configures the XML 1.1 pipeline.
1011: * Note: this method also resets the new XML11 components.
1012: */
1013: protected void configureXML11Pipeline() {
1014: if (fCurrentDVFactory != fXML11DatatypeFactory) {
1015: fCurrentDVFactory = fXML11DatatypeFactory;
1016: setProperty(DATATYPE_VALIDATOR_FACTORY, fCurrentDVFactory);
1017: }
1018: if (fCurrentDTDScanner != fXML11DTDScanner) {
1019: fCurrentDTDScanner = fXML11DTDScanner;
1020: setProperty(DTD_SCANNER, fCurrentDTDScanner);
1021: setProperty(DTD_PROCESSOR, fXML11DTDProcessor);
1022: }
1023:
1024: fXML11DTDScanner.setDTDHandler(fXML11DTDProcessor);
1025: fXML11DTDProcessor.setDTDSource(fXML11DTDScanner);
1026: fXML11DTDProcessor.setDTDHandler(fDTDHandler);
1027: if (fDTDHandler != null) {
1028: fDTDHandler.setDTDSource(fXML11DTDProcessor);
1029: }
1030:
1031: fXML11DTDScanner.setDTDContentModelHandler(fXML11DTDProcessor);
1032: fXML11DTDProcessor.setDTDContentModelSource(fXML11DTDScanner);
1033: fXML11DTDProcessor
1034: .setDTDContentModelHandler(fDTDContentModelHandler);
1035: if (fDTDContentModelHandler != null) {
1036: fDTDContentModelHandler
1037: .setDTDContentModelSource(fXML11DTDProcessor);
1038: }
1039:
1040: // setup XML 1.1 document pipeline
1041: if (fFeatures.get(NAMESPACES) == Boolean.TRUE) {
1042: if (fCurrentScanner != fXML11NSDocScanner) {
1043: fCurrentScanner = fXML11NSDocScanner;
1044: setProperty(DOCUMENT_SCANNER, fXML11NSDocScanner);
1045: setProperty(DTD_VALIDATOR, fXML11NSDTDValidator);
1046: }
1047:
1048: fXML11NSDocScanner.setDTDValidator(fXML11NSDTDValidator);
1049: fXML11NSDocScanner.setDocumentHandler(fXML11NSDTDValidator);
1050: fXML11NSDTDValidator.setDocumentSource(fXML11NSDocScanner);
1051: fXML11NSDTDValidator.setDocumentHandler(fDocumentHandler);
1052:
1053: if (fDocumentHandler != null) {
1054: fDocumentHandler
1055: .setDocumentSource(fXML11NSDTDValidator);
1056: }
1057: fLastComponent = fXML11NSDTDValidator;
1058:
1059: } else {
1060: // create components
1061: if (fXML11DocScanner == null) {
1062: // non namespace document pipeline
1063: fXML11DocScanner = new XML11DocumentScannerImpl();
1064: addXML11Component(fXML11DocScanner);
1065: fXML11DTDValidator = new XML11DTDValidator();
1066: addXML11Component(fXML11DTDValidator);
1067: }
1068: if (fCurrentScanner != fXML11DocScanner) {
1069: fCurrentScanner = fXML11DocScanner;
1070: setProperty(DOCUMENT_SCANNER, fXML11DocScanner);
1071: setProperty(DTD_VALIDATOR, fXML11DTDValidator);
1072: }
1073: fXML11DocScanner.setDocumentHandler(fXML11DTDValidator);
1074: fXML11DTDValidator.setDocumentSource(fXML11DocScanner);
1075: fXML11DTDValidator.setDocumentHandler(fDocumentHandler);
1076:
1077: if (fDocumentHandler != null) {
1078: fDocumentHandler.setDocumentSource(fXML11DTDValidator);
1079: }
1080: fLastComponent = fXML11DTDValidator;
1081: }
1082:
1083: // setup document pipeline
1084: if (fFeatures.get(XMLSCHEMA_VALIDATION) == Boolean.TRUE) {
1085: // If schema validator was not in the pipeline insert it.
1086: if (fSchemaValidator == null) {
1087: fSchemaValidator = new XMLSchemaValidator();
1088: // add schema component
1089: setProperty(SCHEMA_VALIDATOR, fSchemaValidator);
1090: addCommonComponent(fSchemaValidator);
1091: fSchemaValidator.reset(this );
1092: // add schema message formatter
1093: if (fErrorReporter
1094: .getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) {
1095: XSMessageFormatter xmft = new XSMessageFormatter();
1096: fErrorReporter.putMessageFormatter(
1097: XSMessageFormatter.SCHEMA_DOMAIN, xmft);
1098: }
1099: }
1100:
1101: fLastComponent.setDocumentHandler(fSchemaValidator);
1102: fSchemaValidator.setDocumentSource(fLastComponent);
1103: fSchemaValidator.setDocumentHandler(fDocumentHandler);
1104: if (fDocumentHandler != null) {
1105: fDocumentHandler.setDocumentSource(fSchemaValidator);
1106: }
1107: fLastComponent = fSchemaValidator;
1108: }
1109:
1110: } // configureXML11Pipeline()
1111:
1112: /** Configures the pipeline. */
1113: protected void configurePipeline() {
1114: if (fCurrentDVFactory != fDatatypeValidatorFactory) {
1115: fCurrentDVFactory = fDatatypeValidatorFactory;
1116: // use XML 1.0 datatype library
1117: setProperty(DATATYPE_VALIDATOR_FACTORY, fCurrentDVFactory);
1118: }
1119:
1120: // setup DTD pipeline
1121: if (fCurrentDTDScanner != fDTDScanner) {
1122: fCurrentDTDScanner = fDTDScanner;
1123: setProperty(DTD_SCANNER, fCurrentDTDScanner);
1124: setProperty(DTD_PROCESSOR, fDTDProcessor);
1125: }
1126: fDTDScanner.setDTDHandler(fDTDProcessor);
1127: fDTDProcessor.setDTDSource(fDTDScanner);
1128: fDTDProcessor.setDTDHandler(fDTDHandler);
1129: if (fDTDHandler != null) {
1130: fDTDHandler.setDTDSource(fDTDProcessor);
1131: }
1132:
1133: fDTDScanner.setDTDContentModelHandler(fDTDProcessor);
1134: fDTDProcessor.setDTDContentModelSource(fDTDScanner);
1135: fDTDProcessor
1136: .setDTDContentModelHandler(fDTDContentModelHandler);
1137: if (fDTDContentModelHandler != null) {
1138: fDTDContentModelHandler
1139: .setDTDContentModelSource(fDTDProcessor);
1140: }
1141:
1142: // setup document pipeline
1143: if (fFeatures.get(NAMESPACES) == Boolean.TRUE) {
1144: if (fCurrentScanner != fNamespaceScanner) {
1145: fCurrentScanner = fNamespaceScanner;
1146: setProperty(DOCUMENT_SCANNER, fNamespaceScanner);
1147: setProperty(DTD_VALIDATOR, fDTDValidator);
1148: }
1149: fNamespaceScanner.setDTDValidator(fDTDValidator);
1150: fNamespaceScanner.setDocumentHandler(fDTDValidator);
1151: fDTDValidator.setDocumentSource(fNamespaceScanner);
1152: fDTDValidator.setDocumentHandler(fDocumentHandler);
1153: if (fDocumentHandler != null) {
1154: fDocumentHandler.setDocumentSource(fDTDValidator);
1155: }
1156: fLastComponent = fDTDValidator;
1157: } else {
1158: // create components
1159: if (fNonNSScanner == null) {
1160: fNonNSScanner = new XMLDocumentScannerImpl();
1161: fNonNSDTDValidator = new XMLDTDValidator();
1162: // add components
1163: addComponent((XMLComponent) fNonNSScanner);
1164: addComponent((XMLComponent) fNonNSDTDValidator);
1165: }
1166: if (fCurrentScanner != fNonNSScanner) {
1167: fCurrentScanner = fNonNSScanner;
1168: setProperty(DOCUMENT_SCANNER, fNonNSScanner);
1169: setProperty(DTD_VALIDATOR, fNonNSDTDValidator);
1170: }
1171:
1172: fNonNSScanner.setDocumentHandler(fNonNSDTDValidator);
1173: fNonNSDTDValidator.setDocumentSource(fNonNSScanner);
1174: fNonNSDTDValidator.setDocumentHandler(fDocumentHandler);
1175: if (fDocumentHandler != null) {
1176: fDocumentHandler.setDocumentSource(fNonNSDTDValidator);
1177: }
1178: fLastComponent = fNonNSDTDValidator;
1179: }
1180:
1181: // add XML Schema validator if needed
1182: if (fFeatures.get(XMLSCHEMA_VALIDATION) == Boolean.TRUE) {
1183: // If schema validator was not in the pipeline insert it.
1184: if (fSchemaValidator == null) {
1185: fSchemaValidator = new XMLSchemaValidator();
1186: // add schema component
1187: setProperty(SCHEMA_VALIDATOR, fSchemaValidator);
1188: addCommonComponent(fSchemaValidator);
1189: fSchemaValidator.reset(this );
1190: // add schema message formatter
1191: if (fErrorReporter
1192: .getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) {
1193: XSMessageFormatter xmft = new XSMessageFormatter();
1194: fErrorReporter.putMessageFormatter(
1195: XSMessageFormatter.SCHEMA_DOMAIN, xmft);
1196: }
1197:
1198: }
1199: fLastComponent.setDocumentHandler(fSchemaValidator);
1200: fSchemaValidator.setDocumentSource(fLastComponent);
1201: fSchemaValidator.setDocumentHandler(fDocumentHandler);
1202: if (fDocumentHandler != null) {
1203: fDocumentHandler.setDocumentSource(fSchemaValidator);
1204: }
1205: fLastComponent = fSchemaValidator;
1206: }
1207: } // configurePipeline()
1208:
1209: // features and properties
1210:
1211: /**
1212: * Check a feature. If feature is know and supported, this method simply
1213: * returns. Otherwise, the appropriate exception is thrown.
1214: *
1215: * @param featureId The unique identifier (URI) of the feature.
1216: *
1217: * @throws XMLConfigurationException Thrown for configuration error.
1218: * In general, components should
1219: * only throw this exception if
1220: * it is <strong>really</strong>
1221: * a critical error.
1222: */
1223: protected void checkFeature(String featureId)
1224: throws XMLConfigurationException {
1225:
1226: //
1227: // Xerces Features
1228: //
1229:
1230: if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) {
1231: final int suffixLength = featureId.length()
1232: - Constants.XERCES_FEATURE_PREFIX.length();
1233:
1234: //
1235: // http://apache.org/xml/features/validation/dynamic
1236: // Allows the parser to validate a document only when it
1237: // contains a grammar. Validation is turned on/off based
1238: // on each document instance, automatically.
1239: //
1240: if (suffixLength == Constants.DYNAMIC_VALIDATION_FEATURE
1241: .length()
1242: && featureId
1243: .endsWith(Constants.DYNAMIC_VALIDATION_FEATURE)) {
1244: return;
1245: }
1246:
1247: //
1248: // http://apache.org/xml/features/validation/default-attribute-values
1249: //
1250: if (suffixLength == Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE
1251: .length()
1252: && featureId
1253: .endsWith(Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE)) {
1254: // REVISIT
1255: short type = XMLConfigurationException.NOT_SUPPORTED;
1256: throw new XMLConfigurationException(type, featureId);
1257: }
1258: //
1259: // http://apache.org/xml/features/validation/default-attribute-values
1260: //
1261: if (suffixLength == Constants.VALIDATE_CONTENT_MODELS_FEATURE
1262: .length()
1263: && featureId
1264: .endsWith(Constants.VALIDATE_CONTENT_MODELS_FEATURE)) {
1265: // REVISIT
1266: short type = XMLConfigurationException.NOT_SUPPORTED;
1267: throw new XMLConfigurationException(type, featureId);
1268: }
1269: //
1270: // http://apache.org/xml/features/validation/nonvalidating/load-dtd-grammar
1271: //
1272: if (suffixLength == Constants.LOAD_DTD_GRAMMAR_FEATURE
1273: .length()
1274: && featureId
1275: .endsWith(Constants.LOAD_DTD_GRAMMAR_FEATURE)) {
1276: return;
1277: }
1278: //
1279: // http://apache.org/xml/features/validation/nonvalidating/load-external-dtd
1280: //
1281: if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE
1282: .length()
1283: && featureId
1284: .endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) {
1285: return;
1286: }
1287:
1288: //
1289: // http://apache.org/xml/features/validation/default-attribute-values
1290: //
1291: if (suffixLength == Constants.VALIDATE_DATATYPES_FEATURE
1292: .length()
1293: && featureId
1294: .endsWith(Constants.VALIDATE_DATATYPES_FEATURE)) {
1295: short type = XMLConfigurationException.NOT_SUPPORTED;
1296: throw new XMLConfigurationException(type, featureId);
1297: }
1298:
1299: //
1300: // http://apache.org/xml/features/validation/schema
1301: // Lets the user turn Schema validation support on/off.
1302: //
1303: if (suffixLength == Constants.SCHEMA_VALIDATION_FEATURE
1304: .length()
1305: && featureId
1306: .endsWith(Constants.SCHEMA_VALIDATION_FEATURE)) {
1307: return;
1308: }
1309: // activate full schema checking
1310: if (suffixLength == Constants.SCHEMA_FULL_CHECKING.length()
1311: && featureId
1312: .endsWith(Constants.SCHEMA_FULL_CHECKING)) {
1313: return;
1314: }
1315: // Feature identifier: expose schema normalized value
1316: // http://apache.org/xml/features/validation/schema/normalized-value
1317: if (suffixLength == Constants.SCHEMA_NORMALIZED_VALUE
1318: .length()
1319: && featureId
1320: .endsWith(Constants.SCHEMA_NORMALIZED_VALUE)) {
1321: return;
1322: }
1323: // Feature identifier: send element default value via characters()
1324: // http://apache.org/xml/features/validation/schema/element-default
1325: if (suffixLength == Constants.SCHEMA_ELEMENT_DEFAULT
1326: .length()
1327: && featureId
1328: .endsWith(Constants.SCHEMA_ELEMENT_DEFAULT)) {
1329: return;
1330: }
1331:
1332: // special performance feature: only component manager is allowed to set it.
1333: if (suffixLength == Constants.PARSER_SETTINGS.length()
1334: && featureId.endsWith(Constants.PARSER_SETTINGS)) {
1335: short type = XMLConfigurationException.NOT_SUPPORTED;
1336: throw new XMLConfigurationException(type, featureId);
1337: }
1338:
1339: }
1340:
1341: //
1342: // Not recognized
1343: //
1344:
1345: super .checkFeature(featureId);
1346:
1347: } // checkFeature(String)
1348:
1349: /**
1350: * Check a property. If the property is know and supported, this method
1351: * simply returns. Otherwise, the appropriate exception is thrown.
1352: *
1353: * @param propertyId The unique identifier (URI) of the property
1354: * being set.
1355: *
1356: * @throws XMLConfigurationException Thrown for configuration error.
1357: * In general, components should
1358: * only throw this exception if
1359: * it is <strong>really</strong>
1360: * a critical error.
1361: */
1362: protected void checkProperty(String propertyId)
1363: throws XMLConfigurationException {
1364:
1365: //
1366: // Xerces Properties
1367: //
1368:
1369: if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
1370: final int suffixLength = propertyId.length()
1371: - Constants.XERCES_PROPERTY_PREFIX.length();
1372:
1373: if (suffixLength == Constants.DTD_SCANNER_PROPERTY.length()
1374: && propertyId
1375: .endsWith(Constants.DTD_SCANNER_PROPERTY)) {
1376: return;
1377: }
1378: if (suffixLength == Constants.SCHEMA_LOCATION.length()
1379: && propertyId.endsWith(Constants.SCHEMA_LOCATION)) {
1380: return;
1381: }
1382: if (suffixLength == Constants.SCHEMA_NONS_LOCATION.length()
1383: && propertyId
1384: .endsWith(Constants.SCHEMA_NONS_LOCATION)) {
1385: return;
1386: }
1387: }
1388:
1389: if (propertyId.startsWith(Constants.JAXP_PROPERTY_PREFIX)) {
1390: final int suffixLength = propertyId.length()
1391: - Constants.JAXP_PROPERTY_PREFIX.length();
1392:
1393: if (suffixLength == Constants.SCHEMA_SOURCE.length()
1394: && propertyId.endsWith(Constants.SCHEMA_SOURCE)) {
1395: return;
1396: }
1397: }
1398:
1399: // special cases
1400: if (propertyId.startsWith(Constants.SAX_PROPERTY_PREFIX)) {
1401: final int suffixLength = propertyId.length()
1402: - Constants.SAX_PROPERTY_PREFIX.length();
1403:
1404: //
1405: // http://xml.org/sax/properties/xml-string
1406: // Value type: String
1407: // Access: read-only
1408: // Get the literal string of characters associated with the
1409: // current event. If the parser recognises and supports this
1410: // property but is not currently parsing text, it should return
1411: // null (this is a good way to check for availability before the
1412: // parse begins).
1413: //
1414: if (suffixLength == Constants.XML_STRING_PROPERTY.length()
1415: && propertyId
1416: .endsWith(Constants.XML_STRING_PROPERTY)) {
1417: // REVISIT - we should probably ask xml-dev for a precise
1418: // definition of what this is actually supposed to return, and
1419: // in exactly which circumstances.
1420: short type = XMLConfigurationException.NOT_SUPPORTED;
1421: throw new XMLConfigurationException(type, propertyId);
1422: }
1423: }
1424:
1425: //
1426: // Not recognized
1427: //
1428:
1429: super .checkProperty(propertyId);
1430:
1431: } // checkProperty(String)
1432:
1433: /**
1434: * Adds a component to the parser configuration. This method will
1435: * also add all of the component's recognized features and properties
1436: * to the list of default recognized features and properties.
1437: *
1438: * @param component The component to add.
1439: */
1440: protected void addComponent(XMLComponent component) {
1441:
1442: // don't add a component more than once
1443: if (fComponents.contains(component)) {
1444: return;
1445: }
1446: fComponents.add(component);
1447: addRecognizedParamsAndSetDefaults(component);
1448:
1449: } // addComponent(XMLComponent)
1450:
1451: /**
1452: * Adds common component to the parser configuration. This method will
1453: * also add all of the component's recognized features and properties
1454: * to the list of default recognized features and properties.
1455: *
1456: * @param component The component to add.
1457: */
1458: protected void addCommonComponent(XMLComponent component) {
1459:
1460: // don't add a component more than once
1461: if (fCommonComponents.contains(component)) {
1462: return;
1463: }
1464: fCommonComponents.add(component);
1465: addRecognizedParamsAndSetDefaults(component);
1466:
1467: } // addCommonComponent(XMLComponent)
1468:
1469: /**
1470: * Adds an XML 1.1 component to the parser configuration. This method will
1471: * also add all of the component's recognized features and properties
1472: * to the list of default recognized features and properties.
1473: *
1474: * @param component The component to add.
1475: */
1476: protected void addXML11Component(XMLComponent component) {
1477:
1478: // don't add a component more than once
1479: if (fXML11Components.contains(component)) {
1480: return;
1481: }
1482: fXML11Components.add(component);
1483: addRecognizedParamsAndSetDefaults(component);
1484:
1485: } // addXML11Component(XMLComponent)
1486:
1487: /**
1488: * Adds all of the component's recognized features and properties
1489: * to the list of default recognized features and properties, and
1490: * sets default values on the configuration for features and
1491: * properties which were previously absent from the configuration.
1492: *
1493: * @param component The component whose recognized features
1494: * and properties will be added to the configuration
1495: */
1496: protected void addRecognizedParamsAndSetDefaults(
1497: XMLComponent component) {
1498:
1499: // register component's recognized features
1500: String[] recognizedFeatures = component.getRecognizedFeatures();
1501: addRecognizedFeatures(recognizedFeatures);
1502:
1503: // register component's recognized properties
1504: String[] recognizedProperties = component
1505: .getRecognizedProperties();
1506: addRecognizedProperties(recognizedProperties);
1507:
1508: // set default values
1509: if (recognizedFeatures != null) {
1510: for (int i = 0; i < recognizedFeatures.length; ++i) {
1511: String featureId = recognizedFeatures[i];
1512: Boolean state = component.getFeatureDefault(featureId);
1513: if (state != null) {
1514: // Do not overwrite values already set on the configuration.
1515: if (!fFeatures.containsKey(featureId)) {
1516: fFeatures.put(featureId, state);
1517: // For newly added components who recognize this feature
1518: // but did not offer a default value, we need to make
1519: // sure these components will get an opportunity to read
1520: // the value before parsing begins.
1521: fConfigUpdated = true;
1522: }
1523: }
1524: }
1525: }
1526: if (recognizedProperties != null) {
1527: for (int i = 0; i < recognizedProperties.length; ++i) {
1528: String propertyId = recognizedProperties[i];
1529: Object value = component.getPropertyDefault(propertyId);
1530: if (value != null) {
1531: // Do not overwrite values already set on the configuration.
1532: if (!fProperties.containsKey(propertyId)) {
1533: fProperties.put(propertyId, value);
1534: // For newly added components who recognize this property
1535: // but did not offer a default value, we need to make
1536: // sure these components will get an opportunity to read
1537: // the value before parsing begins.
1538: fConfigUpdated = true;
1539: }
1540: }
1541: }
1542: }
1543: }
1544:
1545: private void initXML11Components() {
1546: if (!f11Initialized) {
1547:
1548: // create datatype factory
1549: fXML11DatatypeFactory = DTDDVFactory
1550: .getInstance(XML11_DATATYPE_VALIDATOR_FACTORY);
1551:
1552: // setup XML 1.1 DTD pipeline
1553: fXML11DTDScanner = new XML11DTDScannerImpl();
1554: addXML11Component(fXML11DTDScanner);
1555: fXML11DTDProcessor = new XML11DTDProcessor();
1556: addXML11Component(fXML11DTDProcessor);
1557:
1558: // setup XML 1.1. document pipeline - namespace aware
1559: fXML11NSDocScanner = new XML11NSDocumentScannerImpl();
1560: addXML11Component(fXML11NSDocScanner);
1561: fXML11NSDTDValidator = new XML11NSDTDValidator();
1562: addXML11Component(fXML11NSDTDValidator);
1563:
1564: f11Initialized = true;
1565: }
1566: }
1567:
1568: /**
1569: * Returns the state of a feature. This method calls getFeature()
1570: * on ParserConfigurationSettings, bypassing getFeature() on this
1571: * class.
1572: */
1573: boolean getFeature0(String featureId)
1574: throws XMLConfigurationException {
1575: return super .getFeature(featureId);
1576: }
1577:
1578: } // class XML11Configuration
|