KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > XMLSchemaLoader


1 /*
2  * Copyright 2000-2005 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.impl.xs;
18
19 import java.io.BufferedInputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.io.StringReader JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Locale JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import org.apache.xerces.dom.DOMErrorImpl;
33 import org.apache.xerces.dom.DOMMessageFormatter;
34 import org.apache.xerces.dom.DOMStringListImpl;
35 import org.apache.xerces.impl.Constants;
36 import org.apache.xerces.impl.XMLEntityManager;
37 import org.apache.xerces.impl.XMLErrorReporter;
38 import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
39 import org.apache.xerces.impl.xs.models.CMBuilder;
40 import org.apache.xerces.impl.xs.models.CMNodeFactory;
41 import org.apache.xerces.impl.xs.traversers.XSDHandler;
42 import org.apache.xerces.util.DOMEntityResolverWrapper;
43 import org.apache.xerces.util.DOMErrorHandlerWrapper;
44 import org.apache.xerces.util.DefaultErrorHandler;
45 import org.apache.xerces.util.ParserConfigurationSettings;
46 import org.apache.xerces.util.SymbolTable;
47 import org.apache.xerces.util.XMLSymbols;
48 import org.apache.xerces.xni.XNIException;
49 import org.apache.xerces.xni.grammars.Grammar;
50 import org.apache.xerces.xni.grammars.XMLGrammarDescription;
51 import org.apache.xerces.xni.grammars.XMLGrammarLoader;
52 import org.apache.xerces.xni.grammars.XMLGrammarPool;
53 import org.apache.xerces.xni.grammars.XSGrammar;
54 import org.apache.xerces.xni.parser.XMLComponent;
55 import org.apache.xerces.xni.parser.XMLComponentManager;
56 import org.apache.xerces.xni.parser.XMLConfigurationException;
57 import org.apache.xerces.xni.parser.XMLEntityResolver;
58 import org.apache.xerces.xni.parser.XMLErrorHandler;
59 import org.apache.xerces.xni.parser.XMLInputSource;
60 import org.apache.xerces.xs.LSInputList;
61 import org.apache.xerces.xs.StringList;
62 import org.apache.xerces.xs.XSLoader;
63 import org.apache.xerces.xs.XSModel;
64 import org.w3c.dom.DOMConfiguration JavaDoc;
65 import org.w3c.dom.DOMError JavaDoc;
66 import org.w3c.dom.DOMErrorHandler JavaDoc;
67 import org.w3c.dom.DOMStringList JavaDoc;
68 import org.w3c.dom.DOMException JavaDoc;
69 import org.w3c.dom.ls.LSInput JavaDoc;
70 import org.w3c.dom.ls.LSResourceResolver JavaDoc;
71 import org.xml.sax.InputSource JavaDoc;
72
73 /**
74  * This class implements xni.grammars.XMLGrammarLoader.
75  * It also serves as implementation of xs.XSLoader interface and DOMConfiguration interface.
76  *
77  * This class is designed to interact either with a proxy for a user application
78  * which wants to preparse schemas, or with our own Schema validator.
79  * It is hoped that none of these "external" classes will therefore need to communicate directly
80  * with XSDHandler in future.
81  * <p>This class only knows how to make XSDHandler do its thing.
82  * The caller must ensure that all its properties (schemaLocation, JAXPSchemaSource
83  * etc.) have been properly set.
84  *
85  * @xerces.internal
86  *
87  * @author Neil Graham, IBM
88  * @version $Id: XMLSchemaLoader.java,v 1.42 2005/07/25 04:54:15 mrglavas Exp $
89  */

