KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > xs > XMLSchemaLoader


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000-2003 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.impl.xs;
59
60 import java.io.BufferedInputStream JavaDoc;
61 import java.io.File JavaDoc;
62 import java.io.FileInputStream JavaDoc;
63 import java.io.FileNotFoundException JavaDoc;
64 import java.io.IOException JavaDoc;
65 import java.io.InputStream JavaDoc;
66 import java.io.Reader JavaDoc;
67 import java.io.StringReader JavaDoc;
68 import java.util.Hashtable JavaDoc;
69 import java.util.Locale JavaDoc;
70 import java.util.StringTokenizer JavaDoc;
71 import java.util.Vector JavaDoc;
72
73 import javax.xml.transform.Source JavaDoc;
74
75 import com.sun.org.apache.xerces.internal.dom.DOMErrorImpl;
76 import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;
77 import com.sun.org.apache.xerces.internal.dom.DOMStringListImpl;
78 import com.sun.org.apache.xerces.internal.impl.Constants;
79 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
80 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
81 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
82 import com.sun.org.apache.xerces.internal.impl.xs.models.CMBuilder;
83 import com.sun.org.apache.xerces.internal.impl.xs.models.CMNodeFactory;
84 import com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler;
85 import com.sun.org.apache.xerces.internal.util.DOMErrorHandlerWrapper;
86 import com.sun.org.apache.xerces.internal.util.DefaultErrorHandler;
87 import com.sun.org.apache.xerces.internal.util.ParserConfigurationSettings;
88 import com.sun.org.apache.xerces.internal.util.SymbolTable;
89 import com.sun.org.apache.xerces.internal.util.XMLSymbols;
90 import com.sun.org.apache.xerces.internal.xni.XNIException;
91 import com.sun.org.apache.xerces.internal.xni.grammars.Grammar;
92 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
93 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarLoader;
94 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
95 import com.sun.org.apache.xerces.internal.xni.grammars.XSGrammar;
96 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
97 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
98 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
99 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver;
100 import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler;
101 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
102 import com.sun.org.apache.xerces.internal.xs.LSInputList;
103 import com.sun.org.apache.xerces.internal.xs.StringList;
104 import com.sun.org.apache.xerces.internal.xs.XSLoader;
105 import com.sun.org.apache.xerces.internal.xs.XSModel;
106 import org.w3c.dom.DOMConfiguration JavaDoc;
107 import org.w3c.dom.DOMError JavaDoc;
108 import org.w3c.dom.DOMErrorHandler JavaDoc;
109 import org.w3c.dom.DOMException JavaDoc;
110 import org.w3c.dom.DOMStringList JavaDoc;
111 import org.w3c.dom.ls.LSInput JavaDoc;
112 import org.xml.sax.InputSource JavaDoc;
113
114 /**
115  * This class implements xni.grammars.XMLGrammarLoader.
116  * It also serves as implementation of xs.XSLoader interface and DOMConfiguration interface.
117  *
118  * This class is designed to interact either with a proxy for a user application
119  * which wants to preparse schemas, or with our own Schema validator.
120  * It is hoped that none of these "external" classes will therefore need to communicate directly
121  * with XSDHandler in future.
122  * <p>This class only knows how to make XSDHandler do its thing.
123  * The caller must ensure that all its properties (schemaLocation, JAXPSchemaSource
124  * etc.) have been properly set.
125  *
126  * @author Neil Graham, IBM
127  * @version $Id: XMLSchemaLoader.java,v 1.26 2004/01/29 22:26:00 mrglavas Exp $
128  */

