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 com.sun.org.apache.xerces.internal.impl.Constants;
  59. import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator;
  60. import com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter;
  61. import com.sun.org.apache.xerces.internal.util.SymbolTable;
  62. import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
  63. import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
  64. import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
  65. /**
  66. * This is the "standard" parser configuration. It extends the DTD
  67. * configuration with the standard set of parser components.
  68. * The standard set of parser components include those needed
  69. * to parse and validate with DTD's, and those needed for XML
  70. * Schema.</p>
  71. * <p>
  72. * In addition to the features and properties recognized by the base
  73. * parser configuration, this class recognizes these additional
  74. * features and properties:
  75. * <ul>
  76. * <li>Features
  77. * <ul>
  78. * <li>http://apache.org/xml/features/validation/schema</li>
  79. * <li>http://apache.org/xml/features/validation/schema-full-checking</li>
  80. * <li>http://apache.org/xml/features/validation/schema/normalized-value</li>
  81. * <li>http://apache.org/xml/features/validation/schema/element-default</li>
  82. * </ul>
  83. * <li>Properties
  84. * <ul>
  85. * <li>http://apache.org/xml/properties/internal/error-reporter</li>
  86. * <li>http://apache.org/xml/properties/internal/entity-manager</li>
  87. * <li>http://apache.org/xml/properties/internal/document-scanner</li>
  88. * <li>http://apache.org/xml/properties/internal/dtd-scanner</li>
  89. * <li>http://apache.org/xml/properties/internal/grammar-pool</li>
  90. * <li>http://apache.org/xml/properties/internal/validator/dtd</li>
  91. * <li>http://apache.org/xml/properties/internal/datatype-validator-factory</li>
  92. * </ul>
  93. * </ul>
  94. *
  95. * @author Arnaud Le Hors, IBM
  96. * @author Andy Clark, IBM
  97. *
  98. * @version $Id: StandardParserConfiguration.java,v 1.33 2004/01/26 17:28:10 mrglavas Exp $
  99. */
  100. public class StandardParserConfiguration
  101. extends DTDConfiguration {
  102. //
  103. // Constants
  104. //
  105. // feature identifiers
  106. /** Feature identifier: expose schema normalized value */
  107. protected static final String NORMALIZE_DATA =
  108. Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE;
  109. /** Feature identifier: send element default value via characters() */
  110. protected static final String SCHEMA_ELEMENT_DEFAULT =
  111. Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_ELEMENT_DEFAULT;
  112. /** Feature identifier: augment PSVI */
  113. protected static final String SCHEMA_AUGMENT_PSVI =
  114. Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_AUGMENT_PSVI;
  115. /** feature identifier: XML Schema validation */
  116. protected static final String XMLSCHEMA_VALIDATION =
  117. Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE;
  118. /** feature identifier: XML Schema validation -- full checking */
  119. protected static final String XMLSCHEMA_FULL_CHECKING =
  120. Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING;
  121. // property identifiers
  122. /** Property identifier: XML Schema validator. */
  123. protected static final String SCHEMA_VALIDATOR =
  124. Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY;
  125. /** Property identifier: schema location. */
  126. protected static final String SCHEMA_LOCATION =
  127. Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_LOCATION;
  128. /** Property identifier: no namespace schema location. */
  129. protected static final String SCHEMA_NONS_LOCATION =
  130. Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_NONS_LOCATION;
  131. //
  132. // Data
  133. //
  134. // components (non-configurable)
  135. /** XML Schema Validator. */
  136. protected XMLSchemaValidator fSchemaValidator;
  137. //
  138. // Constructors
  139. //
  140. /** Default constructor. */
  141. public StandardParserConfiguration() {
  142. this(null, null, null);
  143. } // <init>()
  144. /**
  145. * Constructs a parser configuration using the specified symbol table.
  146. *
  147. * @param symbolTable The symbol table to use.
  148. */
  149. public StandardParserConfiguration(SymbolTable symbolTable) {
  150. this(symbolTable, null, null);
  151. } // <init>(SymbolTable)
  152. /**
  153. * Constructs a parser configuration using the specified symbol table and
  154. * grammar pool.
  155. * <p>
  156. * <strong>REVISIT:</strong>
  157. * Grammar pool will be updated when the new validation engine is
  158. * implemented.
  159. *
  160. * @param symbolTable The symbol table to use.
  161. * @param grammarPool The grammar pool to use.
  162. */
  163. public StandardParserConfiguration(SymbolTable symbolTable,
  164. XMLGrammarPool grammarPool) {
  165. this(symbolTable, grammarPool, null);
  166. } // <init>(SymbolTable,XMLGrammarPool)
  167. /**
  168. * Constructs a parser configuration using the specified symbol table,
  169. * grammar pool, and parent settings.
  170. * <p>
  171. * <strong>REVISIT:</strong>
  172. * Grammar pool will be updated when the new validation engine is
  173. * implemented.
  174. *
  175. * @param symbolTable The symbol table to use.
  176. * @param grammarPool The grammar pool to use.
  177. * @param parentSettings The parent settings.
  178. */
  179. public StandardParserConfiguration(SymbolTable symbolTable,
  180. XMLGrammarPool grammarPool,
  181. XMLComponentManager parentSettings) {
  182. super(symbolTable, grammarPool, parentSettings);
  183. // add default recognized features
  184. final String[] recognizedFeatures = {
  185. NORMALIZE_DATA,
  186. SCHEMA_ELEMENT_DEFAULT,
  187. SCHEMA_AUGMENT_PSVI,
  188. // NOTE: These shouldn't really be here but since the XML Schema
  189. // validator is constructed dynamically, its recognized
  190. // features might not have been set and it would cause a
  191. // not-recognized exception to be thrown. -Ac
  192. XMLSCHEMA_VALIDATION,
  193. XMLSCHEMA_FULL_CHECKING,
  194. };
  195. addRecognizedFeatures(recognizedFeatures);
  196. // set state for default features
  197. setFeature(SCHEMA_ELEMENT_DEFAULT, true);
  198. setFeature(NORMALIZE_DATA, true);
  199. setFeature(SCHEMA_AUGMENT_PSVI, true);
  200. // add default recognized properties
  201. final String[] recognizedProperties = {
  202. // NOTE: These shouldn't really be here but since the XML Schema
  203. // validator is constructed dynamically, its recognized
  204. // properties might not have been set and it would cause a
  205. // not-recognized exception to be thrown. -Ac
  206. SCHEMA_LOCATION,
  207. SCHEMA_NONS_LOCATION,
  208. };
  209. addRecognizedProperties(recognizedProperties);
  210. } // <init>(SymbolTable,XMLGrammarPool)
  211. //
  212. // Public methods
  213. //
  214. /** Configures the pipeline. */
  215. protected void configurePipeline() {
  216. super.configurePipeline();
  217. if ( getFeature(XMLSCHEMA_VALIDATION )) {
  218. // If schema validator was not in the pipeline insert it.
  219. if (fSchemaValidator == null) {
  220. fSchemaValidator = new XMLSchemaValidator();
  221. // add schema component
  222. fProperties.put(SCHEMA_VALIDATOR, fSchemaValidator);
  223. addComponent(fSchemaValidator);
  224. // add schema message formatter
  225. if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) {
  226. XSMessageFormatter xmft = new XSMessageFormatter();
  227. fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, xmft);
  228. }
  229. }
  230. fLastComponent = fSchemaValidator;
  231. fNamespaceBinder.setDocumentHandler(fSchemaValidator);
  232. fSchemaValidator.setDocumentHandler(fDocumentHandler);
  233. fSchemaValidator.setDocumentSource(fNamespaceBinder);
  234. }
  235. } // configurePipeline()
  236. // features and properties
  237. /**
  238. * Check a feature. If feature is know and supported, this method simply
  239. * returns. Otherwise, the appropriate exception is thrown.
  240. *
  241. * @param featureId The unique identifier (URI) of the feature.
  242. *
  243. * @throws XMLConfigurationException Thrown for configuration error.
  244. * In general, components should
  245. * only throw this exception if
  246. * it is <strong>really</strong>
  247. * a critical error.
  248. */
  249. protected void checkFeature(String featureId)
  250. throws XMLConfigurationException {
  251. //
  252. // Xerces Features
  253. //
  254. if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) {
  255. final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length();
  256. //
  257. // http://apache.org/xml/features/validation/schema
  258. // Lets the user turn Schema validation support on/off.
  259. //
  260. if (suffixLength == Constants.SCHEMA_VALIDATION_FEATURE.length() &&
  261. featureId.endsWith(Constants.SCHEMA_VALIDATION_FEATURE)) {
  262. return;
  263. }
  264. // activate full schema checking
  265. if (suffixLength == Constants.SCHEMA_FULL_CHECKING.length() &&
  266. featureId.endsWith(Constants.SCHEMA_FULL_CHECKING)) {
  267. return;
  268. }
  269. // Feature identifier: expose schema normalized value
  270. // http://apache.org/xml/features/validation/schema/normalized-value
  271. if (suffixLength == Constants.SCHEMA_NORMALIZED_VALUE.length() &&
  272. featureId.endsWith(Constants.SCHEMA_NORMALIZED_VALUE)) {
  273. return;
  274. }
  275. // Feature identifier: send element default value via characters()
  276. // http://apache.org/xml/features/validation/schema/element-default
  277. if (suffixLength == Constants.SCHEMA_ELEMENT_DEFAULT.length() &&
  278. featureId.endsWith(Constants.SCHEMA_ELEMENT_DEFAULT)) {
  279. return;
  280. }
  281. }
  282. //
  283. // Not recognized
  284. //
  285. super.checkFeature(featureId);
  286. } // checkFeature(String)
  287. /**
  288. * Check a property. If the property is know and supported, this method
  289. * simply returns. Otherwise, the appropriate exception is thrown.
  290. *
  291. * @param propertyId The unique identifier (URI) of the property
  292. * being set.
  293. *
  294. * @throws XMLConfigurationException Thrown for configuration error.
  295. * In general, components should
  296. * only throw this exception if
  297. * it is <strong>really</strong>
  298. * a critical error.
  299. */
  300. protected void checkProperty(String propertyId)
  301. throws XMLConfigurationException {
  302. //
  303. // Xerces Properties
  304. //
  305. if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
  306. final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length();
  307. if (suffixLength == Constants.SCHEMA_LOCATION.length() &&
  308. propertyId.endsWith(Constants.SCHEMA_LOCATION)) {
  309. return;
  310. }
  311. if (suffixLength == Constants.SCHEMA_NONS_LOCATION.length() &&
  312. propertyId.endsWith(Constants.SCHEMA_NONS_LOCATION)) {
  313. return;
  314. }
  315. }
  316. if (propertyId.startsWith(Constants.JAXP_PROPERTY_PREFIX)) {
  317. final int suffixLength = propertyId.length() - Constants.JAXP_PROPERTY_PREFIX.length();
  318. if (suffixLength == Constants.SCHEMA_SOURCE.length() &&
  319. propertyId.endsWith(Constants.SCHEMA_SOURCE)) {
  320. return;
  321. }
  322. }
  323. //
  324. // Not recognized
  325. //
  326. super.checkProperty(propertyId);
  327. } // checkProperty(String)
  328. } // class StandardParserConfiguration