90
91 public class XMLSchemaLoader implements XMLGrammarLoader, XMLComponent,
92 // XML Component API
93
XSLoader, DOMConfiguration JavaDoc {
94     
95     // Feature identifiers:
96

97     /** Feature identifier: schema full checking*/
98     protected static final String JavaDoc SCHEMA_FULL_CHECKING =
99         Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING;
100     
101     /** Feature identifier: continue after fatal error. */
102     protected static final String JavaDoc CONTINUE_AFTER_FATAL_ERROR =
103         Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE;
104     
105     /** Feature identifier: allow java encodings to be recognized when parsing schema docs. */
106     protected static final String JavaDoc ALLOW_JAVA_ENCODINGS =
107         Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE;
108     
109     /** Feature identifier: standard uri conformant feature. */
110     protected static final String JavaDoc STANDARD_URI_CONFORMANT_FEATURE =
111         Constants.XERCES_FEATURE_PREFIX + Constants.STANDARD_URI_CONFORMANT_FEATURE;
112     
113     /** Feature identifier: validate annotations. */
114     protected static final String JavaDoc VALIDATE_ANNOTATIONS =
115         Constants.XERCES_FEATURE_PREFIX + Constants.VALIDATE_ANNOTATIONS_FEATURE;
116         
117     /** Feature: disallow doctype*/
118     protected static final String JavaDoc DISALLOW_DOCTYPE =
119         Constants.XERCES_FEATURE_PREFIX + Constants.DISALLOW_DOCTYPE_DECL_FEATURE;
120     
121     /** Feature: generate synthetic annotations */
122     protected static final String JavaDoc GENERATE_SYNTHETIC_ANNOTATIONS =
123         Constants.XERCES_FEATURE_PREFIX + Constants.GENERATE_SYNTHETIC_ANNOTATIONS_FEATURE;
124     
125     /** Feature identifier: honour all schemaLocations */
126     protected static final String JavaDoc HONOUR_ALL_SCHEMALOCATIONS =
127         Constants.XERCES_FEATURE_PREFIX + Constants.HONOUR_ALL_SCHEMALOCATIONS_FEATURE;
128     
129     protected static final String JavaDoc AUGMENT_PSVI =
130         Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_AUGMENT_PSVI;
131     
132     protected static final String JavaDoc PARSER_SETTINGS =
133         Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS;
134     
135     // recognized features:
136
private static final String JavaDoc[] RECOGNIZED_FEATURES = {
137         SCHEMA_FULL_CHECKING,
138         AUGMENT_PSVI,
139         CONTINUE_AFTER_FATAL_ERROR,
140         ALLOW_JAVA_ENCODINGS,
141         STANDARD_URI_CONFORMANT_FEATURE,
142         DISALLOW_DOCTYPE,
143         GENERATE_SYNTHETIC_ANNOTATIONS,
144         VALIDATE_ANNOTATIONS,
145         HONOUR_ALL_SCHEMALOCATIONS
146     };
147     
148     // property identifiers
149

150     /** Property identifier: symbol table. */
151     public static final String JavaDoc SYMBOL_TABLE =
152         Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
153     
154     /** Property identifier: error reporter. */
155     public static final String JavaDoc ERROR_REPORTER =
156         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
157     
158     /** Property identifier: error handler. */
159     protected static final String JavaDoc ERROR_HANDLER =
160         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY;
161     
162     /** Property identifier: entity resolver. */
163     public static final String JavaDoc ENTITY_RESOLVER =
164         Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY;
165     
166     /** Property identifier: grammar pool. */
167     public static final String JavaDoc XMLGRAMMAR_POOL =
168         Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
169     
170     /** Property identifier: schema location. */
171     protected static final String JavaDoc SCHEMA_LOCATION =
172         Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_LOCATION;
173     
174     /** Property identifier: no namespace schema location. */
175     protected static final String JavaDoc SCHEMA_NONS_LOCATION =
176         Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_NONS_LOCATION;
177     
178     /** Property identifier: JAXP schema source. */
179     protected static final String JavaDoc JAXP_SCHEMA_SOURCE =
180         Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE;
181     
182     protected static final String JavaDoc SECURITY_MANAGER =
183         Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY;
184     
185     protected static final String JavaDoc ENTITY_MANAGER =
186         Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
187     
188     // recognized properties
189
private static final String JavaDoc [] RECOGNIZED_PROPERTIES = {
190         ENTITY_MANAGER,
191         SYMBOL_TABLE,
192         ERROR_REPORTER,
193         ERROR_HANDLER,
194         ENTITY_RESOLVER,
195         XMLGRAMMAR_POOL,
196         SCHEMA_LOCATION,
197         SCHEMA_NONS_LOCATION,
198         JAXP_SCHEMA_SOURCE,
199         SECURITY_MANAGER
200     };
201     
202     // Data
203

204     // features and properties
205
private ParserConfigurationSettings fLoaderConfig = new ParserConfigurationSettings();
206     private SymbolTable fSymbolTable = null;
207     private XMLErrorReporter fErrorReporter = new XMLErrorReporter ();
208     private XMLEntityManager fEntityManager = null;
209     private XMLEntityResolver fUserEntityResolver = null;
210     private XMLGrammarPool fGrammarPool = null;
211     private String JavaDoc fExternalSchemas = null;
212     private String JavaDoc fExternalNoNSSchema = null;
213     // JAXP property: schema source
214
private Object JavaDoc fJAXPSource = null;
215     // is Schema Full Checking enabled
216
private boolean fIsCheckedFully = false;
217     // boolean that tells whether we've tested the JAXP property.
218
private boolean fJAXPProcessed = false;
219     // if features/properties has not been changed, the value of this attribute is "false"
220
private boolean fSettingsChanged = true;
221     
222     // xml schema parsing
223
private XSDHandler fSchemaHandler;
224     private XSGrammarBucket fGrammarBucket;
225     private XSDeclarationPool fDeclPool = null;
226     private SubstitutionGroupHandler fSubGroupHandler;
227     private CMBuilder fCMBuilder;
228     private XSDDescription fXSDDescription = new XSDDescription();
229     
230     private Hashtable JavaDoc fJAXPCache;
231     private Locale JavaDoc fLocale = Locale.getDefault();
232     
233     // XSLoader attributes
234
private DOMStringList JavaDoc fRecognizedParameters = null;
235     
236     /** DOM L3 error handler */
237     private DOMErrorHandlerWrapper fErrorHandler = null;
238     
239     /** DOM L3 resource resolver */
240     private DOMEntityResolverWrapper fResourceResolver = null;
241     
242     // default constructor. Create objects we absolutely need:
243
public XMLSchemaLoader() {
244         this( new SymbolTable(), null, new XMLEntityManager(), null, null, null);
245     }
246     
247     public XMLSchemaLoader(SymbolTable symbolTable) {
248         this( symbolTable, null, new XMLEntityManager(), null, null, null);
249     }
250     
251     /**
252      * This constractor is used by the XMLSchemaValidator. Additional properties, i.e. XMLEntityManager,
253      * will be passed during reset(XMLComponentManager).
254      * @param errorReporter
255      * @param grammarBucket
256      * @param sHandler
257      * @param builder
258      */

259     XMLSchemaLoader(XMLErrorReporter errorReporter,
260             XSGrammarBucket grammarBucket,
261             SubstitutionGroupHandler sHandler, CMBuilder builder) {
262         this(null, errorReporter, null, grammarBucket, sHandler, builder);
263     }
264     
265     XMLSchemaLoader(SymbolTable symbolTable,
266             XMLErrorReporter errorReporter,
267             XMLEntityManager entityResolver,
268             XSGrammarBucket grammarBucket,
269             SubstitutionGroupHandler sHandler,
270             CMBuilder builder) {
271         
272         // store properties and features in configuration
273
fLoaderConfig.addRecognizedFeatures(RECOGNIZED_FEATURES);
274         fLoaderConfig.addRecognizedProperties(RECOGNIZED_PROPERTIES);
275         if (symbolTable != null){
276             fLoaderConfig.setProperty(SYMBOL_TABLE, symbolTable);
277         }
278         
279         if(errorReporter == null) {
280             errorReporter = new XMLErrorReporter ();
281             errorReporter.setLocale(fLocale);
282             errorReporter.setProperty(ERROR_HANDLER, new DefaultErrorHandler());
283             
284         }
285         fErrorReporter = errorReporter;
286         // make sure error reporter knows about schemas...
287
if(fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) {
288             fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new XSMessageFormatter());
289         }
290         fLoaderConfig.setProperty(ERROR_REPORTER, fErrorReporter);
291         fEntityManager = entityResolver;
292         // entity manager is null if XMLSchemaValidator creates the loader
293
if (fEntityManager != null){
294             fLoaderConfig.setProperty(ENTITY_MANAGER, fEntityManager);
295         }
296         
297         // by default augment PSVI (i.e. don't use declaration pool)
298
fLoaderConfig.setFeature(AUGMENT_PSVI, true);
299         
300         if(grammarBucket == null ) {
301             grammarBucket = new XSGrammarBucket();
302         }
303         fGrammarBucket = grammarBucket;
304         if(sHandler == null) {
305             sHandler = new SubstitutionGroupHandler(fGrammarBucket);
306         }
307         fSubGroupHandler = sHandler;
308         
309         //get an instance of the CMNodeFactory */
310
CMNodeFactory nodeFactory = new CMNodeFactory() ;
311         
312         if(builder == null) {
313             builder = new CMBuilder(nodeFactory);
314         }
315         fCMBuilder = builder;
316         fSchemaHandler = new XSDHandler(fGrammarBucket);
317         fDeclPool = new XSDeclarationPool();
318         fJAXPCache = new Hashtable JavaDoc();
319         
320         fSettingsChanged = true;
321     }
322     
323     /**
324      * Returns a list of feature identifiers that are recognized by
325      * this XMLGrammarLoader. This method may return null if no features
326      * are recognized.
327      */