129
130 public class XMLSchemaLoader implements XMLGrammarLoader, XMLComponent,
131                                         // XML Component API
132
XSLoader, DOMConfiguration JavaDoc {
133
134     // Feature identifiers:
135

136     /** Feature identifier: schema full checking*/
137     protected static final String JavaDoc SCHEMA_FULL_CHECKING =
138      Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING;
139
140     /** Feature identifier: continue after fatal error. */
141     protected static final String JavaDoc CONTINUE_AFTER_FATAL_ERROR =
142         Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE;
143
144     /** Feature identifier: allow java encodings to be recognized when parsing schema docs. */
145     protected static final String JavaDoc ALLOW_JAVA_ENCODINGS =
146         Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE;
147
148     /** Feature identifier: standard uri conformant feature. */
149     protected static final String JavaDoc STANDARD_URI_CONFORMANT_FEATURE =
150         Constants.XERCES_FEATURE_PREFIX + Constants.STANDARD_URI_CONFORMANT_FEATURE;
151         
152     /** Feature: disallow doctype*/
153     protected static final String JavaDoc DISALLOW_DOCTYPE =
154         Constants.XERCES_FEATURE_PREFIX + Constants.DISALLOW_DOCTYPE_DECL_FEATURE;
155         
156          
157     protected static final String JavaDoc AUGMENT_PSVI =
158        Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_AUGMENT_PSVI;
159            
160     protected static final String JavaDoc PARSER_SETTINGS =
161                 Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS;
162
163     // recognized features:
164
private static final String JavaDoc[] RECOGNIZED_FEATURES = {
165         SCHEMA_FULL_CHECKING,
166         AUGMENT_PSVI,
167         CONTINUE_AFTER_FATAL_ERROR,
168         ALLOW_JAVA_ENCODINGS,
169         STANDARD_URI_CONFORMANT_FEATURE,
170         DISALLOW_DOCTYPE
171     };
172
173     // property identifiers
174

175     /** Property identifier: symbol table. */
176     public static final String JavaDoc SYMBOL_TABLE =
177         Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
178
179     /** Property identifier: error reporter. */
180     public static final String JavaDoc ERROR_REPORTER =
181         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
182
183     /** Property identifier: error handler. */
184     protected static final String JavaDoc ERROR_HANDLER =
185         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY;
186
187     /** Property identifier: entity resolver. */
188     public static final String JavaDoc ENTITY_RESOLVER =
189         Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY;
190
191     /** Property identifier: grammar pool. */
192     public static final String JavaDoc XMLGRAMMAR_POOL =
193         Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
194
195     /** Property identifier: schema location. */
196     protected static final String JavaDoc SCHEMA_LOCATION =
197         Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_LOCATION;
198
199     /** Property identifier: no namespace schema location. */
200     protected static final String JavaDoc SCHEMA_NONS_LOCATION =
201         Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_NONS_LOCATION;
202
203     /** Property identifier: JAXP schema source. */
204     protected static final String JavaDoc JAXP_SCHEMA_SOURCE =
205         Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE;
206    
207     protected static final String JavaDoc SECURITY_MANAGER =
208         Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY;
209         
210    protected static final String JavaDoc ENTITY_MANAGER =
211                 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
212
213     // recognized properties
214
private static final String JavaDoc [] RECOGNIZED_PROPERTIES = {
215         ENTITY_MANAGER,
216         SYMBOL_TABLE,
217         ERROR_REPORTER,
218         ERROR_HANDLER,
219         ENTITY_RESOLVER,
220         XMLGRAMMAR_POOL,
221         SCHEMA_LOCATION,
222         SCHEMA_NONS_LOCATION,
223         JAXP_SCHEMA_SOURCE,
224         SECURITY_MANAGER
225     };
226
227     // Data
228

229     // features and properties
230
private ParserConfigurationSettings fLoaderConfig = new ParserConfigurationSettings();
231     private SymbolTable fSymbolTable = null;
232     private XMLErrorReporter fErrorReporter = new XMLErrorReporter ();
233     private XMLEntityManager fEntityManager = null;
234     private XMLEntityResolver fUserEntityResolver = null;
235     private XMLGrammarPool fGrammarPool = null;
236     private String JavaDoc fExternalSchemas = null;
237     private String JavaDoc fExternalNoNSSchema = null;
238     // JAXP property: schema source
239
private Object JavaDoc fJAXPSource = null;
240     // is Schema Full Checking enabled
241
private boolean fIsCheckedFully = false;
242     // boolean that tells whether we've tested the JAXP property.
243
private boolean fJAXPProcessed = false;
244     // if features/properties has not been changed, the value of this attribute is "false"
245
private boolean fSettingsChanged = true;
246
247     // xml schema parsing
248
private XSDHandler fSchemaHandler;
249     private XSGrammarBucket fGrammarBucket;
250     private XSDeclarationPool fDeclPool = null;
251     private SubstitutionGroupHandler fSubGroupHandler;
252     private CMBuilder fCMBuilder;
253     private XSDDescription fXSDDescription = new XSDDescription();
254     
255     private Hashtable JavaDoc fJAXPCache;
256     private Locale JavaDoc fLocale = Locale.getDefault();
257
258     // XSLoader attributes
259
private DOMStringList JavaDoc fRecognizedParameters = null;
260     /** DOM L3 error handler */
261     private DOMErrorHandlerWrapper fErrorHandler = null;
262
263     // default constructor. Create objects we absolutely need:
264
public XMLSchemaLoader() {
265         this( new SymbolTable(), null, new XMLEntityManager(), null, null, null);
266     }
267
268     public XMLSchemaLoader(SymbolTable symbolTable) {
269         this( symbolTable, null, new XMLEntityManager(), null, null, null);
270     }
271
272     /**
273      * This constractor is used by the XMLSchemaValidator. Additional properties, i.e. XMLEntityManager,
274      * will be passed during reset(XMLComponentManager).
275      * @param errorReporter
276      * @param grammarBucket
277      * @param sHandler
278      * @param builder
279      */

280     XMLSchemaLoader(XMLErrorReporter errorReporter,
281                 XSGrammarBucket grammarBucket,
282                 SubstitutionGroupHandler sHandler, CMBuilder builder) {
283         this(null, errorReporter, new XMLEntityManager(), grammarBucket, sHandler, builder);
284     }
285
286     XMLSchemaLoader(SymbolTable symbolTable,
287                 XMLErrorReporter errorReporter,
288                 XMLEntityManager entityResolver,
289                 XSGrammarBucket grammarBucket,
290                 SubstitutionGroupHandler sHandler,
291                 CMBuilder builder) {
292                     
293         // store properties and features in configuration
294
fLoaderConfig.addRecognizedFeatures(RECOGNIZED_FEATURES);
295         fLoaderConfig.addRecognizedProperties(RECOGNIZED_PROPERTIES);
296         if (symbolTable != null){
297             fLoaderConfig.setProperty(SYMBOL_TABLE, symbolTable);
298         }
299                  
300         if(errorReporter == null) {
301             errorReporter = new XMLErrorReporter ();
302             errorReporter.setLocale(fLocale);
303             errorReporter.setProperty(ERROR_HANDLER, new DefaultErrorHandler());
304             
305         }
306         fErrorReporter = errorReporter;
307         // make sure error reporter knows about schemas...
308
if(fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) {
309             fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new XSMessageFormatter());
310         }
311         fLoaderConfig.setProperty(ERROR_REPORTER, fErrorReporter);
312         fEntityManager = entityResolver;
313         // entity manager is null if XMLSchemaValidator creates the loader
314
if (fEntityManager != null){
315             fLoaderConfig.setProperty(ENTITY_MANAGER, fEntityManager);
316         }
317               
318         // by default augment PSVI (i.e. don't use declaration pool)
319
fLoaderConfig.setFeature(AUGMENT_PSVI, true);
320         
321         if(grammarBucket == null ) {
322             grammarBucket = new XSGrammarBucket();
323         }
324         fGrammarBucket = grammarBucket;
325         if(sHandler == null) {
326             sHandler = new SubstitutionGroupHandler(fGrammarBucket);
327         }
328         fSubGroupHandler = sHandler;
329
330         //get an instance of the CMNodeFactory */
331
CMNodeFactory nodeFactory = new CMNodeFactory() ;
332
333         if(builder == null) {
334             builder = new CMBuilder(nodeFactory);
335         }
336         fCMBuilder = builder;
337         fSchemaHandler = new XSDHandler(fGrammarBucket);
338         fDeclPool = new XSDeclarationPool();
339         fJAXPCache = new Hashtable JavaDoc();
340
341         fSettingsChanged = true;
342     }
343
344     /**
345      * Returns a list of feature identifiers that are recognized by
346      * this XMLGrammarLoader. This method may return null if no features
347      * are recognized.
348      */

349     public String JavaDoc[] getRecognizedFeatures() {
350         return (String JavaDoc[])(RECOGNIZED_FEATURES.clone());
351     } // getRecognizedFeatures(): String[]
352

353     /**
354      * Returns the state of a feature.
355      *
356      * @param featureId The feature identifier.
357      *
358      * @throws XMLConfigurationException Thrown on configuration error.
359      */

360     public boolean getFeature(String JavaDoc featureId)
361             throws XMLConfigurationException {
362         return fLoaderConfig.getFeature(featureId);
363     } // getFeature (String): boolean
364

