KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > dom > DOMConfigurationImpl


1 /*
2  * Copyright 2001-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.dom;
18
19 import java.io.IOException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.w3c.dom.DOMConfiguration JavaDoc;
26 import org.w3c.dom.DOMErrorHandler JavaDoc;
27 import org.w3c.dom.DOMStringList JavaDoc;
28
29 import org.apache.xerces.impl.Constants;
30 import org.apache.xerces.impl.XMLEntityManager;
31 import org.apache.xerces.impl.XMLErrorReporter;
32 import org.apache.xerces.impl.dv.DTDDVFactory;
33 import org.apache.xerces.impl.msg.XMLMessageFormatter;
34 import org.apache.xerces.impl.validation.ValidationManager;
35 import org.apache.xerces.util.DOMEntityResolverWrapper;
36 import org.apache.xerces.util.DOMErrorHandlerWrapper;
37 import org.apache.xerces.util.MessageFormatter;
38 import org.apache.xerces.util.ParserConfigurationSettings;
39 import org.apache.xerces.util.SymbolTable;
40 import org.apache.xerces.xni.XMLDTDContentModelHandler;
41 import org.apache.xerces.xni.XMLDTDHandler;
42 import org.apache.xerces.xni.XMLDocumentHandler;
43 import org.apache.xerces.xni.XNIException;
44 import org.apache.xerces.xni.grammars.XMLGrammarPool;
45 import org.apache.xerces.xni.parser.XMLComponent;
46 import org.apache.xerces.xni.parser.XMLComponentManager;
47 import org.apache.xerces.xni.parser.XMLConfigurationException;
48 import org.apache.xerces.xni.parser.XMLEntityResolver;
49 import org.apache.xerces.xni.parser.XMLErrorHandler;
50 import org.apache.xerces.xni.parser.XMLInputSource;
51 import org.apache.xerces.xni.parser.XMLParserConfiguration;
52 import org.w3c.dom.DOMException JavaDoc;
53 import org.w3c.dom.ls.LSResourceResolver JavaDoc;
54
55
56
57 /**
58  * Xerces implementation of DOMConfiguration that maintains a table of recognized parameters.
59  *
60  * @xerces.internal
61  *
62  * @author Elena Litani, IBM
63  * @author Neeraj Bajaj, Sun Microsystems.
64  * @version $Id: DOMConfigurationImpl.java,v 1.36 2005/07/25 04:24:21 mrglavas Exp $
65  */