328     public String JavaDoc[] getRecognizedFeatures() {
329         return (String JavaDoc[])(RECOGNIZED_FEATURES.clone());
330     } // getRecognizedFeatures(): String[]
331

332     /**
333      * Returns the state of a feature.
334      *
335      * @param featureId The feature identifier.
336      *
337      * @throws XMLConfigurationException Thrown on configuration error.
338      */

339     public boolean getFeature(String JavaDoc featureId)
340     throws XMLConfigurationException {
341         return fLoaderConfig.getFeature(featureId);
342     } // getFeature (String): boolean
343

344     /**
345      * Sets the state of a feature.
346      *
347      * @param featureId The feature identifier.
348      * @param state The state of the feature.
349      *
350      * @throws XMLConfigurationException Thrown when a feature is not
351      * recognized or cannot be set.
352      */

353     public void setFeature(String JavaDoc featureId,
354             boolean state) throws XMLConfigurationException {
355         fSettingsChanged = true;
356         if(featureId.equals(CONTINUE_AFTER_FATAL_ERROR)) {
357             fErrorReporter.setFeature(CONTINUE_AFTER_FATAL_ERROR, state);
358         }
359         else if(featureId.equals(GENERATE_SYNTHETIC_ANNOTATIONS)) {
360             fSchemaHandler.setGenerateSyntheticAnnotations(state);
361         }
362         fLoaderConfig.setFeature(featureId, state);
363     } // setFeature(String, boolean)
364