365     /**
366      * Sets the state of a feature.
367      *
368      * @param featureId The feature identifier.
369      * @param state The state of the feature.
370      *
371      * @throws XMLConfigurationException Thrown when a feature is not
372      * recognized or cannot be set.
373      */

374     public void setFeature(String JavaDoc featureId,
375                 boolean state) throws XMLConfigurationException {
376         fSettingsChanged = true;
377         if(featureId.equals(CONTINUE_AFTER_FATAL_ERROR)) {
378             fErrorReporter.setFeature(CONTINUE_AFTER_FATAL_ERROR, state);
379         }
380         fLoaderConfig.setFeature(featureId, state);
381     } // setFeature(String, boolean)
382

383     /**
384      * Returns a list of property identifiers that are recognized by
385      * this XMLGrammarLoader. This method may return null if no properties
386      * are recognized.
387      */

388     public String JavaDoc[] getRecognizedProperties() {
389         return (String JavaDoc[])(RECOGNIZED_PROPERTIES.clone());
390     } // getRecognizedProperties(): String[]
391

392     /**
393      * Returns the state of a property.
394      *
395      * @param propertyId The property identifier.
396      *
397      * @throws XMLConfigurationException Thrown on configuration error.
398      */

399     public Object JavaDoc getProperty(String JavaDoc propertyId)
400             throws XMLConfigurationException {
401         return fLoaderConfig.getProperty(propertyId);
402     } // getProperty(String): Object
403

404     /**
405      * Sets the state of a property.
406      *
407      * @param propertyId The property identifier.
408      * @param state The state of the property.
409      *
410      * @throws XMLConfigurationException Thrown when a property is not
411      * recognized or cannot be set.
412      */

413     public void setProperty(String JavaDoc propertyId,
414                 Object JavaDoc state) throws XMLConfigurationException {
415         fSettingsChanged = true;
416         fLoaderConfig.setProperty(propertyId, state);
417         if(propertyId.equals( JAXP_SCHEMA_SOURCE)) {
418             fJAXPSource = state;
419             fJAXPProcessed = false;
420         }
421         else if(propertyId.equals( XMLGRAMMAR_POOL)) {
422              fGrammarPool = (XMLGrammarPool)state;
423         }
424         else if (propertyId.equals(SCHEMA_LOCATION)){
425             fExternalSchemas = (String JavaDoc)state;
426         }
427         else if (propertyId.equals(SCHEMA_NONS_LOCATION)){
428             fExternalNoNSSchema = (String JavaDoc) state;
429         }
430         else if (propertyId.equals(ENTITY_RESOLVER)){
431             fEntityManager.setProperty(ENTITY_RESOLVER, state);
432         }
433         else if (propertyId.equals(ERROR_REPORTER)){
434             fErrorReporter = (XMLErrorReporter)state;
435             if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) {
436                 fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new XSMessageFormatter());
437             }
438         }
439     } // setProperty(String, Object)
440

441     /**
442      * Set the locale to use for messages.
443      *
444      * @param locale The locale object to use for localization of messages.
445      *
446      * @exception XNIException Thrown if the parser does not support the
447      * specified locale.
448      */

449     public void setLocale(Locale JavaDoc locale) {
450         fLocale = locale;
451         fErrorReporter.setLocale(locale);
452     } // setLocale(Locale)
453

454     /** Return the Locale the XMLGrammarLoader is using. */
455     public Locale JavaDoc getLocale() {
456         return fLocale;
457     } // getLocale(): Locale
458

459     /**
460      * Sets the error handler.
461      *
462      * @param errorHandler The error handler.
463      */

464     public void setErrorHandler(XMLErrorHandler errorHandler) {
465         fErrorReporter.setProperty(ERROR_HANDLER, errorHandler);
466     } // setErrorHandler(XMLErrorHandler)
467

468     /** Returns the registered error handler. */
469     public XMLErrorHandler getErrorHandler() {
470         return fErrorReporter.getErrorHandler();
471     } // getErrorHandler(): XMLErrorHandler
472

473     /**
474      * Sets the entity resolver.
475      *
476      * @param entityResolver The new entity resolver.
477      */

478     public void setEntityResolver(XMLEntityResolver entityResolver) {
479         fUserEntityResolver = entityResolver;
480         fLoaderConfig.setProperty(ENTITY_RESOLVER, entityResolver);
481         fEntityManager.setProperty(ENTITY_RESOLVER, entityResolver);
482     } // setEntityResolver(XMLEntityResolver)
483

484     /** Returns the registered entity resolver. */
485     public XMLEntityResolver getEntityResolver() {
486         return fUserEntityResolver;
487     } // getEntityResolver(): XMLEntityResolver
488

489     
490     /**
491      * Returns a Grammar object by parsing the contents of the
492      * entity pointed to by source.
493      *
494      * @param source the location of the entity which forms
495      * the starting point of the grammar to be constructed.
496      * @throws IOException When a problem is encountered reading the entity
497      * XNIException When a condition arises (such as a FatalError) that requires parsing
498      * of the entity be terminated.
499      */

500     public Grammar loadGrammar(XMLInputSource source) throws IOException JavaDoc, XNIException {
501         return loadGrammar( source.toSource() );
502     }
503     
504     public Grammar loadGrammar(Source JavaDoc source)
505                 throws IOException JavaDoc, XNIException {
506                     
507         // REVISIT: this method should have a namespace parameter specified by
508
// user. In this case we can easily detect if a schema asked to be loaded
509
// is already in the local cache.
510

511         reset(fLoaderConfig);
512         fSettingsChanged = false;
513         XSDDescription desc = new XSDDescription();
514         desc.fContextType = XSDDescription.CONTEXT_PREPARSE;
515         desc.setLiteralSystemId( source.getSystemId());
516         // none of the other fields make sense for preparsing
517
Hashtable JavaDoc locationPairs = new Hashtable JavaDoc();
518         // Process external schema location properties.
519
// We don't call tokenizeSchemaLocationStr here, because we also want
520
// to check whether the values are valid URI.
521
processExternalHints(fExternalSchemas, fExternalNoNSSchema,
522                              locationPairs, fErrorReporter);
523         SchemaGrammar grammar = loadSchema(desc, source, locationPairs);
524
525         if(grammar != null && fGrammarPool != null) {
526             fGrammarPool.cacheGrammars(XMLGrammarDescription.XML_SCHEMA, fGrammarBucket.getGrammars());
527             // NOTE: we only need to verify full checking in case the schema was not provided via JAXP
528
// since full checking already verified for all JAXP schemas
529
if(fIsCheckedFully && fJAXPCache.get(grammar) != grammar) {
530                 XSConstraints.fullSchemaChecking(fGrammarBucket, fSubGroupHandler, fCMBuilder, fErrorReporter);
531             }
532         }
533         return grammar;
534     } // loadGrammar(XMLInputSource): Grammar
535