66 public class DOMConfigurationImpl extends ParserConfigurationSettings
67     implements XMLParserConfiguration, DOMConfiguration JavaDoc {
68
69     //
70
// Constants
71
//
72

73     // feature identifiers
74

75     /** Feature identifier: validation. */
76     protected static final String JavaDoc XERCES_VALIDATION =
77         Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE;
78
79     /** Feature identifier: namespaces. */
80     protected static final String JavaDoc XERCES_NAMESPACES =
81         Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE;
82
83     protected static final String JavaDoc SCHEMA =
84         Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE;
85     
86     protected static final String JavaDoc SCHEMA_FULL_CHECKING =
87         Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING;
88
89     protected static final String JavaDoc DYNAMIC_VALIDATION =
90         Constants.XERCES_FEATURE_PREFIX + Constants.DYNAMIC_VALIDATION_FEATURE;
91
92     protected static final String JavaDoc NORMALIZE_DATA =
93         Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE;
94
95     /** sending psvi in the pipeline */
96     protected static final String JavaDoc SEND_PSVI =
97         Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_AUGMENT_PSVI;
98
99     protected final static String JavaDoc DTD_VALIDATOR_FACTORY_PROPERTY =
100         Constants.XERCES_PROPERTY_PREFIX + Constants.DATATYPE_VALIDATOR_FACTORY_PROPERTY;
101     
102     // property identifiers
103

104     /** Property identifier: entity manager. */
105     protected static final String JavaDoc ENTITY_MANAGER =
106         Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
107
108     /** Property identifier: error reporter. */
109     protected static final String JavaDoc ERROR_REPORTER =
110         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
111
112     /** Property identifier: xml string. */
113     protected static final String JavaDoc XML_STRING =
114         Constants.SAX_PROPERTY_PREFIX + Constants.XML_STRING_PROPERTY;
115
116     /** Property identifier: symbol table. */
117     protected static final String JavaDoc SYMBOL_TABLE =
118         Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
119
120     /** Property id: Grammar pool*/
121     protected static final String JavaDoc GRAMMAR_POOL =
122     Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
123
124     /** Property identifier: error handler. */
125     protected static final String JavaDoc ERROR_HANDLER =
126         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY;
127
128     /** Property identifier: entity resolver. */
129     protected static final String JavaDoc ENTITY_RESOLVER =
130         Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY;
131
132     /** Property identifier: JAXP schema language / DOM schema-type. */
133     protected static final String JavaDoc JAXP_SCHEMA_LANGUAGE =
134     Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE;
135
136     /** Property identifier: JAXP schema source/ DOM schema-location. */
137     protected static final String JavaDoc JAXP_SCHEMA_SOURCE =
138     Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE;
139
140     protected static final String JavaDoc VALIDATION_MANAGER =
141         Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY;
142     //
143
// Data
144
//
145
XMLDocumentHandler fDocumentHandler;
146
147     /** Normalization features*/
148     protected short features = 0;
149
150     protected final static short NAMESPACES = 0x1<<0;
151     protected final static short DTNORMALIZATION = 0x1<<1;
152     protected final static short ENTITIES = 0x1<<2;
153     protected final static short CDATA = 0x1<<3;
154     protected final static short SPLITCDATA = 0x1<<4;
155     protected final static short COMMENTS = 0x1<<5;
156     protected final static short VALIDATE = 0x1<<6;
157     protected final static short PSVI = 0x1<<7;
158     protected final static short WELLFORMED = 0x1<<8;
159     protected final static short NSDECL = 0x1<<9;
160
161     protected final static short INFOSET_TRUE_PARAMS = NAMESPACES | COMMENTS | WELLFORMED | NSDECL;
162     protected final static short INFOSET_FALSE_PARAMS = ENTITIES | DTNORMALIZATION | CDATA;
163     protected final static short INFOSET_MASK = INFOSET_TRUE_PARAMS | INFOSET_FALSE_PARAMS;
164
165     // components
166

167     /** Symbol table. */
168     protected SymbolTable fSymbolTable;
169
170     /** Components. */
171     protected ArrayList JavaDoc fComponents;
172
173     protected ValidationManager fValidationManager;
174
175     /** Locale. */
176     protected Locale JavaDoc fLocale;
177
178     /** Error reporter */
179     protected XMLErrorReporter fErrorReporter;
180
181     protected final DOMErrorHandlerWrapper fErrorHandlerWrapper =
182                 new DOMErrorHandlerWrapper();
183
184     // private data
185

186     private DOMStringList JavaDoc fRecognizedParameters;
187
188
189     //
190
// Constructors
191
//
192

193     /** Default Constructor. */
194     protected DOMConfigurationImpl() {
195         this(null, null);
196     } // <init>()
197

198     /**
199      * Constructs a parser configuration using the specified symbol table.
200      *
201      * @param symbolTable The symbol table to use.
202      */

203     protected DOMConfigurationImpl(SymbolTable symbolTable) {
204         this(symbolTable, null);
205     } // <init>(SymbolTable)
206

207     /**
208      * Constructs a parser configuration using the specified symbol table
209      * and parent settings.
210      *
211      * @param symbolTable The symbol table to use.
212      * @param parentSettings The parent settings.
213      */

214     protected DOMConfigurationImpl(SymbolTable symbolTable,
215                                     XMLComponentManager parentSettings) {
216         super(parentSettings);
217
218         // create storage for recognized features and properties
219
fRecognizedFeatures = new ArrayList JavaDoc();
220         fRecognizedProperties = new ArrayList JavaDoc();
221
222         // create table for features and properties
223
fFeatures = new HashMap JavaDoc();
224         fProperties = new HashMap JavaDoc();
225
226         // add default recognized features
227
final String JavaDoc[] recognizedFeatures = {
228             XERCES_VALIDATION,
229             XERCES_NAMESPACES,
230             SCHEMA,
231             SCHEMA_FULL_CHECKING,
232             DYNAMIC_VALIDATION,
233             NORMALIZE_DATA,
234             SEND_PSVI,
235         };
236         addRecognizedFeatures(recognizedFeatures);
237
238         // set state for default features
239
setFeature(XERCES_VALIDATION, false);
240         setFeature(SCHEMA, false);
241         setFeature(SCHEMA_FULL_CHECKING, false);
242         setFeature(DYNAMIC_VALIDATION, false);
243         setFeature(NORMALIZE_DATA, false);
244         setFeature(XERCES_NAMESPACES, true);
245         setFeature(SEND_PSVI, true);
246
247         // add default recognized properties
248
final String JavaDoc[] recognizedProperties = {
249             XML_STRING,
250             SYMBOL_TABLE,
251             ERROR_HANDLER,
252             ENTITY_RESOLVER,
253             ERROR_REPORTER,
254             ENTITY_MANAGER,
255             VALIDATION_MANAGER,
256             GRAMMAR_POOL,
257             JAXP_SCHEMA_SOURCE,
258             JAXP_SCHEMA_LANGUAGE,
259             DTD_VALIDATOR_FACTORY_PROPERTY
260         };
261         addRecognizedProperties(recognizedProperties);
262
263         // set default values for normalization features
264
features |= NAMESPACES;
265         features |= ENTITIES;
266         features |= COMMENTS;
267         features |= CDATA;
268         features |= SPLITCDATA;
269         features |= WELLFORMED;
270         features |= NSDECL;
271
272         if (symbolTable == null) {
273             symbolTable = new SymbolTable();
274         }
275         fSymbolTable = symbolTable;
276
277         fComponents = new ArrayList JavaDoc();
278
279         setProperty(SYMBOL_TABLE, fSymbolTable);
280         fErrorReporter = new XMLErrorReporter();
281         setProperty(ERROR_REPORTER, fErrorReporter);
282         addComponent(fErrorReporter);
283
284         setProperty(DTD_VALIDATOR_FACTORY_PROPERTY, DTDDVFactory.getInstance());
285         
286         XMLEntityManager manager = new XMLEntityManager();
287         setProperty(ENTITY_MANAGER, manager);
288         addComponent(manager);
289
290         fValidationManager = createValidationManager();
291         setProperty(VALIDATION_MANAGER, fValidationManager);
292
293
294         // add message formatters
295
if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) {
296             XMLMessageFormatter xmft = new XMLMessageFormatter();
297             fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft);
298             fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft);
299         }
300
301         // REVISIT: try to include XML Schema formatter.
302
// This is a hack to allow DTD configuration to be build.
303
//
304
if (fErrorReporter.getMessageFormatter("http://www.w3.org/TR/xml-schema-1") == null) {
305             MessageFormatter xmft = null;
306             try {
307                xmft = (MessageFormatter)(
308                     ObjectFactory.newInstance("org.apache.xerces.impl.xs.XSMessageFormatter",
309                     ObjectFactory.findClassLoader(), true));
310             } catch (Exception JavaDoc exception){
311             }
312
313              if (xmft != null) {
314                  fErrorReporter.putMessageFormatter("http://www.w3.org/TR/xml-schema-1", xmft);
315              }
316         }
317
318
319         // set locale
320
try {
321             setLocale(Locale.getDefault());
322         }
323         catch (XNIException e) {
324             // do nothing
325
// REVISIT: What is the right thing to do? -Ac
326
}
327
328
329     } // <init>(SymbolTable)
330