365     /**
366      * Returns a list of property identifiers that are recognized by
367      * this XMLGrammarLoader. This method may return null if no properties
368      * are recognized.
369      */

370     public String JavaDoc[] getRecognizedProperties() {
371         return (String JavaDoc[])(RECOGNIZED_PROPERTIES.clone());
372     } // getRecognizedProperties(): String[]
373

374     /**
375      * Returns the state of a property.
376      *
377      * @param propertyId The property identifier.
378      *
379      * @throws XMLConfigurationException Thrown on configuration error.
380      */

381     public Object JavaDoc getProperty(String JavaDoc propertyId)
382     throws XMLConfigurationException {
383         return fLoaderConfig.getProperty(propertyId);
384     } // getProperty(String): Object
385

386     /**
387      * Sets the state of a property.
388      *
389      * @param propertyId The property identifier.
390      * @param state The state of the property.
391      *
392      * @throws XMLConfigurationException Thrown when a property is not
393      * recognized or cannot be set.
394      */

395     public void setProperty(String JavaDoc propertyId,
396             Object JavaDoc state) throws XMLConfigurationException {
397         fSettingsChanged = true;
398         fLoaderConfig.setProperty(propertyId, state);
399         if(propertyId.equals( JAXP_SCHEMA_SOURCE)) {
400             fJAXPSource = state;
401             fJAXPProcessed = false;
402         }
403         else if(propertyId.equals( XMLGRAMMAR_POOL)) {
404             fGrammarPool = (XMLGrammarPool)state;
405         }
406         else if (propertyId.equals(SCHEMA_LOCATION)){
407             fExternalSchemas = (String JavaDoc)state;
408         }
409         else if (propertyId.equals(SCHEMA_NONS_LOCATION)){
410             fExternalNoNSSchema = (String JavaDoc) state;
411         }
412         else if (propertyId.equals(ENTITY_RESOLVER)){
413             fEntityManager.setProperty(ENTITY_RESOLVER, state);
414         }
415         else if (propertyId.equals(ERROR_REPORTER)){
416             fErrorReporter = (XMLErrorReporter)state;
417             if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) {
418                 fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new XSMessageFormatter());
419             }
420         }
421     } // setProperty(String, Object)
422

