KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > util > ParserConfigurationSettings


1 /*
2  * Copyright 2001, 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.util;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21
22 import org.apache.xerces.impl.Constants;
23 import org.apache.xerces.xni.parser.XMLComponentManager;
24 import org.apache.xerces.xni.parser.XMLConfigurationException;
25
26 /**
27  * This class implements the basic operations for managing parser
28  * configuration features and properties. This utility class can
29  * be used as a base class for parser configurations or separately
30  * to encapsulate a number of parser settings as a component
31  * manager.
32  * <p>
33  * This class can be constructed with a "parent" settings object
34  * (in the form of an <code>XMLComponentManager</code>) that allows
35  * parser configuration settings to be "chained" together.
36  *
37  * @author Andy Clark, IBM
38  *
39  * @version $Id: ParserConfigurationSettings.java,v 1.11 2004/04/25 05:05:50 mrglavas Exp $
40  */

41 public class ParserConfigurationSettings
42     implements XMLComponentManager {
43         
44     protected static final String JavaDoc PARSER_SETTINGS =
45             Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS;
46
47     //
48
// Data
49
//
50

51     // data
52

53     /** Recognized properties. */
54     protected ArrayList JavaDoc fRecognizedProperties;
55
56     /** Properties. */
57     protected HashMap JavaDoc fProperties;
58
59     /** Recognized features. */
60     protected ArrayList JavaDoc fRecognizedFeatures;
61
62     /** Features. */
63     protected HashMap JavaDoc fFeatures;
64
65     /** Parent parser configuration settings. */
66     protected XMLComponentManager fParentSettings;
67
68     //
69
// Constructors
70
//
71

72     /** Default Constructor. */
73     public ParserConfigurationSettings() {
74         this(null);
75     } // <init>()
76

77     /**
78      * Constructs a parser configuration settings object with a
79      * parent settings object.
80      */

81     public ParserConfigurationSettings(XMLComponentManager parent) {
82
83         // create storage for recognized features and properties
84
fRecognizedFeatures = new ArrayList JavaDoc();
85         fRecognizedProperties = new ArrayList JavaDoc();
86
87         // create table for features and properties
88
fFeatures = new HashMap JavaDoc();
89         fProperties = new HashMap JavaDoc();
90
91         // save parent
92
fParentSettings = parent;
93
94     } // <init>(XMLComponentManager)
95

96     //
97
// XMLParserConfiguration methods
98
//
99

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 JavaDoc[] featureIds) {
108
109         // add recognized features
110
int featureIdsCount = featureIds != null ? featureIds.length : 0;
111         for (int i = 0; i < featureIdsCount; i++) {
112             String JavaDoc featureId = featureIds[i];
113             if (!fRecognizedFeatures.contains(featureId)) {
114                 fRecognizedFeatures.add(featureId);
115             }
116         }
117
118     } // addRecognizedFeatures(String[])
119

120     /**
121      * Set the state of a feature.
122      *
123      * Set the state of any feature in a SAX2 parser. The parser
124      * might not recognize the feature, and if it does recognize
125      * it, it might not be able to fulfill the request.
126      *
127      * @param featureId The unique identifier (URI) of the feature.
128      * @param state The requested state of the feature (true or false).
129      *
130      * @exception org.apache.xerces.xni.parser.XMLConfigurationException If the
131      * requested feature is not known.
132      */

133     public void setFeature(String JavaDoc featureId, boolean state)
134         throws XMLConfigurationException {
135
136         // check and store
137
checkFeature(featureId);
138
139         fFeatures.put(featureId, state ? Boolean.TRUE : Boolean.FALSE);
140     } // setFeature(String,boolean)
141

142     /**
143      * Allows a parser to add parser specific properties to be recognized
144      * and managed by the parser configuration.
145      *
146      * @param propertyIds An array of the additional property identifiers
147      * to be recognized.
148      */