536     /**
537      * This method is called either from XMLGrammarLoader.loadGrammar or from XMLSchemaValidator.
538      * Note: in either case, the EntityManager (or EntityResolvers) are not going to be invoked
539      * to resolve the location of the schema in XSDDescription
540      * @param desc
541      * @param source
542      * @param locationPairs
543      * @return An XML Schema grammar
544      * @throws IOException
545      * @throws XNIException
546      */

547     SchemaGrammar loadSchema(XSDDescription desc,
548             Source JavaDoc source,
549             Hashtable JavaDoc locationPairs) throws IOException JavaDoc, XNIException {
550
551         // this should only be done once per invocation of this object;
552
// unless application alters JAXPSource in the mean time.
553
if(!fJAXPProcessed) {
554             processJAXPSchemaSource(locationPairs);
555         }
556         SchemaGrammar grammar = fSchemaHandler.parseSchema(source, desc, locationPairs);
557
558         return grammar;
559     } // loadSchema(XSDDescription, XMLInputSource): SchemaGrammar
560

561     /** This method tries to resolve location of the given schema.
562      * The loader stores the namespace/location pairs in a hashtable (use "" as the
563      * namespace of absent namespace). When resolving an entity, loader first tries
564      * to find in the hashtable whether there is a value for that namespace,
565      * if so, pass that location value to the user-defined entity resolver.
566      *
567      * @param desc
568      * @param locationPairs
569      * @param entityResolver
570      * @return
571      * @throws IOException
572      */

573     public static XMLInputSource resolveDocument(XSDDescription desc, Hashtable JavaDoc locationPairs,
574             XMLEntityResolver entityResolver) throws IOException JavaDoc {
575         String JavaDoc loc = null;
576         // we consider the schema location properties for import
577
if (desc.getContextType() == XSDDescription.CONTEXT_IMPORT ||
578             desc.fromInstance()) {
579             // use empty string as the key for absent namespace
580
String JavaDoc namespace = desc.getTargetNamespace();
581             String JavaDoc ns = namespace == null ? XMLSymbols.EMPTY_STRING : namespace;
582             // get the location hint for that namespace
583
LocationArray tempLA = (LocationArray)locationPairs.get(ns);
584             if(tempLA != null)
585                 loc = tempLA.getFirstLocation();
586         }
587
588         // if it's not import, or if the target namespace is not set
589
// in the schema location properties, use location hint
590
if (loc == null) {
591             String JavaDoc[] hints = desc.getLocationHints();
592             if (hints != null && hints.length > 0)
593                 loc = hints[0];
594         }
595
596         String JavaDoc expandedLoc = XMLEntityManager.expandSystemId(loc, desc.getBaseSystemId(), false);
597         desc.setLiteralSystemId(loc);
598         desc.setExpandedSystemId(expandedLoc);
599         return entityResolver.resolveEntity(desc);
600     }
601
602     // add external schema locations to the location pairs
603
public static void processExternalHints(String JavaDoc sl, String JavaDoc nsl,
604                                             Hashtable JavaDoc locations,
605                                             XMLErrorReporter er) {
606         if (sl != null) {
607             try {
608                 // get the attribute decl for xsi:schemaLocation
609
// because external schema location property has the same syntax
610
// as xsi:schemaLocation
611
XSAttributeDecl attrDecl = SchemaGrammar.SG_XSI.getGlobalAttributeDecl(SchemaSymbols.XSI_SCHEMALOCATION);
612                 // validation the string value to get the list of URI's
613
attrDecl.fType.validate(sl, null, null);
614                 if (!tokenizeSchemaLocationStr(sl, locations)) {
615                     // report warning (odd number of items)
616
er.reportError(XSMessageFormatter.SCHEMA_DOMAIN,
617                                    "SchemaLocation",
618                                    new Object JavaDoc[]{sl},
619                                    XMLErrorReporter.SEVERITY_WARNING);
620                 }
621             }
622             catch (InvalidDatatypeValueException ex) {
623                 // report warning (not list of URI's)
624
er.reportError(XSMessageFormatter.SCHEMA_DOMAIN,
625                                ex.getKey(), ex.getArgs(),
626                                XMLErrorReporter.SEVERITY_WARNING);
627             }
628         }
629
630         if (nsl != null) {
631             try {
632                 // similarly for no ns schema location property
633
XSAttributeDecl attrDecl = SchemaGrammar.SG_XSI.getGlobalAttributeDecl(SchemaSymbols.XSI_NONAMESPACESCHEMALOCATION);
634                 attrDecl.fType.validate(nsl, null, null);
635                 LocationArray la = ((LocationArray)locations.get(XMLSymbols.EMPTY_STRING));
636                 if(la == null) {
637                     la = new LocationArray();
638                     locations.put(XMLSymbols.EMPTY_STRING, la);
639                 }
640                 la.addLocation(nsl);
641             }
642             catch (InvalidDatatypeValueException ex) {
643                 // report warning (not a URI)
644
er.reportError(XSMessageFormatter.SCHEMA_DOMAIN,
645                                ex.getKey(), ex.getArgs(),
646                                XMLErrorReporter.SEVERITY_WARNING);
647             }
648         }
649     }
650     // this method takes a SchemaLocation string.
651
// If an error is encountered, false is returned;
652
// otherwise, true is returned. In either case, locations
653
// is augmented to include as many tokens as possible.
654
// @param schemaStr The schemaLocation string to tokenize
655
// @param locations Hashtable mapping namespaces to LocationArray objects holding lists of locaitons
656
// @return true if no problems; false if string could not be tokenized
657
public static boolean tokenizeSchemaLocationStr(String JavaDoc schemaStr, Hashtable JavaDoc locations) {
658         if (schemaStr!= null) {
659             StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(schemaStr, " \n\t\r");
660             String JavaDoc namespace, location;
661             while (t.hasMoreTokens()) {
662                 namespace = t.nextToken ();
663                 if (!t.hasMoreTokens()) {
664                     return false; // error!
665
}
666                 location = t.nextToken();
667                 LocationArray la = ((LocationArray)locations.get(namespace));
668                 if(la == null) {
669                     la = new LocationArray();
670                     locations.put(namespace, la);
671                 }
672                 la.addLocation(location);
673             }
674         }
675         return true;
676     } // tokenizeSchemaLocation(String, Hashtable): boolean
677