423     /**
424      * Set the locale to use for messages.
425      *
426      * @param locale The locale object to use for localization of messages.
427      *
428      * @exception XNIException Thrown if the parser does not support the
429      * specified locale.
430      */

431     public void setLocale(Locale JavaDoc locale) {
432         fLocale = locale;
433         fErrorReporter.setLocale(locale);
434     } // setLocale(Locale)
435

436     /** Return the Locale the XMLGrammarLoader is using. */
437     public Locale JavaDoc getLocale() {
438         return fLocale;
439     } // getLocale(): Locale
440

441     /**
442      * Sets the error handler.
443      *
444      * @param errorHandler The error handler.
445      */

446     public void setErrorHandler(XMLErrorHandler errorHandler) {
447         fErrorReporter.setProperty(ERROR_HANDLER, errorHandler);
448     } // setErrorHandler(XMLErrorHandler)
449

450     /** Returns the registered error handler. */
451     public XMLErrorHandler getErrorHandler() {
452         return fErrorReporter.getErrorHandler();
453     } // getErrorHandler(): XMLErrorHandler
454

455     /**
456      * Sets the entity resolver.
457      *
458      * @param entityResolver The new entity resolver.
459      */

460     public void setEntityResolver(XMLEntityResolver entityResolver) {
461         fUserEntityResolver = entityResolver;
462         fLoaderConfig.setProperty(ENTITY_RESOLVER, entityResolver);
463         fEntityManager.setProperty(ENTITY_RESOLVER, entityResolver);
464     } // setEntityResolver(XMLEntityResolver)
465

466     /** Returns the registered entity resolver. */
467     public XMLEntityResolver getEntityResolver() {
468         return fUserEntityResolver;
469     } // getEntityResolver(): XMLEntityResolver
470

471     /**
472      * Returns a Grammar object by parsing the contents of the
473      * entities pointed to by sources.
474      *
475      * @param source[] the locations of the entity which forms
476      * the staring point of the grammars to be constructed
477      * @throws IOException when a problem is encounted reading the entity
478      * @throws XNIException when a condition arises (such as a FatalError) that requires parsing
479      * of the entity be terminated
480      */

481     public void loadGrammar(XMLInputSource source[])
482     throws IOException JavaDoc, XNIException {
483         int numSource = source.length;
484         for (int i = 0; i < numSource; ++i) {
485             loadGrammar(source[i]);
486         }
487     }
488     
489     /**
490      * Returns a Grammar object by parsing the contents of the
491      * entity pointed to by source.
492      *
493      * @param source the location of the entity which forms
494      * the starting point of the grammar to be constructed.
495      * @throws IOException When a problem is encountered reading the entity
496      * XNIException When a condition arises (such as a FatalError) that requires parsing
497      * of the entity be terminated.
498      */

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

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

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

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

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

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

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

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

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

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

882         public String JavaDoc [] getLocationArray(){
883             if(length < locations.length ){
884                 resize(locations.length, length);
885             }
886             return locations;
887         }//getLocationArray()
888

889         public String JavaDoc getFirstLocation(){
890             return length > 0 ? locations[0] : null;
891         }
892         
893         public int getLength(){
894             return length ;
895         }
896         
897     } //locationArray
898

899     /* (non-Javadoc)
900      * @see org.apache.xerces.xni.parser.XMLComponent#getFeatureDefault(java.lang.String)
901      */

902     public Boolean JavaDoc getFeatureDefault(String JavaDoc featureId) {
903         if (featureId.equals(AUGMENT_PSVI)){
904             return Boolean.TRUE;
905         }
906         return null;
907     }
908     
909     /* (non-Javadoc)
910      * @see org.apache.xerces.xni.parser.XMLComponent#getPropertyDefault(java.lang.String)
911      */

