1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001, 2002 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.util;
  58. import java.util.ArrayList;
  59. import java.util.HashMap;
  60. import com.sun.org.apache.xerces.internal.impl.Constants;
  61. import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
  62. import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
  63. /**
  64. * This class implements the basic operations for managing parser
  65. * configuration features and properties. This utility class can
  66. * be used as a base class for parser configurations or separately
  67. * to encapsulate a number of parser settings as a component
  68. * manager.
  69. * <p>
  70. * This class can be constructed with a "parent" settings object
  71. * (in the form of an <code>XMLComponentManager</code>) that allows
  72. * parser configuration settings to be "chained" together.
  73. *
  74. * @author Andy Clark, IBM
  75. *
  76. * @version $Id: ParserConfigurationSettings.java,v 1.11 2004/04/25 05:05:50 mrglavas Exp $
  77. */
  78. public class ParserConfigurationSettings
  79. implements XMLComponentManager {
  80. protected static final String PARSER_SETTINGS =
  81. Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS;
  82. //
  83. // Data
  84. //
  85. // data
  86. /** Recognized properties. */
  87. protected ArrayList fRecognizedProperties;
  88. /** Properties. */
  89. protected HashMap fProperties;
  90. /** Recognized features. */
  91. protected ArrayList fRecognizedFeatures;
  92. /** Features. */
  93. protected HashMap fFeatures;
  94. /** Parent parser configuration settings. */
  95. protected XMLComponentManager fParentSettings;
  96. //
  97. // Constructors
  98. //
  99. /** Default Constructor. */
  100. public ParserConfigurationSettings() {
  101. this(null);
  102. } // <init>()
  103. /**
  104. * Constructs a parser configuration settings object with a
  105. * parent settings object.
  106. */
  107. public ParserConfigurationSettings(XMLComponentManager parent) {
  108. // create storage for recognized features and properties
  109. fRecognizedFeatures = new ArrayList();
  110. fRecognizedProperties = new ArrayList();
  111. // create table for features and properties
  112. fFeatures = new HashMap();
  113. fProperties = new HashMap();
  114. // save parent
  115. fParentSettings = parent;
  116. } // <init>(XMLComponentManager)
  117. //
  118. // XMLParserConfiguration methods
  119. //
  120. /**
  121. * Allows a parser to add parser specific features to be recognized
  122. * and managed by the parser configuration.
  123. *
  124. * @param featureIds An array of the additional feature identifiers
  125. * to be recognized.
  126. */
  127. public void addRecognizedFeatures(String[] featureIds) {
  128. // add recognized features
  129. int featureIdsCount = featureIds != null ? featureIds.length : 0;
  130. for (int i = 0; i < featureIdsCount; i++) {
  131. String featureId = featureIds[i];
  132. if (!fRecognizedFeatures.contains(featureId)) {
  133. fRecognizedFeatures.add(featureId);
  134. }
  135. }
  136. } // addRecognizedFeatures(String[])
  137. /**
  138. * Set the state of a feature.
  139. *
  140. * Set the state of any feature in a SAX2 parser. The parser
  141. * might not recognize the feature, and if it does recognize
  142. * it, it might not be able to fulfill the request.
  143. *
  144. * @param featureId The unique identifier (URI) of the feature.
  145. * @param state The requested state of the feature (true or false).
  146. *
  147. * @exception com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException If the
  148. * requested feature is not known.
  149. */
  150. public void setFeature(String featureId, boolean state)
  151. throws XMLConfigurationException {
  152. // check and store
  153. checkFeature(featureId);
  154. fFeatures.put(featureId, state ? Boolean.TRUE : Boolean.FALSE);
  155. } // setFeature(String,boolean)
  156. /**
  157. * Allows a parser to add parser specific properties to be recognized
  158. * and managed by the parser configuration.
  159. *
  160. * @param propertyIds An array of the additional property identifiers
  161. * to be recognized.
  162. */
  163. public void addRecognizedProperties(String[] propertyIds) {
  164. // add recognizedProperties
  165. int propertyIdsCount = propertyIds != null ? propertyIds.length : 0;
  166. for (int i = 0; i < propertyIdsCount; i++) {
  167. String propertyId = propertyIds[i];
  168. if (!fRecognizedProperties.contains(propertyId)) {
  169. fRecognizedProperties.add(propertyId);
  170. }
  171. }
  172. } // addRecognizedProperties(String[])
  173. /**
  174. * setProperty
  175. *
  176. * @param propertyId
  177. * @param value
  178. * @exception com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException If the
  179. * requested feature is not known.
  180. */
  181. public void setProperty(String propertyId, Object value)
  182. throws XMLConfigurationException {
  183. // check and store
  184. checkProperty(propertyId);
  185. if(value == null){
  186. fProperties.remove(propertyId);
  187. }else
  188. fProperties.put(propertyId, value);
  189. } // setProperty(String,Object)
  190. //
  191. // XMLComponentManager methods
  192. //
  193. /**
  194. * Returns the state of a feature.
  195. *
  196. * @param featureId The feature identifier.
  197. * @return true if the feature is supported
  198. *
  199. * @throws XMLConfigurationException Thrown for configuration error.
  200. * In general, components should
  201. * only throw this exception if
  202. * it is <strong>really</strong>
  203. * a critical error.
  204. */
  205. public boolean getFeature(String featureId)
  206. throws XMLConfigurationException {
  207. Boolean state = (Boolean) fFeatures.get(featureId);
  208. if (state == null) {
  209. checkFeature(featureId);
  210. return false;
  211. }
  212. return state.booleanValue();
  213. } // getFeature(String):boolean
  214. /**
  215. * Returns the value of a property.
  216. *
  217. * @param propertyId The property identifier.
  218. * @return the value of the property
  219. *
  220. * @throws XMLConfigurationException Thrown for configuration error.
  221. * In general, components should
  222. * only throw this exception if
  223. * it is <strong>really</strong>
  224. * a critical error.
  225. */
  226. public Object getProperty(String propertyId)
  227. throws XMLConfigurationException {
  228. Object propertyValue = fProperties.get(propertyId);
  229. if (propertyValue == null) {
  230. checkProperty(propertyId);
  231. }
  232. return propertyValue;
  233. } // getProperty(String):Object
  234. //
  235. // Protected methods
  236. //
  237. /**
  238. * Check a feature. If feature is known 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. * @exception com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException If the
  244. * requested feature is not known.
  245. */
  246. protected void checkFeature(String featureId)
  247. throws XMLConfigurationException {
  248. // check feature
  249. if (!fRecognizedFeatures.contains(featureId)) {
  250. if (fParentSettings != null) {
  251. fParentSettings.getFeature(featureId);
  252. }
  253. else {
  254. short type = XMLConfigurationException.NOT_RECOGNIZED;
  255. throw new XMLConfigurationException(type, featureId);
  256. }
  257. }
  258. } // checkFeature(String)
  259. /**
  260. * Check a property. If the property is known and supported, this method
  261. * simply returns. Otherwise, the appropriate exception is thrown.
  262. *
  263. * @param propertyId The unique identifier (URI) of the property
  264. * being set.
  265. * @exception com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException If the
  266. * requested feature is not known.
  267. */
  268. protected void checkProperty(String propertyId)
  269. throws XMLConfigurationException {
  270. // check property
  271. if (!fRecognizedProperties.contains(propertyId)) {
  272. if (fParentSettings != null) {
  273. fParentSettings.getProperty(propertyId);
  274. }
  275. else {
  276. short type = XMLConfigurationException.NOT_RECOGNIZED;
  277. throw new XMLConfigurationException(type, propertyId);
  278. }
  279. }
  280. } // checkProperty(String)
  281. } // class ParserConfigurationSettings