678     /**
679      * Translate the various JAXP SchemaSource property types to XNI
680      * XMLInputSource. Valid types are: String, org.xml.sax.InputSource,
681      * InputStream, File, or Object[] of any of previous types.
682      * REVISIT: the JAXP 1.2 spec is less than clear as to whether this property
683      * should be available to imported schemas. I have assumed
684      * that it should. - NG
685      * Note: all JAXP schema files will be checked for full-schema validity if the feature was set up
686      *
687      */

688     private void processJAXPSchemaSource(Hashtable JavaDoc locationPairs) throws IOException JavaDoc {
689         fJAXPProcessed = true;
690         if (fJAXPSource == null) {
691             return;
692         }
693
694         Class JavaDoc componentType = fJAXPSource.getClass().getComponentType();
695         XMLInputSource xis = null;
696         String JavaDoc sid = null;
697         if (componentType == null) {
698             // Not an array
699
if(fJAXPSource instanceof InputStream JavaDoc ||
700                     fJAXPSource instanceof InputSource) {
701                 SchemaGrammar g = (SchemaGrammar)fJAXPCache.get(fJAXPSource);
702                 if(g != null) {
703                     fGrammarBucket.putGrammar(g);
704                     return;
705                 }
706             }
707             fXSDDescription.reset();
708             xis = xsdToXMLInputSource(fJAXPSource);
709             sid = xis.getSystemId();
710             fXSDDescription.fContextType = XSDDescription.CONTEXT_PREPARSE;
711             if (sid != null) {
712                 fXSDDescription.setBaseSystemId(xis.getBaseSystemId());
713                 fXSDDescription.setLiteralSystemId(sid);
714                 fXSDDescription.setExpandedSystemId(sid);
715                 fXSDDescription.fLocationHints = new String JavaDoc[]{sid};
716             }
717             SchemaGrammar g = loadSchema(fXSDDescription, xis.toSource(), locationPairs);
718             // it is possible that we won't be able to resolve JAXP schema-source location
719
if (g != null){
720                 if(fJAXPSource instanceof InputStream JavaDoc ||
721                     fJAXPSource instanceof InputSource) {
722                         fJAXPCache.put(fJAXPSource, g);
723                         if(fIsCheckedFully) {
724                             XSConstraints.fullSchemaChecking(fGrammarBucket, fSubGroupHandler, fCMBuilder, fErrorReporter);
725                         }
726                 }
727                 fGrammarBucket.putGrammar(g);
728             }
729             return ;
730         } else if ( (componentType != Object JavaDoc.class) &&
731                     (componentType != String JavaDoc.class) &&
732                     (componentType != File JavaDoc.class) &&
733                     (componentType != InputStream JavaDoc.class) &&
734                     (componentType != InputSource.class)
735                   ) {
736             // Not an Object[], String[], File[], InputStream[], InputSource[]
737
throw new XMLConfigurationException(
738                 XMLConfigurationException.NOT_SUPPORTED, "\""+JAXP_SCHEMA_SOURCE+
739                 "\" property cannot have an array of type {"+componentType.getName()+
740                 "}. Possible types of the array supported are Object, String, File, "+
741                 "InputStream, InputSource.");
742         }
743
744         // JAXP spec. allow []s of type String, File, InputStream,
745
// InputSource also, apart from [] of type Object.
746
Object JavaDoc[] objArr = (Object JavaDoc[]) fJAXPSource;
747         //make local vector for storing targetn namespaces of schemasources specified in object arrays.
748
Vector JavaDoc jaxpSchemaSourceNamespaces = new Vector JavaDoc() ;
749         for (int i = 0; i < objArr.length; i++) {
750             if(objArr[i] instanceof InputStream JavaDoc ||
751                     objArr[i] instanceof InputSource) {
752                 SchemaGrammar g = (SchemaGrammar)fJAXPCache.get(objArr[i]);
753                 if (g != null) {
754                     fGrammarBucket.putGrammar(g);
755                     continue;
756                 }
757             }
758             fXSDDescription.reset();
759             xis = xsdToXMLInputSource(objArr[i]);
760             sid = xis.getSystemId();
761             fXSDDescription.fContextType = XSDDescription.CONTEXT_PREPARSE;
762             if (sid != null) {
763                 fXSDDescription.setBaseSystemId(xis.getBaseSystemId());
764                 fXSDDescription.setLiteralSystemId(sid);
765                 fXSDDescription.setExpandedSystemId(sid);
766                 fXSDDescription.fLocationHints = new String JavaDoc[]{sid};
767             }
768             String JavaDoc targetNamespace = null ;
769             // load schema
770
SchemaGrammar grammar = fSchemaHandler.parseSchema(xis.toSource(),fXSDDescription, locationPairs);
771
772             if(fIsCheckedFully) {
773                 XSConstraints.fullSchemaChecking(fGrammarBucket, fSubGroupHandler, fCMBuilder, fErrorReporter);
774             }
775             if(grammar != null){
776                 targetNamespace = grammar.getTargetNamespace() ;
777                 if(jaxpSchemaSourceNamespaces.contains(targetNamespace)){
778                     //when an array of objects is passed it is illegal to have two schemas that share same namespace.
779
throw new java.lang.IllegalArgumentException JavaDoc(
780                         " When using array of Objects as the value of SCHEMA_SOURCE property , " +
781                         "no two Schemas should share the same targetNamespace. " );
782                 }
783                 else{
784                     jaxpSchemaSourceNamespaces.add(targetNamespace) ;
785                 }
786                 if(objArr[i] instanceof InputStream JavaDoc ||
787                         objArr[i] instanceof InputSource) {
788                     fJAXPCache.put(objArr[i], grammar);
789                 }
790                 fGrammarBucket.putGrammar(grammar);
791             }
792             else{
793                 //REVISIT: What should be the acutal behavior if grammar can't be loaded as specified in schema source?
794
}
795         }
796     }//processJAXPSchemaSource
797