912     public Object JavaDoc getPropertyDefault(String JavaDoc propertyId) {
913         // TODO Auto-generated method stub
914
return null;
915     }
916     
917     /* (non-Javadoc)
918      * @see org.apache.xerces.xni.parser.XMLComponent#reset(org.apache.xerces.xni.parser.XMLComponentManager)
919      */

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

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

1040    public XSModel load(LSInput JavaDoc is) {
1041        try {
1042            Grammar g = loadGrammar(dom2xmlInputSource(is));
1043            return ((XSGrammar) g).toXSModel();
1044        } catch (Exception JavaDoc e) {
1045            reportDOMFatalError(e);
1046            return null;
1047        }
1048    }
1049    
1050    /* (non-Javadoc)
1051     * @see org.apache.xerces.xs.XSLoader#loadInputList(org.apache.xerces.xs.DOMInputList)
1052     */

1053    public XSModel loadInputList(LSInputList is) {
1054        int length = is.getLength();
1055        if (length == 0) {
1056            return null;
1057        }
1058        SchemaGrammar[] gs = new SchemaGrammar[length];
1059        for (int i = 0; i < length; i++) {
1060            try {
1061                gs[i] = (SchemaGrammar) loadGrammar(dom2xmlInputSource(is.item(i)));
1062            } catch (Exception JavaDoc e) {
1063                reportDOMFatalError(e);
1064                return null;
1065            }
1066        }
1067        return new XSModelImpl(gs);
1068    }
1069    
1070    /* (non-Javadoc)
1071     * @see org.apache.xerces.xs.XSLoader#loadURI(java.lang.String)
1072     */

1073    public XSModel loadURI(String JavaDoc uri) {
1074        try {
1075            Grammar g = loadGrammar(new XMLInputSource(null, uri, null));
1076            return ((XSGrammar)g).toXSModel();
1077        }
1078        catch (Exception JavaDoc e){
1079            reportDOMFatalError(e);
1080            return null;
1081        }
1082    }
1083    
1084    /* (non-Javadoc)
1085     * @see org.apache.xerces.xs.XSLoader#loadURIList(org.apache.xerces.xs.StringList)
1086     */

1087    public XSModel loadURIList(StringList uriList) {
1088        int length = uriList.getLength();
1089        if (length == 0) {
1090            return null;
1091        }
1092        SchemaGrammar[] gs = new SchemaGrammar[length];
1093        for (int i = 0; i < length; i++) {
1094            try {
1095                gs[i] =
1096                    (SchemaGrammar) loadGrammar(new XMLInputSource(null, uriList.item(i), null));
1097            } catch (Exception JavaDoc e) {
1098                reportDOMFatalError(e);
1099                return null;
1100            }
1101        }
1102        return new XSModelImpl(gs);
1103    }
1104    
1105    void reportDOMFatalError(Exception JavaDoc e) {
1106                if (fErrorHandler != null) {
1107                    DOMErrorImpl error = new DOMErrorImpl();
1108                    error.fException = e;
1109                    error.fMessage = e.getMessage();
1110                    error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
1111                    fErrorHandler.getErrorHandler().handleError(error);
1112                }
1113            }
1114    
1115    /* (non-Javadoc)
1116     * @see org.apache.xerces.dom3.DOMConfiguration#canSetParameter(java.lang.String, java.lang.Object)
1117     */