149     public void addRecognizedProperties(String JavaDoc[] propertyIds) {
150
151         // add recognizedProperties
152
int propertyIdsCount = propertyIds != null ? propertyIds.length : 0;
153         for (int i = 0; i < propertyIdsCount; i++) {
154             String JavaDoc propertyId = propertyIds[i];
155             if (!fRecognizedProperties.contains(propertyId)) {
156                 fRecognizedProperties.add(propertyId);
157             }
158         }
159
160     } // addRecognizedProperties(String[])
161

162     /**
163      * setProperty
164      *
165      * @param propertyId
166      * @param value
167      * @exception org.apache.xerces.xni.parser.XMLConfigurationException If the
168      * requested feature is not known.
169      */

170     public void setProperty(String JavaDoc propertyId, Object JavaDoc value)
171         throws XMLConfigurationException {
172
173         // check and store
174
checkProperty(propertyId);
175         fProperties.put(propertyId, value);
176
177     } // setProperty(String,Object)
178

179     //
180
// XMLComponentManager methods
181
//
182

183     /**
184      * Returns the state of a feature.
185      *
186      * @param featureId The feature identifier.
187          * @return true if the feature is supported
188      *
189      * @throws XMLConfigurationException Thrown for configuration error.
190      * In general, components should
191      * only throw this exception if
192      * it is <strong>really</strong>
193      * a critical error.
194      */

195     public boolean getFeature(String JavaDoc featureId)
196         throws XMLConfigurationException {
197
198         Boolean JavaDoc state = (Boolean JavaDoc) fFeatures.get(featureId);
199
200         if (state == null) {
201             checkFeature(featureId);
202             return false;
203         }
204         return state.booleanValue();
205
206     } // getFeature(String):boolean
207

208     /**
209      * Returns the value of a property.
210      *
211      * @param propertyId The property identifier.
212          * @return the value of the property
213      *
214      * @throws XMLConfigurationException Thrown for configuration error.
215      * In general, components should
216      * only throw this exception if
217      * it is <strong>really</strong>
218      * a critical error.
219      */

220     public Object JavaDoc getProperty(String JavaDoc propertyId)
221         throws XMLConfigurationException {
222
223         Object JavaDoc propertyValue = fProperties.get(propertyId);
224
225         if (propertyValue == null) {
226             checkProperty(propertyId);
227         }
228
229         return propertyValue;
230
231     } // getProperty(String):Object
232

233     //
234
// Protected methods
235
//
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 org.apache.xerces.xni.parser.XMLConfigurationException If the
244      * requested feature is not known.
245      */

246     protected void checkFeature(String JavaDoc featureId)
247         throws XMLConfigurationException {
248
249         // check feature
250
if (!fRecognizedFeatures.contains(featureId)) {
251             if (fParentSettings != null) {
252                 fParentSettings.getFeature(featureId);
253             }
254             else {
255                 short type = XMLConfigurationException.NOT_RECOGNIZED;
256                 throw new XMLConfigurationException(type, featureId);
257             }
258         }
259
260     } // checkFeature(String)
261

262     /**
263      * Check a property. If the property is known and supported, this method
264      * simply returns. Otherwise, the appropriate exception is thrown.
265      *
266      * @param propertyId The unique identifier (URI) of the property
267      * being set.
268      * @exception org.apache.xerces.xni.parser.XMLConfigurationException If the
269      * requested feature is not known.
270      */

271     protected void checkProperty(String JavaDoc propertyId)
272         throws XMLConfigurationException {
273
274         // check property
275
if (!fRecognizedProperties.contains(propertyId)) {
276             if (fParentSettings != null) {
277                 fParentSettings.getProperty(propertyId);
278             }
279             else {
280                 short type = XMLConfigurationException.NOT_RECOGNIZED;
281                 throw new XMLConfigurationException(type, propertyId);
282             }
283         }
284
285     } // checkProperty(String)
286

287 } // class ParserConfigurationSettings
288
Popular Tags