331
332     //
333
// XMLParserConfiguration methods
334
//
335

336     /**
337      * Parse an XML document.
338      * <p>
339      * The parser can use this method to instruct this configuration
340      * to begin parsing an XML document from any valid input source
341      * (a character stream, a byte stream, or a URI).
342      * <p>
343      * Parsers may not invoke this method while a parse is in progress.
344      * Once a parse is complete, the parser may then parse another XML
345      * document.
346      * <p>
347      * This method is synchronous: it will not return until parsing
348      * has ended. If a client application wants to terminate
349      * parsing early, it should throw an exception.
350      *
351      * @param source The input source for the top-level of the
352      * XML document.
353      *
354      * @exception XNIException Any XNI exception, possibly wrapping
355      * another exception.
356      * @exception IOException An IO exception from the parser, possibly
357      * from a byte stream or character stream
358      * supplied by the parser.
359      */

360     public void parse(XMLInputSource inputSource)
361         throws XNIException, IOException JavaDoc{
362         // no-op
363
}
364
365     /**
366      * Sets the document handler on the last component in the pipeline
367      * to receive information about the document.
368      *
369      * @param documentHandler The document handler.
370      */

371     public void setDocumentHandler(XMLDocumentHandler documentHandler) {
372         fDocumentHandler = documentHandler;
373     } // setDocumentHandler(XMLDocumentHandler)
374

375     /** Returns the registered document handler. */
376     public XMLDocumentHandler getDocumentHandler() {
377         return fDocumentHandler;
378     } // getDocumentHandler():XMLDocumentHandler
379

380     /**
381      * Sets the DTD handler.
382      *
383      * @param dtdHandler The DTD handler.
384      */

385     public void setDTDHandler(XMLDTDHandler dtdHandler) {
386         //no-op
387
} // setDTDHandler(XMLDTDHandler)
388

389     /** Returns the registered DTD handler. */
390     public XMLDTDHandler getDTDHandler() {
391         return null;
392     } // getDTDHandler():XMLDTDHandler
393

394     /**
395      * Sets the DTD content model handler.
396      *
397      * @param handler The DTD content model handler.
398      */

399     public void setDTDContentModelHandler(XMLDTDContentModelHandler handler) {
400         //no-op
401

402     } // setDTDContentModelHandler(XMLDTDContentModelHandler)
403

404     /** Returns the registered DTD content model handler. */
405     public XMLDTDContentModelHandler getDTDContentModelHandler() {
406         return null;
407     } // getDTDContentModelHandler():XMLDTDContentModelHandler
408

409     /**
410      * Sets the resolver used to resolve external entities. The EntityResolver
411      * interface supports resolution of public and system identifiers.
412      *
413      * @param resolver The new entity resolver. Passing a null value will
414      * uninstall the currently installed resolver.
415      */

416     public void setEntityResolver(XMLEntityResolver resolver) {
417         if (resolver !=null) {
418             fProperties.put(ENTITY_RESOLVER, resolver);
419         }
420     } // setEntityResolver(XMLEntityResolver)
421

422     /**
423      * Return the current entity resolver.
424      *
425      * @return The current entity resolver, or null if none
426      * has been registered.
427      * @see #setEntityResolver
428      */

429     public XMLEntityResolver getEntityResolver() {
430         return (XMLEntityResolver)fProperties.get(ENTITY_RESOLVER);
431     } // getEntityResolver():XMLEntityResolver
432