1118    public boolean canSetParameter(String JavaDoc name, Object JavaDoc value) {
1119        if(value instanceof Boolean JavaDoc){
1120            if (name.equals(Constants.DOM_VALIDATE) ||
1121                name.equals(SCHEMA_FULL_CHECKING) ||
1122                name.equals(VALIDATE_ANNOTATIONS) ||
1123                name.equals(CONTINUE_AFTER_FATAL_ERROR) ||
1124                name.equals(ALLOW_JAVA_ENCODINGS) ||
1125                name.equals(STANDARD_URI_CONFORMANT_FEATURE) ||
1126                name.equals(GENERATE_SYNTHETIC_ANNOTATIONS) ||
1127                name.equals(HONOUR_ALL_SCHEMALOCATIONS)) {
1128                return true;
1129                
1130            }
1131            return false;
1132        }
1133        if (name.equals(Constants.DOM_ERROR_HANDLER) ||
1134            name.equals(Constants.DOM_RESOURCE_RESOLVER) ||
1135            name.equals(SYMBOL_TABLE) ||
1136            name.equals(ERROR_REPORTER) ||
1137            name.equals(ERROR_HANDLER) ||
1138            name.equals(ENTITY_RESOLVER) ||
1139            name.equals(XMLGRAMMAR_POOL) ||
1140            name.equals(SCHEMA_LOCATION) ||
1141            name.equals(SCHEMA_NONS_LOCATION) ||
1142            name.equals(JAXP_SCHEMA_SOURCE)) {
1143            return true;
1144        }
1145        return false;
1146    }
1147    
1148    /* (non-Javadoc)
1149     * @see org.apache.xerces.dom3.DOMConfiguration#getParameter(java.lang.String)
1150     */

1151    public Object JavaDoc getParameter(String JavaDoc name) throws DOMException JavaDoc {
1152        
1153        if (name.equals(Constants.DOM_ERROR_HANDLER)){
1154            return (fErrorHandler != null) ? fErrorHandler.getErrorHandler() : null;
1155        }
1156        else if (name.equals(Constants.DOM_RESOURCE_RESOLVER)) {
1157            return (fResourceResolver != null) ? fResourceResolver.getEntityResolver() : null;
1158        }
1159        
1160        try {
1161            boolean feature = getFeature(name);
1162            return (feature) ? Boolean.TRUE : Boolean.FALSE;
1163        } catch (Exception JavaDoc e) {
1164            Object JavaDoc property;
1165            try {
1166                property = getProperty(name);
1167                return property;
1168            } catch (Exception JavaDoc ex) {
1169                String JavaDoc msg =
1170                    DOMMessageFormatter.formatMessage(
1171                            DOMMessageFormatter.DOM_DOMAIN,
1172                            "FEATURE_NOT_SUPPORTED",
1173                            new Object JavaDoc[] { name });
1174                throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
1175            }
1176        }
1177    }
1178    
1179    /* (non-Javadoc)
1180     * @see org.apache.xerces.dom3.DOMConfiguration#getParameterNames()
1181     */

1182    public DOMStringList JavaDoc getParameterNames() {
1183        if (fRecognizedParameters == null){
1184            Vector JavaDoc v = new Vector JavaDoc();
1185            v.add(Constants.DOM_VALIDATE);
1186            v.add(Constants.DOM_ERROR_HANDLER);
1187            v.add(Constants.DOM_RESOURCE_RESOLVER);
1188            v.add(SYMBOL_TABLE);
1189            v.add(ERROR_REPORTER);
1190            v.add(ERROR_HANDLER);
1191            v.add(ENTITY_RESOLVER);
1192            v.add(XMLGRAMMAR_POOL);
1193            v.add(SCHEMA_LOCATION);
1194            v.add(SCHEMA_NONS_LOCATION);
1195            v.add(JAXP_SCHEMA_SOURCE);
1196            v.add(SCHEMA_FULL_CHECKING);
1197            v.add(CONTINUE_AFTER_FATAL_ERROR);
1198            v.add(ALLOW_JAVA_ENCODINGS);
1199            v.add(STANDARD_URI_CONFORMANT_FEATURE);
1200            v.add(VALIDATE_ANNOTATIONS);
1201            v.add(GENERATE_SYNTHETIC_ANNOTATIONS);
1202            v.add(HONOUR_ALL_SCHEMALOCATIONS);
1203            fRecognizedParameters = new DOMStringListImpl(v);
1204        }
1205        return fRecognizedParameters;
1206    }
1207    
1208    /* (non-Javadoc)
1209     * @see org.apache.xerces.dom3.DOMConfiguration#setParameter(java.lang.String, java.lang.Object)
1210     */