798     private XMLInputSource xsdToXMLInputSource(
799             Object JavaDoc val)
800     {
801         if (val instanceof String JavaDoc) {
802             // String value is treated as a URI that is passed through the
803
// EntityResolver
804
String JavaDoc loc = (String JavaDoc) val;
805                 fXSDDescription.reset();
806                 fXSDDescription.setValues(null, loc, null, null);
807                 XMLInputSource xis = null;
808                 try {
809                     xis = fEntityManager.resolveEntity(fXSDDescription);
810                 } catch (IOException JavaDoc ex) {
811                     fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN,
812                         "schema_reference.4",
813                                       new Object JavaDoc[] { loc }, XMLErrorReporter.SEVERITY_ERROR);
814                 }
815                 if (xis == null) {
816                     // REVISIT: can this happen?
817
// Treat value as a URI and pass in as systemId
818
return new XMLInputSource(null, loc, null);
819                 }
820                 return xis;
821         } else if (val instanceof InputSource) {
822             return saxToXMLInputSource((InputSource) val);
823         } else if (val instanceof InputStream JavaDoc) {
824             return new XMLInputSource(null, null, null,
825                                       (InputStream JavaDoc) val, null);
826         } else if (val instanceof File JavaDoc) {
827             File JavaDoc file = (File JavaDoc) val;
828             InputStream JavaDoc is = null;
829             try {
830                 is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
831             } catch (FileNotFoundException JavaDoc ex) {
832                 fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN,
833                 "schema_reference.4", new Object JavaDoc[] { file.toString() },
834                 XMLErrorReporter.SEVERITY_ERROR);
835             }
836             return new XMLInputSource(null, null, null, is, null);
837         }
838         throw new XMLConfigurationException(
839             XMLConfigurationException.NOT_SUPPORTED, "\""+JAXP_SCHEMA_SOURCE+
840             "\" property cannot have a value of type {"+val.getClass().getName()+
841             "}. Possible types of the value supported are String, File, InputStream, "+
842             "InputSource OR an array of these types.");
843     }
844
845
846      //Convert a SAX InputSource to an equivalent XNI XMLInputSource
847

848     private static XMLInputSource saxToXMLInputSource(InputSource sis) {
849         String JavaDoc publicId = sis.getPublicId();
850         String JavaDoc systemId = sis.getSystemId();
851
852         Reader JavaDoc charStream = sis.getCharacterStream();
853         if (charStream != null) {
854             return new XMLInputSource(publicId, systemId, null, charStream,
855                                       null);
856         }
857
858         InputStream JavaDoc byteStream = sis.getByteStream();
859         if (byteStream != null) {
860             return new XMLInputSource(publicId, systemId, null, byteStream,
861                                       sis.getEncoding());
862         }
863
864         return new XMLInputSource(publicId, systemId, null);
865     }
866
867     static class LocationArray{
868
869         int length ;
870         String JavaDoc [] locations = new String JavaDoc[2];
871
872         public void resize(int oldLength , int newLength){
873             String JavaDoc [] temp = new String JavaDoc[newLength] ;
874             System.arraycopy(locations, 0, temp, 0, Math.min(oldLength, newLength));
875             locations = temp ;
876             length = Math.min(oldLength, newLength);
877         }
878
879         public void addLocation(String JavaDoc location){
880             if(length >= locations.length ){
881                 resize(length, Math.max(1, length*2));
882             }
883             locations[length++] = location;
884         }//setLocation()
885

886         public String JavaDoc [] getLocationArray(){
887             if(length < locations.length ){
888                 resize(locations.length, length);
889             }
890             return locations;
891         }//getLocationArray()
892

893         public String JavaDoc getFirstLocation(){
894             return length > 0 ? locations[0] : null;
895         }
896
897         public int getLength(){
898             return length ;
899         }
900
901     } //locationArray
902

903     /* (non-Javadoc)
904      * @see com.sun.org.apache.xerces.internal.xni.parser.XMLComponent#getFeatureDefault(java.lang.String)
905      */

906     public Boolean JavaDoc getFeatureDefault(String JavaDoc featureId) {
907         if (featureId.equals(AUGMENT_PSVI)){
908             return Boolean.TRUE;
909         }
910         return null;
911     }
912
913     /* (non-Javadoc)
914      * @see com.sun.org.apache.xerces.internal.xni.parser.XMLComponent#getPropertyDefault(java.lang.String)
915      */

916     public Object JavaDoc getPropertyDefault(String JavaDoc propertyId) {
917         // TODO Auto-generated method stub
918
return null;
919     }
920
921     /* (non-Javadoc)
922      * @see com.sun.org.apache.xerces.internal.xni.parser.XMLComponent#reset(com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager)
923      */

924     public void reset(XMLComponentManager componentManager) throws XMLConfigurationException {
925         
926         fGrammarBucket.reset();
927         
928         fSubGroupHandler.reset();
929         
930         // set full validation to false
931
fIsCheckedFully = false;
932         boolean parser_settings;
933         try {
934             parser_settings = componentManager.getFeature(PARSER_SETTINGS);
935         }
936         catch (XMLConfigurationException e){
937             parser_settings = true;
938         }
939         if (!parser_settings || !fSettingsChanged){
940             // reinitialize grammar bucket
941
initGrammarBucket();
942             return;
943         }
944         
945         // get registered entity manager to be able to resolve JAXP schema-source property:
946
// Note: in case XMLSchemaValidator has created the loader,
947
// the entity manager property is null
948
fEntityManager = (XMLEntityManager)componentManager.getProperty(ENTITY_MANAGER);
949         
950         boolean psvi = true;
951         try {
952             psvi = componentManager.getFeature(AUGMENT_PSVI);
953         } catch (XMLConfigurationException e) {
954             psvi = false;
955         }
956         
957         if (!psvi) {
958             fDeclPool.reset();
959             fCMBuilder.setDeclPool(fDeclPool);
960             fSchemaHandler.setDeclPool(fDeclPool);
961         } else {
962             fCMBuilder.setDeclPool(null);
963             fSchemaHandler.setDeclPool(null);
964         }
965                 
966         // get schema location properties
967
try {
968             fExternalSchemas = (String JavaDoc) componentManager.getProperty(SCHEMA_LOCATION);
969             fExternalNoNSSchema =
970                 (String JavaDoc) componentManager.getProperty(SCHEMA_NONS_LOCATION);
971         } catch (XMLConfigurationException e) {
972             fExternalSchemas = null;
973             fExternalNoNSSchema = null;
974         }
975         // get JAXP sources if available
976
try {
977             fJAXPSource = componentManager.getProperty(JAXP_SCHEMA_SOURCE);
978             fJAXPProcessed = false;
979
980         } catch (XMLConfigurationException e) {
981             fJAXPSource = null;
982             fJAXPProcessed = false;
983         }
984         
985         // clear grammars, and put the one for schema namespace there
986
try {
987             fGrammarPool = (XMLGrammarPool) componentManager.getProperty(XMLGRAMMAR_POOL);
988         } catch (XMLConfigurationException e) {
989             fGrammarPool = null;
990         }
991         
992         initGrammarBucket();
993         // get continue-after-fatal-error feature
994
try {
995             boolean fatalError = componentManager.getFeature(CONTINUE_AFTER_FATAL_ERROR);
996             fErrorReporter.setFeature(CONTINUE_AFTER_FATAL_ERROR, fatalError);
997         } catch (XMLConfigurationException e) {
998         }
999         // set full validation to false
1000
try {
1001            fIsCheckedFully = componentManager.getFeature(SCHEMA_FULL_CHECKING);
1002        }
1003        catch (XMLConfigurationException e){
1004            fIsCheckedFully = false;
1005        }
1006                
1007        fSchemaHandler.reset(componentManager);
1008    }
1009    
1010    private void initGrammarBucket(){
1011        if(fGrammarPool != null) {
1012            Grammar [] initialGrammars = fGrammarPool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
1013            for (int i = 0; i < initialGrammars.length; i++) {
1014                // put this grammar into the bucket, along with grammars
1015
// imported by it (directly or indirectly)
1016
if (!fGrammarBucket.putGrammar((SchemaGrammar)(initialGrammars[i]), true)) {
1017                    // REVISIT: a conflict between new grammar(s) and grammars
1018
// in the bucket. What to do? A warning? An exception?
1019
fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN,
1020                                                                 "GrammarConflict", null,
1021                                                                 XMLErrorReporter.SEVERITY_WARNING);
1022                }
1023            }
1024        }
1025    }
1026        
1027
1028    /* (non-Javadoc)
1029     * @see com.sun.org.apache.xerces.internal.xs.XSLoader#getConfig()
1030     */