433     /**
434      * Allow an application to register an error event handler.
435      *
436      * <p>If the application does not register an error handler, all
437      * error events reported by the SAX parser will be silently
438      * ignored; however, normal processing may not continue. It is
439      * highly recommended that all SAX applications implement an
440      * error handler to avoid unexpected bugs.</p>
441      *
442      * <p>Applications may register a new or different handler in the
443      * middle of a parse, and the SAX parser must begin using the new
444      * handler immediately.</p>
445      *
446      * @param errorHandler The error handler.
447      * @exception java.lang.NullPointerException If the handler
448      * argument is null.
449      * @see #getErrorHandler
450      */

451     public void setErrorHandler(XMLErrorHandler errorHandler) {
452         if (errorHandler != null) {
453             fProperties.put(ERROR_HANDLER, errorHandler);
454         }
455     } // setErrorHandler(XMLErrorHandler)
456

457     /**
458      * Return the current error handler.
459      *
460      * @return The current error handler, or null if none
461      * has been registered.
462      * @see #setErrorHandler
463      */

464     public XMLErrorHandler getErrorHandler() {
465         return (XMLErrorHandler)fProperties.get(ERROR_HANDLER);
466     } // getErrorHandler():XMLErrorHandler
467

468     /**
469      * Set the state of a feature.
470      *
471      * Set the state of any feature in a SAX2 parser. The parser
472      * might not recognize the feature, and if it does recognize
473      * it, it might not be able to fulfill the request.
474      *
475      * @param featureId The unique identifier (URI) of the feature.
476      * @param state The requested state of the feature (true or false).
477      *
478      * @exception org.apache.xerces.xni.parser.XMLConfigurationException If the
479      * requested feature is not known.
480      */

481     public void setFeature(String JavaDoc featureId, boolean state)
482         throws XMLConfigurationException {
483
484         // save state if noone "objects"
485
super.setFeature(featureId, state);
486
487     } // setFeature(String,boolean)
488

489     /**
490      * setProperty
491      *
492      * @param propertyId
493      * @param value
494      */

495     public void setProperty(String JavaDoc propertyId, Object JavaDoc value)
496         throws XMLConfigurationException {
497
498         // store value if noone "objects"
499
super.setProperty(propertyId, value);
500
501     } // setProperty(String,Object)
502

503     /**
504      * Set the locale to use for messages.
505      *
506      * @param locale The locale object to use for localization of messages.
507      *
508      * @exception XNIException Thrown if the parser does not support the
509      * specified locale.
510      */

511     public void setLocale(Locale JavaDoc locale) throws XNIException {
512         fLocale = locale;
513         fErrorReporter.setLocale(locale);
514
515     } // setLocale(Locale)
516

517     /** Returns the locale. */
518     public Locale JavaDoc getLocale() {
519         return fLocale;
520     } // getLocale():Locale
521

522     /**
523      * DOM Level 3 WD - Experimental.
524      * setParameter
525      */