1211    public void setParameter(String JavaDoc name, Object JavaDoc value) throws DOMException JavaDoc {
1212        if (value instanceof Boolean JavaDoc) {
1213            boolean state = ((Boolean JavaDoc) value).booleanValue();
1214            if (name.equals("validate") && state) {
1215                return;
1216            }
1217            try {
1218                setFeature(name, state);
1219            } catch (Exception JavaDoc e) {
1220                String JavaDoc msg =
1221                    DOMMessageFormatter.formatMessage(
1222                            DOMMessageFormatter.DOM_DOMAIN,
1223                            "FEATURE_NOT_SUPPORTED",
1224                            new Object JavaDoc[] { name });
1225                throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
1226            }
1227            return;
1228        }
1229        if (name.equals(Constants.DOM_ERROR_HANDLER)) {
1230            if (value instanceof DOMErrorHandler JavaDoc) {
1231                try {
1232                    fErrorHandler = new DOMErrorHandlerWrapper((DOMErrorHandler JavaDoc) value);
1233                    setErrorHandler(fErrorHandler);
1234                } catch (XMLConfigurationException e) {
1235                }
1236            } else {
1237                // REVISIT: type mismatch
1238
String JavaDoc msg =
1239                    DOMMessageFormatter.formatMessage(
1240                            DOMMessageFormatter.DOM_DOMAIN,
1241                            "FEATURE_NOT_SUPPORTED",
1242                            new Object JavaDoc[] { name });
1243                throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
1244            }
1245            return;
1246            
1247        }
1248        if (name.equals(Constants.DOM_RESOURCE_RESOLVER)) {
1249            if (value instanceof LSResourceResolver JavaDoc) {
1250                try {
1251                    fResourceResolver = new DOMEntityResolverWrapper((LSResourceResolver JavaDoc) value);
1252                    setEntityResolver(fResourceResolver);
1253                }
1254                catch (XMLConfigurationException e) {}
1255            } else {
1256                // REVISIT: type mismatch
1257
String JavaDoc msg =
1258                    DOMMessageFormatter.formatMessage(
1259                            DOMMessageFormatter.DOM_DOMAIN,
1260                            "FEATURE_NOT_SUPPORTED",
1261                            new Object JavaDoc[] { name });
1262                throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
1263            }
1264            return;
1265        }
1266        
1267        try {
1268            setProperty(name, value);
1269        } catch (Exception JavaDoc ex) {
1270            
1271            String JavaDoc msg =
1272                DOMMessageFormatter.formatMessage(
1273                        DOMMessageFormatter.DOM_DOMAIN,
1274                        "FEATURE_NOT_SUPPORTED",
1275                        new Object JavaDoc[] { name });
1276            throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
1277            
1278        }
1279        
1280    }
1281    
1282    XMLInputSource dom2xmlInputSource(LSInput JavaDoc is) {
1283        // need to wrap the LSInput with an XMLInputSource
1284
XMLInputSource xis = null;
1285        
1286        /**
1287         * An LSParser looks at inputs specified in LSInput in
1288         * the following order: characterStream, byteStream,
1289         * stringData, systemId, publicId. For consistency
1290         * have the same behaviour for XSLoader.
1291         */

1292        
1293        // check whether there is a Reader
1294
// according to DOM, we need to treat such reader as "UTF-16".
1295
if (is.getCharacterStream() != null) {
1296            xis = new XMLInputSource(is.getPublicId(), is.getSystemId(),
1297                    is.getBaseURI(), is.getCharacterStream(),
1298            "UTF-16");
1299        }
1300        // check whether there is an InputStream
1301
else if (is.getByteStream() != null) {
1302            xis = new XMLInputSource(is.getPublicId(), is.getSystemId(),
1303                    is.getBaseURI(), is.getByteStream(),
1304                    is.getEncoding());
1305        }
1306        // if there is a string data, use a StringReader
1307
// according to DOM, we need to treat such data as "UTF-16".
1308
else if (is.getStringData() != null && is.getStringData().length() != 0) {
1309            xis = new XMLInputSource(is.getPublicId(), is.getSystemId(),
1310                    is.getBaseURI(), new StringReader JavaDoc(is.getStringData()),
1311            "UTF-16");
1312        }
1313        // otherwise, just use the public/system/base Ids
1314
else {
1315            xis = new XMLInputSource(is.getPublicId(), is.getSystemId(),
1316                    is.getBaseURI());
1317        }
1318        
1319        return xis;
1320    }
1321    
1322} // XMLGrammarLoader
1323

1324
Popular Tags