1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001-2004 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) 2002, 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.parsers;
  58. import java.io.IOException;
  59. import java.util.ArrayList;
  60. import java.util.HashMap;
  61. import java.util.Locale;
  62. import com.sun.org.apache.xerces.internal.impl.Constants;
  63. import com.sun.org.apache.xerces.internal.impl.XML11DTDScannerImpl;
  64. import com.sun.org.apache.xerces.internal.impl.XML11DocumentScannerImpl;
  65. import com.sun.org.apache.xerces.internal.impl.XML11NSDocumentScannerImpl;
  66. import com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl;
  67. import com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl;
  68. import com.sun.org.apache.xerces.internal.impl.XMLEntityHandler;
  69. import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
  70. import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
  71. import com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl;
  72. import com.sun.org.apache.xerces.internal.impl.XMLVersionDetector;
  73. import com.sun.org.apache.xerces.internal.impl.dtd.XML11DTDProcessor;
  74. import com.sun.org.apache.xerces.internal.impl.dtd.XML11DTDValidator;
  75. import com.sun.org.apache.xerces.internal.impl.dtd.XML11NSDTDValidator;
  76. import com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDProcessor;
  77. import com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator;
  78. import com.sun.org.apache.xerces.internal.impl.dtd.XMLNSDTDValidator;
  79. import com.sun.org.apache.xerces.internal.impl.dv.DTDDVFactory;
  80. import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
  81. import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;
  82. import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator;
  83. import com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter;
  84. import com.sun.org.apache.xerces.internal.util.ParserConfigurationSettings;
  85. import com.sun.org.apache.xerces.internal.util.SymbolTable;
  86. import com.sun.org.apache.xerces.internal.xni.XMLDTDContentModelHandler;
  87. import com.sun.org.apache.xerces.internal.xni.XMLDTDHandler;
  88. import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler;
  89. import com.sun.org.apache.xerces.internal.xni.XMLLocator;
  90. import com.sun.org.apache.xerces.internal.xni.XNIException;
  91. import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
  92. import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
  93. import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
  94. import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
  95. import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDScanner;
  96. import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentScanner;
  97. import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource;
  98. import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver;
  99. import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler;
  100. import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
  101. import com.sun.org.apache.xerces.internal.xni.parser.XMLPullParserConfiguration;
  102. /**
  103. * This class is the configuration used to parse XML 1.0 and XML 1.1 documents.
  104. * This is the default Xerces configuration.
  105. *
  106. * @author Elena Litani, IBM
  107. * @author Neil Graham, IBM
  108. * @author Michael Glavassevich, IBM
  109. *
  110. * @version $Id: XML11Configuration.java,v 1.17 2004/04/12 21:56:02 mrglavas Exp $
  111. */
  112. public class XML11Configuration extends ParserConfigurationSettings
  113. implements XMLPullParserConfiguration {
  114. //
  115. // Constants
  116. //
  117. protected final static String XML11_DATATYPE_VALIDATOR_FACTORY =
  118. "com.sun.org.apache.xerces.internal.impl.dv.dtd.XML11DTDDVFactoryImpl";
  119. // feature identifiers
  120. /** Feature identifier: warn on duplicate attribute definition. */
  121. protected static final String WARN_ON_DUPLICATE_ATTDEF =
  122. Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE;
  123. /** Feature identifier: warn on duplicate entity definition. */
  124. protected static final String WARN_ON_DUPLICATE_ENTITYDEF =
  125. Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE;
  126. /** Feature identifier: warn on undeclared element definition. */
  127. protected static final String WARN_ON_UNDECLARED_ELEMDEF =
  128. Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_UNDECLARED_ELEMDEF_FEATURE;
  129. /** Feature identifier: allow Java encodings. */
  130. protected static final String ALLOW_JAVA_ENCODINGS =
  131. Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE;
  132. /** Feature identifier: continue after fatal error. */
  133. protected static final String CONTINUE_AFTER_FATAL_ERROR =
  134. Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE;
  135. /** Feature identifier: load external DTD. */
  136. protected static final String LOAD_EXTERNAL_DTD =
  137. Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE;
  138. /** Feature identifier: notify built-in refereces. */
  139. protected static final String NOTIFY_BUILTIN_REFS =
  140. Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_BUILTIN_REFS_FEATURE;
  141. /** Feature identifier: notify character refereces. */
  142. protected static final String NOTIFY_CHAR_REFS =
  143. Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_CHAR_REFS_FEATURE;
  144. /** Feature identifier: expose schema normalized value */
  145. protected static final String NORMALIZE_DATA =
  146. Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE;
  147. /** Feature identifier: send element default value via characters() */
  148. protected static final String SCHEMA_ELEMENT_DEFAULT =
  149. Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_ELEMENT_DEFAULT;
  150. /** Feature identifier: augment PSVI */
  151. protected static final String SCHEMA_AUGMENT_PSVI =
  152. Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_AUGMENT_PSVI;
  153. /** feature identifier: XML Schema validation */
  154. protected static final String XMLSCHEMA_VALIDATION =
  155. Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE;
  156. /** feature identifier: XML Schema validation -- full checking */
  157. protected static final String XMLSCHEMA_FULL_CHECKING =
  158. Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING;
  159. // feature identifiers
  160. /** Feature identifier: validation. */
  161. protected static final String VALIDATION =
  162. Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE;
  163. /** Feature identifier: namespaces. */
  164. protected static final String NAMESPACES =
  165. Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE;
  166. /** Feature identifier: external general entities. */
  167. protected static final String EXTERNAL_GENERAL_ENTITIES =
  168. Constants.SAX_FEATURE_PREFIX + Constants.EXTERNAL_GENERAL_ENTITIES_FEATURE;
  169. /** Feature identifier: external parameter entities. */
  170. protected static final String EXTERNAL_PARAMETER_ENTITIES =
  171. Constants.SAX_FEATURE_PREFIX + Constants.EXTERNAL_PARAMETER_ENTITIES_FEATURE;
  172. protected static final String EXPAND_URIS = Constants.SAX_FEATURE_PREFIX + Constants.RESOLVE_DTD_URIS_FEATURE;
  173. protected static final String XML_VERSION_SUPPORT = Constants.SAX_FEATURE_PREFIX + Constants.XML_11_FEATURE;
  174. protected static final String NORMALIZAION_CHECKS = Constants.SAX_FEATURE_PREFIX + Constants.UNICODE_NORMALIZATION_CHECKING_FEATURE;
  175. // property identifiers
  176. /** Property identifier: xml string. */
  177. protected static final String XML_STRING =
  178. Constants.SAX_PROPERTY_PREFIX + Constants.XML_STRING_PROPERTY;
  179. /** Property identifier: symbol table. */
  180. protected static final String SYMBOL_TABLE =
  181. Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
  182. /** Property identifier: error handler. */
  183. protected static final String ERROR_HANDLER =
  184. Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY;
  185. /** Property identifier: entity resolver. */
  186. protected static final String ENTITY_RESOLVER =
  187. Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY;
  188. /** Property identifier: XML Schema validator. */
  189. protected static final String SCHEMA_VALIDATOR =
  190. Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY;
  191. /** Property identifier: schema location. */
  192. protected static final String SCHEMA_LOCATION =
  193. Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_LOCATION;
  194. /** Property identifier: no namespace schema location. */
  195. protected static final String SCHEMA_NONS_LOCATION =
  196. Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_NONS_LOCATION;
  197. // property identifiers
  198. /** Property identifier: error reporter. */
  199. protected static final String ERROR_REPORTER =
  200. Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
  201. /** Property identifier: entity manager. */
  202. protected static final String ENTITY_MANAGER =
  203. Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
  204. /** Property identifier document scanner: */
  205. protected static final String DOCUMENT_SCANNER =
  206. Constants.XERCES_PROPERTY_PREFIX + Constants.DOCUMENT_SCANNER_PROPERTY;
  207. /** Property identifier: DTD scanner. */
  208. protected static final String DTD_SCANNER =
  209. Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_SCANNER_PROPERTY;
  210. /** Property identifier: grammar pool. */
  211. protected static final String XMLGRAMMAR_POOL =
  212. Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
  213. /** Property identifier: DTD loader. */
  214. protected static final String DTD_PROCESSOR =
  215. Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_PROCESSOR_PROPERTY;
  216. /** Property identifier: DTD validator. */
  217. protected static final String DTD_VALIDATOR =
  218. Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_VALIDATOR_PROPERTY;
  219. /** Property identifier: namespace binder. */
  220. protected static final String NAMESPACE_BINDER =
  221. Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_BINDER_PROPERTY;
  222. /** Property identifier: datatype validator factory. */
  223. protected static final String DATATYPE_VALIDATOR_FACTORY =
  224. Constants.XERCES_PROPERTY_PREFIX + Constants.DATATYPE_VALIDATOR_FACTORY_PROPERTY;
  225. protected static final String VALIDATION_MANAGER =
  226. Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY;
  227. /** Property identifier: JAXP schema language / DOM schema-type. */
  228. protected static final String JAXP_SCHEMA_LANGUAGE =
  229. Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE;
  230. /** Property identifier: JAXP schema source/ DOM schema-location. */
  231. protected static final String JAXP_SCHEMA_SOURCE =
  232. Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE;
  233. // debugging
  234. /** Set to true and recompile to print exception stack trace. */
  235. protected static final boolean PRINT_EXCEPTION_STACK_TRACE = false;
  236. //
  237. // Data
  238. //
  239. protected SymbolTable fSymbolTable;
  240. protected XMLInputSource fInputSource;
  241. protected ValidationManager fValidationManager;
  242. protected XMLVersionDetector fVersionDetector;
  243. protected XMLLocator fLocator;
  244. protected Locale fLocale;
  245. /** XML 1.0 Components. */
  246. protected ArrayList fComponents;
  247. /** XML 1.1. Components. */
  248. protected ArrayList fXML11Components = null;
  249. /** Common components: XMLEntityManager, XMLErrorReporter, XMLSchemaValidator */
  250. protected ArrayList fCommonComponents = null;
  251. /** The document handler. */
  252. protected XMLDocumentHandler fDocumentHandler;
  253. /** The DTD handler. */
  254. protected XMLDTDHandler fDTDHandler;
  255. /** The DTD content model handler. */
  256. protected XMLDTDContentModelHandler fDTDContentModelHandler;
  257. /** Last component in the document pipeline */
  258. protected XMLDocumentSource fLastComponent;
  259. /**
  260. * True if a parse is in progress. This state is needed because
  261. * some features/properties cannot be set while parsing (e.g.
  262. * validation and namespaces).
  263. */
  264. protected boolean fParseInProgress = false;
  265. /** fConfigUpdated is set to true if there has been any change to the configuration settings,
  266. * i.e a feature or a property was changed.
  267. */
  268. protected boolean fConfigUpdated = false;
  269. //
  270. // XML 1.0 components
  271. //
  272. /** The XML 1.0 Datatype validator factory. */
  273. protected DTDDVFactory fDatatypeValidatorFactory;
  274. /** The XML 1.0 Document scanner that does namespace binding. */
  275. protected XMLNSDocumentScannerImpl fNamespaceScanner;
  276. /** The XML 1.0 Non-namespace implementation of scanner */
  277. protected XMLDocumentScannerImpl fNonNSScanner;
  278. /** The XML 1.0 DTD Validator: binds namespaces */
  279. protected XMLDTDValidator fDTDValidator;
  280. /** The XML 1.0 DTD Validator that does not bind namespaces */
  281. protected XMLDTDValidator fNonNSDTDValidator;
  282. /** The XML 1.0 DTD scanner. */
  283. protected XMLDTDScanner fDTDScanner;
  284. /** The XML 1.0 DTD Processor . */
  285. protected XMLDTDProcessor fDTDProcessor;
  286. //
  287. // XML 1.1 components
  288. //
  289. /** The XML 1.1 datatype factory. **/
  290. protected DTDDVFactory fXML11DatatypeFactory = null;
  291. /** The XML 1.1 document scanner that does namespace binding. **/
  292. protected XML11NSDocumentScannerImpl fXML11NSDocScanner = null;
  293. /** The XML 1.1 document scanner that does not do namespace binding. **/
  294. protected XML11DocumentScannerImpl fXML11DocScanner = null;
  295. /** The XML 1.1 DTD validator that does namespace binding. **/
  296. protected XML11NSDTDValidator fXML11NSDTDValidator = null;
  297. /** The XML 1.1 DTD validator that does not do namespace binding. **/
  298. protected XML11DTDValidator fXML11DTDValidator = null;
  299. /** The XML 1.1 DTD scanner. **/
  300. protected XML11DTDScannerImpl fXML11DTDScanner = null;
  301. /** The XML 1.1 DTD processor. **/
  302. protected XML11DTDProcessor fXML11DTDProcessor = null;
  303. //
  304. // Common components
  305. //
  306. /** Grammar pool. */
  307. protected XMLGrammarPool fGrammarPool;
  308. /** Error reporter. */
  309. protected XMLErrorReporter fErrorReporter;
  310. /** Entity manager. */
  311. protected XMLEntityManager fEntityManager;
  312. /** XML Schema Validator. */
  313. protected XMLSchemaValidator fSchemaValidator;
  314. /** Current scanner */
  315. protected XMLDocumentScanner fCurrentScanner;
  316. /** Current Datatype validator factory. */
  317. protected DTDDVFactory fCurrentDVFactory;
  318. /** Current DTD scanner. */
  319. protected XMLDTDScanner fCurrentDTDScanner;
  320. /** Flag indiciating whether XML11 components have been initialized. */
  321. private boolean f11Initialized = false;
  322. //
  323. // Constructors
  324. //
  325. /** Default constructor. */
  326. public XML11Configuration() {
  327. this(null, null, null);
  328. } // <init>()
  329. /**
  330. * Constructs a parser configuration using the specified symbol table.
  331. *
  332. * @param symbolTable The symbol table to use.
  333. */
  334. public XML11Configuration(SymbolTable symbolTable) {
  335. this(symbolTable, null, null);
  336. } // <init>(SymbolTable)
  337. /**
  338. * Constructs a parser configuration using the specified symbol table and
  339. * grammar pool.
  340. * <p>
  341. * <strong>REVISIT:</strong>
  342. * Grammar pool will be updated when the new validation engine is
  343. * implemented.
  344. *
  345. * @param symbolTable The symbol table to use.
  346. * @param grammarPool The grammar pool to use.
  347. */
  348. public XML11Configuration(SymbolTable symbolTable, XMLGrammarPool grammarPool) {
  349. this(symbolTable, grammarPool, null);
  350. } // <init>(SymbolTable,XMLGrammarPool)
  351. /**
  352. * Constructs a parser configuration using the specified symbol table,
  353. * grammar pool, and parent settings.
  354. * <p>
  355. * <strong>REVISIT:</strong>
  356. * Grammar pool will be updated when the new validation engine is
  357. * implemented.
  358. *
  359. * @param symbolTable The symbol table to use.
  360. * @param grammarPool The grammar pool to use.
  361. * @param parentSettings The parent settings.
  362. */
  363. public XML11Configuration(
  364. SymbolTable symbolTable,
  365. XMLGrammarPool grammarPool,
  366. XMLComponentManager parentSettings) {
  367. super(parentSettings);
  368. // create a vector to hold all the components in use
  369. // XML 1.0 specialized components
  370. fComponents = new ArrayList();
  371. // XML 1.1 specialized components
  372. fXML11Components = new ArrayList();
  373. // Common components for XML 1.1. and XML 1.0
  374. fCommonComponents = new ArrayList();
  375. // create storage for recognized features and properties
  376. fRecognizedFeatures = new ArrayList();
  377. fRecognizedProperties = new ArrayList();
  378. // create table for features and properties
  379. fFeatures = new HashMap();
  380. fProperties = new HashMap();
  381. // add default recognized features
  382. final String[] recognizedFeatures = {
  383. CONTINUE_AFTER_FATAL_ERROR, LOAD_EXTERNAL_DTD, // from XMLDTDScannerImpl
  384. VALIDATION,
  385. NAMESPACES,
  386. NORMALIZE_DATA, SCHEMA_ELEMENT_DEFAULT, SCHEMA_AUGMENT_PSVI,
  387. // NOTE: These shouldn't really be here but since the XML Schema
  388. // validator is constructed dynamically, its recognized
  389. // features might not have been set and it would cause a
  390. // not-recognized exception to be thrown. -Ac
  391. XMLSCHEMA_VALIDATION, XMLSCHEMA_FULL_CHECKING,
  392. EXTERNAL_GENERAL_ENTITIES,
  393. EXTERNAL_PARAMETER_ENTITIES,
  394. PARSER_SETTINGS,
  395. EXPAND_URIS,
  396. XML_VERSION_SUPPORT,
  397. NORMALIZAION_CHECKS
  398. };
  399. addRecognizedFeatures(recognizedFeatures);
  400. // set state for default features
  401. fFeatures.put(VALIDATION, Boolean.FALSE);
  402. fFeatures.put(NAMESPACES, Boolean.TRUE);
  403. fFeatures.put(EXTERNAL_GENERAL_ENTITIES, Boolean.TRUE);
  404. fFeatures.put(EXTERNAL_PARAMETER_ENTITIES, Boolean.TRUE);
  405. fFeatures.put(CONTINUE_AFTER_FATAL_ERROR, Boolean.FALSE);
  406. fFeatures.put(LOAD_EXTERNAL_DTD, Boolean.TRUE);
  407. fFeatures.put(SCHEMA_ELEMENT_DEFAULT, Boolean.TRUE);
  408. fFeatures.put(NORMALIZE_DATA, Boolean.TRUE);
  409. fFeatures.put(SCHEMA_AUGMENT_PSVI, Boolean.TRUE);
  410. fFeatures.put(PARSER_SETTINGS, Boolean.TRUE);
  411. fFeatures.put(EXPAND_URIS, Boolean.TRUE);
  412. fFeatures.put(XML_VERSION_SUPPORT, Boolean.TRUE);
  413. fFeatures.put(NORMALIZAION_CHECKS, Boolean.FALSE);
  414. // add default recognized properties
  415. final String[] recognizedProperties = {
  416. SYMBOL_TABLE,
  417. ERROR_HANDLER,
  418. ENTITY_RESOLVER,
  419. ERROR_REPORTER,
  420. ENTITY_MANAGER,
  421. DOCUMENT_SCANNER,
  422. DTD_SCANNER,
  423. DTD_PROCESSOR,
  424. DTD_VALIDATOR,
  425. DATATYPE_VALIDATOR_FACTORY,
  426. VALIDATION_MANAGER,
  427. SCHEMA_VALIDATOR,
  428. XML_STRING,
  429. XMLGRAMMAR_POOL,
  430. JAXP_SCHEMA_SOURCE,
  431. JAXP_SCHEMA_LANGUAGE,
  432. // NOTE: These shouldn't really be here but since the XML Schema
  433. // validator is constructed dynamically, its recognized
  434. // properties might not have been set and it would cause a
  435. // not-recognized exception to be thrown. -Ac
  436. SCHEMA_LOCATION, SCHEMA_NONS_LOCATION, };
  437. addRecognizedProperties(recognizedProperties);
  438. if (symbolTable == null) {
  439. symbolTable = new SymbolTable();
  440. }
  441. fSymbolTable = symbolTable;
  442. fProperties.put(SYMBOL_TABLE, fSymbolTable);
  443. fGrammarPool = grammarPool;
  444. if (fGrammarPool != null) {
  445. fProperties.put(XMLGRAMMAR_POOL, fGrammarPool);
  446. }
  447. fEntityManager = new XMLEntityManager();
  448. fProperties.put(ENTITY_MANAGER, fEntityManager);
  449. addCommonComponent(fEntityManager);
  450. fErrorReporter = new XMLErrorReporter();
  451. fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner());
  452. fProperties.put(ERROR_REPORTER, fErrorReporter);
  453. addCommonComponent(fErrorReporter);
  454. fNamespaceScanner = new XMLNSDocumentScannerImpl();
  455. fProperties.put(DOCUMENT_SCANNER, fNamespaceScanner);
  456. addComponent((XMLComponent) fNamespaceScanner);
  457. fDTDScanner = new XMLDTDScannerImpl();
  458. fProperties.put(DTD_SCANNER, fDTDScanner);
  459. addComponent((XMLComponent) fDTDScanner);
  460. fDTDProcessor = new XMLDTDProcessor();
  461. fProperties.put(DTD_PROCESSOR, fDTDProcessor);
  462. addComponent((XMLComponent) fDTDProcessor);
  463. fDTDValidator = new XMLNSDTDValidator();
  464. fProperties.put(DTD_VALIDATOR, fDTDValidator);
  465. addComponent(fDTDValidator);
  466. fDatatypeValidatorFactory = DTDDVFactory.getInstance();
  467. fProperties.put(DATATYPE_VALIDATOR_FACTORY, fDatatypeValidatorFactory);
  468. fValidationManager = new ValidationManager();
  469. fProperties.put(VALIDATION_MANAGER, fValidationManager);
  470. fVersionDetector = new XMLVersionDetector();
  471. // add message formatters
  472. if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) {
  473. XMLMessageFormatter xmft = new XMLMessageFormatter();
  474. fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft);
  475. fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft);
  476. }
  477. // set locale
  478. try {
  479. setLocale(Locale.getDefault());
  480. } catch (XNIException e) {
  481. // do nothing
  482. // REVISIT: What is the right thing to do? -Ac
  483. }
  484. fConfigUpdated = false;
  485. } // <init>(SymbolTable,XMLGrammarPool)
  486. //
  487. // Public methods
  488. //
  489. /**
  490. * Sets the input source for the document to parse.
  491. *
  492. * @param inputSource The document's input source.
  493. *
  494. * @exception XMLConfigurationException Thrown if there is a
  495. * configuration error when initializing the
  496. * parser.
  497. * @exception IOException Thrown on I/O error.
  498. *
  499. * @see #parse(boolean)
  500. */
  501. public void setInputSource(XMLInputSource inputSource)
  502. throws XMLConfigurationException, IOException {
  503. // REVISIT: this method used to reset all the components and
  504. // construct the pipeline. Now reset() is called
  505. // in parse (boolean) just before we parse the document
  506. // Should this method still throw exceptions..?
  507. fInputSource = inputSource;
  508. } // setInputSource(XMLInputSource)
  509. /**
  510. * Set the locale to use for messages.
  511. *
  512. * @param locale The locale object to use for localization of messages.
  513. *
  514. * @exception XNIException Thrown if the parser does not support the
  515. * specified locale.
  516. */
  517. public void setLocale(Locale locale) throws XNIException {
  518. fLocale = locale;
  519. fErrorReporter.setLocale(locale);
  520. } // setLocale(Locale)
  521. /**
  522. * Sets the document handler on the last component in the pipeline
  523. * to receive information about the document.
  524. *
  525. * @param documentHandler The document handler.
  526. */
  527. public void setDocumentHandler(XMLDocumentHandler documentHandler) {
  528. fDocumentHandler = documentHandler;
  529. if (fLastComponent != null) {
  530. fLastComponent.setDocumentHandler(fDocumentHandler);
  531. if (fDocumentHandler !=null){
  532. fDocumentHandler.setDocumentSource(fLastComponent);
  533. }
  534. }
  535. } // setDocumentHandler(XMLDocumentHandler)
  536. /** Returns the registered document handler. */
  537. public XMLDocumentHandler getDocumentHandler() {
  538. return fDocumentHandler;
  539. } // getDocumentHandler():XMLDocumentHandler
  540. /**
  541. * Sets the DTD handler.
  542. *
  543. * @param dtdHandler The DTD handler.
  544. */
  545. public void setDTDHandler(XMLDTDHandler dtdHandler) {
  546. fDTDHandler = dtdHandler;
  547. } // setDTDHandler(XMLDTDHandler)
  548. /** Returns the registered DTD handler. */
  549. public XMLDTDHandler getDTDHandler() {
  550. return fDTDHandler;
  551. } // getDTDHandler():XMLDTDHandler
  552. /**
  553. * Sets the DTD content model handler.
  554. *
  555. * @param handler The DTD content model handler.
  556. */
  557. public void setDTDContentModelHandler(XMLDTDContentModelHandler handler) {
  558. fDTDContentModelHandler = handler;
  559. } // setDTDContentModelHandler(XMLDTDContentModelHandler)
  560. /** Returns the registered DTD content model handler. */
  561. public XMLDTDContentModelHandler getDTDContentModelHandler() {
  562. return fDTDContentModelHandler;
  563. } // getDTDContentModelHandler():XMLDTDContentModelHandler
  564. /**
  565. * Sets the resolver used to resolve external entities. The EntityResolver
  566. * interface supports resolution of public and system identifiers.
  567. *
  568. * @param resolver The new entity resolver. Passing a null value will
  569. * uninstall the currently installed resolver.
  570. */
  571. public void setEntityResolver(XMLEntityResolver resolver) {
  572. fProperties.put(ENTITY_RESOLVER, resolver);
  573. } // setEntityResolver(XMLEntityResolver)
  574. /**
  575. * Return the current entity resolver.
  576. *
  577. * @return The current entity resolver, or null if none
  578. * has been registered.
  579. * @see #setEntityResolver
  580. */
  581. public XMLEntityResolver getEntityResolver() {
  582. return (XMLEntityResolver)fProperties.get(ENTITY_RESOLVER);
  583. } // getEntityResolver():XMLEntityResolver
  584. /**
  585. * Allow an application to register an error event handler.
  586. *
  587. * <p>If the application does not register an error handler, all
  588. * error events reported by the SAX parser will be silently
  589. * ignored; however, normal processing may not continue. It is
  590. * highly recommended that all SAX applications implement an
  591. * error handler to avoid unexpected bugs.</p>
  592. *
  593. * <p>Applications may register a new or different handler in the
  594. * middle of a parse, and the SAX parser must begin using the new
  595. * handler immediately.</p>
  596. *
  597. * @param errorHandler The error handler.
  598. * @exception java.lang.NullPointerException If the handler
  599. * argument is null.
  600. * @see #getErrorHandler
  601. */
  602. public void setErrorHandler(XMLErrorHandler errorHandler) {
  603. fProperties.put(ERROR_HANDLER, errorHandler);
  604. } // setErrorHandler(XMLErrorHandler)
  605. /**
  606. * Return the current error handler.
  607. *
  608. * @return The current error handler, or null if none
  609. * has been registered.
  610. * @see #setErrorHandler
  611. */
  612. public XMLErrorHandler getErrorHandler() {
  613. // REVISIT: Should this be a property?
  614. return (XMLErrorHandler)fProperties.get(ERROR_HANDLER);
  615. } // getErrorHandler():XMLErrorHandler
  616. /**
  617. * If the application decides to terminate parsing before the xml document
  618. * is fully parsed, the application should call this method to free any
  619. * resource allocated during parsing. For example, close all opened streams.
  620. */
  621. public void cleanup() {
  622. fEntityManager.closeReaders();
  623. }
  624. /**
  625. * Parses the specified input source.
  626. *
  627. * @param source The input source.
  628. *
  629. * @exception XNIException Throws exception on XNI error.
  630. * @exception java.io.IOException Throws exception on i/o error.
  631. */
  632. public void parse(XMLInputSource source) throws XNIException, IOException {
  633. if (fParseInProgress) {
  634. // REVISIT - need to add new error message
  635. throw new XNIException("FWK005 parse may not be called while parsing.");
  636. }
  637. fParseInProgress = true;
  638. try {
  639. setInputSource(source);
  640. parse(true);
  641. } catch (XNIException ex) {
  642. if (PRINT_EXCEPTION_STACK_TRACE)
  643. ex.printStackTrace();
  644. throw ex;
  645. } catch (IOException ex) {
  646. if (PRINT_EXCEPTION_STACK_TRACE)
  647. ex.printStackTrace();
  648. throw ex;
  649. } catch (RuntimeException ex) {
  650. if (PRINT_EXCEPTION_STACK_TRACE)
  651. ex.printStackTrace();
  652. throw ex;
  653. } catch (Exception ex) {
  654. if (PRINT_EXCEPTION_STACK_TRACE)
  655. ex.printStackTrace();
  656. throw new XNIException(ex);
  657. } finally {
  658. fParseInProgress = false;
  659. // close all streams opened by xerces
  660. this.cleanup();
  661. }
  662. } // parse(InputSource)
  663. public boolean parse(boolean complete) throws XNIException, IOException {
  664. //
  665. // reset and configure pipeline and set InputSource.
  666. if (fInputSource != null) {
  667. try {
  668. fValidationManager.reset();
  669. fVersionDetector.reset(this);
  670. resetCommon();
  671. short version = fVersionDetector.determineDocVersion(fInputSource);
  672. if (version == Constants.XML_VERSION_1_1) {
  673. initXML11Components();
  674. configureXML11Pipeline();
  675. resetXML11();
  676. } else {
  677. configurePipeline();
  678. reset();
  679. }
  680. // mark configuration as fixed
  681. fConfigUpdated = false;
  682. // resets and sets the pipeline.
  683. fVersionDetector.startDocumentParsing((XMLEntityHandler) fCurrentScanner, version);
  684. fInputSource = null;
  685. } catch (XNIException ex) {
  686. if (PRINT_EXCEPTION_STACK_TRACE)
  687. ex.printStackTrace();
  688. throw ex;
  689. } catch (IOException ex) {
  690. if (PRINT_EXCEPTION_STACK_TRACE)
  691. ex.printStackTrace();
  692. throw ex;
  693. } catch (RuntimeException ex) {
  694. if (PRINT_EXCEPTION_STACK_TRACE)
  695. ex.printStackTrace();
  696. throw ex;
  697. } catch (Exception ex) {
  698. if (PRINT_EXCEPTION_STACK_TRACE)
  699. ex.printStackTrace();
  700. throw new XNIException(ex);
  701. }
  702. }
  703. try {
  704. return fCurrentScanner.scanDocument(complete);
  705. } catch (XNIException ex) {
  706. if (PRINT_EXCEPTION_STACK_TRACE)
  707. ex.printStackTrace();
  708. throw ex;
  709. } catch (IOException ex) {
  710. if (PRINT_EXCEPTION_STACK_TRACE)
  711. ex.printStackTrace();
  712. throw ex;
  713. } catch (RuntimeException ex) {
  714. if (PRINT_EXCEPTION_STACK_TRACE)
  715. ex.printStackTrace();
  716. throw ex;
  717. } catch (Exception ex) {
  718. if (PRINT_EXCEPTION_STACK_TRACE)
  719. ex.printStackTrace();
  720. throw new XNIException(ex);
  721. }
  722. } // parse(boolean):boolean
  723. /**
  724. * Returns the state of a feature.
  725. *
  726. * @param featureId The feature identifier.
  727. * @return true if the feature is supported
  728. *
  729. * @throws XMLConfigurationException Thrown for configuration error.
  730. * In general, components should
  731. * only throw this exception if
  732. * it is <strong>really</strong>
  733. * a critical error.
  734. */
  735. public boolean getFeature(String featureId)
  736. throws XMLConfigurationException {
  737. // make this feature special
  738. if (featureId.equals(PARSER_SETTINGS)){
  739. return fConfigUpdated;
  740. }
  741. return super.getFeature(featureId);
  742. } // getFeature(String):boolean
  743. /**
  744. * Set the state of a feature.
  745. *
  746. * Set the state of any feature in a SAX2 parser. The parser
  747. * might not recognize the feature, and if it does recognize
  748. * it, it might not be able to fulfill the request.
  749. *
  750. * @param featureId The unique identifier (URI) of the feature.
  751. * @param state The requested state of the feature (true or false).
  752. *
  753. * @exception com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException If the
  754. * requested feature is not known.
  755. */
  756. public void setFeature(String featureId, boolean state)
  757. throws XMLConfigurationException {
  758. fConfigUpdated = true;
  759. // forward to every XML 1.0 component
  760. int count = fComponents.size();
  761. for (int i = 0; i < count; i++) {
  762. XMLComponent c = (XMLComponent) fComponents.get(i);
  763. c.setFeature(featureId, state);
  764. }
  765. // forward it to common components
  766. count = fCommonComponents.size();
  767. for (int i = 0; i < count; i++) {
  768. XMLComponent c = (XMLComponent) fCommonComponents.get(i);
  769. c.setFeature(featureId, state);
  770. }
  771. // forward to every XML 1.1 component
  772. count = fXML11Components.size();
  773. for (int i = 0; i < count; i++) {
  774. XMLComponent c = (XMLComponent) fXML11Components.get(i);
  775. try{
  776. c.setFeature(featureId, state);
  777. }
  778. catch (Exception e){
  779. // no op
  780. }
  781. }
  782. // save state if noone "objects"
  783. super.setFeature(featureId, state);
  784. } // setFeature(String,boolean)
  785. /**
  786. * setProperty
  787. *
  788. * @param propertyId
  789. * @param value
  790. */
  791. public void setProperty(String propertyId, Object value)
  792. throws XMLConfigurationException {
  793. fConfigUpdated = true;
  794. // forward to every XML 1.0 component
  795. int count = fComponents.size();
  796. for (int i = 0; i < count; i++) {
  797. XMLComponent c = (XMLComponent) fComponents.get(i);
  798. c.setProperty(propertyId, value);
  799. }
  800. // forward it to every common Component
  801. count = fCommonComponents.size();
  802. for (int i = 0; i < count; i++) {
  803. XMLComponent c = (XMLComponent) fCommonComponents.get(i);
  804. c.setProperty(propertyId, value);
  805. }
  806. // forward it to every XML 1.1 component
  807. count = fXML11Components.size();
  808. for (int i = 0; i < count; i++) {
  809. XMLComponent c = (XMLComponent) fXML11Components.get(i);
  810. try{
  811. c.setProperty(propertyId, value);
  812. }
  813. catch (Exception e){
  814. // ignore it
  815. }
  816. }
  817. // store value if noone "objects"
  818. super.setProperty(propertyId, value);
  819. } // setProperty(String,Object)
  820. /** Returns the locale. */
  821. public Locale getLocale() {
  822. return fLocale;
  823. } // getLocale():Locale
  824. /**
  825. * reset all XML 1.0 components before parsing and namespace context
  826. */
  827. protected void reset() throws XNIException {
  828. int count = fComponents.size();
  829. for (int i = 0; i < count; i++) {
  830. XMLComponent c = (XMLComponent) fComponents.get(i);
  831. c.reset(this);
  832. }
  833. } // reset()
  834. /**
  835. * reset all common components before parsing
  836. */
  837. protected void resetCommon() throws XNIException {
  838. // reset common components
  839. int count = fCommonComponents.size();
  840. for (int i = 0; i < count; i++) {
  841. XMLComponent c = (XMLComponent) fCommonComponents.get(i);
  842. c.reset(this);
  843. }
  844. } // resetCommon()
  845. /**
  846. * reset all components before parsing and namespace context
  847. */
  848. protected void resetXML11() throws XNIException {
  849. // reset every component
  850. int count = fXML11Components.size();
  851. for (int i = 0; i < count; i++) {
  852. XMLComponent c = (XMLComponent) fXML11Components.get(i);
  853. c.reset(this);
  854. }
  855. } // resetXML11()
  856. /**
  857. * Configures the XML 1.1 pipeline.
  858. * Note: this method also resets the new XML11 components.
  859. */
  860. protected void configureXML11Pipeline() {
  861. if (fCurrentDVFactory != fXML11DatatypeFactory) {
  862. fCurrentDVFactory = fXML11DatatypeFactory;
  863. setProperty(DATATYPE_VALIDATOR_FACTORY, fCurrentDVFactory);
  864. }
  865. if (fCurrentDTDScanner != fXML11DTDScanner) {
  866. fCurrentDTDScanner = fXML11DTDScanner;
  867. setProperty(DTD_SCANNER, fCurrentDTDScanner);
  868. setProperty(DTD_PROCESSOR, fXML11DTDProcessor);
  869. }
  870. fXML11DTDScanner.setDTDHandler(fXML11DTDProcessor);
  871. fXML11DTDProcessor.setDTDSource(fXML11DTDScanner);
  872. fXML11DTDProcessor.setDTDHandler(fDTDHandler);
  873. if (fDTDHandler != null) {
  874. fDTDHandler.setDTDSource(fXML11DTDProcessor);
  875. }
  876. fXML11DTDScanner.setDTDContentModelHandler(fXML11DTDProcessor);
  877. fXML11DTDProcessor.setDTDContentModelSource(fXML11DTDScanner);
  878. fXML11DTDProcessor.setDTDContentModelHandler(fDTDContentModelHandler);
  879. if (fDTDContentModelHandler != null) {
  880. fDTDContentModelHandler.setDTDContentModelSource(fXML11DTDProcessor);
  881. }
  882. // setup XML 1.1 document pipeline
  883. if (fFeatures.get(NAMESPACES) == Boolean.TRUE) {
  884. if (fCurrentScanner != fXML11NSDocScanner) {
  885. fCurrentScanner = fXML11NSDocScanner;
  886. setProperty(DOCUMENT_SCANNER, fXML11NSDocScanner);
  887. setProperty(DTD_VALIDATOR, fXML11NSDTDValidator);
  888. }
  889. fXML11NSDocScanner.setDTDValidator(fXML11NSDTDValidator);
  890. fXML11NSDocScanner.setDocumentHandler(fXML11NSDTDValidator);
  891. fXML11NSDTDValidator.setDocumentSource(fXML11NSDocScanner);
  892. fXML11NSDTDValidator.setDocumentHandler(fDocumentHandler);
  893. if (fDocumentHandler != null) {
  894. fDocumentHandler.setDocumentSource(fXML11NSDTDValidator);
  895. }
  896. fLastComponent = fXML11NSDTDValidator;
  897. } else {
  898. // create components
  899. if (fXML11DocScanner == null) {
  900. // non namespace document pipeline
  901. fXML11DocScanner = new XML11DocumentScannerImpl();
  902. addXML11Component(fXML11DocScanner);
  903. fXML11DTDValidator = new XML11DTDValidator();
  904. addXML11Component(fXML11DTDValidator);
  905. }
  906. if (fCurrentScanner != fXML11DocScanner) {
  907. fCurrentScanner = fXML11DocScanner;
  908. setProperty(DOCUMENT_SCANNER, fXML11DocScanner);
  909. setProperty(DTD_VALIDATOR, fXML11DTDValidator);
  910. }
  911. fXML11DocScanner.setDocumentHandler(fXML11DTDValidator);
  912. fXML11DTDValidator.setDocumentSource(fXML11DocScanner);
  913. fXML11DTDValidator.setDocumentHandler(fDocumentHandler);
  914. if (fDocumentHandler != null) {
  915. fDocumentHandler.setDocumentSource(fXML11DTDValidator);
  916. }
  917. fLastComponent = fXML11DTDValidator;
  918. }
  919. // setup document pipeline
  920. if (fFeatures.get(XMLSCHEMA_VALIDATION) == Boolean.TRUE) {
  921. // If schema validator was not in the pipeline insert it.
  922. if (fSchemaValidator == null) {
  923. fSchemaValidator = new XMLSchemaValidator();
  924. // add schema component
  925. setProperty(SCHEMA_VALIDATOR, fSchemaValidator);
  926. addCommonComponent(fSchemaValidator);
  927. fSchemaValidator.reset(this);
  928. // add schema message formatter
  929. if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) {
  930. XSMessageFormatter xmft = new XSMessageFormatter();
  931. fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, xmft);
  932. }
  933. }
  934. fLastComponent.setDocumentHandler(fSchemaValidator);
  935. fSchemaValidator.setDocumentSource(fLastComponent);
  936. fSchemaValidator.setDocumentHandler(fDocumentHandler);
  937. if (fDocumentHandler != null) {
  938. fDocumentHandler.setDocumentSource(fSchemaValidator);
  939. }
  940. fLastComponent = fSchemaValidator;
  941. }
  942. } // configureXML11Pipeline()
  943. /** Configures the pipeline. */
  944. protected void configurePipeline() {
  945. if (fCurrentDVFactory != fDatatypeValidatorFactory) {
  946. fCurrentDVFactory = fDatatypeValidatorFactory;
  947. // use XML 1.0 datatype library
  948. setProperty(DATATYPE_VALIDATOR_FACTORY, fCurrentDVFactory);
  949. }
  950. // setup DTD pipeline
  951. if (fCurrentDTDScanner != fDTDScanner) {
  952. fCurrentDTDScanner = fDTDScanner;
  953. setProperty(DTD_SCANNER, fCurrentDTDScanner);
  954. setProperty(DTD_PROCESSOR, fDTDProcessor);
  955. }
  956. fDTDScanner.setDTDHandler(fDTDProcessor);
  957. fDTDProcessor.setDTDSource(fDTDScanner);
  958. fDTDProcessor.setDTDHandler(fDTDHandler);
  959. if (fDTDHandler != null) {
  960. fDTDHandler.setDTDSource(fDTDProcessor);
  961. }
  962. fDTDScanner.setDTDContentModelHandler(fDTDProcessor);
  963. fDTDProcessor.setDTDContentModelSource(fDTDScanner);
  964. fDTDProcessor.setDTDContentModelHandler(fDTDContentModelHandler);
  965. if (fDTDContentModelHandler != null) {
  966. fDTDContentModelHandler.setDTDContentModelSource(fDTDProcessor);
  967. }
  968. // setup document pipeline
  969. if (fFeatures.get(NAMESPACES) == Boolean.TRUE) {
  970. if (fCurrentScanner != fNamespaceScanner) {
  971. fCurrentScanner = fNamespaceScanner;
  972. setProperty(DOCUMENT_SCANNER, fNamespaceScanner);
  973. setProperty(DTD_VALIDATOR, fDTDValidator);
  974. }
  975. fNamespaceScanner.setDTDValidator(fDTDValidator);
  976. fNamespaceScanner.setDocumentHandler(fDTDValidator);
  977. fDTDValidator.setDocumentSource(fNamespaceScanner);
  978. fDTDValidator.setDocumentHandler(fDocumentHandler);
  979. if (fDocumentHandler != null) {
  980. fDocumentHandler.setDocumentSource(fDTDValidator);
  981. }
  982. fLastComponent = fDTDValidator;
  983. } else {
  984. // create components
  985. if (fNonNSScanner == null) {
  986. fNonNSScanner = new XMLDocumentScannerImpl();
  987. fNonNSDTDValidator = new XMLDTDValidator();
  988. // add components
  989. addComponent((XMLComponent) fNonNSScanner);
  990. addComponent((XMLComponent) fNonNSDTDValidator);
  991. }
  992. if (fCurrentScanner != fNonNSScanner) {
  993. fCurrentScanner = fNonNSScanner;
  994. setProperty(DOCUMENT_SCANNER, fNonNSScanner);
  995. setProperty(DTD_VALIDATOR, fNonNSDTDValidator);
  996. }
  997. fNonNSScanner.setDocumentHandler(fNonNSDTDValidator);
  998. fNonNSDTDValidator.setDocumentSource(fNonNSScanner);
  999. fNonNSDTDValidator.setDocumentHandler(fDocumentHandler);
  1000. if (fDocumentHandler != null) {
  1001. fDocumentHandler.setDocumentSource(fNonNSDTDValidator);
  1002. }
  1003. fLastComponent = fNonNSDTDValidator;
  1004. }
  1005. // add XML Schema validator if needed
  1006. if (fFeatures.get(XMLSCHEMA_VALIDATION) == Boolean.TRUE) {
  1007. // If schema validator was not in the pipeline insert it.
  1008. if (fSchemaValidator == null) {
  1009. fSchemaValidator = new XMLSchemaValidator();
  1010. // add schema component
  1011. setProperty(SCHEMA_VALIDATOR, fSchemaValidator);
  1012. addCommonComponent(fSchemaValidator);
  1013. fSchemaValidator.reset(this);
  1014. // add schema message formatter
  1015. if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) {
  1016. XSMessageFormatter xmft = new XSMessageFormatter();
  1017. fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, xmft);
  1018. }
  1019. }
  1020. fLastComponent.setDocumentHandler(fSchemaValidator);
  1021. fSchemaValidator.setDocumentSource(fLastComponent);
  1022. fSchemaValidator.setDocumentHandler(fDocumentHandler);
  1023. if (fDocumentHandler != null) {
  1024. fDocumentHandler.setDocumentSource(fSchemaValidator);
  1025. }
  1026. fLastComponent = fSchemaValidator;
  1027. }
  1028. } // configurePipeline()
  1029. // features and properties
  1030. /**
  1031. * Check a feature. If feature is know and supported, this method simply
  1032. * returns. Otherwise, the appropriate exception is thrown.
  1033. *
  1034. * @param featureId The unique identifier (URI) of the feature.
  1035. *
  1036. * @throws XMLConfigurationException Thrown for configuration error.
  1037. * In general, components should
  1038. * only throw this exception if
  1039. * it is <strong>really</strong>
  1040. * a critical error.
  1041. */
  1042. protected void checkFeature(String featureId) throws XMLConfigurationException {
  1043. //
  1044. // Xerces Features
  1045. //
  1046. if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) {
  1047. final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length();
  1048. //
  1049. // http://apache.org/xml/features/validation/dynamic
  1050. // Allows the parser to validate a document only when it
  1051. // contains a grammar. Validation is turned on/off based
  1052. // on each document instance, automatically.
  1053. //
  1054. if (suffixLength == Constants.DYNAMIC_VALIDATION_FEATURE.length() &&
  1055. featureId.endsWith(Constants.DYNAMIC_VALIDATION_FEATURE)) {
  1056. return;
  1057. }
  1058. //
  1059. // http://apache.org/xml/features/validation/default-attribute-values
  1060. //
  1061. if (suffixLength == Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE.length() &&
  1062. featureId.endsWith(Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE)) {
  1063. // REVISIT
  1064. short type = XMLConfigurationException.NOT_SUPPORTED;
  1065. throw new XMLConfigurationException(type, featureId);
  1066. }
  1067. //
  1068. // http://apache.org/xml/features/validation/default-attribute-values
  1069. //
  1070. if (suffixLength == Constants.VALIDATE_CONTENT_MODELS_FEATURE.length() &&
  1071. featureId.endsWith(Constants.VALIDATE_CONTENT_MODELS_FEATURE)) {
  1072. // REVISIT
  1073. short type = XMLConfigurationException.NOT_SUPPORTED;
  1074. throw new XMLConfigurationException(type, featureId);
  1075. }
  1076. //
  1077. // http://apache.org/xml/features/validation/nonvalidating/load-dtd-grammar
  1078. //
  1079. if (suffixLength == Constants.LOAD_DTD_GRAMMAR_FEATURE.length() &&
  1080. featureId.endsWith(Constants.LOAD_DTD_GRAMMAR_FEATURE)) {
  1081. return;
  1082. }
  1083. //
  1084. // http://apache.org/xml/features/validation/nonvalidating/load-external-dtd
  1085. //
  1086. if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() &&
  1087. featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) {
  1088. return;
  1089. }
  1090. //
  1091. // http://apache.org/xml/features/validation/default-attribute-values
  1092. //
  1093. if (suffixLength == Constants.VALIDATE_DATATYPES_FEATURE.length() &&
  1094. featureId.endsWith(Constants.VALIDATE_DATATYPES_FEATURE)) {
  1095. short type = XMLConfigurationException.NOT_SUPPORTED;
  1096. throw new XMLConfigurationException(type, featureId);
  1097. }
  1098. //
  1099. // http://apache.org/xml/features/validation/schema
  1100. // Lets the user turn Schema validation support on/off.
  1101. //
  1102. if (suffixLength == Constants.SCHEMA_VALIDATION_FEATURE.length() &&
  1103. featureId.endsWith(Constants.SCHEMA_VALIDATION_FEATURE)) {
  1104. return;
  1105. }
  1106. // activate full schema checking
  1107. if (suffixLength == Constants.SCHEMA_FULL_CHECKING.length() &&
  1108. featureId.endsWith(Constants.SCHEMA_FULL_CHECKING)) {
  1109. return;
  1110. }
  1111. // Feature identifier: expose schema normalized value
  1112. // http://apache.org/xml/features/validation/schema/normalized-value
  1113. if (suffixLength == Constants.SCHEMA_NORMALIZED_VALUE.length() &&
  1114. featureId.endsWith(Constants.SCHEMA_NORMALIZED_VALUE)) {
  1115. return;
  1116. }
  1117. // Feature identifier: send element default value via characters()
  1118. // http://apache.org/xml/features/validation/schema/element-default
  1119. if (suffixLength == Constants.SCHEMA_ELEMENT_DEFAULT.length() &&
  1120. featureId.endsWith(Constants.SCHEMA_ELEMENT_DEFAULT)) {
  1121. return;
  1122. }
  1123. // special performance feature: only component manager is allowed to set it.
  1124. if (suffixLength == Constants.PARSER_SETTINGS.length() &&
  1125. featureId.endsWith(Constants.PARSER_SETTINGS)) {
  1126. short type = XMLConfigurationException.NOT_SUPPORTED;
  1127. throw new XMLConfigurationException(type, featureId);
  1128. }
  1129. }
  1130. //
  1131. // Not recognized
  1132. //
  1133. super.checkFeature(featureId);
  1134. } // checkFeature(String)
  1135. /**
  1136. * Check a property. If the property is know and supported, this method
  1137. * simply returns. Otherwise, the appropriate exception is thrown.
  1138. *
  1139. * @param propertyId The unique identifier (URI) of the property
  1140. * being set.
  1141. *
  1142. * @throws XMLConfigurationException Thrown for configuration error.
  1143. * In general, components should
  1144. * only throw this exception if
  1145. * it is <strong>really</strong>
  1146. * a critical error.
  1147. */
  1148. protected void checkProperty(String propertyId) throws XMLConfigurationException {
  1149. //
  1150. // Xerces Properties
  1151. //
  1152. if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
  1153. final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length();
  1154. if (suffixLength == Constants.DTD_SCANNER_PROPERTY.length() &&
  1155. propertyId.endsWith(Constants.DTD_SCANNER_PROPERTY)) {
  1156. return;
  1157. }
  1158. if (suffixLength == Constants.SCHEMA_LOCATION.length() &&
  1159. propertyId.endsWith(Constants.SCHEMA_LOCATION)) {
  1160. return;
  1161. }
  1162. if (suffixLength == Constants.SCHEMA_NONS_LOCATION.length() &&
  1163. propertyId.endsWith(Constants.SCHEMA_NONS_LOCATION)) {
  1164. return;
  1165. }
  1166. }
  1167. if (propertyId.startsWith(Constants.JAXP_PROPERTY_PREFIX)) {
  1168. final int suffixLength = propertyId.length() - Constants.JAXP_PROPERTY_PREFIX.length();
  1169. if (suffixLength == Constants.SCHEMA_SOURCE.length() &&
  1170. propertyId.endsWith(Constants.SCHEMA_SOURCE)) {
  1171. return;
  1172. }
  1173. }
  1174. // special cases
  1175. if (propertyId.startsWith(Constants.SAX_PROPERTY_PREFIX)) {
  1176. final int suffixLength = propertyId.length() - Constants.SAX_PROPERTY_PREFIX.length();
  1177. //
  1178. // http://xml.org/sax/properties/xml-string
  1179. // Value type: String
  1180. // Access: read-only
  1181. // Get the literal string of characters associated with the
  1182. // current event. If the parser recognises and supports this
  1183. // property but is not currently parsing text, it should return
  1184. // null (this is a good way to check for availability before the
  1185. // parse begins).
  1186. //
  1187. if (suffixLength == Constants.XML_STRING_PROPERTY.length() &&
  1188. propertyId.endsWith(Constants.XML_STRING_PROPERTY)) {
  1189. // REVISIT - we should probably ask xml-dev for a precise
  1190. // definition of what this is actually supposed to return, and
  1191. // in exactly which circumstances.
  1192. short type = XMLConfigurationException.NOT_SUPPORTED;
  1193. throw new XMLConfigurationException(type, propertyId);
  1194. }
  1195. }
  1196. //
  1197. // Not recognized
  1198. //
  1199. super.checkProperty(propertyId);
  1200. } // checkProperty(String)
  1201. /**
  1202. * Adds a component to the parser configuration. This method will
  1203. * also add all of the component's recognized features and properties
  1204. * to the list of default recognized features and properties.
  1205. *
  1206. * @param component The component to add.
  1207. */
  1208. protected void addComponent(XMLComponent component) {
  1209. // don't add a component more than once
  1210. if (fComponents.contains(component)) {
  1211. return;
  1212. }
  1213. fComponents.add(component);
  1214. addRecognizedParamsAndSetDefaults(component);
  1215. } // addComponent(XMLComponent)
  1216. /**
  1217. * Adds common component to the parser configuration. This method will
  1218. * also add all of the component's recognized features and properties
  1219. * to the list of default recognized features and properties.
  1220. *
  1221. * @param component The component to add.
  1222. */
  1223. protected void addCommonComponent(XMLComponent component) {
  1224. // don't add a component more than once
  1225. if (fCommonComponents.contains(component)) {
  1226. return;
  1227. }
  1228. fCommonComponents.add(component);
  1229. addRecognizedParamsAndSetDefaults(component);
  1230. } // addCommonComponent(XMLComponent)
  1231. /**
  1232. * Adds an XML 1.1 component to the parser configuration. This method will
  1233. * also add all of the component's recognized features and properties
  1234. * to the list of default recognized features and properties.
  1235. *
  1236. * @param component The component to add.
  1237. */
  1238. protected void addXML11Component(XMLComponent component) {
  1239. // don't add a component more than once
  1240. if (fXML11Components.contains(component)) {
  1241. return;
  1242. }
  1243. fXML11Components.add(component);
  1244. addRecognizedParamsAndSetDefaults(component);
  1245. } // addXML11Component(XMLComponent)
  1246. /**
  1247. * Adds all of the component's recognized features and properties
  1248. * to the list of default recognized features and properties, and
  1249. * sets default values on the configuration for features and
  1250. * properties which were previously absent from the configuration.
  1251. *
  1252. * @param component The component whose recognized features
  1253. * and properties will be added to the configuration
  1254. */
  1255. protected void addRecognizedParamsAndSetDefaults(XMLComponent component) {
  1256. // register component's recognized features
  1257. String[] recognizedFeatures = component.getRecognizedFeatures();
  1258. addRecognizedFeatures(recognizedFeatures);
  1259. // register component's recognized properties
  1260. String[] recognizedProperties = component.getRecognizedProperties();
  1261. addRecognizedProperties(recognizedProperties);
  1262. // set default values
  1263. if (recognizedFeatures != null) {
  1264. for (int i = 0; i < recognizedFeatures.length; ++i) {
  1265. String featureId = recognizedFeatures[i];
  1266. Boolean state = component.getFeatureDefault(featureId);
  1267. if (state != null) {
  1268. // Do not overwrite values already set on the configuration.
  1269. if (!fFeatures.containsKey(featureId)) {
  1270. fFeatures.put(featureId, state);
  1271. // For newly added components who recognize this feature
  1272. // but did not offer a default value, we need to make
  1273. // sure these components will get an opportunity to read
  1274. // the value before parsing begins.
  1275. fConfigUpdated = true;
  1276. }
  1277. }
  1278. }
  1279. }
  1280. if (recognizedProperties != null) {
  1281. for (int i = 0; i < recognizedProperties.length; ++i) {
  1282. String propertyId = recognizedProperties[i];
  1283. Object value = component.getPropertyDefault(propertyId);
  1284. if (value != null) {
  1285. // Do not overwrite values already set on the configuration.
  1286. if (!fProperties.containsKey(propertyId)) {
  1287. fProperties.put(propertyId, value);
  1288. // For newly added components who recognize this property
  1289. // but did not offer a default value, we need to make
  1290. // sure these components will get an opportunity to read
  1291. // the value before parsing begins.
  1292. fConfigUpdated = true;
  1293. }
  1294. }
  1295. }
  1296. }
  1297. }
  1298. private void initXML11Components() {
  1299. if (!f11Initialized) {
  1300. // create datatype factory
  1301. fXML11DatatypeFactory = DTDDVFactory.getInstance(XML11_DATATYPE_VALIDATOR_FACTORY);
  1302. // setup XML 1.1 DTD pipeline
  1303. fXML11DTDScanner = new XML11DTDScannerImpl();
  1304. addXML11Component(fXML11DTDScanner);
  1305. fXML11DTDProcessor = new XML11DTDProcessor();
  1306. addXML11Component(fXML11DTDProcessor);
  1307. // setup XML 1.1. document pipeline - namespace aware
  1308. fXML11NSDocScanner = new XML11NSDocumentScannerImpl();
  1309. addXML11Component(fXML11NSDocScanner);
  1310. fXML11NSDTDValidator = new XML11NSDTDValidator();
  1311. addXML11Component(fXML11NSDTDValidator);
  1312. f11Initialized = true;
  1313. }
  1314. }
  1315. } // class XML11Configuration