526     public void setParameter(String JavaDoc name, Object JavaDoc value) throws DOMException JavaDoc {
527         boolean found = true;
528
529         // REVISIT: Recognizes DOM L3 default features only.
530
// Does not yet recognize Xerces features.
531
if(value instanceof Boolean JavaDoc){
532             boolean state = ((Boolean JavaDoc)value).booleanValue();
533
534             if (name.equalsIgnoreCase(Constants.DOM_COMMENTS)) {
535                 features = (short) (state ? features | COMMENTS : features & ~COMMENTS);
536             }
537             else if (name.equalsIgnoreCase(Constants.DOM_DATATYPE_NORMALIZATION)) {
538                 setFeature(NORMALIZE_DATA, state);
539                 features =
540                     (short) (state ? features | DTNORMALIZATION : features & ~DTNORMALIZATION);
541                 if (state) {
542                     features = (short) (features | VALIDATE);
543                 }
544             }
545             else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACES)) {
546                 features = (short) (state ? features | NAMESPACES : features & ~NAMESPACES);
547             }
548             else if (name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)) {
549                 features = (short) (state ? features | CDATA : features & ~CDATA);
550             }
551             else if (name.equalsIgnoreCase(Constants.DOM_ENTITIES)) {
552                 features = (short) (state ? features | ENTITIES : features & ~ENTITIES);
553             }
554             else if (name.equalsIgnoreCase(Constants.DOM_SPLIT_CDATA)) {
555                 features = (short) (state ? features | SPLITCDATA : features & ~SPLITCDATA);
556             }
557             else if (name.equalsIgnoreCase(Constants.DOM_VALIDATE)) {
558                 features = (short) (state ? features | VALIDATE : features & ~VALIDATE);
559             }
560             else if (name.equalsIgnoreCase(Constants.DOM_WELLFORMED)) {
561                 features = (short) (state ? features | WELLFORMED : features & ~WELLFORMED );
562             }
563             else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS)) {
564                 features = (short) (state ? features | NSDECL : features & ~NSDECL);
565             }
566             else if (name.equalsIgnoreCase(Constants.DOM_INFOSET)) {
567                 // Setting to false has no effect.
568
if (state) {
569                     features = (short) (features | INFOSET_TRUE_PARAMS);
570                     features = (short) (features & ~INFOSET_FALSE_PARAMS);
571                     setFeature(NORMALIZE_DATA, false);
572                 }
573             }
574             else if (name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS)
575                     || name.equalsIgnoreCase(Constants.DOM_CANONICAL_FORM)
576                     || name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA)
577                     || name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION)
578                     ) {
579                 if (state) { // true is not supported
580
String JavaDoc msg =
581                         DOMMessageFormatter.formatMessage(
582                             DOMMessageFormatter.DOM_DOMAIN,
583                             "FEATURE_NOT_SUPPORTED",
584                             new Object JavaDoc[] { name });
585                     throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
586                 }
587             }
588             else if ( name.equalsIgnoreCase(Constants.DOM_ELEMENT_CONTENT_WHITESPACE)) {
589                 if (!state) { // false is not supported
590
String JavaDoc msg =
591                         DOMMessageFormatter.formatMessage(
592                             DOMMessageFormatter.DOM_DOMAIN,
593                             "FEATURE_NOT_SUPPORTED",
594                             new Object JavaDoc[] { name });
595                    throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
596                 }
597             }
598             else if (name.equalsIgnoreCase(SEND_PSVI) ){
599                 // REVISIT: turning augmentation of PSVI is not support,
600
// because in this case we won't be able to retrieve element
601
// default value.
602
if (!state) { // false is not supported
603
String JavaDoc msg =
604                         DOMMessageFormatter.formatMessage(
605                             DOMMessageFormatter.DOM_DOMAIN,
606                             "FEATURE_NOT_SUPPORTED",
607                             new Object JavaDoc[] { name });
608                     throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
609                 }
610             }
611             else if (name.equalsIgnoreCase(Constants.DOM_PSVI)){
612                   features = (short) (state ? features | PSVI : features & ~PSVI);
613             }
614             else {
615                 found = false;
616                 /*
617                 String msg =
618                     DOMMessageFormatter.formatMessage(
619                         DOMMessageFormatter.DOM_DOMAIN,
620                         "FEATURE_NOT_FOUND",
621                         new Object[] { name });
622                 throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
623                 */

624             }
625             
626         }
627         
628         if (!found || !(value instanceof Boolean JavaDoc)) { // set properties
629
found = true;
630
631             if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER)) {
632                 if (value instanceof DOMErrorHandler JavaDoc || value == null) {
633                     fErrorHandlerWrapper.setErrorHandler((DOMErrorHandler JavaDoc)value);
634                     setErrorHandler(fErrorHandlerWrapper);
635                 }
636
637                 else {
638                     // REVISIT: type mismatch
639
String JavaDoc msg =
640                         DOMMessageFormatter.formatMessage(
641                             DOMMessageFormatter.DOM_DOMAIN,
642                             "TYPE_MISMATCH_ERR",
643                             new Object JavaDoc[] { name });
644                     throw new DOMException JavaDoc(DOMException.TYPE_MISMATCH_ERR, msg);
645                 }
646             }
647             else if (name.equalsIgnoreCase(Constants.DOM_RESOURCE_RESOLVER)) {
648                 if (value instanceof LSResourceResolver JavaDoc || value == null) {
649                     try {
650                         setEntityResolver(new DOMEntityResolverWrapper((LSResourceResolver JavaDoc) value));
651                     }
652                     catch (XMLConfigurationException e) {}
653                 }
654                 else {
655                     // REVISIT: type mismatch
656
String JavaDoc msg =
657                         DOMMessageFormatter.formatMessage(
658                             DOMMessageFormatter.DOM_DOMAIN,
659                             "TYPE_MISMATCH_ERR",
660                             new Object JavaDoc[] { name });
661                     throw new DOMException JavaDoc(DOMException.TYPE_MISMATCH_ERR, msg);
662                 }
663
664             }
665             else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_LOCATION)) {
666                 if (value instanceof String JavaDoc || value == null) {
667                     try {
668                         // map DOM schema-location to JAXP schemaSource property
669
setProperty(
670                             Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE,
671                             value);
672                     }
673                     catch (XMLConfigurationException e) {}
674                 }
675                 else {
676                     // REVISIT: type mismatch
677
String JavaDoc msg =
678                         DOMMessageFormatter.formatMessage(
679                             DOMMessageFormatter.DOM_DOMAIN,
680                             "TYPE_MISMATCH_ERR",
681                             new Object JavaDoc[] { name });
682                     throw new DOMException JavaDoc(DOMException.TYPE_MISMATCH_ERR, msg);
683                 }
684
685             }
686             else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_TYPE)) {
687                 if (value instanceof String JavaDoc || value == null) {
688                     try {
689                         if (value == null) {
690                             setProperty(
691                                 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE,
692                                 null);
693                         }
694                         else if (value.equals(Constants.NS_XMLSCHEMA)) {
695                             // REVISIT: when add support to DTD validation
696
setProperty(
697                                 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE,
698                                 Constants.NS_XMLSCHEMA);
699                         }
700                         else if (value.equals(Constants.NS_DTD)) {
701                             // Added support for revalidation against DTDs
702
setProperty(Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE,
703                                     Constants.NS_DTD);
704                         }
705                     }
706                     catch (XMLConfigurationException e) {}
707                 }
708                 else {
709                     String JavaDoc msg =
710                         DOMMessageFormatter.formatMessage(
711                             DOMMessageFormatter.DOM_DOMAIN,
712                             "TYPE_MISMATCH_ERR",
713                             new Object JavaDoc[] { name });
714                     throw new DOMException JavaDoc(DOMException.TYPE_MISMATCH_ERR, msg);
715                 }
716
717             }
718             else if (name.equalsIgnoreCase(SYMBOL_TABLE)){
719                 // Xerces Symbol Table
720
if (value instanceof SymbolTable){
721                     setProperty(SYMBOL_TABLE, value);
722                 }
723                 else {
724                     // REVISIT: type mismatch
725
String JavaDoc msg =
726                         DOMMessageFormatter.formatMessage(
727                             DOMMessageFormatter.DOM_DOMAIN,
728                             "TYPE_MISMATCH_ERR",
729                             new Object JavaDoc[] { name });
730                     throw new DOMException JavaDoc(DOMException.TYPE_MISMATCH_ERR, msg);
731                 }
732             }
733             else if (name.equalsIgnoreCase (GRAMMAR_POOL)){
734                 if (value instanceof XMLGrammarPool){
735                     setProperty(GRAMMAR_POOL, value);
736                 }
737                 else {
738                     // REVISIT: type mismatch
739
String JavaDoc msg =
740                         DOMMessageFormatter.formatMessage(
741                             DOMMessageFormatter.DOM_DOMAIN,
742                             "TYPE_MISMATCH_ERR",
743                             new Object JavaDoc[] { name });
744                     throw new DOMException JavaDoc(DOMException.TYPE_MISMATCH_ERR, msg);
745                 }
746
747             }
748             else {
749                 // REVISIT: check if this is a boolean parameter -- type mismatch should be thrown.
750
//parameter is not recognized
751
String JavaDoc msg =
752                     DOMMessageFormatter.formatMessage(
753                         DOMMessageFormatter.DOM_DOMAIN,
754                         "FEATURE_NOT_FOUND",
755                         new Object JavaDoc[] { name });
756                 throw new DOMException JavaDoc(DOMException.NOT_FOUND_ERR, msg);
757             }
758         }
759
760     }
761
762
763     /**
764      * DOM Level 3 WD - Experimental.
765      * getParameter
766      */

