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) 1999, 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.Locale;
  60. import com.sun.org.apache.xerces.internal.impl.Constants;
  61. import com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl;
  62. import com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl;
  63. import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
  64. import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
  65. import com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl;
  66. import com.sun.org.apache.xerces.internal.impl.dv.DTDDVFactory;
  67. import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
  68. import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;
  69. import com.sun.org.apache.xerces.internal.util.SymbolTable;
  70. import com.sun.org.apache.xerces.internal.xni.XMLLocator;
  71. import com.sun.org.apache.xerces.internal.xni.XNIException;
  72. import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
  73. import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
  74. import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
  75. import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
  76. import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDScanner;
  77. import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentScanner;
  78. import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
  79. import com.sun.org.apache.xerces.internal.xni.parser.XMLPullParserConfiguration;
  80. /**
  81. * This is the non validating parser configuration. It extends the basic
  82. * configuration with the set of following parser components:
  83. * Document scanner, DTD scanner, namespace binder, document handler.
  84. * <p>
  85. * Xerces parser that uses this configuration is <strong>not</strong> <a href="http://www.w3.org/TR/REC-xml#sec-conformance">conformant</a>
  86. * non-validating XML processor, since conformant non-validating processor is required
  87. * to process "all the declarations they read in the internal DTD subset ... must use the information in those declarations to normalize attribute values,
  88. * include the replacement text of internal entities, and supply default attribute values".
  89. *
  90. * @author Elena Litani, IBM
  91. * @version $Id: NonValidatingConfiguration.java,v 1.12 2004/01/26 17:28:10 mrglavas Exp $
  92. */
  93. public class NonValidatingConfiguration
  94. extends BasicParserConfiguration
  95. implements XMLPullParserConfiguration {
  96. //
  97. // Constants
  98. //
  99. // feature identifiers
  100. /** Feature identifier: warn on duplicate attribute definition. */
  101. protected static final String WARN_ON_DUPLICATE_ATTDEF =
  102. Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE;
  103. /** Feature identifier: warn on duplicate entity definition. */
  104. protected static final String WARN_ON_DUPLICATE_ENTITYDEF =
  105. Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE;
  106. /** Feature identifier: warn on undeclared element definition. */
  107. protected static final String WARN_ON_UNDECLARED_ELEMDEF =
  108. Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_UNDECLARED_ELEMDEF_FEATURE;
  109. /** Feature identifier: allow Java encodings. */
  110. protected static final String ALLOW_JAVA_ENCODINGS =
  111. Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE;
  112. /** Feature identifier: continue after fatal error. */
  113. protected static final String CONTINUE_AFTER_FATAL_ERROR =
  114. Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE;
  115. /** Feature identifier: load external DTD. */
  116. protected static final String LOAD_EXTERNAL_DTD =
  117. Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE;
  118. /** Feature identifier: notify built-in refereces. */
  119. protected static final String NOTIFY_BUILTIN_REFS =
  120. Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_BUILTIN_REFS_FEATURE;
  121. /** Feature identifier: notify character refereces. */
  122. protected static final String NOTIFY_CHAR_REFS =
  123. Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_CHAR_REFS_FEATURE;
  124. /** Feature identifier: expose schema normalized value */
  125. protected static final String NORMALIZE_DATA =
  126. Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE;
  127. /** Feature identifier: send element default value via characters() */
  128. protected static final String SCHEMA_ELEMENT_DEFAULT =
  129. Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_ELEMENT_DEFAULT;
  130. // property identifiers
  131. /** Property identifier: error reporter. */
  132. protected static final String ERROR_REPORTER =
  133. Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
  134. /** Property identifier: entity manager. */
  135. protected static final String ENTITY_MANAGER =
  136. Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
  137. /** Property identifier document scanner: */
  138. protected static final String DOCUMENT_SCANNER =
  139. Constants.XERCES_PROPERTY_PREFIX + Constants.DOCUMENT_SCANNER_PROPERTY;
  140. /** Property identifier: DTD scanner. */
  141. protected static final String DTD_SCANNER =
  142. Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_SCANNER_PROPERTY;
  143. /** Property identifier: grammar pool. */
  144. protected static final String XMLGRAMMAR_POOL =
  145. Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
  146. /** Property identifier: DTD validator. */
  147. protected static final String DTD_VALIDATOR =
  148. Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_VALIDATOR_PROPERTY;
  149. /** Property identifier: namespace binder. */
  150. protected static final String NAMESPACE_BINDER =
  151. Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_BINDER_PROPERTY;
  152. /** Property identifier: datatype validator factory. */
  153. protected static final String DATATYPE_VALIDATOR_FACTORY =
  154. Constants.XERCES_PROPERTY_PREFIX + Constants.DATATYPE_VALIDATOR_FACTORY_PROPERTY;
  155. protected static final String VALIDATION_MANAGER =
  156. Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY;
  157. /** Property identifier: XML Schema validator. */
  158. protected static final String SCHEMA_VALIDATOR =
  159. Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY;
  160. // debugging
  161. /** Set to true and recompile to print exception stack trace. */
  162. private static final boolean PRINT_EXCEPTION_STACK_TRACE = false;
  163. //
  164. // Data
  165. //
  166. // components (non-configurable)
  167. /** Grammar pool. */
  168. protected XMLGrammarPool fGrammarPool;
  169. /** Datatype validator factory. */
  170. protected DTDDVFactory fDatatypeValidatorFactory;
  171. // components (configurable)
  172. /** Error reporter. */
  173. protected XMLErrorReporter fErrorReporter;
  174. /** Entity manager. */
  175. protected XMLEntityManager fEntityManager;
  176. /** Document scanner. */
  177. protected XMLDocumentScanner fScanner;
  178. /** Input Source */
  179. protected XMLInputSource fInputSource;
  180. /** DTD scanner. */
  181. protected XMLDTDScanner fDTDScanner;
  182. protected ValidationManager fValidationManager;
  183. // private data
  184. /** Document scanner that does namespace binding. */
  185. private XMLNSDocumentScannerImpl fNamespaceScanner;
  186. /** Default Xerces implementation of scanner*/
  187. private XMLDocumentScannerImpl fNonNSScanner;
  188. /** fConfigUpdated is set to true if there has been any change to the configuration settings,
  189. * i.e a feature or a property was changed.
  190. */
  191. protected boolean fConfigUpdated = false;
  192. // state
  193. /** Locator */
  194. protected XMLLocator fLocator;
  195. /**
  196. * True if a parse is in progress. This state is needed because
  197. * some features/properties cannot be set while parsing (e.g.
  198. * validation and namespaces).
  199. */
  200. protected boolean fParseInProgress = false;
  201. //
  202. // Constructors
  203. //
  204. /** Default constructor. */
  205. public NonValidatingConfiguration() {
  206. this(null, null, null);
  207. } // <init>()
  208. /**
  209. * Constructs a parser configuration using the specified symbol table.
  210. *
  211. * @param symbolTable The symbol table to use.
  212. */
  213. public NonValidatingConfiguration(SymbolTable symbolTable) {
  214. this(symbolTable, null, null);
  215. } // <init>(SymbolTable)
  216. /**
  217. * Constructs a parser configuration using the specified symbol table and
  218. * grammar pool.
  219. * <p>
  220. * <strong>REVISIT:</strong>
  221. * Grammar pool will be updated when the new validation engine is
  222. * implemented.
  223. *
  224. * @param symbolTable The symbol table to use.
  225. * @param grammarPool The grammar pool to use.
  226. */
  227. public NonValidatingConfiguration(SymbolTable symbolTable,
  228. XMLGrammarPool grammarPool) {
  229. this(symbolTable, grammarPool, null);
  230. } // <init>(SymbolTable,XMLGrammarPool)
  231. /**
  232. * Constructs a parser configuration using the specified symbol table,
  233. * grammar pool, and parent settings.
  234. * <p>
  235. * <strong>REVISIT:</strong>
  236. * Grammar pool will be updated when the new validation engine is
  237. * implemented.
  238. *
  239. * @param symbolTable The symbol table to use.
  240. * @param grammarPool The grammar pool to use.
  241. * @param parentSettings The parent settings.
  242. */
  243. public NonValidatingConfiguration(SymbolTable symbolTable,
  244. XMLGrammarPool grammarPool,
  245. XMLComponentManager parentSettings) {
  246. super(symbolTable, parentSettings);
  247. // add default recognized features
  248. final String[] recognizedFeatures = {
  249. PARSER_SETTINGS,
  250. NAMESPACES,
  251. //WARN_ON_DUPLICATE_ATTDEF, // from XMLDTDScannerImpl
  252. //WARN_ON_UNDECLARED_ELEMDEF, // from XMLDTDScannerImpl
  253. //ALLOW_JAVA_ENCODINGS, // from XMLEntityManager
  254. CONTINUE_AFTER_FATAL_ERROR,
  255. //LOAD_EXTERNAL_DTD, // from XMLDTDScannerImpl
  256. //NOTIFY_BUILTIN_REFS, // from XMLDocumentFragmentScannerImpl
  257. //NOTIFY_CHAR_REFS, // from XMLDocumentFragmentScannerImpl
  258. //WARN_ON_DUPLICATE_ENTITYDEF // from XMLEntityManager
  259. };
  260. addRecognizedFeatures(recognizedFeatures);
  261. // set state for default features
  262. //setFeature(WARN_ON_DUPLICATE_ATTDEF, false); // from XMLDTDScannerImpl
  263. //setFeature(WARN_ON_UNDECLARED_ELEMDEF, false); // from XMLDTDScannerImpl
  264. //setFeature(ALLOW_JAVA_ENCODINGS, false); // from XMLEntityManager
  265. fFeatures.put(CONTINUE_AFTER_FATAL_ERROR, Boolean.FALSE);
  266. fFeatures.put(PARSER_SETTINGS, Boolean.TRUE);
  267. fFeatures.put(NAMESPACES, Boolean.TRUE);
  268. //setFeature(LOAD_EXTERNAL_DTD, true); // from XMLDTDScannerImpl
  269. //setFeature(NOTIFY_BUILTIN_REFS, false); // from XMLDocumentFragmentScannerImpl
  270. //setFeature(NOTIFY_CHAR_REFS, false); // from XMLDocumentFragmentScannerImpl
  271. //setFeature(WARN_ON_DUPLICATE_ENTITYDEF, false); // from XMLEntityManager
  272. // add default recognized properties
  273. final String[] recognizedProperties = {
  274. ERROR_REPORTER,
  275. ENTITY_MANAGER,
  276. DOCUMENT_SCANNER,
  277. DTD_SCANNER,
  278. DTD_VALIDATOR,
  279. NAMESPACE_BINDER,
  280. XMLGRAMMAR_POOL,
  281. DATATYPE_VALIDATOR_FACTORY,
  282. VALIDATION_MANAGER
  283. };
  284. addRecognizedProperties(recognizedProperties);
  285. fGrammarPool = grammarPool;
  286. if(fGrammarPool != null){
  287. fProperties.put(XMLGRAMMAR_POOL, fGrammarPool);
  288. }
  289. fEntityManager = createEntityManager();
  290. fProperties.put(ENTITY_MANAGER, fEntityManager);
  291. addComponent(fEntityManager);
  292. fErrorReporter = createErrorReporter();
  293. fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner());
  294. fProperties.put(ERROR_REPORTER, fErrorReporter);
  295. addComponent(fErrorReporter);
  296. // this configuration delays creation of the scanner
  297. // till it is known if namespace processing should be performed
  298. fDTDScanner = createDTDScanner();
  299. if (fDTDScanner != null) {
  300. fProperties.put(DTD_SCANNER, fDTDScanner);
  301. if (fDTDScanner instanceof XMLComponent) {
  302. addComponent((XMLComponent)fDTDScanner);
  303. }
  304. }
  305. fDatatypeValidatorFactory = createDatatypeValidatorFactory();
  306. if (fDatatypeValidatorFactory != null) {
  307. fProperties.put(DATATYPE_VALIDATOR_FACTORY,
  308. fDatatypeValidatorFactory);
  309. }
  310. fValidationManager = createValidationManager();
  311. if (fValidationManager != null) {
  312. fProperties.put(VALIDATION_MANAGER, fValidationManager);
  313. }
  314. // add message formatters
  315. if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) {
  316. XMLMessageFormatter xmft = new XMLMessageFormatter();
  317. fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft);
  318. fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft);
  319. }
  320. fConfigUpdated = false;
  321. // set locale
  322. try {
  323. setLocale(Locale.getDefault());
  324. }
  325. catch (XNIException e) {
  326. // do nothing
  327. // REVISIT: What is the right thing to do? -Ac
  328. }
  329. } // <init>(SymbolTable,XMLGrammarPool)
  330. //
  331. // Public methods
  332. //
  333. public void setFeature(String featureId, boolean state)
  334. throws XMLConfigurationException {
  335. fConfigUpdated = true;
  336. super.setFeature(featureId, state);
  337. }
  338. public void setProperty(String propertyId, Object value)
  339. throws XMLConfigurationException {
  340. fConfigUpdated = true;
  341. super.setProperty(propertyId, value);
  342. }
  343. /**
  344. * Set the locale to use for messages.
  345. *
  346. * @param locale The locale object to use for localization of messages.
  347. *
  348. * @exception XNIException Thrown if the parser does not support the
  349. * specified locale.
  350. */
  351. public void setLocale(Locale locale) throws XNIException {
  352. super.setLocale(locale);
  353. fErrorReporter.setLocale(locale);
  354. } // setLocale(Locale)
  355. public boolean getFeature(String featureId)
  356. throws XMLConfigurationException {
  357. // make this feature special
  358. if (featureId.equals(PARSER_SETTINGS)){
  359. return fConfigUpdated;
  360. }
  361. return super.getFeature(featureId);
  362. } // getFeature(String):boolean
  363. //
  364. // XMLPullParserConfiguration methods
  365. //
  366. // parsing
  367. /**
  368. * Sets the input source for the document to parse.
  369. *
  370. * @param inputSource The document's input source.
  371. *
  372. * @exception XMLConfigurationException Thrown if there is a
  373. * configuration error when initializing the
  374. * parser.
  375. * @exception IOException Thrown on I/O error.
  376. *
  377. * @see #parse(boolean)
  378. */
  379. public void setInputSource(XMLInputSource inputSource)
  380. throws XMLConfigurationException, IOException {
  381. // REVISIT: this method used to reset all the components and
  382. // construct the pipeline. Now reset() is called
  383. // in parse (boolean) just before we parse the document
  384. // Should this method still throw exceptions..?
  385. fInputSource = inputSource;
  386. } // setInputSource(XMLInputSource)
  387. /**
  388. * Parses the document in a pull parsing fashion.
  389. *
  390. * @param complete True if the pull parser should parse the
  391. * remaining document completely.
  392. *
  393. * @return True if there is more document to parse.
  394. *
  395. * @exception XNIException Any XNI exception, possibly wrapping
  396. * another exception.
  397. * @exception IOException An IO exception from the parser, possibly
  398. * from a byte stream or character stream
  399. * supplied by the parser.
  400. *
  401. * @see #setInputSource
  402. */
  403. public boolean parse(boolean complete) throws XNIException, IOException {
  404. //
  405. // reset and configure pipeline and set InputSource.
  406. if (fInputSource !=null) {
  407. try {
  408. // resets and sets the pipeline.
  409. reset();
  410. fScanner.setInputSource(fInputSource);
  411. fInputSource = null;
  412. }
  413. catch (XNIException ex) {
  414. if (PRINT_EXCEPTION_STACK_TRACE)
  415. ex.printStackTrace();
  416. throw ex;
  417. }
  418. catch (IOException ex) {
  419. if (PRINT_EXCEPTION_STACK_TRACE)
  420. ex.printStackTrace();
  421. throw ex;
  422. }
  423. catch (RuntimeException ex) {
  424. if (PRINT_EXCEPTION_STACK_TRACE)
  425. ex.printStackTrace();
  426. throw ex;
  427. }
  428. catch (Exception ex) {
  429. if (PRINT_EXCEPTION_STACK_TRACE)
  430. ex.printStackTrace();
  431. throw new XNIException(ex);
  432. }
  433. }
  434. try {
  435. return fScanner.scanDocument(complete);
  436. }
  437. catch (XNIException ex) {
  438. if (PRINT_EXCEPTION_STACK_TRACE)
  439. ex.printStackTrace();
  440. throw ex;
  441. }
  442. catch (IOException ex) {
  443. if (PRINT_EXCEPTION_STACK_TRACE)
  444. ex.printStackTrace();
  445. throw ex;
  446. }
  447. catch (RuntimeException ex) {
  448. if (PRINT_EXCEPTION_STACK_TRACE)
  449. ex.printStackTrace();
  450. throw ex;
  451. }
  452. catch (Exception ex) {
  453. if (PRINT_EXCEPTION_STACK_TRACE)
  454. ex.printStackTrace();
  455. throw new XNIException(ex);
  456. }
  457. } // parse(boolean):boolean
  458. /**
  459. * If the application decides to terminate parsing before the xml document
  460. * is fully parsed, the application should call this method to free any
  461. * resource allocated during parsing. For example, close all opened streams.
  462. */
  463. public void cleanup() {
  464. fEntityManager.closeReaders();
  465. }
  466. //
  467. // XMLParserConfiguration methods
  468. //
  469. /**
  470. * Parses the specified input source.
  471. *
  472. * @param source The input source.
  473. *
  474. * @exception XNIException Throws exception on XNI error.
  475. * @exception java.io.IOException Throws exception on i/o error.
  476. */
  477. public void parse(XMLInputSource source) throws XNIException, IOException {
  478. if (fParseInProgress) {
  479. // REVISIT - need to add new error message
  480. throw new XNIException("FWK005 parse may not be called while parsing.");
  481. }
  482. fParseInProgress = true;
  483. try {
  484. setInputSource(source);
  485. parse(true);
  486. }
  487. catch (XNIException ex) {
  488. if (PRINT_EXCEPTION_STACK_TRACE)
  489. ex.printStackTrace();
  490. throw ex;
  491. }
  492. catch (IOException ex) {
  493. if (PRINT_EXCEPTION_STACK_TRACE)
  494. ex.printStackTrace();
  495. throw ex;
  496. }
  497. catch (RuntimeException ex) {
  498. if (PRINT_EXCEPTION_STACK_TRACE)
  499. ex.printStackTrace();
  500. throw ex;
  501. }
  502. catch (Exception ex) {
  503. if (PRINT_EXCEPTION_STACK_TRACE)
  504. ex.printStackTrace();
  505. throw new XNIException(ex);
  506. }
  507. finally {
  508. fParseInProgress = false;
  509. // close all streams opened by xerces
  510. this.cleanup();
  511. }
  512. } // parse(InputSource)
  513. //
  514. // Protected methods
  515. //
  516. /**
  517. * Reset all components before parsing.
  518. *
  519. * @throws XNIException Thrown if an error occurs during initialization.
  520. */
  521. protected void reset() throws XNIException {
  522. if (fValidationManager != null)
  523. fValidationManager.reset();
  524. // configure the pipeline and initialize the components
  525. configurePipeline();
  526. super.reset();
  527. } // reset()
  528. /** Configures the pipeline. */
  529. protected void configurePipeline() {
  530. // create appropriate scanner
  531. // and register it as one of the components.
  532. if (fFeatures.get(NAMESPACES) == Boolean.TRUE) {
  533. if (fNamespaceScanner == null) {
  534. fNamespaceScanner = new XMLNSDocumentScannerImpl();
  535. addComponent((XMLComponent)fNamespaceScanner);
  536. }
  537. fProperties.put(DOCUMENT_SCANNER, fNamespaceScanner);
  538. fNamespaceScanner.setDTDValidator(null);
  539. fScanner = fNamespaceScanner;
  540. }
  541. else {
  542. if (fNonNSScanner == null) {
  543. fNonNSScanner = new XMLDocumentScannerImpl();
  544. addComponent((XMLComponent)fNonNSScanner);
  545. }
  546. fProperties.put(DOCUMENT_SCANNER, fNonNSScanner);
  547. fScanner = fNonNSScanner;
  548. }
  549. fScanner.setDocumentHandler(fDocumentHandler);
  550. fLastComponent = fScanner;
  551. // setup dtd pipeline
  552. if (fDTDScanner != null) {
  553. fDTDScanner.setDTDHandler(fDTDHandler);
  554. fDTDScanner.setDTDContentModelHandler(fDTDContentModelHandler);
  555. }
  556. } // configurePipeline()
  557. // features and properties
  558. /**
  559. * Check a feature. If feature is know and supported, this method simply
  560. * returns. Otherwise, the appropriate exception is thrown.
  561. *
  562. * @param featureId The unique identifier (URI) of the feature.
  563. *
  564. * @throws XMLConfigurationException Thrown for configuration error.
  565. * In general, components should
  566. * only throw this exception if
  567. * it is <strong>really</strong>
  568. * a critical error.
  569. */
  570. protected void checkFeature(String featureId)
  571. throws XMLConfigurationException {
  572. //
  573. // Xerces Features
  574. //
  575. if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) {
  576. final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length();
  577. //
  578. // http://apache.org/xml/features/validation/dynamic
  579. // Allows the parser to validate a document only when it
  580. // contains a grammar. Validation is turned on/off based
  581. // on each document instance, automatically.
  582. //
  583. if (suffixLength == Constants.DYNAMIC_VALIDATION_FEATURE.length() &&
  584. featureId.endsWith(Constants.DYNAMIC_VALIDATION_FEATURE)) {
  585. return;
  586. }
  587. //
  588. // http://apache.org/xml/features/validation/default-attribute-values
  589. //
  590. if (suffixLength == Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE.length() &&
  591. featureId.endsWith(Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE)) {
  592. // REVISIT
  593. short type = XMLConfigurationException.NOT_SUPPORTED;
  594. throw new XMLConfigurationException(type, featureId);
  595. }
  596. //
  597. // http://apache.org/xml/features/validation/default-attribute-values
  598. //
  599. if (suffixLength == Constants.VALIDATE_CONTENT_MODELS_FEATURE.length() &&
  600. featureId.endsWith(Constants.VALIDATE_CONTENT_MODELS_FEATURE)) {
  601. // REVISIT
  602. short type = XMLConfigurationException.NOT_SUPPORTED;
  603. throw new XMLConfigurationException(type, featureId);
  604. }
  605. //
  606. // http://apache.org/xml/features/validation/nonvalidating/load-dtd-grammar
  607. //
  608. if (suffixLength == Constants.LOAD_DTD_GRAMMAR_FEATURE.length() &&
  609. featureId.endsWith(Constants.LOAD_DTD_GRAMMAR_FEATURE)) {
  610. return;
  611. }
  612. //
  613. // http://apache.org/xml/features/validation/nonvalidating/load-external-dtd
  614. //
  615. if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() &&
  616. featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) {
  617. return;
  618. }
  619. //
  620. // http://apache.org/xml/features/validation/default-attribute-values
  621. //
  622. if (suffixLength == Constants.VALIDATE_DATATYPES_FEATURE.length() &&
  623. featureId.endsWith(Constants.VALIDATE_DATATYPES_FEATURE)) {
  624. short type = XMLConfigurationException.NOT_SUPPORTED;
  625. throw new XMLConfigurationException(type, featureId);
  626. }
  627. }
  628. //
  629. // Not recognized
  630. //
  631. super.checkFeature(featureId);
  632. } // checkFeature(String)
  633. /**
  634. * Check a property. If the property is know and supported, this method
  635. * simply returns. Otherwise, the appropriate exception is thrown.
  636. *
  637. * @param propertyId The unique identifier (URI) of the property
  638. * being set.
  639. *
  640. * @throws XMLConfigurationException Thrown for configuration error.
  641. * In general, components should
  642. * only throw this exception if
  643. * it is <strong>really</strong>
  644. * a critical error.
  645. */
  646. protected void checkProperty(String propertyId)
  647. throws XMLConfigurationException {
  648. //
  649. // Xerces Properties
  650. //
  651. if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
  652. final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length();
  653. if (suffixLength == Constants.DTD_SCANNER_PROPERTY.length() &&
  654. propertyId.endsWith(Constants.DTD_SCANNER_PROPERTY)) {
  655. return;
  656. }
  657. }
  658. if (propertyId.startsWith(Constants.JAXP_PROPERTY_PREFIX)) {
  659. final int suffixLength = propertyId.length() - Constants.JAXP_PROPERTY_PREFIX.length();
  660. if (suffixLength == Constants.SCHEMA_SOURCE.length() &&
  661. propertyId.endsWith(Constants.SCHEMA_SOURCE)) {
  662. return;
  663. }
  664. }
  665. //
  666. // Not recognized
  667. //
  668. super.checkProperty(propertyId);
  669. } // checkProperty(String)
  670. // factory methods
  671. /** Creates an entity manager. */
  672. protected XMLEntityManager createEntityManager() {
  673. return new XMLEntityManager();
  674. } // createEntityManager():XMLEntityManager
  675. /** Creates an error reporter. */
  676. protected XMLErrorReporter createErrorReporter() {
  677. return new XMLErrorReporter();
  678. } // createErrorReporter():XMLErrorReporter
  679. /** Create a document scanner. */
  680. protected XMLDocumentScanner createDocumentScanner() {
  681. return null;
  682. } // createDocumentScanner():XMLDocumentScanner
  683. /** Create a DTD scanner. */
  684. protected XMLDTDScanner createDTDScanner() {
  685. return new XMLDTDScannerImpl();
  686. } // createDTDScanner():XMLDTDScanner
  687. /** Create a datatype validator factory. */
  688. protected DTDDVFactory createDatatypeValidatorFactory() {
  689. return DTDDVFactory.getInstance();
  690. } // createDatatypeValidatorFactory():DatatypeValidatorFactory
  691. protected ValidationManager createValidationManager(){
  692. return new ValidationManager();
  693. }
  694. } // class NonValidatingConfiguration