1031    public DOMConfiguration JavaDoc getConfig() {
1032        return this;
1033    }
1034
1035    /* (non-Javadoc)
1036     * @see com.sun.org.apache.xerces.internal.xs.XSLoader#load(org.w3c.dom.ls.LSInput)
1037     */

1038    public XSModel load(LSInput JavaDoc is) {
1039        try {
1040            Grammar g = loadGrammar(dom2xmlInputSource(is));
1041            return ((XSGrammar) g).toXSModel();
1042        } catch (Exception JavaDoc e) {
1043            if (fErrorHandler != null) {
1044                DOMErrorImpl error = new DOMErrorImpl();
1045                error.fException = e;
1046                error.fMessage = e.getMessage();
1047                error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
1048                fErrorHandler.getErrorHandler().handleError(error);
1049            }
1050            return null;
1051        }
1052    }
1053
1054    /* (non-Javadoc)
1055     * @see com.sun.org.apache.xerces.internal.xs.XSLoader#loadInputList(com.sun.org.apache.xerces.internal.xs.DOMInputList)
1056     */

1057    public XSModel loadInputList(LSInputList is) {
1058        int length = is.getLength();
1059        if (length == 0) {
1060            return null;
1061        }
1062        SchemaGrammar[] gs = new SchemaGrammar[length];
1063        for (int i = 0; i < length; i++) {
1064            try {
1065                gs[i] = (SchemaGrammar) loadGrammar(dom2xmlInputSource(is.item(i)));
1066            } catch (Exception JavaDoc e) {
1067                if (fErrorHandler != null) {
1068                    DOMErrorImpl error = new DOMErrorImpl();
1069                    error.fException = e;
1070                    error.fMessage = e.getMessage();
1071                    error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
1072                    fErrorHandler.getErrorHandler().handleError(error);
1073                }
1074                return null;
1075            }
1076        }
1077        return new XSModelImpl(gs);
1078    }
1079
1080    /* (non-Javadoc)
1081     * @see com.sun.org.apache.xerces.internal.xs.XSLoader#loadURI(java.lang.String)
1082     */

1083    public XSModel loadURI(String JavaDoc uri) {
1084        try {
1085            Grammar g = loadGrammar(new XMLInputSource(null, uri, null));
1086            return ((XSGrammar)g).toXSModel();
1087        }
1088        catch (Exception JavaDoc e){
1089            if (fErrorHandler != null) {
1090                DOMErrorImpl error = new DOMErrorImpl();
1091                error.fException = e;
1092                error.fMessage = e.getMessage();
1093                error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
1094                fErrorHandler.getErrorHandler().handleError(error);
1095            }
1096            return null;
1097        }
1098    }
1099
1100    /* (non-Javadoc)
1101     * @see com.sun.org.apache.xerces.internal.xs.XSLoader#loadURIList(com.sun.org.apache.xerces.internal.xs.StringList)
1102     */

1103    public XSModel loadURIList(StringList uriList) {
1104        int length = uriList.getLength();
1105        if (length == 0) {
1106            return null;
1107        }
1108        SchemaGrammar[] gs = new SchemaGrammar[length];
1109        for (int i = 0; i < length; i++) {
1110            try {
1111                gs[i] =
1112                    (SchemaGrammar) loadGrammar(new XMLInputSource(null, uriList.item(i), null));
1113            } catch (Exception JavaDoc e) {
1114                if (fErrorHandler != null) {
1115                    DOMErrorImpl error = new DOMErrorImpl();
1116                    error.fException = e;
1117                    error.fMessage = e.getMessage();
1118                    error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
1119                    fErrorHandler.getErrorHandler().handleError(error);
1120                }
1121                return null;
1122            }
1123        }
1124        return new XSModelImpl(gs);
1125    }
1126
1127    /* (non-Javadoc)
1128     * @see org.w3c.dom.DOMConfiguration#canSetParameter(java.lang.String, java.lang.Object)
1129     */

1130    public boolean canSetParameter(String JavaDoc name, Object JavaDoc value) {
1131        if(value instanceof Boolean JavaDoc){
1132// boolean state = ((Boolean)value).booleanValue();
1133
if (name.equals(Constants.DOM_VALIDATE) ||
1134                name.equals(SCHEMA_FULL_CHECKING) ||
1135                name.equals(CONTINUE_AFTER_FATAL_ERROR) ||
1136                name.equals(ALLOW_JAVA_ENCODINGS) ||
1137                name.equals(STANDARD_URI_CONFORMANT_FEATURE)){
1138                    return true;
1139
1140            }
1141            return false;
1142        }
1143        if (name.equals(Constants.DOM_ERROR_HANDLER)||
1144            name.equals(SYMBOL_TABLE) ||
1145            name.equals(ERROR_REPORTER) ||
1146            name.equals(ERROR_HANDLER) ||
1147            name.equals(ENTITY_RESOLVER) ||
1148            name.equals(XMLGRAMMAR_POOL) ||
1149            name.equals(SCHEMA_LOCATION) ||
1150            name.equals(SCHEMA_NONS_LOCATION) ||
1151            name.equals(JAXP_SCHEMA_SOURCE)){
1152                return true;
1153        }
1154        return false;
1155    }
1156
1157    /* (non-Javadoc)
1158     * @see org.w3c.dom.DOMConfiguration#getParameter(java.lang.String)
1159     */