767     public Object JavaDoc getParameter(String JavaDoc name) throws DOMException JavaDoc {
768
769         // REVISIT: Recognizes DOM L3 default features only.
770
// Does not yet recognize Xerces features.
771

772         if (name.equalsIgnoreCase(Constants.DOM_COMMENTS)) {
773             return ((features & COMMENTS) != 0) ? Boolean.TRUE : Boolean.FALSE;
774         }
775         else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACES)) {
776             return (features & NAMESPACES) != 0 ? Boolean.TRUE : Boolean.FALSE;
777         }
778         else if (name.equalsIgnoreCase(Constants.DOM_DATATYPE_NORMALIZATION)) {
779             // REVISIT: datatype-normalization only takes effect if validation is on
780
return (features & DTNORMALIZATION) != 0 ? Boolean.TRUE : Boolean.FALSE;
781         }
782         else if (name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)) {
783             return (features & CDATA) != 0 ? Boolean.TRUE : Boolean.FALSE;
784         }
785         else if (name.equalsIgnoreCase(Constants.DOM_ENTITIES)) {
786             return (features & ENTITIES) != 0 ? Boolean.TRUE : Boolean.FALSE;
787         }
788         else if (name.equalsIgnoreCase(Constants.DOM_SPLIT_CDATA)) {
789             return (features & SPLITCDATA) != 0 ? Boolean.TRUE : Boolean.FALSE;
790         }
791         else if (name.equalsIgnoreCase(Constants.DOM_VALIDATE)) {
792             return (features & VALIDATE) != 0 ? Boolean.TRUE : Boolean.FALSE;
793         }
794         else if (name.equalsIgnoreCase(Constants.DOM_WELLFORMED)) {
795             return (features & WELLFORMED) != 0 ? Boolean.TRUE : Boolean.FALSE;
796         }
797         else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS)) {
798             return (features & NSDECL) != 0 ? Boolean.TRUE : Boolean.FALSE;
799         }
800         else if (name.equalsIgnoreCase(Constants.DOM_INFOSET)) {
801             return (features & INFOSET_MASK) == INFOSET_TRUE_PARAMS ? Boolean.TRUE : Boolean.FALSE;
802         }
803         else if (name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS)
804                 || name.equalsIgnoreCase(Constants.DOM_CANONICAL_FORM)
805                 || name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA)
806                 || name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION)
807                 ) {
808             return Boolean.FALSE;
809         }
810         else if (name.equalsIgnoreCase(SEND_PSVI)) {
811             return Boolean.TRUE;
812         }
813         else if (name.equalsIgnoreCase(Constants.DOM_PSVI)) {
814             return (features & PSVI) != 0 ? Boolean.TRUE : Boolean.FALSE;
815         }
816         else if (name.equalsIgnoreCase(Constants.DOM_ELEMENT_CONTENT_WHITESPACE)) {
817             return Boolean.TRUE;
818         }
819         else if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER)) {
820             return fErrorHandlerWrapper.getErrorHandler();
821         }
822         else if (name.equalsIgnoreCase(Constants.DOM_RESOURCE_RESOLVER)) {
823             XMLEntityResolver entityResolver = getEntityResolver();
824             if (entityResolver != null && entityResolver instanceof DOMEntityResolverWrapper) {
825                 return ((DOMEntityResolverWrapper) entityResolver).getEntityResolver();
826             }
827             return null;
828         }
829         else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_TYPE)) {
830             return getProperty(Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE);
831         }
832         else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_LOCATION)) {
833             return getProperty(Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE);
834         }
835         else if (name.equalsIgnoreCase(SYMBOL_TABLE)){
836             return getProperty(SYMBOL_TABLE);
837         }
838         else if (name.equalsIgnoreCase(GRAMMAR_POOL)){
839             return getProperty(GRAMMAR_POOL);
840         }
841         else {
842             String JavaDoc msg =
843                 DOMMessageFormatter.formatMessage(
844                     DOMMessageFormatter.DOM_DOMAIN,
845                     "FEATURE_NOT_FOUND",
846                     new Object JavaDoc[] { name });
847             throw new DOMException JavaDoc(DOMException.NOT_FOUND_ERR, msg);
848         }
849
850     }
851
852     /**
853      * DOM Level 3 WD - Experimental.
854      * Check if setting a parameter to a specific value is supported.
855      *
856      * @param name The name of the parameter to check.
857      *
858      * @param value An object. if null, the returned value is true.
859      *
860      * @return true if the parameter could be successfully set to the
861      * specified value, or false if the parameter is not recognized or
862      * the requested value is not supported. This does not change the
863      * current value of the parameter itself.
864      */

