001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.util;
019:
020: import java.util.ArrayList;
021: import java.util.HashMap;
022:
023: import org.apache.xerces.impl.Constants;
024: import org.apache.xerces.xni.parser.XMLComponentManager;
025: import org.apache.xerces.xni.parser.XMLConfigurationException;
026:
027: /**
028: * This class implements the basic operations for managing parser
029: * configuration features and properties. This utility class can
030: * be used as a base class for parser configurations or separately
031: * to encapsulate a number of parser settings as a component
032: * manager.
033: * <p>
034: * This class can be constructed with a "parent" settings object
035: * (in the form of an <code>XMLComponentManager</code>) that allows
036: * parser configuration settings to be "chained" together.
037: *
038: * @author Andy Clark, IBM
039: *
040: * @version $Id: ParserConfigurationSettings.java 447241 2006-09-18 05:12:57Z mrglavas $
041: */
042: public class ParserConfigurationSettings implements XMLComponentManager {
043:
044: protected static final String PARSER_SETTINGS = Constants.XERCES_FEATURE_PREFIX
045: + Constants.PARSER_SETTINGS;
046:
047: //
048: // Data
049: //
050:
051: // data
052:
053: /** Recognized properties. */
054: protected ArrayList fRecognizedProperties;
055:
056: /** Properties. */
057: protected HashMap fProperties;
058:
059: /** Recognized features. */
060: protected ArrayList fRecognizedFeatures;
061:
062: /** Features. */
063: protected HashMap fFeatures;
064:
065: /** Parent parser configuration settings. */
066: protected XMLComponentManager fParentSettings;
067:
068: //
069: // Constructors
070: //
071:
072: /** Default Constructor. */
073: public ParserConfigurationSettings() {
074: this (null);
075: } // <init>()
076:
077: /**
078: * Constructs a parser configuration settings object with a
079: * parent settings object.
080: */
081: public ParserConfigurationSettings(XMLComponentManager parent) {
082:
083: // create storage for recognized features and properties
084: fRecognizedFeatures = new ArrayList();
085: fRecognizedProperties = new ArrayList();
086:
087: // create table for features and properties
088: fFeatures = new HashMap();
089: fProperties = new HashMap();
090:
091: // save parent
092: fParentSettings = parent;
093:
094: } // <init>(XMLComponentManager)
095:
096: //
097: // XMLParserConfiguration methods
098: //
099:
100: /**
101: * Allows a parser to add parser specific features to be recognized
102: * and managed by the parser configuration.
103: *
104: * @param featureIds An array of the additional feature identifiers
105: * to be recognized.
106: */
107: public void addRecognizedFeatures(String[] featureIds) {
108:
109: // add recognized features
110: int featureIdsCount = featureIds != null ? featureIds.length
111: : 0;
112: for (int i = 0; i < featureIdsCount; i++) {
113: String featureId = featureIds[i];
114: if (!fRecognizedFeatures.contains(featureId)) {
115: fRecognizedFeatures.add(featureId);
116: }
117: }
118:
119: } // addRecognizedFeatures(String[])
120:
121: /**
122: * Set the state of a feature.
123: *
124: * Set the state of any feature in a SAX2 parser. The parser
125: * might not recognize the feature, and if it does recognize
126: * it, it might not be able to fulfill the request.
127: *
128: * @param featureId The unique identifier (URI) of the feature.
129: * @param state The requested state of the feature (true or false).
130: *
131: * @exception org.apache.xerces.xni.parser.XMLConfigurationException If the
132: * requested feature is not known.
133: */
134: public void setFeature(String featureId, boolean state)
135: throws XMLConfigurationException {
136:
137: // check and store
138: checkFeature(featureId);
139:
140: fFeatures.put(featureId, state ? Boolean.TRUE : Boolean.FALSE);
141: } // setFeature(String,boolean)
142:
143: /**
144: * Allows a parser to add parser specific properties to be recognized
145: * and managed by the parser configuration.
146: *
147: * @param propertyIds An array of the additional property identifiers
148: * to be recognized.
149: */
150: public void addRecognizedProperties(String[] propertyIds) {
151:
152: // add recognizedProperties
153: int propertyIdsCount = propertyIds != null ? propertyIds.length
154: : 0;
155: for (int i = 0; i < propertyIdsCount; i++) {
156: String propertyId = propertyIds[i];
157: if (!fRecognizedProperties.contains(propertyId)) {
158: fRecognizedProperties.add(propertyId);
159: }
160: }
161:
162: } // addRecognizedProperties(String[])
163:
164: /**
165: * setProperty
166: *
167: * @param propertyId
168: * @param value
169: * @exception org.apache.xerces.xni.parser.XMLConfigurationException If the
170: * requested feature is not known.
171: */
172: public void setProperty(String propertyId, Object value)
173: throws XMLConfigurationException {
174:
175: // check and store
176: checkProperty(propertyId);
177: fProperties.put(propertyId, value);
178:
179: } // setProperty(String,Object)
180:
181: //
182: // XMLComponentManager methods
183: //
184:
185: /**
186: * Returns the state of a feature.
187: *
188: * @param featureId The feature identifier.
189: * @return true if the feature is supported
190: *
191: * @throws XMLConfigurationException Thrown for configuration error.
192: * In general, components should
193: * only throw this exception if
194: * it is <strong>really</strong>
195: * a critical error.
196: */
197: public boolean getFeature(String featureId)
198: throws XMLConfigurationException {
199:
200: Boolean state = (Boolean) fFeatures.get(featureId);
201:
202: if (state == null) {
203: checkFeature(featureId);
204: return false;
205: }
206: return state.booleanValue();
207:
208: } // getFeature(String):boolean
209:
210: /**
211: * Returns the value of a property.
212: *
213: * @param propertyId The property identifier.
214: * @return the value of the property
215: *
216: * @throws XMLConfigurationException Thrown for configuration error.
217: * In general, components should
218: * only throw this exception if
219: * it is <strong>really</strong>
220: * a critical error.
221: */
222: public Object getProperty(String propertyId)
223: throws XMLConfigurationException {
224:
225: Object propertyValue = fProperties.get(propertyId);
226:
227: if (propertyValue == null) {
228: checkProperty(propertyId);
229: }
230:
231: return propertyValue;
232:
233: } // getProperty(String):Object
234:
235: //
236: // Protected methods
237: //
238:
239: /**
240: * Check a feature. If feature is known and supported, this method simply
241: * returns. Otherwise, the appropriate exception is thrown.
242: *
243: * @param featureId The unique identifier (URI) of the feature.
244: *
245: * @exception org.apache.xerces.xni.parser.XMLConfigurationException If the
246: * requested feature is not known.
247: */
248: protected void checkFeature(String featureId)
249: throws XMLConfigurationException {
250:
251: // check feature
252: if (!fRecognizedFeatures.contains(featureId)) {
253: if (fParentSettings != null) {
254: fParentSettings.getFeature(featureId);
255: } else {
256: short type = XMLConfigurationException.NOT_RECOGNIZED;
257: throw new XMLConfigurationException(type, featureId);
258: }
259: }
260:
261: } // checkFeature(String)
262:
263: /**
264: * Check a property. If the property is known and supported, this method
265: * simply returns. Otherwise, the appropriate exception is thrown.
266: *
267: * @param propertyId The unique identifier (URI) of the property
268: * being set.
269: * @exception org.apache.xerces.xni.parser.XMLConfigurationException If the
270: * requested feature is not known.
271: */
272: protected void checkProperty(String propertyId)
273: throws XMLConfigurationException {
274:
275: // check property
276: if (!fRecognizedProperties.contains(propertyId)) {
277: if (fParentSettings != null) {
278: fParentSettings.getProperty(propertyId);
279: } else {
280: short type = XMLConfigurationException.NOT_RECOGNIZED;
281: throw new XMLConfigurationException(type, propertyId);
282: }
283: }
284:
285: } // checkProperty(String)
286:
287: } // class ParserConfigurationSettings
|