1160    public Object JavaDoc getParameter(String JavaDoc name) throws DOMException JavaDoc {
1161        if (name.equals(Constants.DOM_ERROR_HANDLER)){
1162            if (fErrorHandler != null){
1163                return fErrorHandler.getErrorHandler();
1164            }
1165        }
1166        try {
1167            boolean feature = getFeature(name);
1168            return (feature) ? Boolean.TRUE : Boolean.FALSE;
1169        } catch (Exception JavaDoc e) {
1170            Object JavaDoc property;
1171            try {
1172                property = getProperty(name);
1173                return property;
1174            } catch (Exception JavaDoc ex) {
1175                String JavaDoc msg =
1176                    DOMMessageFormatter.formatMessage(
1177                        DOMMessageFormatter.DOM_DOMAIN,
1178                        "FEATURE_NOT_SUPPORTED",
1179                        new Object JavaDoc[] { name });
1180                throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
1181            }
1182        }
1183    }
1184
1185    /* (non-Javadoc)
1186     * @see org.w3c.dom.DOMConfiguration#getParameterNames()
1187     */

1188    public DOMStringList JavaDoc getParameterNames() {
1189        if (fRecognizedParameters == null){
1190            Vector JavaDoc v = new Vector JavaDoc();
1191            v.add("validate");
1192            v.add(SYMBOL_TABLE);
1193            v.add(ERROR_REPORTER);
1194            v.add(ERROR_HANDLER);
1195            v.add(ENTITY_RESOLVER);
1196            v.add(XMLGRAMMAR_POOL);
1197            v.add(SCHEMA_LOCATION);
1198            v.add(SCHEMA_NONS_LOCATION);
1199            v.add(JAXP_SCHEMA_SOURCE);
1200            v.add(SCHEMA_FULL_CHECKING);
1201            v.add(CONTINUE_AFTER_FATAL_ERROR);
1202            v.add(ALLOW_JAVA_ENCODINGS);
1203            v.add(STANDARD_URI_CONFORMANT_FEATURE);
1204            fRecognizedParameters = new DOMStringListImpl(v);
1205        }
1206        return fRecognizedParameters;
1207    }
1208
1209    /* (non-Javadoc)
1210     * @see org.w3c.dom.DOMConfiguration#setParameter(java.lang.String, java.lang.Object)
1211     */

1212    public void setParameter(String JavaDoc name, Object JavaDoc value) throws DOMException JavaDoc {
1213        if (value instanceof Boolean JavaDoc) {
1214            boolean state = ((Boolean JavaDoc) value).booleanValue();
1215            if (name.equals("validate") && state) {
1216                return;
1217            }
1218            try {
1219                setFeature(name, state);
1220            } catch (Exception JavaDoc e) {
1221                String JavaDoc msg =
1222                    DOMMessageFormatter.formatMessage(
1223                        DOMMessageFormatter.DOM_DOMAIN,
1224                        "FEATURE_NOT_SUPPORTED",
1225                        new Object JavaDoc[] { name });
1226                throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
1227            }
1228            return;
1229        }
1230        if (name.equals(Constants.DOM_ERROR_HANDLER)) {
1231            if (value instanceof DOMErrorHandler JavaDoc) {
1232                try {
1233                    fErrorHandler = new DOMErrorHandlerWrapper((DOMErrorHandler JavaDoc) value);
1234                    setErrorHandler(fErrorHandler);
1235                } catch (XMLConfigurationException e) {
1236                }
1237            } else {
1238                // REVISIT: type mismatch
1239
String JavaDoc msg =
1240                    DOMMessageFormatter.formatMessage(
1241                        DOMMessageFormatter.DOM_DOMAIN,
1242                        "FEATURE_NOT_SUPPORTED",
1243                        new Object JavaDoc[] { name });
1244                throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
1245            }
1246            return;
1247
1248        }
1249
1250        try {
1251            setProperty(name, value);
1252        } catch (Exception JavaDoc ex) {
1253
1254            String JavaDoc msg =
1255                DOMMessageFormatter.formatMessage(
1256                    DOMMessageFormatter.DOM_DOMAIN,
1257                    "FEATURE_NOT_SUPPORTED",
1258                    new Object JavaDoc[] { name });
1259            throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
1260
1261        }
1262
1263    }
1264    
1265    private XMLInputSource dom2xmlInputSource(LSInput JavaDoc is) {
1266        // need to wrap the LSInput with an XMLInputSource
1267
XMLInputSource xis = null;
1268        // if there is a string data, use a StringReader
1269
// according to DOM, we need to treat such data as "UTF-16".
1270
if (is.getStringData() != null) {
1271            xis = new XMLInputSource(is.getPublicId(), is.getSystemId(),
1272                                     is.getBaseURI(), new StringReader JavaDoc(is.getStringData()),
1273                                     "UTF-16");
1274        }
1275        // check whether there is a Reader
1276
// according to DOM, we need to treat such reader as "UTF-16".
1277
else if (is.getCharacterStream() != null) {
1278            xis = new XMLInputSource(is.getPublicId(), is.getSystemId(),
1279                                     is.getBaseURI(), is.getCharacterStream(),
1280                                     "UTF-16");
1281        }
1282        // check whether there is an InputStream
1283
else if (is.getByteStream() != null) {
1284            xis = new XMLInputSource(is.getPublicId(), is.getSystemId(),
1285                                     is.getBaseURI(), is.getByteStream(),
1286                                     is.getEncoding());
1287        }
1288        // otherwise, just use the public/system/base Ids
1289
else {
1290            xis = new XMLInputSource(is.getPublicId(), is.getSystemId(),
1291                                     is.getBaseURI());
1292        }
1293
1294        return xis;
1295    }
1296
1297} // XMLGrammarLoader
1298

1299
Popular Tags