865     public boolean canSetParameter(String JavaDoc name, Object JavaDoc value) {
866
867         if (value == null){
868             //if null, the returned value is true.
869
//REVISIT: I dont like this --- even for unrecognized parameter it would
870
//return 'true'. I think it should return false in that case.
871
// Application will be surprised to find that setParameter throws not
872
//recognized exception when canSetParameter returns 'true' Then what is the use
873
//of having canSetParameter ??? - nb.
874
return true ;
875         }
876         if( value instanceof Boolean JavaDoc ){
877             //features whose parameter value can be set either 'true' or 'false'
878
// or they accept any boolean value -- so we just need to check that
879
// its a boolean value..
880
if (name.equalsIgnoreCase(Constants.DOM_COMMENTS)
881                 || name.equalsIgnoreCase(Constants.DOM_DATATYPE_NORMALIZATION)
882                 || name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)
883                 || name.equalsIgnoreCase(Constants.DOM_ENTITIES)
884                 || name.equalsIgnoreCase(Constants.DOM_SPLIT_CDATA)
885                 || name.equalsIgnoreCase(Constants.DOM_NAMESPACES)
886                 || name.equalsIgnoreCase(Constants.DOM_VALIDATE)
887                 || name.equalsIgnoreCase(Constants.DOM_WELLFORMED)
888                 || name.equalsIgnoreCase(Constants.DOM_INFOSET)
889                 || name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS)
890                 ) {
891                 return true;
892             }//features whose parameter value can not be set to 'true'
893
else if (
894                 name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS)
895                     || name.equalsIgnoreCase(Constants.DOM_CANONICAL_FORM)
896                     || name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA)
897                     || name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION)
898                     ) {
899                     return (value.equals(Boolean.TRUE)) ? false : true;
900             }//features whose parameter value can not be set to 'false'
901
else if( name.equalsIgnoreCase(Constants.DOM_ELEMENT_CONTENT_WHITESPACE)
902                     || name.equalsIgnoreCase(SEND_PSVI)
903                     ) {
904                     return (value.equals(Boolean.TRUE)) ? true : false;
905             }// if name is not among the above listed above -- its not recognized. return false
906
else {
907                 return false ;
908             }
909         }
910         else if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER)) {
911             return (value instanceof DOMErrorHandler JavaDoc) ? true : false ;
912         }
913         else if (name.equalsIgnoreCase(Constants.DOM_RESOURCE_RESOLVER)) {
914             return (value instanceof LSResourceResolver JavaDoc) ? true : false ;
915         }
916         else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_LOCATION)) {
917             return (value instanceof String JavaDoc) ? true : false ;
918         }
919         else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_TYPE)) {
920             // REVISIT: should null value be supported?
921
//as of now we are only supporting W3C XML Schema
922
return ( (value instanceof String JavaDoc) && value.equals(Constants.NS_XMLSCHEMA) ) ? true : false ;
923         }
924         else if (name.equalsIgnoreCase(SYMBOL_TABLE)){
925             // Xerces Symbol Table
926
return (value instanceof SymbolTable) ? true : false ;
927         }
928         else if (name.equalsIgnoreCase (GRAMMAR_POOL)){
929             return (value instanceof XMLGrammarPool) ? true : false ;
930         }
931         else {
932             //false if the parameter is not recognized or the requested value is not supported.
933
return false ;
934         }
935
936     } //canSetParameter
937

