12005
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 2001-2004 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*/
|
|
20 |
|
|
21 |
package com.sun.org.apache.xerces.internal.parsers;
|
|
22 |
|
|
23 |
import java.io.IOException;
|
|
24 |
import java.util.Locale;
|
|
25 |
|
|
26 |
import com.sun.org.apache.xerces.internal.impl.Constants;
|
|
27 |
import com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl;
|
|
28 |
import com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl;
|
|
29 |
import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
|
|
30 |
import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
|
|
31 |
import com.sun.org.apache.xerces.internal.impl.XMLNamespaceBinder;
|
|
32 |
import com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDProcessor;
|
|
33 |
import com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator;
|
|
34 |
import com.sun.org.apache.xerces.internal.impl.dv.DTDDVFactory;
|
|
35 |
import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
|
|
36 |
import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;
|
|
37 |
import com.sun.org.apache.xerces.internal.util.FeatureState;
|
|
38 |
import com.sun.org.apache.xerces.internal.util.PropertyState;
|
|
39 |
import com.sun.org.apache.xerces.internal.util.Status;
|
|
40 |
import com.sun.org.apache.xerces.internal.util.SymbolTable;
|
|
41 |
import com.sun.org.apache.xerces.internal.xni.XMLLocator;
|
|
42 |
import com.sun.org.apache.xerces.internal.xni.XNIException;
|
|
43 |
import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
|
|
44 |
import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
|
|
45 |
import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
|
|
46 |
import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
|
|
47 |
import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDScanner;
|
|
48 |
import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentScanner;
|
|
49 |
import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
|
|
50 |
import com.sun.org.apache.xerces.internal.xni.parser.XMLPullParserConfiguration;
|
|
51 |
|
|
52 |
/**
|
|
53 |
* This is the DTD-only parser configuration. It extends the basic
|
|
54 |
* configuration with a standard set of parser components appropriate
|
|
55 |
* to DTD-centric validation. Since
|
|
56 |
* the Xerces2 reference implementation document and DTD scanner
|
|
57 |
* implementations are capable of acting as pull parsers, this
|
|
58 |
* configuration implements the
|
|
59 |
* <code>XMLPullParserConfiguration</code> interface.
|
|
60 |
* <p>
|
|
61 |
* In addition to the features and properties recognized by the base
|
|
62 |
* parser configuration, this class recognizes these additional
|
|
63 |
* features and properties:
|
|
64 |
* <ul>
|
|
65 |
* <li>Features
|
|
66 |
* <ul>
|
|
67 |
* <li>http://apache.org/xml/features/validation/warn-on-duplicate-attdef</li>
|
|
68 |
* <li>http://apache.org/xml/features/validation/warn-on-undeclared-elemdef</li>
|
|
69 |
* <li>http://apache.org/xml/features/allow-java-encodings</li>
|
|
70 |
* <li>http://apache.org/xml/features/continue-after-fatal-error</li>
|
|
71 |
* <li>http://apache.org/xml/features/load-external-dtd</li>
|
|
72 |
* </ul>
|
|
73 |
* <li>Properties
|
|
74 |
* <ul>
|
|
75 |
* <li>http://apache.org/xml/properties/internal/error-reporter</li>
|
|
76 |
* <li>http://apache.org/xml/properties/internal/entity-manager</li>
|
|
77 |
* <li>http://apache.org/xml/properties/internal/document-scanner</li>
|
|
78 |
* <li>http://apache.org/xml/properties/internal/dtd-scanner</li>
|
|
79 |
* <li>http://apache.org/xml/properties/internal/grammar-pool</li>
|
|
80 |
* <li>http://apache.org/xml/properties/internal/validator/dtd</li>
|
|
81 |
* <li>http://apache.org/xml/properties/internal/datatype-validator-factory</li>
|
|
82 |
* </ul>
|
|
83 |
* </ul>
|
|
84 |
*
|
|
85 |
* @author Arnaud Le Hors, IBM
|
|
86 |
* @author Andy Clark, IBM
|
|
87 |
* @author Neil Graham, IBM
|
|
88 |
*
|
|
89 |
* @version $Id: DTDConfiguration.java,v 1.7 2010-11-01 04:40:09 joehw Exp $
|
|
90 |
*/
|
|
91 |
public class DTDConfiguration
|
|
92 |
extends BasicParserConfiguration
|
|
93 |
implements XMLPullParserConfiguration {
|
|
94 |
|
|
95 |
//
|
|
96 |
// Constants
|
|
97 |
//
|
|
98 |
|
|
99 |
// feature identifiers
|
|
100 |
|
|
101 |
/** Feature identifier: warn on duplicate attribute definition. */
|
|
102 |
protected static final String WARN_ON_DUPLICATE_ATTDEF =
|
|
103 |
Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE;
|
|
104 |
|
|
105 |
/** Feature identifier: warn on duplicate entity definition. */
|
|
106 |
protected static final String WARN_ON_DUPLICATE_ENTITYDEF =
|
|
107 |
Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE;
|
|
108 |
|
|
109 |
/** Feature identifier: warn on undeclared element definition. */
|
|
110 |
protected static final String WARN_ON_UNDECLARED_ELEMDEF =
|
|
111 |
Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_UNDECLARED_ELEMDEF_FEATURE;
|
|
112 |
|
|
113 |
/** Feature identifier: allow Java encodings. */
|
|
114 |
protected static final String ALLOW_JAVA_ENCODINGS =
|
|
115 |
Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE;
|
|
116 |
|
|
117 |
/** Feature identifier: continue after fatal error. */
|
|
118 |
protected static final String CONTINUE_AFTER_FATAL_ERROR =
|
|
119 |
Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE;
|
|
120 |
|
|
121 |
/** Feature identifier: load external DTD. */
|
|
122 |
protected static final String LOAD_EXTERNAL_DTD =
|
|
123 |
Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE;
|
|
124 |
|
|
125 |
/** Feature identifier: notify built-in refereces. */
|
|
126 |
protected static final String NOTIFY_BUILTIN_REFS =
|
|
127 |
Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_BUILTIN_REFS_FEATURE;
|
|
128 |
|
|
129 |
/** Feature identifier: notify character refereces. */
|
|
130 |
protected static final String NOTIFY_CHAR_REFS =
|
|
131 |
Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_CHAR_REFS_FEATURE;
|
|
132 |
|
|
133 |
|
|
134 |
// property identifiers
|
|
135 |
|
|
136 |
/** Property identifier: error reporter. */
|
|
137 |
protected static final String ERROR_REPORTER =
|
|
138 |
Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
|
|
139 |
|
|
140 |
/** Property identifier: entity manager. */
|
|
141 |
protected static final String ENTITY_MANAGER =
|
|
142 |
Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
|
|
143 |
|
|
144 |
/** Property identifier document scanner: */
|
|
145 |
protected static final String DOCUMENT_SCANNER =
|
|
146 |
Constants.XERCES_PROPERTY_PREFIX + Constants.DOCUMENT_SCANNER_PROPERTY;
|
|
147 |
|
|
148 |
/** Property identifier: DTD scanner. */
|
|
149 |
protected static final String DTD_SCANNER =
|
|
150 |
Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_SCANNER_PROPERTY;
|
|
151 |
|
|
152 |
/** Property identifier: grammar pool. */
|
|
153 |
protected static final String XMLGRAMMAR_POOL =
|
|
154 |
Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
|
|
155 |
|
|
156 |
/** Property identifier: DTD loader. */
|
|
157 |
protected static final String DTD_PROCESSOR =
|
|
158 |
Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_PROCESSOR_PROPERTY;
|
|
159 |
|
|
160 |
/** Property identifier: DTD validator. */
|
|
161 |
protected static final String DTD_VALIDATOR =
|
|
162 |
Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_VALIDATOR_PROPERTY;
|
|
163 |
|
|
164 |
/** Property identifier: namespace binder. */
|
|
165 |
protected static final String NAMESPACE_BINDER =
|
|
166 |
Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_BINDER_PROPERTY;
|
|
167 |
|
|
168 |
/** Property identifier: datatype validator factory. */
|
|
169 |
protected static final String DATATYPE_VALIDATOR_FACTORY =
|
|
170 |
Constants.XERCES_PROPERTY_PREFIX + Constants.DATATYPE_VALIDATOR_FACTORY_PROPERTY;
|
|
171 |
|
|
172 |
protected static final String VALIDATION_MANAGER =
|
|
173 |
Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY;
|
|
174 |
|
|
175 |
/** Property identifier: JAXP schema language / DOM schema-type. */
|
|
176 |
protected static final String JAXP_SCHEMA_LANGUAGE =
|
|
177 |
Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE;
|
|
178 |
|
|
179 |
/** Property identifier: JAXP schema source/ DOM schema-location. */
|
|
180 |
protected static final String JAXP_SCHEMA_SOURCE =
|
|
181 |
Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE;
|
|
182 |
|
|
183 |
/** Property identifier: locale. */
|
|
184 |
protected static final String LOCALE =
|
|
185 |
Constants.XERCES_PROPERTY_PREFIX + Constants.LOCALE_PROPERTY;
|
|
186 |
|
20973
|
187 |
/** Property identifier: Security property manager. */
|
|
188 |
protected static final String XML_SECURITY_PROPERTY_MANAGER =
|
|
189 |
Constants.XML_SECURITY_PROPERTY_MANAGER;
|
|
190 |
|
|
191 |
/** Property identifier: Security manager. */
|
|
192 |
private static final String SECURITY_MANAGER = Constants.SECURITY_MANAGER;
|
|
193 |
|
12005
|
194 |
// debugging
|
|
195 |
|
|
196 |
/** Set to true and recompile to print exception stack trace. */
|
|
197 |
protected static final boolean PRINT_EXCEPTION_STACK_TRACE = false;
|
|
198 |
|
|
199 |
//
|
|
200 |
// Data
|
|
201 |
//
|
|
202 |
|
|
203 |
// components (non-configurable)
|
|
204 |
|
|
205 |
/** Grammar pool. */
|
|
206 |
protected XMLGrammarPool fGrammarPool;
|
|
207 |
|
|
208 |
/** Datatype validator factory. */
|
|
209 |
protected DTDDVFactory fDatatypeValidatorFactory;
|
|
210 |
|
|
211 |
// components (configurable)
|
|
212 |
|
|
213 |
/** Error reporter. */
|
|
214 |
protected XMLErrorReporter fErrorReporter;
|
|
215 |
|
|
216 |
/** Entity manager. */
|
|
217 |
protected XMLEntityManager fEntityManager;
|
|
218 |
|
|
219 |
/** Document scanner. */
|
|
220 |
protected XMLDocumentScanner fScanner;
|
|
221 |
|
|
222 |
/** Input Source */
|
|
223 |
protected XMLInputSource fInputSource;
|
|
224 |
|
|
225 |
/** DTD scanner. */
|
|
226 |
protected XMLDTDScanner fDTDScanner;
|
|
227 |
|
|
228 |
/** DTD Processor . */
|
|
229 |
protected XMLDTDProcessor fDTDProcessor;
|
|
230 |
|
|
231 |
/** DTD Validator. */
|
|
232 |
protected XMLDTDValidator fDTDValidator;
|
|
233 |
|
|
234 |
/** Namespace binder. */
|
|
235 |
protected XMLNamespaceBinder fNamespaceBinder;
|
|
236 |
|
|
237 |
protected ValidationManager fValidationManager;
|
|
238 |
// state
|
|
239 |
|
|
240 |
/** Locator */
|
|
241 |
protected XMLLocator fLocator;
|
|
242 |
|
|
243 |
/**
|
|
244 |
* True if a parse is in progress. This state is needed because
|
|
245 |
* some features/properties cannot be set while parsing (e.g.
|
|
246 |
* validation and namespaces).
|
|
247 |
*/
|
|
248 |
protected boolean fParseInProgress = false;
|
|
249 |
|
|
250 |
//
|
|
251 |
// Constructors
|
|
252 |
//
|
|
253 |
|
|
254 |
/** Default constructor. */
|
|
255 |
public DTDConfiguration() {
|
|
256 |
this(null, null, null);
|
|
257 |
} // <init>()
|
|
258 |
|
|
259 |
/**
|
|
260 |
* Constructs a parser configuration using the specified symbol table.
|
|
261 |
*
|
|
262 |
* @param symbolTable The symbol table to use.
|
|
263 |
*/
|
|
264 |
public DTDConfiguration(SymbolTable symbolTable) {
|
|
265 |
this(symbolTable, null, null);
|
|
266 |
} // <init>(SymbolTable)
|
|
267 |
|
|
268 |
/**
|
|
269 |
* Constructs a parser configuration using the specified symbol table and
|
|
270 |
* grammar pool.
|
|
271 |
* <p>
|
|
272 |
* <strong>REVISIT:</strong>
|
|
273 |
* Grammar pool will be updated when the new validation engine is
|
|
274 |
* implemented.
|
|
275 |
*
|
|
276 |
* @param symbolTable The symbol table to use.
|
|
277 |
* @param grammarPool The grammar pool to use.
|
|
278 |
*/
|
|
279 |
public DTDConfiguration(SymbolTable symbolTable,
|
|
280 |
XMLGrammarPool grammarPool) {
|
|
281 |
this(symbolTable, grammarPool, null);
|
|
282 |
} // <init>(SymbolTable,XMLGrammarPool)
|
|
283 |
|
|
284 |
/**
|
|
285 |
* Constructs a parser configuration using the specified symbol table,
|
|
286 |
* grammar pool, and parent settings.
|
|
287 |
* <p>
|
|
288 |
* <strong>REVISIT:</strong>
|
|
289 |
* Grammar pool will be updated when the new validation engine is
|
|
290 |
* implemented.
|
|
291 |
*
|
|
292 |
* @param symbolTable The symbol table to use.
|
|
293 |
* @param grammarPool The grammar pool to use.
|
|
294 |
* @param parentSettings The parent settings.
|
|
295 |
*/
|
|
296 |
public DTDConfiguration(SymbolTable symbolTable,
|
|
297 |
XMLGrammarPool grammarPool,
|
|
298 |
XMLComponentManager parentSettings) {
|
|
299 |
super(symbolTable, parentSettings);
|
|
300 |
|
|
301 |
// add default recognized features
|
|
302 |
final String[] recognizedFeatures = {
|
|
303 |
//WARN_ON_DUPLICATE_ATTDEF, // from XMLDTDScannerImpl
|
|
304 |
//WARN_ON_UNDECLARED_ELEMDEF, // from XMLDTDScannerImpl
|
|
305 |
//ALLOW_JAVA_ENCODINGS, // from XMLEntityManager
|
|
306 |
CONTINUE_AFTER_FATAL_ERROR,
|
|
307 |
LOAD_EXTERNAL_DTD, // from XMLDTDScannerImpl
|
|
308 |
//NOTIFY_BUILTIN_REFS, // from XMLDocumentFragmentScannerImpl
|
|
309 |
//NOTIFY_CHAR_REFS, // from XMLDocumentFragmentScannerImpl
|
|
310 |
//WARN_ON_DUPLICATE_ENTITYDEF, // from XMLEntityManager
|
|
311 |
};
|
|
312 |
addRecognizedFeatures(recognizedFeatures);
|
|
313 |
|
|
314 |
// set state for default features
|
|
315 |
//setFeature(WARN_ON_DUPLICATE_ATTDEF, false); // from XMLDTDScannerImpl
|
|
316 |
//setFeature(WARN_ON_UNDECLARED_ELEMDEF, false); // from XMLDTDScannerImpl
|
|
317 |
//setFeature(ALLOW_JAVA_ENCODINGS, false); // from XMLEntityManager
|
|
318 |
setFeature(CONTINUE_AFTER_FATAL_ERROR, false);
|
|
319 |
setFeature(LOAD_EXTERNAL_DTD, true); // from XMLDTDScannerImpl
|
|
320 |
//setFeature(NOTIFY_BUILTIN_REFS, false); // from XMLDocumentFragmentScannerImpl
|
|
321 |
//setFeature(NOTIFY_CHAR_REFS, false); // from XMLDocumentFragmentScannerImpl
|
|
322 |
//setFeature(WARN_ON_DUPLICATE_ENTITYDEF, false); // from XMLEntityManager
|
|
323 |
|
|
324 |
// add default recognized properties
|
|
325 |
final String[] recognizedProperties = {
|
|
326 |
ERROR_REPORTER,
|
|
327 |
ENTITY_MANAGER,
|
|
328 |
DOCUMENT_SCANNER,
|
|
329 |
DTD_SCANNER,
|
|
330 |
DTD_PROCESSOR,
|
|
331 |
DTD_VALIDATOR,
|
|
332 |
NAMESPACE_BINDER,
|
|
333 |
XMLGRAMMAR_POOL,
|
|
334 |
DATATYPE_VALIDATOR_FACTORY,
|
|
335 |
VALIDATION_MANAGER,
|
|
336 |
JAXP_SCHEMA_SOURCE,
|
|
337 |
JAXP_SCHEMA_LANGUAGE,
|
20973
|
338 |
LOCALE,
|
|
339 |
SECURITY_MANAGER,
|
|
340 |
XML_SECURITY_PROPERTY_MANAGER
|
12005
|
341 |
};
|
|
342 |
addRecognizedProperties(recognizedProperties);
|
|
343 |
|
|
344 |
fGrammarPool = grammarPool;
|
|
345 |
if(fGrammarPool != null){
|
|
346 |
setProperty(XMLGRAMMAR_POOL, fGrammarPool);
|
|
347 |
}
|
|
348 |
|
|
349 |
fEntityManager = createEntityManager();
|
|
350 |
setProperty(ENTITY_MANAGER, fEntityManager);
|
|
351 |
addComponent(fEntityManager);
|
|
352 |
|
|
353 |
fErrorReporter = createErrorReporter();
|
|
354 |
fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner());
|
|
355 |
setProperty(ERROR_REPORTER, fErrorReporter);
|
|
356 |
addComponent(fErrorReporter);
|
|
357 |
|
|
358 |
fScanner = createDocumentScanner();
|
|
359 |
setProperty(DOCUMENT_SCANNER, fScanner);
|
|
360 |
if (fScanner instanceof XMLComponent) {
|
|
361 |
addComponent((XMLComponent)fScanner);
|
|
362 |
}
|
|
363 |
|
|
364 |
fDTDScanner = createDTDScanner();
|
|
365 |
if (fDTDScanner != null) {
|
|
366 |
setProperty(DTD_SCANNER, fDTDScanner);
|
|
367 |
if (fDTDScanner instanceof XMLComponent) {
|
|
368 |
addComponent((XMLComponent)fDTDScanner);
|
|
369 |
}
|
|
370 |
}
|
|
371 |
|
|
372 |
fDTDProcessor = createDTDProcessor();
|
|
373 |
if (fDTDProcessor != null) {
|
|
374 |
setProperty(DTD_PROCESSOR, fDTDProcessor);
|
|
375 |
if (fDTDProcessor instanceof XMLComponent) {
|
|
376 |
addComponent((XMLComponent)fDTDProcessor);
|
|
377 |
}
|
|
378 |
}
|
|
379 |
|
|
380 |
fDTDValidator = createDTDValidator();
|
|
381 |
if (fDTDValidator != null) {
|
|
382 |
setProperty(DTD_VALIDATOR, fDTDValidator);
|
|
383 |
addComponent(fDTDValidator);
|
|
384 |
}
|
|
385 |
|
|
386 |
fNamespaceBinder = createNamespaceBinder();
|
|
387 |
if (fNamespaceBinder != null) {
|
|
388 |
setProperty(NAMESPACE_BINDER, fNamespaceBinder);
|
|
389 |
addComponent(fNamespaceBinder);
|
|
390 |
}
|
|
391 |
|
|
392 |
fDatatypeValidatorFactory = createDatatypeValidatorFactory();
|
|
393 |
if (fDatatypeValidatorFactory != null) {
|
|
394 |
setProperty(DATATYPE_VALIDATOR_FACTORY,
|
|
395 |
fDatatypeValidatorFactory);
|
|
396 |
}
|
|
397 |
fValidationManager = createValidationManager();
|
|
398 |
|
|
399 |
if (fValidationManager != null) {
|
|
400 |
setProperty (VALIDATION_MANAGER, fValidationManager);
|
|
401 |
}
|
|
402 |
// add message formatters
|
|
403 |
if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) {
|
|
404 |
XMLMessageFormatter xmft = new XMLMessageFormatter();
|
|
405 |
fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft);
|
|
406 |
fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft);
|
|
407 |
}
|
|
408 |
|
|
409 |
// set locale
|
|
410 |
try {
|
|
411 |
setLocale(Locale.getDefault());
|
|
412 |
}
|
|
413 |
catch (XNIException e) {
|
|
414 |
// do nothing
|
|
415 |
// REVISIT: What is the right thing to do? -Ac
|
|
416 |
}
|
|
417 |
|
|
418 |
} // <init>(SymbolTable,XMLGrammarPool)
|
|
419 |
|
|
420 |
//
|
|
421 |
// Public methods
|
|
422 |
//
|
|
423 |
|
|
424 |
public PropertyState getPropertyState(String propertyId)
|
|
425 |
throws XMLConfigurationException {
|
|
426 |
if (LOCALE.equals(propertyId)) {
|
|
427 |
return PropertyState.is(getLocale());
|
|
428 |
}
|
|
429 |
return super.getPropertyState(propertyId);
|
|
430 |
}
|
|
431 |
|
|
432 |
public void setProperty(String propertyId, Object value)
|
|
433 |
throws XMLConfigurationException {
|
|
434 |
if (LOCALE.equals(propertyId)) {
|
|
435 |
setLocale((Locale) value);
|
|
436 |
}
|
|
437 |
super.setProperty(propertyId, value);
|
|
438 |
}
|
|
439 |
|
|
440 |
/**
|
|
441 |
* Set the locale to use for messages.
|
|
442 |
*
|
|
443 |
* @param locale The locale object to use for localization of messages.
|
|
444 |
*
|
|
445 |
* @exception XNIException Thrown if the parser does not support the
|
|
446 |
* specified locale.
|
|
447 |
*/
|
|
448 |
public void setLocale(Locale locale) throws XNIException {
|
|
449 |
super.setLocale(locale);
|
|
450 |
fErrorReporter.setLocale(locale);
|
|
451 |
} // setLocale(Locale)
|
|
452 |
|
|
453 |
//
|
|
454 |
// XMLPullParserConfiguration methods
|
|
455 |
//
|
|
456 |
|
|
457 |
// parsing
|
|
458 |
|
|
459 |
/**
|
|
460 |
* Sets the input source for the document to parse.
|
|
461 |
*
|
|
462 |
* @param inputSource The document's input source.
|
|
463 |
*
|
|
464 |
* @exception XMLConfigurationException Thrown if there is a
|
|
465 |
* configuration error when initializing the
|
|
466 |
* parser.
|
|
467 |
* @exception IOException Thrown on I/O error.
|
|
468 |
*
|
|
469 |
* @see #parse(boolean)
|
|
470 |
*/
|
|
471 |
public void setInputSource(XMLInputSource inputSource)
|
|
472 |
throws XMLConfigurationException, IOException {
|
|
473 |
|
|
474 |
// REVISIT: this method used to reset all the components and
|
|
475 |
// construct the pipeline. Now reset() is called
|
|
476 |
// in parse (boolean) just before we parse the document
|
|
477 |
// Should this method still throw exceptions..?
|
|
478 |
|
|
479 |
fInputSource = inputSource;
|
|
480 |
|
|
481 |
} // setInputSource(XMLInputSource)
|
|
482 |
|
|
483 |
/**
|
|
484 |
* Parses the document in a pull parsing fashion.
|
|
485 |
*
|
|
486 |
* @param complete True if the pull parser should parse the
|
|
487 |
* remaining document completely.
|
|
488 |
*
|
|
489 |
* @return True if there is more document to parse.
|
|
490 |
*
|
|
491 |
* @exception XNIException Any XNI exception, possibly wrapping
|
|
492 |
* another exception.
|
|
493 |
* @exception IOException An IO exception from the parser, possibly
|
|
494 |
* from a byte stream or character stream
|
|
495 |
* supplied by the parser.
|
|
496 |
*
|
|
497 |
* @see #setInputSource
|
|
498 |
*/
|
|
499 |
public boolean parse(boolean complete) throws XNIException, IOException {
|
|
500 |
//
|
|
501 |
// reset and configure pipeline and set InputSource.
|
|
502 |
if (fInputSource !=null) {
|
|
503 |
try {
|
|
504 |
// resets and sets the pipeline.
|
|
505 |
reset();
|
|
506 |
fScanner.setInputSource(fInputSource);
|
|
507 |
fInputSource = null;
|
|
508 |
}
|
|
509 |
catch (XNIException ex) {
|
|
510 |
if (PRINT_EXCEPTION_STACK_TRACE)
|
|
511 |
ex.printStackTrace();
|
|
512 |
throw ex;
|
|
513 |
}
|
|
514 |
catch (IOException ex) {
|
|
515 |
if (PRINT_EXCEPTION_STACK_TRACE)
|
|
516 |
ex.printStackTrace();
|
|
517 |
throw ex;
|
|
518 |
}
|
|
519 |
catch (RuntimeException ex) {
|
|
520 |
if (PRINT_EXCEPTION_STACK_TRACE)
|
|
521 |
ex.printStackTrace();
|
|
522 |
throw ex;
|
|
523 |
}
|
|
524 |
catch (Exception ex) {
|
|
525 |
if (PRINT_EXCEPTION_STACK_TRACE)
|
|
526 |
ex.printStackTrace();
|
|
527 |
throw new XNIException(ex);
|
|
528 |
}
|
|
529 |
}
|
|
530 |
|
|
531 |
try {
|
|
532 |
return fScanner.scanDocument(complete);
|
|
533 |
}
|
|
534 |
catch (XNIException ex) {
|
|
535 |
if (PRINT_EXCEPTION_STACK_TRACE)
|
|
536 |
ex.printStackTrace();
|
|
537 |
throw ex;
|
|
538 |
}
|
|
539 |
catch (IOException ex) {
|
|
540 |
if (PRINT_EXCEPTION_STACK_TRACE)
|
|
541 |
ex.printStackTrace();
|
|
542 |
throw ex;
|
|
543 |
}
|
|
544 |
catch (RuntimeException ex) {
|
|
545 |
if (PRINT_EXCEPTION_STACK_TRACE)
|
|
546 |
ex.printStackTrace();
|
|
547 |
throw ex;
|
|
548 |
}
|
|
549 |
catch (Exception ex) {
|
|
550 |
if (PRINT_EXCEPTION_STACK_TRACE)
|
|
551 |
ex.printStackTrace();
|
|
552 |
throw new XNIException(ex);
|
|
553 |
}
|
|
554 |
|
|
555 |
} // parse(boolean):boolean
|
|
556 |
|
|
557 |
/**
|
|
558 |
* If the application decides to terminate parsing before the xml document
|
|
559 |
* is fully parsed, the application should call this method to free any
|
|
560 |
* resource allocated during parsing. For example, close all opened streams.
|
|
561 |
*/
|
|
562 |
public void cleanup() {
|
|
563 |
fEntityManager.closeReaders();
|
|
564 |
}
|
|
565 |
|
|
566 |
//
|
|
567 |
// XMLParserConfiguration methods
|
|
568 |
//
|
|
569 |
|
|
570 |
/**
|
|
571 |
* Parses the specified input source.
|
|
572 |
*
|
|
573 |
* @param source The input source.
|
|
574 |
*
|
|
575 |
* @exception XNIException Throws exception on XNI error.
|
|
576 |
* @exception java.io.IOException Throws exception on i/o error.
|
|
577 |
*/
|
|
578 |
public void parse(XMLInputSource source) throws XNIException, IOException {
|
|
579 |
|
|
580 |
if (fParseInProgress) {
|
|
581 |
// REVISIT - need to add new error message
|
|
582 |
throw new XNIException("FWK005 parse may not be called while parsing.");
|
|
583 |
}
|
|
584 |
fParseInProgress = true;
|
|
585 |
|
|
586 |
try {
|
|
587 |
setInputSource(source);
|
|
588 |
parse(true);
|
|
589 |
}
|
|
590 |
catch (XNIException ex) {
|
|
591 |
if (PRINT_EXCEPTION_STACK_TRACE)
|
|
592 |
ex.printStackTrace();
|
|
593 |
throw ex;
|
|
594 |
}
|
|
595 |
catch (IOException ex) {
|
|
596 |
if (PRINT_EXCEPTION_STACK_TRACE)
|
|
597 |
ex.printStackTrace();
|
|
598 |
throw ex;
|
|
599 |
}
|
|
600 |
catch (RuntimeException ex) {
|
|
601 |
if (PRINT_EXCEPTION_STACK_TRACE)
|
|
602 |
ex.printStackTrace();
|
|
603 |
throw ex;
|
|
604 |
}
|
|
605 |
catch (Exception ex) {
|
|
606 |
if (PRINT_EXCEPTION_STACK_TRACE)
|
|
607 |
ex.printStackTrace();
|
|
608 |
throw new XNIException(ex);
|
|
609 |
}
|
|
610 |
finally {
|
|
611 |
fParseInProgress = false;
|
|
612 |
// close all streams opened by xerces
|
|
613 |
this.cleanup();
|
|
614 |
}
|
|
615 |
|
|
616 |
} // parse(InputSource)
|
|
617 |
|
|
618 |
//
|
|
619 |
// Protected methods
|
|
620 |
//
|
|
621 |
|
|
622 |
/**
|
|
623 |
* Reset all components before parsing.
|
|
624 |
*
|
|
625 |
* @throws XNIException Thrown if an error occurs during initialization.
|
|
626 |
*/
|
|
627 |
protected void reset() throws XNIException {
|
|
628 |
|
|
629 |
if (fValidationManager != null)
|
|
630 |
fValidationManager.reset();
|
|
631 |
// configure the pipeline and initialize the components
|
|
632 |
configurePipeline();
|
|
633 |
super.reset();
|
|
634 |
} // reset()
|
|
635 |
|
|
636 |
/** Configures the pipeline. */
|
|
637 |
protected void configurePipeline() {
|
|
638 |
|
|
639 |
// REVISIT: This should be better designed. In other words, we
|
|
640 |
// need to figure out what is the best way for people to
|
|
641 |
// re-use *most* of the standard configuration but do
|
|
642 |
// things common things such as remove a component (e.g.
|
|
643 |
// the validator), insert a new component (e.g. XInclude),
|
|
644 |
// etc... -Ac
|
|
645 |
|
|
646 |
// setup document pipeline
|
|
647 |
if (fDTDValidator != null) {
|
|
648 |
fScanner.setDocumentHandler(fDTDValidator);
|
|
649 |
if (fFeatures.get(NAMESPACES) == Boolean.TRUE) {
|
|
650 |
|
|
651 |
// filters
|
|
652 |
fDTDValidator.setDocumentHandler(fNamespaceBinder);
|
|
653 |
fDTDValidator.setDocumentSource(fScanner);
|
|
654 |
fNamespaceBinder.setDocumentHandler(fDocumentHandler);
|
|
655 |
fNamespaceBinder.setDocumentSource(fDTDValidator);
|
|
656 |
fLastComponent = fNamespaceBinder;
|
|
657 |
}
|
|
658 |
else {
|
|
659 |
fDTDValidator.setDocumentHandler(fDocumentHandler);
|
|
660 |
fDTDValidator.setDocumentSource(fScanner);
|
|
661 |
fLastComponent = fDTDValidator;
|
|
662 |
}
|
|
663 |
}
|
|
664 |
else {
|
|
665 |
if (fFeatures.get(NAMESPACES) == Boolean.TRUE) {
|
|
666 |
fScanner.setDocumentHandler(fNamespaceBinder);
|
|
667 |
fNamespaceBinder.setDocumentHandler(fDocumentHandler);
|
|
668 |
fNamespaceBinder.setDocumentSource(fScanner);
|
|
669 |
fLastComponent = fNamespaceBinder;
|
|
670 |
}
|
|
671 |
else {
|
|
672 |
fScanner.setDocumentHandler(fDocumentHandler);
|
|
673 |
fLastComponent = fScanner;
|
|
674 |
}
|
|
675 |
}
|
|
676 |
|
|
677 |
configureDTDPipeline();
|
|
678 |
} // configurePipeline()
|
|
679 |
|
|
680 |
protected void configureDTDPipeline (){
|
|
681 |
|
|
682 |
// setup dtd pipeline
|
|
683 |
if (fDTDScanner != null) {
|
|
684 |
fProperties.put(DTD_SCANNER, fDTDScanner);
|
|
685 |
if (fDTDProcessor != null) {
|
|
686 |
fProperties.put(DTD_PROCESSOR, fDTDProcessor);
|
|
687 |
fDTDScanner.setDTDHandler(fDTDProcessor);
|
|
688 |
fDTDProcessor.setDTDSource(fDTDScanner);
|
|
689 |
fDTDProcessor.setDTDHandler(fDTDHandler);
|
|
690 |
if (fDTDHandler != null) {
|
|
691 |
fDTDHandler.setDTDSource(fDTDProcessor);
|
|
692 |
}
|
|
693 |
|
|
694 |
fDTDScanner.setDTDContentModelHandler(fDTDProcessor);
|
|
695 |
fDTDProcessor.setDTDContentModelSource(fDTDScanner);
|
|
696 |
fDTDProcessor.setDTDContentModelHandler(fDTDContentModelHandler);
|
|
697 |
if (fDTDContentModelHandler != null) {
|
|
698 |
fDTDContentModelHandler.setDTDContentModelSource(fDTDProcessor);
|
|
699 |
}
|
|
700 |
}
|
|
701 |
else {
|
|
702 |
fDTDScanner.setDTDHandler(fDTDHandler);
|
|
703 |
if (fDTDHandler != null) {
|
|
704 |
fDTDHandler.setDTDSource(fDTDScanner);
|
|
705 |
}
|
|
706 |
fDTDScanner.setDTDContentModelHandler(fDTDContentModelHandler);
|
|
707 |
if (fDTDContentModelHandler != null) {
|
|
708 |
fDTDContentModelHandler.setDTDContentModelSource(fDTDScanner);
|
|
709 |
}
|
|
710 |
}
|
|
711 |
}
|
|
712 |
|
|
713 |
|
|
714 |
}
|
|
715 |
|
|
716 |
// features and properties
|
|
717 |
|
|
718 |
/**
|
|
719 |
* Check a feature. If feature is know and supported, this method simply
|
|
720 |
* returns. Otherwise, the appropriate exception is thrown.
|
|
721 |
*
|
|
722 |
* @param featureId The unique identifier (URI) of the feature.
|
|
723 |
*
|
|
724 |
* @throws XMLConfigurationException Thrown for configuration error.
|
|
725 |
* In general, components should
|
|
726 |
* only throw this exception if
|
|
727 |
* it is <strong>really</strong>
|
|
728 |
* a critical error.
|
|
729 |
*/
|
|
730 |
protected FeatureState checkFeature(String featureId)
|
|
731 |
throws XMLConfigurationException {
|
|
732 |
|
|
733 |
//
|
|
734 |
// Xerces Features
|
|
735 |
//
|
|
736 |
|
|
737 |
if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) {
|
|
738 |
final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length();
|
|
739 |
|
|
740 |
//
|
|
741 |
// http://apache.org/xml/features/validation/dynamic
|
|
742 |
// Allows the parser to validate a document only when it
|
|
743 |
// contains a grammar. Validation is turned on/off based
|
|
744 |
// on each document instance, automatically.
|
|
745 |
//
|
|
746 |
if (suffixLength == Constants.DYNAMIC_VALIDATION_FEATURE.length() &&
|
|
747 |
featureId.endsWith(Constants.DYNAMIC_VALIDATION_FEATURE)) {
|
|
748 |
return FeatureState.RECOGNIZED;
|
|
749 |
}
|
|
750 |
|
|
751 |
//
|
|
752 |
// http://apache.org/xml/features/validation/default-attribute-values
|
|
753 |
//
|
|
754 |
if (suffixLength == Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE.length() &&
|
|
755 |
featureId.endsWith(Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE)) {
|
|
756 |
// REVISIT
|
|
757 |
return FeatureState.NOT_SUPPORTED;
|
|
758 |
}
|
|
759 |
//
|
|
760 |
// http://apache.org/xml/features/validation/default-attribute-values
|
|
761 |
//
|
|
762 |
if (suffixLength == Constants.VALIDATE_CONTENT_MODELS_FEATURE.length() &&
|
|
763 |
featureId.endsWith(Constants.VALIDATE_CONTENT_MODELS_FEATURE)) {
|
|
764 |
// REVISIT
|
|
765 |
return FeatureState.NOT_SUPPORTED;
|
|
766 |
}
|
|
767 |
//
|
|
768 |
// http://apache.org/xml/features/validation/nonvalidating/load-dtd-grammar
|
|
769 |
//
|
|
770 |
if (suffixLength == Constants.LOAD_DTD_GRAMMAR_FEATURE.length() &&
|
|
771 |
featureId.endsWith(Constants.LOAD_DTD_GRAMMAR_FEATURE)) {
|
|
772 |
return FeatureState.RECOGNIZED;
|
|
773 |
}
|
|
774 |
//
|
|
775 |
// http://apache.org/xml/features/validation/nonvalidating/load-external-dtd
|
|
776 |
//
|
|
777 |
if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() &&
|
|
778 |
featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) {
|
|
779 |
return FeatureState.RECOGNIZED;
|
|
780 |
}
|
|
781 |
|
|
782 |
//
|
|
783 |
// http://apache.org/xml/features/validation/default-attribute-values
|
|
784 |
//
|
|
785 |
if (suffixLength == Constants.VALIDATE_DATATYPES_FEATURE.length() &&
|
|
786 |
featureId.endsWith(Constants.VALIDATE_DATATYPES_FEATURE)) {
|
|
787 |
return FeatureState.NOT_SUPPORTED;
|
|
788 |
}
|
|
789 |
}
|
|
790 |
|
|
791 |
//
|
|
792 |
// Not recognized
|
|
793 |
//
|
|
794 |
|
|
795 |
return super.checkFeature(featureId);
|
|
796 |
|
|
797 |
} // checkFeature(String)
|
|
798 |
|
|
799 |
/**
|
|
800 |
* Check a property. If the property is know and supported, this method
|
|
801 |
* simply returns. Otherwise, the appropriate exception is thrown.
|
|
802 |
*
|
|
803 |
* @param propertyId The unique identifier (URI) of the property
|
|
804 |
* being set.
|
|
805 |
*
|
|
806 |
* @throws XMLConfigurationException Thrown for configuration error.
|
|
807 |
* In general, components should
|
|
808 |
* only throw this exception if
|
|
809 |
* it is <strong>really</strong>
|
|
810 |
* a critical error.
|
|
811 |
*/
|
|
812 |
protected PropertyState checkProperty(String propertyId)
|
|
813 |
throws XMLConfigurationException {
|
|
814 |
|
|
815 |
//
|
|
816 |
// Xerces Properties
|
|
817 |
//
|
|
818 |
|
|
819 |
if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
|
|
820 |
final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length();
|
|
821 |
|
|
822 |
if (suffixLength == Constants.DTD_SCANNER_PROPERTY.length() &&
|
|
823 |
propertyId.endsWith(Constants.DTD_SCANNER_PROPERTY)) {
|
|
824 |
return PropertyState.RECOGNIZED;
|
|
825 |
}
|
|
826 |
}
|
|
827 |
|
|
828 |
//
|
|
829 |
// Not recognized
|
|
830 |
//
|
|
831 |
|
|
832 |
return super.checkProperty(propertyId);
|
|
833 |
|
|
834 |
} // checkProperty(String)
|
|
835 |
|
|
836 |
// factory methods
|
|
837 |
|
|
838 |
/** Creates an entity manager. */
|
|
839 |
protected XMLEntityManager createEntityManager() {
|
|
840 |
return new XMLEntityManager();
|
|
841 |
} // createEntityManager():XMLEntityManager
|
|
842 |
|
|
843 |
/** Creates an error reporter. */
|
|
844 |
protected XMLErrorReporter createErrorReporter() {
|
|
845 |
return new XMLErrorReporter();
|
|
846 |
} // createErrorReporter():XMLErrorReporter
|
|
847 |
|
|
848 |
/** Create a document scanner. */
|
|
849 |
protected XMLDocumentScanner createDocumentScanner() {
|
|
850 |
return new XMLDocumentScannerImpl();
|
|
851 |
} // createDocumentScanner():XMLDocumentScanner
|
|
852 |
|
|
853 |
/** Create a DTD scanner. */
|
|
854 |
protected XMLDTDScanner createDTDScanner() {
|
|
855 |
return new XMLDTDScannerImpl();
|
|
856 |
} // createDTDScanner():XMLDTDScanner
|
|
857 |
|
|
858 |
/** Create a DTD loader . */
|
|
859 |
protected XMLDTDProcessor createDTDProcessor() {
|
|
860 |
return new XMLDTDProcessor();
|
|
861 |
} // createDTDProcessor():XMLDTDProcessor
|
|
862 |
|
|
863 |
/** Create a DTD validator. */
|
|
864 |
protected XMLDTDValidator createDTDValidator() {
|
|
865 |
return new XMLDTDValidator();
|
|
866 |
} // createDTDValidator():XMLDTDValidator
|
|
867 |
|
|
868 |
/** Create a namespace binder. */
|
|
869 |
protected XMLNamespaceBinder createNamespaceBinder() {
|
|
870 |
return new XMLNamespaceBinder();
|
|
871 |
} // createNamespaceBinder():XMLNamespaceBinder
|
|
872 |
|
|
873 |
/** Create a datatype validator factory. */
|
|
874 |
protected DTDDVFactory createDatatypeValidatorFactory() {
|
|
875 |
return DTDDVFactory.getInstance();
|
|
876 |
} // createDatatypeValidatorFactory():DatatypeValidatorFactory
|
|
877 |
protected ValidationManager createValidationManager(){
|
|
878 |
return new ValidationManager();
|
|
879 |
}
|
|
880 |
|
|
881 |
} // class DTDConfiguration
|