KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > util > ParserConfigurationSettings


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
58 package com.sun.org.apache.xerces.internal.util;
59
60 import java.util.ArrayList JavaDoc;
61 import java.util.HashMap JavaDoc;
62
63 import com.sun.org.apache.xerces.internal.impl.Constants;
64 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
65 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
66
67 /**
68  * This class implements the basic operations for managing parser
69  * configuration features and properties. This utility class can
70  * be used as a base class for parser configurations or separately
71  * to encapsulate a number of parser settings as a component
72  * manager.
73  * <p>
74  * This class can be constructed with a "parent" settings object
75  * (in the form of an <code>XMLComponentManager</code>) that allows
76  * parser configuration settings to be "chained" together.
77  *
78  * @author Andy Clark, IBM
79  *
80  * @version $Id: ParserConfigurationSettings.java,v 1.11 2004/04/25 05:05:50 mrglavas Exp $
81  */

82 public class ParserConfigurationSettings
83     implements XMLComponentManager {
84        
85    protected static final String JavaDoc PARSER_SETTINGS =
86        Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS;
87
88     //
89
// Data
90
//
91

92     // data
93

94     /** Recognized properties. */
95     protected ArrayList JavaDoc fRecognizedProperties;
96
97     /** Properties. */
98     protected HashMap JavaDoc fProperties;
99
100     /** Recognized features. */
101     protected ArrayList JavaDoc fRecognizedFeatures;
102
103     /** Features. */
104     protected HashMap JavaDoc fFeatures;
105
106     /** Parent parser configuration settings. */
107     protected XMLComponentManager fParentSettings;
108
109     //
110
// Constructors
111
//
112

113     /** Default Constructor. */
114     public ParserConfigurationSettings() {
115         this(null);
116     } // <init>()
117

118     /**
119      * Constructs a parser configuration settings object with a
120      * parent settings object.
121      */

122     public ParserConfigurationSettings(XMLComponentManager parent) {
123
124         // create storage for recognized features and properties
125
fRecognizedFeatures = new ArrayList JavaDoc();
126         fRecognizedProperties = new ArrayList JavaDoc();
127
128         // create table for features and properties
129
fFeatures = new HashMap JavaDoc();
130         fProperties = new HashMap JavaDoc();
131
132         // save parent
133
fParentSettings = parent;
134
135     } // <init>(XMLComponentManager)
136

137     //
138
// XMLParserConfiguration methods
139
//
140

141     /**
142      * Allows a parser to add parser specific features to be recognized
143      * and managed by the parser configuration.
144      *
145      * @param featureIds An array of the additional feature identifiers
146      * to be recognized.
147      */

148     public void addRecognizedFeatures(String JavaDoc[] featureIds) {
149
150         // add recognized features
151
int featureIdsCount = featureIds != null ? featureIds.length : 0;
152         for (int i = 0; i < featureIdsCount; i++) {
153             String JavaDoc featureId = featureIds[i];
154             if (!fRecognizedFeatures.contains(featureId)) {
155                 fRecognizedFeatures.add(featureId);
156             }
157         }
158
159     } // addRecognizedFeatures(String[])
160

161     /**
162      * Set the state of a feature.
163      *
164      * Set the state of any feature in a SAX2 parser. The parser
165      * might not recognize the feature, and if it does recognize
166      * it, it might not be able to fulfill the request.
167      *
168      * @param featureId The unique identifier (URI) of the feature.
169      * @param state The requested state of the feature (true or false).
170      *
171      * @exception com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException If the
172      * requested feature is not known.
173      */

174     public void setFeature(String JavaDoc featureId, boolean state)
175         throws XMLConfigurationException {
176
177         // check and store
178
checkFeature(featureId);
179
180         fFeatures.put(featureId, state ? Boolean.TRUE : Boolean.FALSE);
181     } // setFeature(String,boolean)
182

183     /**
184      * Allows a parser to add parser specific properties to be recognized
185      * and managed by the parser configuration.
186      *
187      * @param propertyIds An array of the additional property identifiers
188      * to be recognized.
189      */

190     public void addRecognizedProperties(String JavaDoc[] propertyIds) {
191
192         // add recognizedProperties
193
int propertyIdsCount = propertyIds != null ? propertyIds.length : 0;
194         for (int i = 0; i < propertyIdsCount; i++) {
195             String JavaDoc propertyId = propertyIds[i];
196             if (!fRecognizedProperties.contains(propertyId)) {
197                 fRecognizedProperties.add(propertyId);
198             }
199         }
200
201     } // addRecognizedProperties(String[])
202

203     /**
204      * setProperty
205      *
206      * @param propertyId
207      * @param value
208      * @exception com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException If the
209      * requested feature is not known.
210      */

211     public void setProperty(String JavaDoc propertyId, Object JavaDoc value)
212         throws XMLConfigurationException {
213
214         // check and store
215
checkProperty(propertyId);
216         if(value == null){
217             fProperties.remove(propertyId);
218         }else
219             fProperties.put(propertyId, value);
220
221     } // setProperty(String,Object)
222

223     //
224
// XMLComponentManager methods
225
//
226

227     /**
228      * Returns the state of a feature.
229      *
230      * @param featureId The feature identifier.
231          * @return true if the feature is supported
232      *
233      * @throws XMLConfigurationException Thrown for configuration error.
234      * In general, components should
235      * only throw this exception if
236      * it is <strong>really</strong>
237      * a critical error.
238      */

239     public boolean getFeature(String JavaDoc featureId)
240         throws XMLConfigurationException {
241
242         Boolean JavaDoc state = (Boolean JavaDoc) fFeatures.get(featureId);
243
244         if (state == null) {
245             checkFeature(featureId);
246             return false;
247         }
248         return state.booleanValue();
249
250     } // getFeature(String):boolean
251

252     /**
253      * Returns the value of a property.
254      *
255      * @param propertyId The property identifier.
256          * @return the value of the property
257      *
258      * @throws XMLConfigurationException Thrown for configuration error.
259      * In general, components should
260      * only throw this exception if
261      * it is <strong>really</strong>
262      * a critical error.
263      */

264     public Object JavaDoc getProperty(String JavaDoc propertyId)
265         throws XMLConfigurationException {
266
267         Object JavaDoc propertyValue = fProperties.get(propertyId);
268
269         if (propertyValue == null) {
270             checkProperty(propertyId);
271         }
272
273         return propertyValue;
274
275     } // getProperty(String):Object
276

277     //
278
// Protected methods
279
//
280

281     /**
282      * Check a feature. If feature is known and supported, this method simply
283      * returns. Otherwise, the appropriate exception is thrown.
284      *
285      * @param featureId The unique identifier (URI) of the feature.
286      *
287      * @exception com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException If the
288      * requested feature is not known.
289      */

290     protected void checkFeature(String JavaDoc featureId)
291         throws XMLConfigurationException {
292
293         // check feature
294
if (!fRecognizedFeatures.contains(featureId)) {
295             if (fParentSettings != null) {
296                 fParentSettings.getFeature(featureId);
297             }
298             else {
299                 short type = XMLConfigurationException.NOT_RECOGNIZED;
300                 throw new XMLConfigurationException(type, featureId);
301             }
302         }
303
304     } // checkFeature(String)
305

306     /**
307      * Check a property. If the property is known and supported, this method
308      * simply returns. Otherwise, the appropriate exception is thrown.
309      *
310      * @param propertyId The unique identifier (URI) of the property
311      * being set.
312      * @exception com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException If the
313      * requested feature is not known.
314      */

315     protected void checkProperty(String JavaDoc propertyId)
316         throws XMLConfigurationException {
317
318         // check property
319
if (!fRecognizedProperties.contains(propertyId)) {
320             if (fParentSettings != null) {
321                 fParentSettings.getProperty(propertyId);
322             }
323             else {
324                 short type = XMLConfigurationException.NOT_RECOGNIZED;
325                 throw new XMLConfigurationException(type, propertyId);
326             }
327         }
328
329     } // checkProperty(String)
330

331 } // class ParserConfigurationSettings
332
Popular Tags