938     /**
939      * DOM Level 3 CR - Experimental.
940      *
941      * The list of the parameters supported by this
942      * <code>DOMConfiguration</code> object and for which at least one value
943      * can be set by the application. Note that this list can also contain
944      * parameter names defined outside this specification.
945      */

946     public DOMStringList JavaDoc getParameterNames() {
947         if (fRecognizedParameters == null){
948             Vector JavaDoc parameters = new Vector JavaDoc();
949
950             //Add DOM recognized parameters
951
//REVISIT: Would have been nice to have a list of
952
//recognized paramters.
953
parameters.add(Constants.DOM_COMMENTS);
954             parameters.add(Constants.DOM_DATATYPE_NORMALIZATION);
955             parameters.add(Constants.DOM_CDATA_SECTIONS);
956             parameters.add(Constants.DOM_ENTITIES);
957             parameters.add(Constants.DOM_SPLIT_CDATA);
958             parameters.add(Constants.DOM_NAMESPACES);
959             parameters.add(Constants.DOM_VALIDATE);
960
961             parameters.add(Constants.DOM_INFOSET);
962             parameters.add(Constants.DOM_NORMALIZE_CHARACTERS);
963             parameters.add(Constants.DOM_CANONICAL_FORM);
964             parameters.add(Constants.DOM_VALIDATE_IF_SCHEMA);
965             parameters.add(Constants.DOM_CHECK_CHAR_NORMALIZATION);
966             parameters.add(Constants.DOM_WELLFORMED);
967
968             parameters.add(Constants.DOM_NAMESPACE_DECLARATIONS);
969             parameters.add(Constants.DOM_ELEMENT_CONTENT_WHITESPACE);
970
971             parameters.add(Constants.DOM_ERROR_HANDLER);
972             parameters.add(Constants.DOM_SCHEMA_TYPE);
973             parameters.add(Constants.DOM_SCHEMA_LOCATION);
974             parameters.add(Constants.DOM_RESOURCE_RESOLVER);
975
976             //Add recognized xerces features and properties
977
parameters.add(GRAMMAR_POOL);
978             parameters.add(SYMBOL_TABLE);
979             parameters.add(SEND_PSVI);
980
981             fRecognizedParameters = new DOMStringListImpl(parameters);
982
983         }
984
985         return fRecognizedParameters;
986     }//getParameterNames
987

988     //
989
// Protected methods
990
//
991

992     /**
993      * reset all components before parsing
994      */

995     protected void reset() throws XNIException {
996
997         if (fValidationManager != null)
998             fValidationManager.reset();
999
1000        int count = fComponents.size();
1001        for (int i = 0; i < count; i++) {
1002            XMLComponent c = (XMLComponent) fComponents.get(i);
1003            c.reset(this);
1004        }
1005
1006    } // reset()
1007

1008    /**
1009     * Check a property. If the property is known and supported, this method
1010     * simply returns. Otherwise, the appropriate exception is thrown.
1011     *
1012     * @param propertyId The unique identifier (URI) of the property
1013     * being set.
1014     * @exception org.apache.xerces.xni.parser.XMLConfigurationException If the
1015     * requested feature is not known or supported.
1016     */

1017    protected void checkProperty(String JavaDoc propertyId)
1018        throws XMLConfigurationException {
1019
1020        // special cases
1021
if (propertyId.startsWith(Constants.SAX_PROPERTY_PREFIX)) {
1022            final int suffixLength = propertyId.length() - Constants.SAX_PROPERTY_PREFIX.length();
1023
1024            //
1025
// http://xml.org/sax/properties/xml-string
1026
// Value type: String
1027
// Access: read-only
1028
// Get the literal string of characters associated with the
1029
// current event. If the parser recognises and supports this
1030
// property but is not currently parsing text, it should return
1031
// null (this is a good way to check for availability before the
1032
// parse begins).
1033
//
1034
if (suffixLength == Constants.XML_STRING_PROPERTY.length() &&
1035                propertyId.endsWith(Constants.XML_STRING_PROPERTY)) {
1036                // REVISIT - we should probably ask xml-dev for a precise
1037
// definition of what this is actually supposed to return, and
1038
// in exactly which circumstances.
1039
short type = XMLConfigurationException.NOT_SUPPORTED;
1040                throw new XMLConfigurationException(type, propertyId);
1041            }
1042        }
1043
1044        // check property
1045
super.checkProperty(propertyId);
1046
1047    } // checkProperty(String)
1048

1049
1050    protected void addComponent(XMLComponent component) {
1051
1052        // don't add a component more than once
1053
if (fComponents.contains(component)) {
1054            return;
1055        }
1056        fComponents.add(component);
1057
1058        // register component's recognized features
1059
String JavaDoc[] recognizedFeatures = component.getRecognizedFeatures();
1060        addRecognizedFeatures(recognizedFeatures);
1061
1062        // register component's recognized properties
1063
String JavaDoc[] recognizedProperties = component.getRecognizedProperties();
1064        addRecognizedProperties(recognizedProperties);
1065
1066    } // addComponent(XMLComponent)
1067

1068    protected ValidationManager createValidationManager(){
1069        return new ValidationManager();
1070    }
1071
1072} // class XMLParser
1073
Popular Tags