001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2001, 2002 The Apache Software Foundation.
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Xerces" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 1999, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package com.sun.xml.stream.xerces.util;
059:
060: import java.util.Hashtable;
061: import java.util.Vector;
062:
063: import com.sun.xml.stream.xerces.xni.parser.XMLComponentManager;
064: import com.sun.xml.stream.xerces.xni.parser.XMLConfigurationException;
065:
066: /**
067: * This class implements the basic operations for managing parser
068: * configuration features and properties. This utility class can
069: * be used as a base class for parser configurations or separately
070: * to encapsulate a number of parser settings as a component
071: * manager.
072: * <p>
073: * This class can be constructed with a "parent" settings object
074: * (in the form of an <code>XMLComponentManager</code>) that allows
075: * parser configuration settings to be "chained" together.
076: *
077: * @author Andy Clark, IBM
078: *
079: * @version $Id: ParserConfigurationSettings.java,v 1.2 2006/04/01 06:01:40 jeffsuttor Exp $
080: */
081: public class ParserConfigurationSettings implements XMLComponentManager {
082:
083: //
084: // Data
085: //
086:
087: // data
088:
089: /** Recognized properties. */
090: protected Vector fRecognizedProperties;
091:
092: /** Properties. */
093: protected Hashtable fProperties;
094:
095: /** Recognized features. */
096: protected Vector fRecognizedFeatures;
097:
098: /** Features. */
099: protected Hashtable fFeatures;
100:
101: /** Parent parser configuration settings. */
102: protected XMLComponentManager fParentSettings;
103:
104: //
105: // Constructors
106: //
107:
108: /** Default Constructor. */
109: public ParserConfigurationSettings() {
110: this (null);
111: } // <init>()
112:
113: /**
114: * Constructs a parser configuration settings object with a
115: * parent settings object.
116: */
117: public ParserConfigurationSettings(XMLComponentManager parent) {
118:
119: // create storage for recognized features and properties
120: fRecognizedFeatures = new Vector();
121: fRecognizedProperties = new Vector();
122:
123: // create table for features and properties
124: fFeatures = new Hashtable();
125: fProperties = new Hashtable();
126:
127: // save parent
128: fParentSettings = parent;
129:
130: } // <init>(XMLComponentManager)
131:
132: //
133: // XMLParserConfiguration methods
134: //
135:
136: /**
137: * Allows a parser to add parser specific features to be recognized
138: * and managed by the parser configuration.
139: *
140: * @param featureIds An array of the additional feature identifiers
141: * to be recognized.
142: */
143: public void addRecognizedFeatures(String[] featureIds) {
144:
145: // add recognized features
146: int featureIdsCount = featureIds != null ? featureIds.length
147: : 0;
148: for (int i = 0; i < featureIdsCount; i++) {
149: String featureId = featureIds[i];
150: if (!fRecognizedFeatures.contains(featureId)) {
151: fRecognizedFeatures.addElement(featureId);
152: }
153: }
154:
155: } // addRecognizedFeatures(String[])
156:
157: /**
158: * Set the state of a feature.
159: *
160: * Set the state of any feature in a SAX2 parser. The parser
161: * might not recognize the feature, and if it does recognize
162: * it, it might not be able to fulfill the request.
163: *
164: * @param featureId The unique identifier (URI) of the feature.
165: * @param state The requested state of the feature (true or false).
166: *
167: * @exception com.sun.xml.stream.xerces.xni.parser.XMLConfigurationException If the
168: * requested feature is not known.
169: */
170: static int counter = 1;
171:
172: public void setFeature(String featureId, boolean state)
173: throws XMLConfigurationException {
174:
175: // check and store
176: checkFeature(featureId);
177:
178: fFeatures.put(featureId, state ? Boolean.TRUE : Boolean.FALSE);
179: } // setFeature(String,boolean)
180:
181: /**
182: * Allows a parser to add parser specific properties to be recognized
183: * and managed by the parser configuration.
184: *
185: * @param propertyIds An array of the additional property identifiers
186: * to be recognized.
187: */
188: public void addRecognizedProperties(String[] propertyIds) {
189:
190: // add recognizedProperties
191: int propertyIdsCount = propertyIds != null ? propertyIds.length
192: : 0;
193: for (int i = 0; i < propertyIdsCount; i++) {
194: String propertyId = propertyIds[i];
195: if (!fRecognizedProperties.contains(propertyId)) {
196: fRecognizedProperties.addElement(propertyId);
197: }
198: }
199:
200: } // addRecognizedProperties(String[])
201:
202: /**
203: * setProperty
204: *
205: * @param propertyId
206: * @param value
207: * @exception com.sun.xml.stream.xerces.xni.parser.XMLConfigurationException If the
208: * requested feature is not known.
209: */
210: public void setProperty(String propertyId, Object value)
211: throws XMLConfigurationException {
212:
213: //System.out.println("propertId = " + propertyId ) ;
214: //System.out.println("value = " + value ) ;
215: // check and store
216: checkProperty(propertyId);
217: fProperties.put(propertyId, value);
218:
219: } // setProperty(String,Object)
220:
221: //
222: // XMLComponentManager methods
223: //
224:
225: /**
226: * Returns the state of a feature.
227: *
228: * @param featureId The feature identifier.
229: * @return true if the feature is supported
230: *
231: * @throws XMLConfigurationException Thrown for configuration error.
232: * In general, components should
233: * only throw this exception if
234: * it is <strong>really</strong>
235: * a critical error.
236: */
237: public boolean getFeature(String featureId)
238: throws XMLConfigurationException {
239:
240: Boolean state = (Boolean) fFeatures.get(featureId);
241:
242: if (state == null) {
243: checkFeature(featureId);
244: return false;
245: }
246: return state.booleanValue();
247:
248: } // getFeature(String):boolean
249:
250: /**
251: * Returns the value of a property.
252: *
253: * @param propertyId The property identifier.
254: * @return the value of the property
255: *
256: * @throws XMLConfigurationException Thrown for configuration error.
257: * In general, components should
258: * only throw this exception if
259: * it is <strong>really</strong>
260: * a critical error.
261: */
262: public Object getProperty(String propertyId)
263: throws XMLConfigurationException {
264:
265: Object propertyValue = fProperties.get(propertyId);
266:
267: if (propertyValue == null) {
268: checkProperty(propertyId);
269: }
270:
271: return propertyValue;
272:
273: } // getProperty(String):Object
274:
275: //
276: // Protected methods
277: //
278:
279: /**
280: * Check a feature. If feature is known and supported, this method simply
281: * returns. Otherwise, the appropriate exception is thrown.
282: *
283: * @param featureId The unique identifier (URI) of the feature.
284: *
285: * @exception com.sun.xml.stream.xerces.xni.parser.XMLConfigurationException If the
286: * requested feature is not known.
287: */
288: protected void checkFeature(String featureId)
289: throws XMLConfigurationException {
290:
291: // check feature
292: if (!fRecognizedFeatures.contains(featureId)) {
293: if (fParentSettings != null) {
294: fParentSettings.getFeature(featureId);
295: } else {
296: short type = XMLConfigurationException.NOT_RECOGNIZED;
297: throw new XMLConfigurationException(type, featureId);
298: }
299: }
300:
301: } // checkFeature(String)
302:
303: /**
304: * Check a property. If the property is known and supported, this method
305: * simply returns. Otherwise, the appropriate exception is thrown.
306: *
307: * @param propertyId The unique identifier (URI) of the property
308: * being set.
309: * @exception com.sun.xml.stream.xerces.xni.parser.XMLConfigurationException If the
310: * requested feature is not known.
311: */
312: protected void checkProperty(String propertyId)
313: throws XMLConfigurationException {
314:
315: // check property
316: if (!fRecognizedProperties.contains(propertyId)) {
317: if (fParentSettings != null) {
318: fParentSettings.getProperty(propertyId);
319: } else {
320: short type = XMLConfigurationException.NOT_RECOGNIZED;
321: throw new XMLConfigurationException(type, propertyId);
322: }
323: }
324:
325: } // checkProperty(String)
326:
327: } // class ParserConfigurationSettings
|