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.dom;
  58. import java.io.IOException;
  59. import java.util.ArrayList;
  60. import java.util.HashMap;
  61. import java.util.Locale;
  62. import java.util.Vector;
  63. import org.w3c.dom.DOMConfiguration;
  64. import org.w3c.dom.DOMErrorHandler;
  65. import org.w3c.dom.DOMStringList;
  66. import com.sun.org.apache.xerces.internal.impl.Constants;
  67. import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
  68. import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
  69. import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
  70. import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;
  71. import com.sun.org.apache.xerces.internal.util.DOMEntityResolverWrapper;
  72. import com.sun.org.apache.xerces.internal.util.DOMErrorHandlerWrapper;
  73. import com.sun.org.apache.xerces.internal.util.MessageFormatter;
  74. import com.sun.org.apache.xerces.internal.util.ParserConfigurationSettings;
  75. import com.sun.org.apache.xerces.internal.util.SymbolTable;
  76. import com.sun.org.apache.xerces.internal.xni.XMLDTDContentModelHandler;
  77. import com.sun.org.apache.xerces.internal.xni.XMLDTDHandler;
  78. import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler;
  79. import com.sun.org.apache.xerces.internal.xni.XNIException;
  80. import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
  81. import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
  82. import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
  83. import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
  84. import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver;
  85. import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler;
  86. import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
  87. import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration;
  88. import org.w3c.dom.DOMException;
  89. import org.w3c.dom.ls.LSResourceResolver;
  90. import com.sun.org.apache.xerces.internal.impl.dv.DTDDVFactory;
  91. /**
  92. * Xerces implementation of DOMConfiguration that maintains a table of recognized parameters.
  93. *
  94. * @author Elena Litani, IBM
  95. * @author Neeraj Bajaj, Sun Microsystems.
  96. * @version $Id: DOMConfigurationImpl.java,v 1.28 2004/04/14 15:54:06 mrglavas Exp $
  97. */
  98. public class DOMConfigurationImpl extends ParserConfigurationSettings
  99. implements XMLParserConfiguration, DOMConfiguration {
  100. //
  101. // Constants
  102. //
  103. // feature identifiers
  104. /** Feature identifier: validation. */
  105. protected static final String XERCES_VALIDATION =
  106. Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE;
  107. /** Feature identifier: namespaces. */
  108. protected static final String XERCES_NAMESPACES =
  109. Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE;
  110. protected static final String SCHEMA =
  111. Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE;
  112. protected static final String DYNAMIC_VALIDATION =
  113. Constants.XERCES_FEATURE_PREFIX + Constants.DYNAMIC_VALIDATION_FEATURE;
  114. protected static final String NORMALIZE_DATA =
  115. Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE;
  116. protected static final String DATATYPE_VALIDATOR_FACTORY =
  117. Constants.XERCES_PROPERTY_PREFIX + Constants.DATATYPE_VALIDATOR_FACTORY_PROPERTY;
  118. /** sending psvi in the pipeline */
  119. protected static final String SEND_PSVI =
  120. Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_AUGMENT_PSVI;
  121. // property identifiers
  122. /** Property identifier: entity manager. */
  123. protected static final String ENTITY_MANAGER =
  124. Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
  125. /** Property identifier: error reporter. */
  126. protected static final String ERROR_REPORTER =
  127. Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
  128. /** Property identifier: xml string. */
  129. protected static final String XML_STRING =
  130. Constants.SAX_PROPERTY_PREFIX + Constants.XML_STRING_PROPERTY;
  131. /** Property identifier: symbol table. */
  132. protected static final String SYMBOL_TABLE =
  133. Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
  134. /** Property id: Grammar pool*/
  135. protected static final String GRAMMAR_POOL =
  136. Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
  137. /** Property identifier: error handler. */
  138. protected static final String ERROR_HANDLER =
  139. Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY;
  140. /** Property identifier: entity resolver. */
  141. protected static final String ENTITY_RESOLVER =
  142. Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY;
  143. /** Property identifier: JAXP schema language / DOM schema-type. */
  144. protected static final String JAXP_SCHEMA_LANGUAGE =
  145. Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE;
  146. /** Property identifier: JAXP schema source/ DOM schema-location. */
  147. protected static final String JAXP_SCHEMA_SOURCE =
  148. Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE;
  149. protected static final String VALIDATION_MANAGER =
  150. Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY;
  151. //
  152. // Data
  153. //
  154. XMLDocumentHandler fDocumentHandler;
  155. /** Normalization features*/
  156. protected short features = 0;
  157. protected final static short NAMESPACES = 0x1<<0;
  158. protected final static short DTNORMALIZATION = 0x1<<1;
  159. protected final static short ENTITIES = 0x1<<2;
  160. protected final static short CDATA = 0x1<<3;
  161. protected final static short SPLITCDATA = 0x1<<4;
  162. protected final static short COMMENTS = 0x1<<5;
  163. protected final static short VALIDATE = 0x1<<6;
  164. protected final static short PSVI = 0x1<<7;
  165. protected final static short WELLFORMED = 0x1<<8;
  166. protected final static short INFOSET_TRUE_PARAMS = NAMESPACES | COMMENTS | WELLFORMED;
  167. protected final static short INFOSET_FALSE_PARAMS = ENTITIES | DTNORMALIZATION | CDATA;
  168. protected final static short INFOSET_MASK = INFOSET_TRUE_PARAMS | INFOSET_FALSE_PARAMS;
  169. // components
  170. /** Symbol table. */
  171. protected SymbolTable fSymbolTable;
  172. protected DTDDVFactory fDatatypeValidatorFactory;
  173. /** Components. */
  174. protected ArrayList fComponents;
  175. protected ValidationManager fValidationManager;
  176. /** Locale. */
  177. protected Locale fLocale;
  178. /** Error reporter */
  179. protected XMLErrorReporter fErrorReporter;
  180. protected final DOMErrorHandlerWrapper fErrorHandlerWrapper =
  181. new DOMErrorHandlerWrapper();
  182. // private data
  183. private DOMStringList fRecognizedParameters;
  184. //
  185. // Constructors
  186. //
  187. /** Default Constructor. */
  188. protected DOMConfigurationImpl() {
  189. this(null, null);
  190. } // <init>()
  191. /**
  192. * Constructs a parser configuration using the specified symbol table.
  193. *
  194. * @param symbolTable The symbol table to use.
  195. */
  196. protected DOMConfigurationImpl(SymbolTable symbolTable) {
  197. this(symbolTable, null);
  198. } // <init>(SymbolTable)
  199. /**
  200. * Constructs a parser configuration using the specified symbol table
  201. * and parent settings.
  202. *
  203. * @param symbolTable The symbol table to use.
  204. * @param parentSettings The parent settings.
  205. */
  206. protected DOMConfigurationImpl(SymbolTable symbolTable,
  207. XMLComponentManager parentSettings) {
  208. super(parentSettings);
  209. // create storage for recognized features and properties
  210. fRecognizedFeatures = new ArrayList();
  211. fRecognizedProperties = new ArrayList();
  212. // create table for features and properties
  213. fFeatures = new HashMap();
  214. fProperties = new HashMap();
  215. // add default recognized features
  216. final String[] recognizedFeatures = {
  217. XERCES_VALIDATION,
  218. XERCES_NAMESPACES,
  219. SCHEMA,
  220. DYNAMIC_VALIDATION,
  221. NORMALIZE_DATA,
  222. SEND_PSVI,
  223. };
  224. addRecognizedFeatures(recognizedFeatures);
  225. // set state for default features
  226. setFeature(XERCES_VALIDATION, false);
  227. setFeature(SCHEMA, false);
  228. setFeature(DYNAMIC_VALIDATION, false);
  229. setFeature(NORMALIZE_DATA, false);
  230. setFeature(XERCES_NAMESPACES, true);
  231. setFeature(SEND_PSVI, true);
  232. // add default recognized properties
  233. final String[] recognizedProperties = {
  234. XML_STRING,
  235. SYMBOL_TABLE,
  236. ERROR_HANDLER,
  237. ENTITY_RESOLVER,
  238. ERROR_REPORTER,
  239. ENTITY_MANAGER,
  240. VALIDATION_MANAGER,
  241. DATATYPE_VALIDATOR_FACTORY,
  242. GRAMMAR_POOL,
  243. JAXP_SCHEMA_SOURCE,
  244. JAXP_SCHEMA_LANGUAGE
  245. };
  246. addRecognizedProperties(recognizedProperties);
  247. // set default values for normalization features
  248. features |= NAMESPACES;
  249. features |= ENTITIES;
  250. features |= COMMENTS;
  251. features |= CDATA;
  252. features |= SPLITCDATA;
  253. features |= WELLFORMED;
  254. if (symbolTable == null) {
  255. symbolTable = new SymbolTable();
  256. }
  257. fSymbolTable = symbolTable;
  258. fComponents = new ArrayList();
  259. setProperty(SYMBOL_TABLE, fSymbolTable);
  260. fErrorReporter = new XMLErrorReporter();
  261. setProperty(ERROR_REPORTER, fErrorReporter);
  262. addComponent(fErrorReporter);
  263. XMLEntityManager manager = new XMLEntityManager();
  264. setProperty(ENTITY_MANAGER, manager);
  265. addComponent(manager);
  266. fValidationManager = createValidationManager();
  267. setProperty(VALIDATION_MANAGER, fValidationManager);
  268. fDatatypeValidatorFactory = createDatatypeValidatorFactory();
  269. if (fDatatypeValidatorFactory != null) {
  270. setProperty(DATATYPE_VALIDATOR_FACTORY,
  271. fDatatypeValidatorFactory);
  272. }
  273. // add message formatters
  274. if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) {
  275. XMLMessageFormatter xmft = new XMLMessageFormatter();
  276. fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft);
  277. fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft);
  278. }
  279. // REVISIT: try to include XML Schema formatter.
  280. // This is a hack to allow DTD configuration to be build.
  281. //
  282. if (fErrorReporter.getMessageFormatter("http://www.w3.org/TR/xml-schema-1") == null) {
  283. MessageFormatter xmft = null;
  284. try {
  285. xmft = (MessageFormatter)(
  286. ObjectFactory.newInstance("com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter",
  287. ObjectFactory.findClassLoader(), true));
  288. } catch (Exception exception){
  289. }
  290. if (xmft != null) {
  291. fErrorReporter.putMessageFormatter("http://www.w3.org/TR/xml-schema-1", xmft);
  292. }
  293. }
  294. // set locale
  295. try {
  296. setLocale(Locale.getDefault());
  297. }
  298. catch (XNIException e) {
  299. // do nothing
  300. // REVISIT: What is the right thing to do? -Ac
  301. }
  302. } // <init>(SymbolTable)
  303. //
  304. // XMLParserConfiguration methods
  305. //
  306. /**
  307. * Parse an XML document.
  308. * <p>
  309. * The parser can use this method to instruct this configuration
  310. * to begin parsing an XML document from any valid input source
  311. * (a character stream, a byte stream, or a URI).
  312. * <p>
  313. * Parsers may not invoke this method while a parse is in progress.
  314. * Once a parse is complete, the parser may then parse another XML
  315. * document.
  316. * <p>
  317. * This method is synchronous: it will not return until parsing
  318. * has ended. If a client application wants to terminate
  319. * parsing early, it should throw an exception.
  320. *
  321. * @param source The input source for the top-level of the
  322. * XML document.
  323. *
  324. * @exception XNIException Any XNI exception, possibly wrapping
  325. * another exception.
  326. * @exception IOException An IO exception from the parser, possibly
  327. * from a byte stream or character stream
  328. * supplied by the parser.
  329. */
  330. public void parse(XMLInputSource inputSource)
  331. throws XNIException, IOException{
  332. // no-op
  333. }
  334. /**
  335. * Sets the document handler on the last component in the pipeline
  336. * to receive information about the document.
  337. *
  338. * @param documentHandler The document handler.
  339. */
  340. public void setDocumentHandler(XMLDocumentHandler documentHandler) {
  341. fDocumentHandler = documentHandler;
  342. } // setDocumentHandler(XMLDocumentHandler)
  343. /** Returns the registered document handler. */
  344. public XMLDocumentHandler getDocumentHandler() {
  345. return fDocumentHandler;
  346. } // getDocumentHandler():XMLDocumentHandler
  347. /**
  348. * Sets the DTD handler.
  349. *
  350. * @param dtdHandler The DTD handler.
  351. */
  352. public void setDTDHandler(XMLDTDHandler dtdHandler) {
  353. //no-op
  354. } // setDTDHandler(XMLDTDHandler)
  355. /** Returns the registered DTD handler. */
  356. public XMLDTDHandler getDTDHandler() {
  357. return null;
  358. } // getDTDHandler():XMLDTDHandler
  359. /**
  360. * Sets the DTD content model handler.
  361. *
  362. * @param handler The DTD content model handler.
  363. */
  364. public void setDTDContentModelHandler(XMLDTDContentModelHandler handler) {
  365. //no-op
  366. } // setDTDContentModelHandler(XMLDTDContentModelHandler)
  367. /** Returns the registered DTD content model handler. */
  368. public XMLDTDContentModelHandler getDTDContentModelHandler() {
  369. return null;
  370. } // getDTDContentModelHandler():XMLDTDContentModelHandler
  371. /**
  372. * Sets the resolver used to resolve external entities. The EntityResolver
  373. * interface supports resolution of public and system identifiers.
  374. *
  375. * @param resolver The new entity resolver. Passing a null value will
  376. * uninstall the currently installed resolver.
  377. */
  378. public void setEntityResolver(XMLEntityResolver resolver) {
  379. if (resolver !=null) {
  380. fProperties.put(ENTITY_RESOLVER, resolver);
  381. }
  382. } // setEntityResolver(XMLEntityResolver)
  383. /**
  384. * Return the current entity resolver.
  385. *
  386. * @return The current entity resolver, or null if none
  387. * has been registered.
  388. * @see #setEntityResolver
  389. */
  390. public XMLEntityResolver getEntityResolver() {
  391. return (XMLEntityResolver)fProperties.get(ENTITY_RESOLVER);
  392. } // getEntityResolver():XMLEntityResolver
  393. /**
  394. * Allow an application to register an error event handler.
  395. *
  396. * <p>If the application does not register an error handler, all
  397. * error events reported by the SAX parser will be silently
  398. * ignored; however, normal processing may not continue. It is
  399. * highly recommended that all SAX applications implement an
  400. * error handler to avoid unexpected bugs.</p>
  401. *
  402. * <p>Applications may register a new or different handler in the
  403. * middle of a parse, and the SAX parser must begin using the new
  404. * handler immediately.</p>
  405. *
  406. * @param errorHandler The error handler.
  407. * @exception java.lang.NullPointerException If the handler
  408. * argument is null.
  409. * @see #getErrorHandler
  410. */
  411. public void setErrorHandler(XMLErrorHandler errorHandler) {
  412. if (errorHandler != null) {
  413. fProperties.put(ERROR_HANDLER, errorHandler);
  414. }
  415. } // setErrorHandler(XMLErrorHandler)
  416. /**
  417. * Return the current error handler.
  418. *
  419. * @return The current error handler, or null if none
  420. * has been registered.
  421. * @see #setErrorHandler
  422. */
  423. public XMLErrorHandler getErrorHandler() {
  424. return (XMLErrorHandler)fProperties.get(ERROR_HANDLER);
  425. } // getErrorHandler():XMLErrorHandler
  426. /**
  427. * Set the state of a feature.
  428. *
  429. * Set the state of any feature in a SAX2 parser. The parser
  430. * might not recognize the feature, and if it does recognize
  431. * it, it might not be able to fulfill the request.
  432. *
  433. * @param featureId The unique identifier (URI) of the feature.
  434. * @param state The requested state of the feature (true or false).
  435. *
  436. * @exception com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException If the
  437. * requested feature is not known.
  438. */
  439. public void setFeature(String featureId, boolean state)
  440. throws XMLConfigurationException {
  441. // save state if noone "objects"
  442. super.setFeature(featureId, state);
  443. } // setFeature(String,boolean)
  444. /**
  445. * setProperty
  446. *
  447. * @param propertyId
  448. * @param value
  449. */
  450. public void setProperty(String propertyId, Object value)
  451. throws XMLConfigurationException {
  452. // store value if noone "objects"
  453. super.setProperty(propertyId, value);
  454. } // setProperty(String,Object)
  455. /**
  456. * Set the locale to use for messages.
  457. *
  458. * @param locale The locale object to use for localization of messages.
  459. *
  460. * @exception XNIException Thrown if the parser does not support the
  461. * specified locale.
  462. */
  463. public void setLocale(Locale locale) throws XNIException {
  464. fLocale = locale;
  465. fErrorReporter.setLocale(locale);
  466. } // setLocale(Locale)
  467. /** Returns the locale. */
  468. public Locale getLocale() {
  469. return fLocale;
  470. } // getLocale():Locale
  471. /**
  472. * DOM Level 3 WD - Experimental.
  473. * setParameter
  474. */
  475. public void setParameter(String name, Object value) throws DOMException {
  476. // REVISIT: Recognizes DOM L3 default features only.
  477. // Does not yet recognize Xerces features.
  478. if(value instanceof Boolean){
  479. boolean state = ((Boolean)value).booleanValue();
  480. if (name.equalsIgnoreCase(Constants.DOM_COMMENTS)) {
  481. features = (short) (state ? features | COMMENTS : features & ~COMMENTS);
  482. }
  483. else if (name.equalsIgnoreCase(Constants.DOM_DATATYPE_NORMALIZATION)) {
  484. setFeature(NORMALIZE_DATA, state);
  485. features =
  486. (short) (state ? features | DTNORMALIZATION : features & ~DTNORMALIZATION);
  487. if (state) {
  488. features = (short) (features | VALIDATE);
  489. }
  490. }
  491. else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACES)) {
  492. features = (short) (state ? features | NAMESPACES : features & ~NAMESPACES);
  493. }
  494. else if (name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)) {
  495. features = (short) (state ? features | CDATA : features & ~CDATA);
  496. }
  497. else if (name.equalsIgnoreCase(Constants.DOM_ENTITIES)) {
  498. features = (short) (state ? features | ENTITIES : features & ~ENTITIES);
  499. }
  500. else if (name.equalsIgnoreCase(Constants.DOM_SPLIT_CDATA)) {
  501. features = (short) (state ? features | SPLITCDATA : features & ~SPLITCDATA);
  502. }
  503. else if (name.equalsIgnoreCase(Constants.DOM_VALIDATE)) {
  504. features = (short) (state ? features | VALIDATE : features & ~VALIDATE);
  505. }
  506. else if (name.equalsIgnoreCase(Constants.DOM_WELLFORMED)) {
  507. features = (short) (state ? features | WELLFORMED : features & ~WELLFORMED );
  508. }
  509. else if (name.equalsIgnoreCase(Constants.DOM_INFOSET)) {
  510. // Setting to false has no effect.
  511. if (state) {
  512. features = (short) (features | INFOSET_TRUE_PARAMS);
  513. features = (short) (features & ~INFOSET_FALSE_PARAMS);
  514. setFeature(NORMALIZE_DATA, false);
  515. }
  516. }
  517. else if (name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS)
  518. || name.equalsIgnoreCase(Constants.DOM_CANONICAL_FORM)
  519. || name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA)
  520. || name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION)
  521. ) {
  522. if (state) { // true is not supported
  523. String msg =
  524. DOMMessageFormatter.formatMessage(
  525. DOMMessageFormatter.DOM_DOMAIN,
  526. "FEATURE_NOT_SUPPORTED",
  527. new Object[] { name });
  528. throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
  529. }
  530. }
  531. else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS)
  532. || name.equalsIgnoreCase(Constants.DOM_ELEMENT_CONTENT_WHITESPACE)) {
  533. if (!state) { // false is not supported
  534. String msg =
  535. DOMMessageFormatter.formatMessage(
  536. DOMMessageFormatter.DOM_DOMAIN,
  537. "FEATURE_NOT_SUPPORTED",
  538. new Object[] { name });
  539. throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
  540. }
  541. }
  542. else if (name.equalsIgnoreCase(SEND_PSVI) ){
  543. // REVISIT: turning augmentation of PSVI is not support,
  544. // because in this case we won't be able to retrieve element
  545. // default value.
  546. if (!state) { // false is not supported
  547. String msg =
  548. DOMMessageFormatter.formatMessage(
  549. DOMMessageFormatter.DOM_DOMAIN,
  550. "FEATURE_NOT_SUPPORTED",
  551. new Object[] { name });
  552. throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
  553. }
  554. }
  555. else if (name.equalsIgnoreCase(Constants.DOM_PSVI)){
  556. features = (short) (state ? features | PSVI : features & ~PSVI);
  557. }
  558. else {
  559. String msg =
  560. DOMMessageFormatter.formatMessage(
  561. DOMMessageFormatter.DOM_DOMAIN,
  562. "FEATURE_NOT_FOUND",
  563. new Object[] { name });
  564. throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
  565. }
  566. }
  567. else { // set properties
  568. if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER)) {
  569. if (value instanceof DOMErrorHandler || value == null) {
  570. fErrorHandlerWrapper.setErrorHandler((DOMErrorHandler)value);
  571. setErrorHandler(fErrorHandlerWrapper);
  572. }
  573. else {
  574. // REVISIT: type mismatch
  575. String msg =
  576. DOMMessageFormatter.formatMessage(
  577. DOMMessageFormatter.DOM_DOMAIN,
  578. "TYPE_MISMATCH_ERR",
  579. new Object[] { name });
  580. throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
  581. }
  582. }
  583. else if (name.equalsIgnoreCase(Constants.DOM_RESOURCE_RESOLVER)) {
  584. if (value instanceof LSResourceResolver || value == null) {
  585. try {
  586. setEntityResolver(new DOMEntityResolverWrapper((LSResourceResolver) value));
  587. }
  588. catch (XMLConfigurationException e) {}
  589. }
  590. else {
  591. // REVISIT: type mismatch
  592. String msg =
  593. DOMMessageFormatter.formatMessage(
  594. DOMMessageFormatter.DOM_DOMAIN,
  595. "TYPE_MISMATCH_ERR",
  596. new Object[] { name });
  597. throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
  598. }
  599. }
  600. else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_LOCATION)) {
  601. if (value instanceof String || value == null) {
  602. try {
  603. String schemaType = (String) getProperty(
  604. Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE);
  605. if (schemaType == Constants.NS_XMLSCHEMA || value == null) {
  606. // map DOM schema-location to JAXP schemaSource property
  607. setProperty(
  608. Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE,
  609. value);
  610. }
  611. else {
  612. // schemaType must not be null.
  613. // REVISIT: allow pre-parsing DTD grammars
  614. String msg =
  615. DOMMessageFormatter.formatMessage(
  616. DOMMessageFormatter.DOM_DOMAIN,
  617. "FEATURE_NOT_SUPPORTED",
  618. new Object[] { name });
  619. throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
  620. }
  621. }
  622. catch (XMLConfigurationException e) {}
  623. }
  624. else {
  625. // REVISIT: type mismatch
  626. String msg =
  627. DOMMessageFormatter.formatMessage(
  628. DOMMessageFormatter.DOM_DOMAIN,
  629. "TYPE_MISMATCH_ERR",
  630. new Object[] { name });
  631. throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
  632. }
  633. }
  634. else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_TYPE)) {
  635. if (value instanceof String || value == null) {
  636. try {
  637. if (value == null) {
  638. setProperty(
  639. Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE,
  640. null);
  641. }
  642. else if (value.equals(Constants.NS_XMLSCHEMA)) {
  643. // REVISIT: when add support to DTD validation
  644. setProperty(
  645. Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE,
  646. Constants.NS_XMLSCHEMA);
  647. }
  648. else if (value.equals(Constants.NS_DTD)) {
  649. // REVISIT: revalidation against DTDs is not supported
  650. String msg = DOMMessageFormatter.formatMessage(
  651. DOMMessageFormatter.DOM_DOMAIN,
  652. "FEATURE_NOT_SUPPORTED",
  653. new Object[] { name });
  654. throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
  655. }
  656. }
  657. catch (XMLConfigurationException e) {}
  658. }
  659. else {
  660. String msg =
  661. DOMMessageFormatter.formatMessage(
  662. DOMMessageFormatter.DOM_DOMAIN,
  663. "TYPE_MISMATCH_ERR",
  664. new Object[] { name });
  665. throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
  666. }
  667. }
  668. else if (name.equalsIgnoreCase(SYMBOL_TABLE)){
  669. // Xerces Symbol Table
  670. if (value instanceof SymbolTable){
  671. setProperty(SYMBOL_TABLE, value);
  672. }
  673. else {
  674. // REVISIT: type mismatch
  675. String msg =
  676. DOMMessageFormatter.formatMessage(
  677. DOMMessageFormatter.DOM_DOMAIN,
  678. "TYPE_MISMATCH_ERR",
  679. new Object[] { name });
  680. throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
  681. }
  682. }
  683. else if (name.equalsIgnoreCase (GRAMMAR_POOL)){
  684. if (value instanceof XMLGrammarPool){
  685. setProperty(GRAMMAR_POOL, value);
  686. }
  687. else {
  688. // REVISIT: type mismatch
  689. String msg =
  690. DOMMessageFormatter.formatMessage(
  691. DOMMessageFormatter.DOM_DOMAIN,
  692. "TYPE_MISMATCH_ERR",
  693. new Object[] { name });
  694. throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
  695. }
  696. }
  697. else {
  698. // REVISIT: check if this is a boolean parameter -- type mismatch should be thrown.
  699. //parameter is not recognized
  700. String msg =
  701. DOMMessageFormatter.formatMessage(
  702. DOMMessageFormatter.DOM_DOMAIN,
  703. "FEATURE_NOT_FOUND",
  704. new Object[] { name });
  705. throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
  706. }
  707. }
  708. }
  709. /**
  710. * DOM Level 3 WD - Experimental.
  711. * getParameter
  712. */
  713. public Object getParameter(String name) throws DOMException {
  714. // REVISIT: Recognizes DOM L3 default features only.
  715. // Does not yet recognize Xerces features.
  716. if (name.equalsIgnoreCase(Constants.DOM_COMMENTS)) {
  717. return ((features & COMMENTS) != 0) ? Boolean.TRUE : Boolean.FALSE;
  718. }
  719. else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACES)) {
  720. return (features & NAMESPACES) != 0 ? Boolean.TRUE : Boolean.FALSE;
  721. }
  722. else if (name.equalsIgnoreCase(Constants.DOM_DATATYPE_NORMALIZATION)) {
  723. // REVISIT: datatype-normalization only takes effect if validation is on
  724. return (features & DTNORMALIZATION) != 0 ? Boolean.TRUE : Boolean.FALSE;
  725. }
  726. else if (name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)) {
  727. return (features & CDATA) != 0 ? Boolean.TRUE : Boolean.FALSE;
  728. }
  729. else if (name.equalsIgnoreCase(Constants.DOM_ENTITIES)) {
  730. return (features & ENTITIES) != 0 ? Boolean.TRUE : Boolean.FALSE;
  731. }
  732. else if (name.equalsIgnoreCase(Constants.DOM_SPLIT_CDATA)) {
  733. return (features & SPLITCDATA) != 0 ? Boolean.TRUE : Boolean.FALSE;
  734. }
  735. else if (name.equalsIgnoreCase(Constants.DOM_VALIDATE)) {
  736. return (features & VALIDATE) != 0 ? Boolean.TRUE : Boolean.FALSE;
  737. }
  738. else if (name.equalsIgnoreCase(Constants.DOM_WELLFORMED)) {
  739. return (features & WELLFORMED) != 0 ? Boolean.TRUE : Boolean.FALSE;
  740. }
  741. else if (name.equalsIgnoreCase(Constants.DOM_INFOSET)) {
  742. return (features & INFOSET_MASK) == INFOSET_TRUE_PARAMS ? Boolean.TRUE : Boolean.FALSE;
  743. }
  744. else if (name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS)
  745. || name.equalsIgnoreCase(Constants.DOM_CANONICAL_FORM)
  746. || name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA)
  747. || name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION)
  748. ) {
  749. return Boolean.FALSE;
  750. }
  751. else if (name.equalsIgnoreCase(SEND_PSVI)) {
  752. return Boolean.TRUE;
  753. }
  754. else if (name.equalsIgnoreCase(Constants.DOM_PSVI)) {
  755. return (features & PSVI) != 0 ? Boolean.TRUE : Boolean.FALSE;
  756. }
  757. else if (
  758. name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS)
  759. || name.equalsIgnoreCase(Constants.DOM_ELEMENT_CONTENT_WHITESPACE)) {
  760. return Boolean.TRUE;
  761. }
  762. else if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER)) {
  763. return fErrorHandlerWrapper.getErrorHandler();
  764. }
  765. else if (name.equalsIgnoreCase(Constants.DOM_RESOURCE_RESOLVER)) {
  766. XMLEntityResolver entityResolver = getEntityResolver();
  767. if (entityResolver != null && entityResolver instanceof DOMEntityResolverWrapper) {
  768. return ((DOMEntityResolverWrapper) entityResolver).getEntityResolver();
  769. }
  770. return null;
  771. }
  772. else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_TYPE)) {
  773. return getProperty(Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE);
  774. }
  775. else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_LOCATION)) {
  776. return getProperty(Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE);
  777. }
  778. else if (name.equalsIgnoreCase(SYMBOL_TABLE)){
  779. return getProperty(SYMBOL_TABLE);
  780. }
  781. else if (name.equalsIgnoreCase(GRAMMAR_POOL)){
  782. return getProperty(GRAMMAR_POOL);
  783. }
  784. else {
  785. String msg =
  786. DOMMessageFormatter.formatMessage(
  787. DOMMessageFormatter.DOM_DOMAIN,
  788. "FEATURE_NOT_FOUND",
  789. new Object[] { name });
  790. throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
  791. }
  792. }
  793. /**
  794. * DOM Level 3 WD - Experimental.
  795. * Check if setting a parameter to a specific value is supported.
  796. *
  797. * @param name The name of the parameter to check.
  798. *
  799. * @param value An object. if null, the returned value is true.
  800. *
  801. * @return true if the parameter could be successfully set to the
  802. * specified value, or false if the parameter is not recognized or
  803. * the requested value is not supported. This does not change the
  804. * current value of the parameter itself.
  805. */
  806. public boolean canSetParameter(String name, Object value) {
  807. if (value == null){
  808. //if null, the returned value is true.
  809. //REVISIT: I dont like this --- even for unrecognized parameter it would
  810. //return 'true'. I think it should return false in that case.
  811. // Application will be surprised to find that setParameter throws not
  812. //recognized exception when canSetParameter returns 'true' Then what is the use
  813. //of having canSetParameter ??? - nb.
  814. return true ;
  815. }
  816. if( value instanceof Boolean ){
  817. //features whose parameter value can be set either 'true' or 'false'
  818. // or they accept any boolean value -- so we just need to check that
  819. // its a boolean value..
  820. if (name.equalsIgnoreCase(Constants.DOM_COMMENTS)
  821. || name.equalsIgnoreCase(Constants.DOM_DATATYPE_NORMALIZATION)
  822. || name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)
  823. || name.equalsIgnoreCase(Constants.DOM_ENTITIES)
  824. || name.equalsIgnoreCase(Constants.DOM_SPLIT_CDATA)
  825. || name.equalsIgnoreCase(Constants.DOM_NAMESPACES)
  826. || name.equalsIgnoreCase(Constants.DOM_VALIDATE)
  827. || name.equalsIgnoreCase(Constants.DOM_WELLFORMED)
  828. || name.equalsIgnoreCase(Constants.DOM_INFOSET)
  829. ) {
  830. return true ;
  831. }//features whose parameter value can not be set to 'true'
  832. else if (
  833. name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS)
  834. || name.equalsIgnoreCase(Constants.DOM_CANONICAL_FORM)
  835. || name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA)
  836. || name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION)
  837. ) {
  838. return (value.equals(Boolean.TRUE)) ? false : true;
  839. }//features whose parameter value can not be set to 'false'
  840. else if( name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS)
  841. || name.equalsIgnoreCase(Constants.DOM_ELEMENT_CONTENT_WHITESPACE)
  842. || name.equalsIgnoreCase(SEND_PSVI)
  843. ) {
  844. return (value.equals(Boolean.TRUE)) ? true : false;
  845. }// if name is not among the above listed above -- its not recognized. return false
  846. else {
  847. return false ;
  848. }
  849. }
  850. else if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER)) {
  851. return (value instanceof DOMErrorHandler) ? true : false ;
  852. }
  853. else if (name.equalsIgnoreCase(Constants.DOM_RESOURCE_RESOLVER)) {
  854. return (value instanceof LSResourceResolver) ? true : false ;
  855. }
  856. else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_LOCATION)) {
  857. return (value instanceof String) ? true : false ;
  858. }
  859. else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_TYPE)) {
  860. // REVISIT: should null value be supported?
  861. //as of now we are only supporting W3C XML Schema
  862. return ( (value instanceof String) && value.equals(Constants.NS_XMLSCHEMA) ) ? true : false ;
  863. }
  864. else if (name.equalsIgnoreCase(SYMBOL_TABLE)){
  865. // Xerces Symbol Table
  866. return (value instanceof SymbolTable) ? true : false ;
  867. }
  868. else if (name.equalsIgnoreCase (GRAMMAR_POOL)){
  869. return (value instanceof XMLGrammarPool) ? true : false ;
  870. }
  871. else {
  872. //false if the parameter is not recognized or the requested value is not supported.
  873. return false ;
  874. }
  875. } //canSetParameter
  876. /**
  877. * DOM Level 3 CR - Experimental.
  878. *
  879. * The list of the parameters supported by this
  880. * <code>DOMConfiguration</code> object and for which at least one value
  881. * can be set by the application. Note that this list can also contain
  882. * parameter names defined outside this specification.
  883. */
  884. public DOMStringList getParameterNames() {
  885. if (fRecognizedParameters == null){
  886. Vector parameters = new Vector();
  887. //Add DOM recognized parameters
  888. //REVISIT: Would have been nice to have a list of
  889. //recognized paramters.
  890. parameters.add(Constants.DOM_COMMENTS);
  891. parameters.add(Constants.DOM_DATATYPE_NORMALIZATION);
  892. parameters.add(Constants.DOM_CDATA_SECTIONS);
  893. parameters.add(Constants.DOM_ENTITIES);
  894. parameters.add(Constants.DOM_SPLIT_CDATA);
  895. parameters.add(Constants.DOM_NAMESPACES);
  896. parameters.add(Constants.DOM_VALIDATE);
  897. parameters.add(Constants.DOM_INFOSET);
  898. parameters.add(Constants.DOM_NORMALIZE_CHARACTERS);
  899. parameters.add(Constants.DOM_CANONICAL_FORM);
  900. parameters.add(Constants.DOM_VALIDATE_IF_SCHEMA);
  901. parameters.add(Constants.DOM_CHECK_CHAR_NORMALIZATION);
  902. parameters.add(Constants.DOM_WELLFORMED);
  903. parameters.add(Constants.DOM_NAMESPACE_DECLARATIONS);
  904. parameters.add(Constants.DOM_ELEMENT_CONTENT_WHITESPACE);
  905. parameters.add(Constants.DOM_ERROR_HANDLER);
  906. parameters.add(Constants.DOM_SCHEMA_TYPE);
  907. parameters.add(Constants.DOM_SCHEMA_LOCATION);
  908. parameters.add(Constants.DOM_RESOURCE_RESOLVER);
  909. parameters.add(GRAMMAR_POOL);
  910. parameters.add(SYMBOL_TABLE);
  911. parameters.add(SEND_PSVI);
  912. fRecognizedParameters = new DOMStringListImpl(parameters);
  913. }
  914. return fRecognizedParameters;
  915. }//getParameterNames
  916. //
  917. // Protected methods
  918. //
  919. /**
  920. * reset all components before parsing
  921. */
  922. protected void reset() throws XNIException {
  923. if (fValidationManager != null)
  924. fValidationManager.reset();
  925. int count = fComponents.size();
  926. for (int i = 0; i < count; i++) {
  927. XMLComponent c = (XMLComponent) fComponents.get(i);
  928. c.reset(this);
  929. }
  930. } // reset()
  931. /**
  932. * Check a property. If the property is known and supported, this method
  933. * simply returns. Otherwise, the appropriate exception is thrown.
  934. *
  935. * @param propertyId The unique identifier (URI) of the property
  936. * being set.
  937. * @exception com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException If the
  938. * requested feature is not known or supported.
  939. */
  940. protected void checkProperty(String propertyId)
  941. throws XMLConfigurationException {
  942. // special cases
  943. if (propertyId.startsWith(Constants.SAX_PROPERTY_PREFIX)) {
  944. final int suffixLength = propertyId.length() - Constants.SAX_PROPERTY_PREFIX.length();
  945. //
  946. // http://xml.org/sax/properties/xml-string
  947. // Value type: String
  948. // Access: read-only
  949. // Get the literal string of characters associated with the
  950. // current event. If the parser recognises and supports this
  951. // property but is not currently parsing text, it should return
  952. // null (this is a good way to check for availability before the
  953. // parse begins).
  954. //
  955. if (suffixLength == Constants.XML_STRING_PROPERTY.length() &&
  956. propertyId.endsWith(Constants.XML_STRING_PROPERTY)) {
  957. // REVISIT - we should probably ask xml-dev for a precise
  958. // definition of what this is actually supposed to return, and
  959. // in exactly which circumstances.
  960. short type = XMLConfigurationException.NOT_SUPPORTED;
  961. throw new XMLConfigurationException(type, propertyId);
  962. }
  963. }
  964. // check property
  965. super.checkProperty(propertyId);
  966. } // checkProperty(String)
  967. protected void addComponent(XMLComponent component) {
  968. // don't add a component more than once
  969. if (fComponents.contains(component)) {
  970. return;
  971. }
  972. fComponents.add(component);
  973. // register component's recognized features
  974. String[] recognizedFeatures = component.getRecognizedFeatures();
  975. addRecognizedFeatures(recognizedFeatures);
  976. // register component's recognized properties
  977. String[] recognizedProperties = component.getRecognizedProperties();
  978. addRecognizedProperties(recognizedProperties);
  979. } // addComponent(XMLComponent)
  980. protected ValidationManager createValidationManager(){
  981. return new ValidationManager();
  982. }
  983. protected DTDDVFactory createDatatypeValidatorFactory() {
  984. return DTDDVFactory.getInstance();
  985. } // createDatatypeValidatorFactory():DatatypeValidatorFactory
  986. } // class XMLParser