KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > loader > Loader


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.schema.loader;
24
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import org.xml.sax.*;
31 import org.xquark.schema.*;
32
33 public class Loader extends org.xquark.util.SAXLoader
34 implements SchemaConstants, ErrorHandler {
35 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
36 private static final String JavaDoc RCSName = "$Name: $";
37   
38   private Schema schema = null;
39   private boolean topLevel = false;
40   private SchemaManager manager;
41   private SchemaLocator locator;
42   private boolean noIncludeTargetNamespace = false;
43   
44   private HashMap JavaDoc componentContextLocators = new HashMap JavaDoc();
45   
46   public Loader(SchemaManager manager) {
47     this(manager, manager, null);
48   }
49   
50   public Loader(SchemaManager manager, SchemaLocator locator) {
51     this(manager, locator, null);
52   }
53   
54   public Loader(SchemaManager manager, SchemaLocator locator, Schema schema) {
55     super(null);
56     this.manager = manager;
57     this.locator = locator;
58     this.schema = schema;
59     if (schema == null) topLevel = true;
60     setElementHandler(new SchemaHandler());
61   }
62   
63   public Schema getSchema() throws SAXException {
64     return schema;
65   }
66   
67   public String JavaDoc getDocumentBase() {
68     if (getDocumentLocator() != null)
69       return getDocumentLocator().getSystemId();
70     return null;
71   }
72     
73   public Schema load(URL JavaDoc url) throws java.io.IOException JavaDoc, SAXException {
74     org.xml.sax.InputSource JavaDoc source = new org.xml.sax.InputSource JavaDoc(url.openStream());
75     source.setSystemId(url.toString());
76     return load(source);
77   }
78   
79   public Schema load(org.xml.sax.InputSource JavaDoc source) throws SAXException, java.io.IOException JavaDoc {
80     org.xml.sax.XMLReader JavaDoc reader = locator.getSchemaReader();
81     reader.setContentHandler(this);
82     reader.setErrorHandler(this);
83     reader.parse(source);
84     return schema;
85   }
86   
87   private void setComponentContextLocators(HashMap JavaDoc map) {
88     componentContextLocators = map;
89   }
90   
91   private Schema resolvePrefix(String JavaDoc qname)
92   throws SchemaException {
93     String JavaDoc prefix = "";
94     int index = qname.indexOf(':');
95     if (index > 0) prefix = qname.substring(0, index);
96     String JavaDoc ns = getPrefixMapping(prefix);
97     if (ns == null && !prefix.equals("")) {
98 // "Unknown namespace prefix: " + prefix;
99
throw new SchemaException("src-qname.1.1");
100     }
101     
102     if ((ns == null && schema.getNamespace() == null)
103     || (ns != null && ns.equals(schema.getNamespace())))
104       return schema;
105     if ( ns == null && noIncludeTargetNamespace ) // it's a inlcuded schema without namespace
106
return schema;
107     if (schema.isImported(ns))
108       return manager.getSchema(ns);
109     
110 // "No schema for namespace: " + ns;
111
throw new SchemaException("src-resolve.4");
112   }
113   
114   // initilization schema
115
private void initialize() throws SAXException {
116     try {
117       InitializationVisitor initializer = new InitializationVisitor(schema, componentContextLocators, this);
118
119       java.util.Iterator JavaDoc it = schema.getTypes().iterator();
120       while (it.hasNext()) ((SchemaVisitable)it.next()).accept(initializer);
121
122       it = schema.getElementDeclarations().iterator();
123       while (it.hasNext()) ((SchemaVisitable)it.next()).accept(initializer);
124
125       it = schema.getAttributeDeclarations().iterator();
126       while (it.hasNext()) ((SchemaVisitable)it.next()).accept(initializer);
127       
128       it = schema.getAttributeGroupDefinitions().iterator();
129       while (it.hasNext()) ((SchemaVisitable)it.next()).accept(initializer);
130     }
131     catch ( SchemaException se ) {
132       Object JavaDoc invalidObject = se.getInvalidObject();
133       Locator componentLocator = (Locator)componentContextLocators.get(invalidObject);
134       throw new SAXParseException(se.getMessage(), componentLocator, se);
135     }
136   }
137   
138   public void addComp2ComponentContextLocators(Object JavaDoc newComp, Object JavaDoc oldComp) {
139       Object JavaDoc componentLocator = componentContextLocators.get(oldComp);
140       if ( componentLocator != null )
141         componentContextLocators.put(newComp, componentLocator);
142   }
143   
144   public org.xml.sax.Locator JavaDoc getComponentLocaltor(Object JavaDoc obj) {
145     return (org.xml.sax.Locator JavaDoc)componentContextLocators.get(obj);
146   }
147     
148   public boolean isSchemaNamespace(String JavaDoc uri) {
149     return uri.equals(XMLSCHEMA_URI);
150   }
151     
152   private String JavaDoc getLocalName(String JavaDoc qname) {
153     int index = qname.indexOf(':');
154     if (index > 0) return qname.substring(index+1);
155     else return qname;
156   }
157   
158   private Loader getLoader() {
159     return this;
160   }
161   
162   public Type getType(String JavaDoc ref) throws SchemaException {
163     if (ref == null) return null;
164     
165     TypeRef typeRef = new TypeRef(resolvePrefix(ref), getLocalName(ref));
166     
167     // store namespace context and locator
168
ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator());
169     componentContextLocators.put(typeRef, contextLoc);
170     
171     return typeRef;
172   }
173   
174   public SchemaComponentRef getRef(String JavaDoc ref, int type) throws SchemaException {
175     if (ref == null) return null;
176     
177     SchemaComponentRef compRef = new SchemaComponentRef(resolvePrefix(ref), getLocalName(ref), type);
178         
179     // store namespace context and locator
180
ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator());
181     componentContextLocators.put(compRef, contextLoc);
182     
183     return compRef;
184   }
185     
186   public Facet buildFacet(String JavaDoc localName, Attributes atts) {
187     Facet result = null;
188     for (int i = 0; i < FACETS.length; i++) {
189       if (FACETS[i].equals(localName)) {
190         result = new Facet(localName, atts.getValue("", VALUE_ATTR), atts.getValue("", FIXED_ATTR));
191         break;
192       }
193     }
194     
195     // store namespace context and locator
196
ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator());
197     componentContextLocators.put(result, contextLoc);
198     
199     return result;
200   }
201   
202   public NotationDeclaration buildNotation(Attributes atts) {
203     String JavaDoc name = atts.getValue("", NAME_ATTR);
204     String JavaDoc publicId = atts.getValue("", PUBLIC_ATTR);
205     String JavaDoc systemId = atts.getValue("", SYSTEM_ATTR);
206     NotationDeclaration result = new NotationDeclaration(schema, name, publicId, systemId);
207     
208     // store namespace context and locator
209
ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator());
210     componentContextLocators.put(result, contextLoc);
211
212     return result;
213   }
214   
215   private int getMethod(String JavaDoc method, int mask) {
216     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(method);
217     int result = 0;
218     while (st.hasMoreTokens()) {
219       String JavaDoc tok = st.nextToken();
220       if ((LIST & mask) != 0 && LIST_VALUE.equals(tok))
221         result |= LIST;
222       else if ((UNION & mask) != 0 && UNION_VALUE.equals(tok))
223         result |= UNION;
224       else if ((RESTRICTION & mask) != 0 && RESTRICTION_VALUE.equals(tok))
225         result |= RESTRICTION;
226       else if ((EXTENSION & mask) != 0 && EXTENSION_VALUE.equals(tok))
227         result |= EXTENSION;
228       else if ((SUBSTITUTION & mask) != 0 && SUBSTITUTION_VALUE.equals(tok))
229         result |= SUBSTITUTION;
230       else if (ALL_VALUE.equals(tok))
231         return mask;
232     }
233     return result;
234   }
235   
236   private void setOccurrence(Particle particle, Attributes atts)
237   throws SchemaException {
238     String JavaDoc minOccurs = atts.getValue("", MIN_OCCURS_ATTR);
239     if (minOccurs != null) {
240       try {
241         particle.setMinOccurs(Integer.parseInt(minOccurs));
242       }
243       catch (NumberFormatException JavaDoc ex) {
244           // @@ should not occur (schema)
245
}
246     }
247     
248     String JavaDoc maxOccurs = atts.getValue("", MAX_OCCURS_ATTR);
249     if (maxOccurs != null) {
250       if (maxOccurs.equals(UNBOUNDED_VALUE)) {
251         particle.setMaxOccursUnbounded();
252       }
253       else {
254         try {
255           particle.setMaxOccurs(Integer.parseInt(maxOccurs));
256         }
257         catch (NumberFormatException JavaDoc ex) {
258           // @@ should not occur (schema)
259
}
260       }
261     }
262     
263     // check minOccurs and maxOccurs
264
int nMin = particle.getMinOccurs();
265     int nMax = particle.getMaxOccurs();
266     
267     if ( nMax < nMin ) {
268 // "minOccurs must not be greater than maxOccurs.";
269
throw new SchemaException("p-props-correct.2.1");
270     }
271   }
272   
273   public Particle buildParticle(String JavaDoc localName, SchemaScope scope, Attributes atts)
274   throws SchemaException {
275     Particle result = null;
276     if (localName.equals(ELEMENT_TAG)) {
277       result = new Particle(buildElementDeclaration(atts, scope));
278     }
279     else if (localName.equals(GROUP_TAG)) {
280       String JavaDoc refName = atts.getValue("", REF_ATTR);
281       result = new Particle(getRef(refName, Schema.MODEL_GROUP_DEFINITION));
282     }
283     else if (localName.equals(SEQUENCE_TAG)) {
284       result = new Particle(new SequenceModelGroup());
285     }
286     else if (localName.equals(CHOICE_TAG)) {
287       result = new Particle(new ChoiceModelGroup());
288     }
289     else if (localName.equals(ALL_TAG)) {
290       result = new Particle(new AllModelGroup());
291     }
292     else if (localName.equals(ANY_TAG)) {
293       result = new Particle(buildWildcard(atts));
294     }
295     
296     if (result != null) {
297       setOccurrence(result, atts);
298     }
299     
300     // store namespace context and locator
301
ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator());
302     componentContextLocators.put(result, contextLoc);
303     
304     return result;
305   }
306   
307   public Wildcard buildWildcard(Attributes atts)
308   throws SchemaException {
309     Wildcard result = new Wildcard(schema, true);
310     String JavaDoc namespaces = atts.getValue("", NAMESPACE_ATTR);
311     if (namespaces != null) {
312       if (OTHER_VALUE.equals(namespaces)) {
313         result.exclude(schema.getNamespace());
314       } else if (!ANY_VALUE.equals(namespaces)) {
315         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(namespaces);
316         while (st.hasMoreTokens()) {
317           String JavaDoc tok = st.nextToken();
318           if (TARGET_NAMESPACE_VALUE.equals(tok)) {
319             result.add(schema.getNamespace());
320           } else if (LOCAL_VALUE.equals(tok)) {
321             result.add(null);
322           } else {
323             result.add(tok);
324           }
325         }
326       }
327     }
328     String JavaDoc process = atts.getValue("", PROCESS_CONTENTS_ATTR);
329     if (process != null) {
330       if (LAX_VALUE.equals(process)) result.setProcessContents(LAX);
331       else if (SKIP_VALUE.equals(process)) result.setProcessContents(SKIP);
332       else if (STRICT_VALUE.equals(process)) result.setProcessContents(STRICT);
333     }
334     
335     // store namespace context and locator
336
ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator());
337     componentContextLocators.put(result, contextLoc);
338
339     return result;
340   }
341   
342   // Schema creation
343
private void initSchema(Attributes atts)
344   throws SchemaException {
345     String JavaDoc targetNamespace = atts.getValue("", TARGET_NAMESPACE_ATTR);
346     if ( XSI_URI.equals(targetNamespace) &&
347          getDocumentBase() != null &&
348          !getDocumentBase().endsWith("XMLSchema-instance.xsd")) {
349         throw new SchemaException("no-xsi");
350     }
351     if (schema == null) {
352       String JavaDoc schemaLocation = null;
353       if (getDocumentLocator() != null)
354         schemaLocation = getDocumentLocator().getSystemId();
355       schema = new Schema(targetNamespace, schemaLocation, manager);
356     } else if (targetNamespace != null && !targetNamespace.equals(schema.getNamespace())) {
357 // "Included target namespace must be equal to including namespace";
358
throw new SchemaException("src-include.2.1");
359     } else if ( targetNamespace == null ) {
360       noIncludeTargetNamespace = true;
361     }
362     
363     schema.setDefault(ATTRIBUTE_FORM_DEFAULT_ATTR,
364     atts.getValue("", ATTRIBUTE_FORM_DEFAULT_ATTR));
365     schema.setDefault(BLOCK_DEFAULT_ATTR,
366     atts.getValue("", BLOCK_DEFAULT_ATTR));
367     schema.setDefault(ELEMENT_FORM_DEFAULT_ATTR,
368     atts.getValue("", ELEMENT_FORM_DEFAULT_ATTR));
369     schema.setDefault(FINAL_DEFAULT_ATTR,
370     atts.getValue("", FINAL_DEFAULT_ATTR));
371     
372     schema.setPrefixMap(getElementPrefixMap());
373   }
374   
375   private URL JavaDoc resolveLocation(String JavaDoc location, String JavaDoc base) throws SAXException {
376     URL JavaDoc context = null;
377     if (base != null) {
378       try {
379         context = new URL JavaDoc(base);
380       }
381       catch (MalformedURLException JavaDoc ex0) {
382     //@commented out to support invalid URL returned by a locator, in the case of import
383
//reportLoadingError(new SchemaException("schema_reference"));
384
}
385     }
386     
387     try {
388       return new URL JavaDoc(context, location);
389     }
390     catch (MalformedURLException JavaDoc ex1) {
391       try {
392         java.io.File JavaDoc file = new java.io.File JavaDoc(location);
393         return file.toURL();
394       }
395       catch (MalformedURLException JavaDoc ex2) {
396     //@commented out to support invalid URL returned by a locator, in the case of import
397
//reportLoadingError(new SchemaException("schema_reference"));
398
}
399     }
400     
401     return null;
402   }
403     
404   private void includeSchema(Attributes atts)
405   throws SAXException {
406     String JavaDoc location = atts.getValue("", SCHEMA_LOCATION_ATTR);
407     location = locator.resolveInclude(location);
408     String JavaDoc schemaLocation = null;
409     if (getDocumentLocator() != null)
410       schemaLocation = getDocumentLocator().getSystemId();
411     
412     URL JavaDoc url = null;
413     url = resolveLocation(location, schemaLocation);
414     if (url == null)
415       reportLoadingError("Could not resolve location "+location, new SchemaException("schema_reference"));
416     location = url.toString();
417     if (!schema.isIncluded(location)) {
418       schema.addIncludedLocation(location);
419       schema.pushDefaults();
420       Loader loader = new Loader(manager, locator, schema);
421       loader.setComponentContextLocators(componentContextLocators);
422       try {
423         loader.load(url);
424       } catch ( java.io.IOException JavaDoc e) {
425         // should be a warning : invalid included schema
426
// "Invalid included schema "+schemaLocation;
427
reportLoadingError(new SchemaException("src-include.1"));
428       }
429       schema.popDefaults();
430     }
431   }
432   
433   private void importSchema(Attributes atts) throws SAXException {
434     String JavaDoc location = atts.getValue("", SCHEMA_LOCATION_ATTR);
435     String JavaDoc schemaLocation = null;
436     if (getDocumentLocator() != null)
437       schemaLocation = getDocumentLocator().getSystemId();
438     String JavaDoc ns = atts.getValue("", NAMESPACE_ATTR);
439     if (!schema.isImported(ns)) {
440       Schema imported = null;
441       imported = manager.getSchema(ns);
442       if (imported == null) {
443           // catch the SAXException throwed by manager.loadSchema
444
if (location != null) {
445             URL JavaDoc url = null;
446             url = resolveLocation(location, schemaLocation);
447             if (url != null) {
448               imported = manager.loadSchema(locator, ns, url);
449             }
450             if (imported == null) imported = manager.loadSchema(locator, ns);
451           }
452           else {
453             URL JavaDoc url = null;
454             url = resolveLocation(ns, schemaLocation);
455             if (url != null) {
456               imported = manager.loadSchema(locator, ns, url);
457             }
458             if (imported == null) imported = manager.loadSchema(locator, ns);
459           }
460       }
461       if (imported == null) {
462         // It is not an error for the application schema reference strategy to fail
463
reportLoadingError(new SchemaException("src-import"));
464       }
465       schema.addImportedNamespace(ns);
466     }
467   }
468   
469   // history
470
// Simple type creation
471
// Schema 2000-10-24
472
// locator is added
473
//
474
public SimpleType buildSimpleType(Attributes atts)
475   throws SchemaException {
476     String JavaDoc strName = atts.getValue("", NAME_ATTR);
477     String JavaDoc strId = atts.getValue("", ID_ATTR);
478     
479     SimpleType aType = new SimpleType(schema, strName, null);
480     String JavaDoc fin = atts.getValue("", FINAL_ATTR);
481     if ( fin == null ) fin = schema.getDefault(FINAL_DEFAULT_ATTR);
482     if ( fin != null )
483       aType.setFinal(getMethod(fin, RESTRICTION | EXTENSION | LIST | UNION));
484     
485     // store namespace context and locator
486
ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator());
487     componentContextLocators.put(aType, contextLoc);
488
489     return aType;
490   }
491   
492   // history
493
// Complex type creation
494
//
495
// Schema 2000-10-24
496
// locator is added
497
//
498
// Schema 2001-05
499
public ComplexType buildComplexType(Attributes atts)
500   throws SchemaException {
501     String JavaDoc name = atts.getValue("", NAME_ATTR);
502     ComplexType type = new ComplexType(schema, name, null);
503     
504     String JavaDoc booleanValue = atts.getValue("", ABSTRACT_ATTR);
505     if ( TRUE_VALUE.equals(booleanValue) || ONE_VALUE.equals(booleanValue) )
506       type.setAbstract(true);
507     
508     // schema 2000-10, only 2 content type :
509
// 1. mixed = true, mixed content type
510
// 2. mixed = false, or default, element only type
511
// noted 15/01/01
512
booleanValue = atts.getValue("", MIXED_VALUE);
513     if ( TRUE_VALUE.equals(booleanValue) || ONE_VALUE.equals(booleanValue) )
514       type.setContentModel(new ContentModel(MIXED));
515     
516     String JavaDoc block = atts.getValue("", BLOCK_ATTR);
517     if ( block == null ) block = schema.getDefault(BLOCK_DEFAULT_ATTR);
518     if (block != null ) type.setBlock(getMethod(block, RESTRICTION | EXTENSION));
519     
520     String JavaDoc fin = atts.getValue("", FINAL_ATTR);
521     if ( fin == null ) fin = schema.getDefault(FINAL_DEFAULT_ATTR);
522     if ( fin != null )
523       type.setFinal(getMethod(fin, RESTRICTION | EXTENSION));
524     
525     // store namespace context and locator
526
ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator());
527     componentContextLocators.put(type, contextLoc);
528     
529     return type;
530   }
531   
532   // Element declaration
533
// schema 2001-05
534
// changed by ZL, May 17 2001
535
public ElementDeclaration buildElementDeclaration(Attributes atts, SchemaScope scope)
536   throws SchemaException {
537     
538     boolean global = (scope != null && scope.isGlobalScope());
539     String JavaDoc name = atts.getValue("", NAME_ATTR);
540     String JavaDoc ref = atts.getValue("", REF_ATTR);
541     ElementDeclaration decl = null;
542     if (name != null) {
543       if ( ref != null ) {
544         throw new SchemaException("src-element.2.1");
545       }
546       decl = new ElementDeclaration(schema, name, scope);
547       decl.setType(getType(atts.getValue("", TYPE_ATTR)));
548       if (global) {
549         decl.setQualified(true);
550         decl.setSubstitutionGroup(getRef(atts.getValue("", SUBSTITUTION_GROUP_ATTR), Schema.ELEMENT_DECLARATION));
551         String JavaDoc fin = atts.getValue("", FINAL_ATTR);
552         if (fin == null) fin = schema.getDefault(FINAL_DEFAULT_ATTR);
553         if (fin != null) decl.setFinal(getMethod(fin, RESTRICTION | EXTENSION));
554       }
555       else {
556         String JavaDoc form = atts.getValue("", FORM_ATTR);
557         if (form == null) form = schema.getDefault(ELEMENT_FORM_DEFAULT_ATTR);
558         if (QUALIFIED_VALUE.equals(form)) decl.setQualified(true);
559       }
560       
561     String JavaDoc booleanValue = atts.getValue("", ABSTRACT_ATTR);
562     if ( TRUE_VALUE.equals(booleanValue) || ONE_VALUE.equals(booleanValue) )
563         decl.setAbstract(true);
564       
565       String JavaDoc block = atts.getValue("", BLOCK_ATTR);
566       if (block == null) block = schema.getDefault(BLOCK_DEFAULT_ATTR);
567       if (block != null) decl.setBlock(getMethod(block, RESTRICTION | EXTENSION | SUBSTITUTION));
568       
569     } else {
570       if ( global ) {
571         // @@ known from schema
572
}
573       else if ( ref == null ) {
574 // "one of <name> or <ref> must be present in element declaration";
575
throw new SchemaException("src-element.2.1");
576       }
577       else {
578         if ( atts.getValue("", NILLABLE_ATTR) != null ||
579         atts.getValue("", DEFAULT_ATTR) != null ||
580         atts.getValue("", FIXED_ATTR) != null ||
581         atts.getValue("", FORM_ATTR) != null ||
582         atts.getValue("", BLOCK_ATTR) != null ||
583         atts.getValue("", TYPE_ATTR) != null ) {
584 // "if <ref> is present, all of <nillable, default, fixed, form, block, final, type> must be absent";
585
throw new SchemaException("src-element.2.2");
586         }
587         decl = new ElementDeclarationRef(resolvePrefix(ref), getLocalName(ref), scope);
588         decl.setQualified(true);
589       }
590     }
591     
592     String JavaDoc booleanValue = atts.getValue("", NILLABLE_ATTR);
593     if ( TRUE_VALUE.equals(booleanValue) || ONE_VALUE.equals(booleanValue) )
594       decl.setNillable(true);
595     
596     // schema 2001-05, value contrraint
597
String JavaDoc defaultValue = atts.getValue("", DEFAULT_ATTR);
598     String JavaDoc fixedValue = atts.getValue("", FIXED_ATTR);
599     
600     if ( defaultValue != null ) {
601       decl.setDefaultValue(defaultValue);
602       if ( fixedValue != null ) {
603 // "<default> and <fixed> must not both be present in attribute declaration";
604
throw new SchemaException("src-element.1");
605       }
606     }
607     else if ( fixedValue != null ) {
608       decl.setFixedValue(fixedValue);
609     }
610     
611     // store namespace context and locator
612
ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator());
613     componentContextLocators.put(decl, contextLoc);
614     
615     return decl;
616   }
617   
618   // schema 2001-05
619
// changed by ZL, May 16 2001
620
public AttributeDeclaration buildAttributeDeclaration(Attributes atts, SchemaScope scope)
621   throws SchemaException {
622     
623     boolean global = (scope != null && scope.isGlobalScope());
624     String JavaDoc name = atts.getValue("", NAME_ATTR);
625     String JavaDoc ref = atts.getValue("", REF_ATTR);
626     AttributeDeclaration decl = null;
627     
628     // one of name or ref must be present, but not both
629
if ( name != null ) {
630       if ( XMLNS_PREFIX.equals(name) )
631         throw new SchemaException("no-xmlns");
632       if ( ref != null ) {
633 // "one of <name> or <ref> must be present, but not both in attribute declaration";
634
throw new SchemaException("src-attribute.3.1");
635       }
636       decl = new AttributeDeclaration(schema, name, scope);
637       decl.setType(getType(atts.getValue("", TYPE_ATTR)));
638       if ( global ) {
639         // attribute element information item has schema as its parent
640
decl.setQualified(true);
641       } else {
642         // local attribute
643
String JavaDoc form = atts.getValue("", FORM_ATTR);
644         if ( form == null )
645           form = schema.getDefault(ATTRIBUTE_FORM_DEFAULT_ATTR);
646         if ( QUALIFIED_VALUE.equals(form) ) decl.setQualified(true);
647       }
648     } else {
649       // name is null, ref must not be null, and it's a local attribute
650
if (ref == null) {
651 // "one of name or ref must be present in attribute declaration";
652
throw new SchemaException("src-attribute.3.1");
653       }
654       else {
655         if (atts.getValue("", FORM_ATTR) != null ||
656         atts.getValue("", TYPE_ATTR) != null ) {
657 // "If <ref> is present, then all of <simpleType, form, type> must be absent";
658
throw new SchemaException("src-attribute.3.2");
659         }
660         decl = new AttributeDeclarationRef(resolvePrefix(ref), getLocalName(ref), scope);
661         decl.setQualified(true);
662       }
663     }
664     
665     // schema 2001-05, use defintion is different from that of schema 2000-10
666
String JavaDoc use = atts.getValue("", USE_ATTR);
667     if ( use != null ) {
668       if ( OPTIONAL_VALUE.equals(use) )
669         decl.setUse(OPTIONAL);
670       else if ( PROHIBITED_VALUE.equals(use) )
671         decl.setUse(PROHIBITED);
672       else if ( REQUIRED_VALUE.equals(use) )
673         decl.setUse(REQUIRED);
674     }
675     
676     // schema 2001-05, value contrraint
677
String JavaDoc defaultValue = atts.getValue("", DEFAULT_ATTR);
678     String JavaDoc fixedValue = atts.getValue("", FIXED_ATTR);
679     
680     if ( defaultValue != null ) {
681       if ( fixedValue != null ) {
682 // "<default> and <fixed> must not both be present";
683
throw new SchemaException("src-attribute.1");
684       }
685       if (use != null && !OPTIONAL_VALUE.equals(use)) {
686 // "<use> must have the value <optional>";
687
throw new SchemaException("src-attribute.2");
688       }
689       decl.setDefaultValue(defaultValue);
690     }
691     else if ( fixedValue != null ) {
692       decl.setFixedValue(fixedValue);
693     }
694     
695     // store namespace context and locator
696
ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator());
697     componentContextLocators.put(decl, contextLoc);
698     
699     return decl;
700   }
701   
702   public ModelGroupDefinition buildModelGroupDefinition(Attributes atts, Schema schema) {
703     String JavaDoc name = atts.getValue("", NAME_ATTR);
704     ModelGroupDefinition def = new ModelGroupDefinition(schema, name);
705     ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator());
706     componentContextLocators.put(def, contextLoc);
707     return def;
708   }
709   
710   public AttributeGroupDefinition buildAttributeGroupDefinition(Attributes atts, Schema schema) {
711     String JavaDoc name = atts.getValue("", NAME_ATTR);
712     AttributeGroupDefinition def = new AttributeGroupDefinition(schema, name);
713     ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator());
714     componentContextLocators.put(def, contextLoc);
715     return def;
716   }
717   
718   // create Identiy-constraint
719
// changed by ZL, May 16 2001
720
public IdentityConstraint buildIdentityConstraint(Attributes atts,
721               String JavaDoc localName, ElementDeclaration context)
722   throws SchemaException {
723      
724     String JavaDoc name = atts.getValue("", NAME_ATTR);
725     String JavaDoc refer = atts.getValue("", REFER_ATTR);
726      
727     IdentityConstraint identityConstraint = new IdentityConstraint(schema, name, context);
728     if (localName.equals(KEY_TAG))
729       identityConstraint.setCategory(IdentityConstraint.CATEGORY_KEY);
730     else if (localName.equals(KEYREF_TAG))
731       identityConstraint.setCategory(IdentityConstraint.CATEGORY_KEYREF);
732     else if (localName.equals(UNIQUE_TAG))
733       identityConstraint.setCategory(IdentityConstraint.CATEGORY_UNIQUE);
734     
735     // store namespace context and locator
736
ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator());
737     componentContextLocators.put(identityConstraint, contextLoc);
738     
739     if (identityConstraint.getCategory() == IdentityConstraint.CATEGORY_KEYREF) {
740       IdentityConstraintRef icRef = new IdentityConstraintRef(resolvePrefix(refer), getLocalName(refer));
741       componentContextLocators.put(icRef, contextLoc);
742       identityConstraint.setReferencedKey(icRef);
743     }
744     
745     
746     return identityConstraint;
747   }
748   
749   public java.util.List JavaDoc buildXPaths(String JavaDoc xpath) throws SchemaException {
750     java.util.ArrayList JavaDoc result = new java.util.ArrayList JavaDoc(4);
751     while (xpath != null) {
752       int i = xpath.indexOf('|');
753       String JavaDoc current = null;
754       if (i == -1) {
755         current = xpath;
756         xpath = null;
757       } else {
758         current = xpath.substring(0, i);
759         xpath = xpath.substring(i+1);
760       }
761       result.add(buildXPath(current));
762     }
763     return result;
764   }
765   
766   public XPathExpr buildXPath(String JavaDoc xpath) throws SchemaException {
767     boolean isAnyLevel = false;
768     boolean isAttribute = false;
769     if (xpath.startsWith(".//")) {
770       isAnyLevel = true;
771       xpath = xpath.substring(3);
772     }
773     XPathExpr result = new XPathExpr(isAnyLevel);
774     while (xpath != null) {
775       int i = xpath.indexOf('/');
776       String JavaDoc qName = null;
777       if (i == -1) {
778         qName = xpath;
779         xpath = null;
780       } else {
781         qName = xpath.substring(0, i);
782         xpath = xpath.substring(i+1);
783       }
784       if (qName.startsWith("child::")) {
785         qName = qName.substring(7);
786       } else if (qName.startsWith("@")) {
787         isAttribute = true;
788         qName = qName.substring(1);
789       } else if (qName.startsWith("attribute::")) {
790         isAttribute = true;
791         qName = qName.substring(11);
792       }
793       result.setAttribute(isAttribute);
794       if (!qName.equals(".")) {
795         String JavaDoc ns = null;
796         String JavaDoc localName = null;
797         int j = qName.indexOf(':');
798         if (j == -1) {
799           localName = qName;
800           // use default prefix if defined, but only for named elements
801
if (!isAttribute && !"*".equals(qName))
802             ns = getPrefixMapping("");
803         } else {
804           ns = getPrefixMapping(qName.substring(0, j));
805           if ( ns == null )
806             throw new SchemaException("src-identity-constraint");
807           localName = qName.substring(j+1);
808         }
809         result.addStep(ns, localName);
810       }
811     }
812     
813     // store namespace context and locator
814
ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator());
815     componentContextLocators.put(result, contextLoc);
816     
817     return result;
818   }
819   
820   public void warning(SAXParseException ex) throws SAXException {
821     throw ex;
822   }
823
824   public void error(SAXParseException ex) throws SAXException {
825     throw ex;
826   }
827
828   public void fatalError(SAXParseException ex) throws SAXException {
829     throw ex;
830   }
831   
832   public void notifyUnknownElement(String JavaDoc uriNamespace, String JavaDoc localName )
833   throws SAXException {
834     // should not be happened
835
// all is verificated by Schema of schemas
836
String JavaDoc errMsg = "Unknown element : {" + uriNamespace + "}" + localName;
837     reportLoadingError(errMsg, null);
838   }
839   
840   public void reportLoadingError(SchemaException se) throws SAXException {
841     reportLoadingError(null, se);
842   }
843
844   public void reportLoadingError(String JavaDoc message, SchemaException se) throws SAXException {
845     if ( message != null && se != null ) message += "\n"+se.getMessage();
846     throw new SAXParseException(message, super.getDocumentLocator(), se);
847   }
848   
849   class SchemaHandler extends org.xquark.util.DefaultElementHandler {
850     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
851     private static final String JavaDoc RCSName = "$Name: $";
852     public org.xquark.util.ElementHandler startElement(String JavaDoc strNamespaceURI, String JavaDoc localName, Attributes atts)
853     throws SAXException {
854       
855     if (isSchemaNamespace(strNamespaceURI)) {
856
857       if (localName.equals(SCHEMA_TAG)) {
858         try {
859           initSchema(atts);
860         }
861         catch ( SchemaException se ) {
862           String JavaDoc errMsg = "Error while processing localName -> " + localName;
863           reportLoadingError(errMsg, se);
864         }
865         return this;
866       }
867
868       else if (localName.equals(INCLUDE_TAG)) {
869         includeSchema(atts);
870         return this;
871       }
872
873       else if (localName.equals(IMPORT_TAG)) {
874         importSchema(atts);
875         return this;
876       }
877
878       else if (localName.equals(REDEFINE_TAG)) {
879         includeSchema(atts);
880         return new RedefineHandler(getLoader());
881       }
882
883       else if ( localName.equals(SIMPLE_TYPE_TAG) ) {
884         SimpleType type = null;
885         try {
886           type = buildSimpleType(atts);
887           schema.register(type);
888         }
889         catch ( SchemaException se ) {
890           String JavaDoc errMsg = "Error while processing localName -> " + localName;
891           reportLoadingError(errMsg, se);
892         }
893         return new SimpleTypeHandler(getLoader(), type, false, false);
894       }
895
896       else if (localName.equals(COMPLEX_TYPE_TAG)) {
897         ComplexType type = null;
898         try {
899           type = buildComplexType(atts);
900           schema.register(type);
901         }
902         catch ( SchemaException se ) {
903           String JavaDoc errMsg = "Error while processing localName -> " + localName;
904           reportLoadingError(errMsg, se);
905         }
906         return new ComplexTypeHandler(getLoader(), type, false, false);
907       }
908
909       else if (localName.equals(ELEMENT_TAG)) {
910         ElementDeclaration decl = null;
911         try {
912           decl = buildElementDeclaration(atts, schema);
913           schema.register(decl);
914         }
915         catch ( SchemaException se ) {
916           String JavaDoc errMsg = "Error while processing localName -> " + localName;
917           reportLoadingError(errMsg, se);
918         }
919         return new ElementTypeHandler(getLoader(), decl);
920       }
921
922       else if (localName.equals(ATTRIBUTE_TAG)) {
923         AttributeDeclaration decl = null;
924         try {
925           decl = buildAttributeDeclaration(atts, schema);
926           schema.register(decl);
927         }
928         catch ( SchemaException se ) {
929           String JavaDoc errMsg = "Error while processing localName -> " + localName;
930           reportLoadingError(errMsg, se);
931         }
932         return new AttributeTypeHandler(getLoader(), decl);
933       }
934
935       else if (localName.equals(GROUP_TAG)) {
936         ModelGroupDefinition def = buildModelGroupDefinition(atts, schema);
937         try {
938           schema.register(def);
939         }
940         catch ( SchemaException se ) {
941           String JavaDoc errMsg = "Error while processing localName -> " + localName;
942           reportLoadingError(errMsg, se);
943         }
944         return new ContentModelHandler(getLoader(), def, false);
945       }
946
947       else if (localName.equals(ATTRIBUTE_GROUP_TAG)) {
948         AttributeGroupDefinition def = buildAttributeGroupDefinition(atts, schema);
949         try {
950           schema.register(def);
951         }
952         catch ( SchemaException se ) {
953           String JavaDoc errMsg = "Error while processing localName -> " + localName;
954           reportLoadingError(errMsg, se);
955         }
956         return new AttributeGroupHandler(getLoader(), def);
957       }
958
959       else if (localName.equals(NOTATION_TAG)) {
960         NotationDeclaration decl = buildNotation(atts);
961         try {
962           schema.register(decl);
963         }
964         catch ( SchemaException se ) {
965           String JavaDoc errMsg = "Error while processing localName -> " + localName;
966           reportLoadingError(errMsg, se);
967         }
968         return this;
969       }
970
971       else if (localName.equals(ANNOTATION_TAG)) {
972         return new AnnotationHandler();
973       }
974     }
975
976     notifyUnknownElement(strNamespaceURI, localName);
977     return this;
978     }
979     
980     public void endElement(String JavaDoc namespace, String JavaDoc localName)
981     throws SAXException {
982       super.endElement(namespace, localName);
983       if ( isSchemaNamespace(namespace) && localName.equals(SCHEMA_TAG) && topLevel ) {
984           initialize();
985       }
986     }
987   }
988   
989 }
990
Popular Tags