KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > SchemaReader


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;
24
25 import java.io.IOException JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.xml.sax.SAXException JavaDoc;
30 import org.xml.sax.helpers.AttributesImpl JavaDoc;
31 import org.xquark.schema.loader.AttributeDeclarationRef;
32 import org.xquark.schema.loader.ElementDeclarationRef;
33 import org.xquark.schema.loader.SchemaComponentRef;
34
35 public class SchemaReader extends org.xquark.util.DefaultXMLReader implements SchemaConstants {
36 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
37 private static final String JavaDoc RCSName = "$Name: $";
38
39   private SchemaManager manager;
40   private ComplexType extensionComplexType = null;
41   private int min = -1;
42   private int max = -1;
43   private boolean unbounded = false;
44   private java.util.HashMap JavaDoc namespaceMap = new java.util.HashMap JavaDoc();
45   private boolean topLevelGroup = false;
46   private int depthCount = 0;
47   private AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
48   private AttributesImpl JavaDoc emptyAtts = new AttributesImpl JavaDoc();
49   private IterationVisitor visitor = null;
50
51   public SchemaReader(SchemaManager manager) {
52     this.manager = manager;
53   }
54
55   public void parse(org.xml.sax.InputSource JavaDoc input) throws SAXException JavaDoc, IOException JavaDoc
56   {
57     parse(input.getSystemId());
58   }
59   
60   public void parse(String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc
61   {
62     contentHandler.startDocument();
63     Schema schema = manager.getSchema(systemId);
64     parse (schema, true) ;
65   }
66   
67   // add from dntt
68
public void parse(Schema schema) throws SAXException JavaDoc, java.io.IOException JavaDoc
69   {
70     parse (schema, true) ;
71   }
72
73   public void parse(Schema schema, boolean withStart)
74     throws SAXException JavaDoc, java.io.IOException JavaDoc
75   {
76     if (withStart)
77         contentHandler.startDocument();
78     
79     try {
80       visitor = new SerializingIterationVisitor(new StartElementVisitor(), new EndElementVisitor());
81       schema.accept(visitor);
82     }
83     catch ( SchemaException se ) {
84       throw new SAXException JavaDoc(se);
85     }
86       
87     if (withStart)
88         contentHandler.endDocument();
89   }
90   
91   public void parse(SchemaComponent component) throws SAXException JavaDoc, IOException JavaDoc
92   {
93     try {
94       visitor = new SerializingIterationVisitor(new StartElementVisitor(), new EndElementVisitor());
95       component.accept(visitor);
96     }
97     catch ( SchemaException se ) {
98       throw new SAXException JavaDoc(se);
99     }
100   }
101   
102   public void setSchemaPrefixMap(String JavaDoc prefix, String JavaDoc namespace) {
103     namespaceMap.put(namespace, prefix);
104   }
105   
106   private String JavaDoc getRef(String JavaDoc uri, String JavaDoc localName) {
107     String JavaDoc prefix = (String JavaDoc)namespaceMap.get(uri);
108     if ( prefix == null ) prefix = "";
109     if ( !"".equals(prefix) ) prefix += ":";
110     return prefix+localName;
111   }
112
113   private String JavaDoc getRef(SchemaComponent comp) {
114     return getRef(comp.getNamespace(), comp.getName());
115   }
116
117   private void addOccurrence(AttributesImpl JavaDoc atts) {
118     if (!visitor.isTopLevel()) {
119       if (min != 1)
120     atts.addAttribute("", MIN_OCCURS_ATTR, null, "CDATA", String.valueOf(min));
121       if (unbounded)
122     atts.addAttribute("", MAX_OCCURS_ATTR, null, "CDATA", UNBOUNDED_VALUE);
123       else if (max != 1)
124     atts.addAttribute("", MAX_OCCURS_ATTR, null, "CDATA", String.valueOf(max));
125     }
126   }
127
128   private void addForm(Declaration decl, AttributesImpl JavaDoc atts, String JavaDoc defForm) {
129     if (decl.getScope() != null && decl.getSchema() != null ) {
130       String JavaDoc defaultValue = decl.getSchema().getDefault(defForm);
131       if (defaultValue == null || defaultValue.equals(UNQUALIFIED_VALUE)) {
132     if (decl.getNamespace() != null)
133      atts.addAttribute("", FORM_ATTR, null, "CDATA", QUALIFIED_VALUE);
134       }
135       else if (decl.getNamespace() == null)
136       atts.addAttribute("", FORM_ATTR, null, "CDATA", UNQUALIFIED_VALUE);
137     }
138   }
139   
140   class StartElementVisitor extends DefaultSchemaVisitor {
141     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
142     private static final String JavaDoc RCSName = "$Name: $";
143
144     public void visit(Schema schema) throws SchemaException {
145       Iterator JavaDoc it = schema.getPrefixMap().entrySet().iterator();
146       while (it.hasNext()) {
147                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
148         if ( ! XML_PREFIX.equals(entry.getKey()) ) {
149           try {
150             getContentHandler().startPrefixMapping((String JavaDoc)entry.getKey(), (String JavaDoc)entry.getValue());
151           }
152           catch ( SAXException JavaDoc se ) {
153             throw new SchemaException(se.getMessage());
154           }
155           namespaceMap.put(entry.getValue(), entry.getKey());
156         }
157       }
158       atts.clear();
159       atts.addAttribute("", TARGET_NAMESPACE_ATTR, null, "CDATA", schema.getNamespace());
160             
161       String JavaDoc value = schema.getDefault(ATTRIBUTE_FORM_DEFAULT_ATTR);
162       if (value != null)
163                 atts.addAttribute("", ATTRIBUTE_FORM_DEFAULT_ATTR, null, "CDATA", value);
164             
165       value = schema.getDefault(BLOCK_DEFAULT_ATTR);
166       if (value != null)
167                 atts.addAttribute("", BLOCK_DEFAULT_ATTR, null, "CDATA", value);
168             
169       value = schema.getDefault(ELEMENT_FORM_DEFAULT_ATTR);
170       if (value != null)
171                 atts.addAttribute("", ELEMENT_FORM_DEFAULT_ATTR, null, "CDATA", value);
172             
173       value = schema.getDefault(FINAL_DEFAULT_ATTR);
174       if (value != null)
175                 atts.addAttribute("", FINAL_DEFAULT_ATTR, null, "CDATA", value);
176             
177       try {
178         getContentHandler().startElement(XMLSCHEMA_URI, SCHEMA_TAG, "", atts);
179       }
180       catch ( SAXException JavaDoc se ) {
181         throw new SchemaException(se.getMessage());
182       }
183       generateImports(schema);
184     }
185     
186     private void generateImports(Schema schema) throws SchemaException {
187       Iterator JavaDoc it = schema.getPrefixMap().values().iterator();
188       while (it.hasNext()) {
189                 String JavaDoc ns = (String JavaDoc)it.next();
190                 if (!ns.equals(XMLSCHEMA_URI) && schema.isImported(ns)) {
191                     atts.clear();
192                     atts.addAttribute("", NAMESPACE_ATTR, null, "CDATA", ns);
193           try {
194             getContentHandler().startElement(XMLSCHEMA_URI, IMPORT_TAG, "", atts);
195             getContentHandler().endElement(XMLSCHEMA_URI, IMPORT_TAG, "");
196           }
197           catch ( SAXException JavaDoc se ) {
198             throw new SchemaException(se.getMessage());
199           }
200                 }
201       }
202     }
203     
204         // Created
205
// schema 2000-10-24
206
//
207
public void visit(SimpleType type) throws SchemaException {
208       try {
209         
210             // get type name
211
atts.clear();
212             if ( type.getName() != null ) {
213                 atts.addAttribute("", NAME_ATTR, null, "CDATA", type.getName());
214                 getContentHandler().startElement(XMLSCHEMA_URI, SIMPLE_TYPE_TAG, "", atts);
215                 atts.clear();
216             }
217             else {
218                 getContentHandler().startElement(XMLSCHEMA_URI, SIMPLE_TYPE_TAG, "", atts);
219             }
220                         
221             // restriction, list, or union
222
switch ( type.getVariety() ) {
223                 
224                 case SimpleType.VARIETY_ATOMIC :
225                     // restriction
226
Type base = type.getBaseType();
227                     if ( base != null ) {
228                         if ( base.getName() != null ) {
229                             atts.addAttribute("", BASE_ATTR, null, "CDATA", getRef(base));
230                             getContentHandler().startElement(XMLSCHEMA_URI, RESTRICTION_TAG, "", atts);
231                             atts.clear();
232                         }
233                     }
234                     
235                     // facets
236
Iterator JavaDoc it = type.getAllFacets().iterator();
237                     while (it.hasNext()) {
238                         Facet facet = (Facet)it.next();
239             if ( facet.getEnumFacets().size() != 0 ) {
240               String JavaDoc name = facet.getName();
241               Iterator JavaDoc fIt = facet.getEnumFacets().iterator();
242               while ( fIt.hasNext() ) {
243                 String JavaDoc value = (String JavaDoc)fIt.next();
244                 atts.addAttribute("", VALUE_ATTR, null, "CDATA", value);
245                 getContentHandler().startElement(XMLSCHEMA_URI, name, "", atts);
246                 atts.clear();
247                 getContentHandler().endElement(XMLSCHEMA_URI, name, "");
248               }
249             }
250             else {
251               String JavaDoc name = facet.getName();
252               String JavaDoc value = facet.getValue();
253               atts.addAttribute("", VALUE_ATTR, null, "CDATA", value);
254               getContentHandler().startElement(XMLSCHEMA_URI, name, "", atts);
255               atts.clear();
256               getContentHandler().endElement(XMLSCHEMA_URI, name, "");
257             }
258           }
259  
260                     break;
261                     
262                 case SimpleType.VARIETY_LIST :
263           
264           if ( type.getItemType().getName() != null ) {
265             atts.addAttribute("", ITEM_TYPE_ATTR, null, "CDATA", getRef(type.getItemType()));
266             getContentHandler().startElement(XMLSCHEMA_URI, LIST_TAG, "", atts);
267             atts.clear();
268           }
269           else {
270             getContentHandler().startElement(XMLSCHEMA_URI, LIST_TAG, "", atts);
271             parse(type.getItemType());
272           }
273                     
274                     break;
275                     
276                 case SimpleType.VARIETY_UNION :
277           
278           if ( type.getMemberTypes() != null ) {
279             String JavaDoc attr = null;
280             for ( int i = 0; i < type.getMemberTypes().size(); i++ ) {
281               SimpleType member = (SimpleType)type.getMemberTypes().get(i);
282               if ( member.getName() != null ) {
283                 if ( i == 0 ) attr = getRef(member) + " ";
284                 else attr += getRef(member) + " ";
285               }
286             }
287             atts.addAttribute("", MEMBER_TYPES_ATTR, null, "CDATA", attr);
288             if ( attr != null )
289               getContentHandler().startElement(XMLSCHEMA_URI, UNION_TAG, "", atts);
290             else {
291               atts.clear();
292               getContentHandler().startElement(XMLSCHEMA_URI, UNION_TAG, "", atts);
293             }
294             atts.clear();
295             for ( int i = 0; i < type.getMemberTypes().size(); i++ ) {
296               SimpleType member = (SimpleType)type.getMemberTypes().get(i);
297               if ( member.getName() == null ) {
298                  parse(member);
299               }
300             }
301           }
302           break;
303             }
304       
305       }
306       catch ( SAXException JavaDoc se ) {
307         throw new SchemaException(se.getMessage());
308       }
309       catch ( java.io.IOException JavaDoc se ) {
310         throw new SchemaException(se.getMessage());
311       }
312         }
313   
314         // schema 2000-10-24
315
//
316
public void visit(ComplexType type) throws SchemaException {
317
318       try {
319         // get name
320
atts.clear();
321         
322         if ( type.getName() != null )
323           atts.addAttribute("", NAME_ATTR, null, "CDATA", type.getName());
324
325         if ( type.isAbstract() )
326           atts.addAttribute("", ABSTRACT_ATTR, null, "CDATA", TRUE_VALUE);
327
328         if ( type.getContentModel().getContentType() == MIXED )
329           atts.addAttribute("", MIXED_VALUE, null, "CDATA", TRUE_VALUE);
330
331         getContentHandler().startElement(XMLSCHEMA_URI, COMPLEX_TYPE_TAG, "", atts);
332
333         // simpleContex
334
Object JavaDoc model = type.getContentModel().getModel();
335         if ( model instanceof SimpleType ) {
336             getContentHandler().startElement(XMLSCHEMA_URI, SIMPLECONTENT_TAG, "", atts);
337             atts.clear();
338             atts.addAttribute("", BASE_ATTR, null, "CDATA", getRef((SimpleType)model));
339             getContentHandler().startElement(XMLSCHEMA_URI, EXTENSION_TAG, "", atts);
340         }
341
342         // complexContent, extension
343
else if ( type.isExtension() && type.getBaseType() != null ) {
344           getContentHandler().startElement(XMLSCHEMA_URI, COMPLEXCONTENT_TAG, "", emptyAtts);
345           atts.clear();
346           extensionComplexType = type;
347           atts.addAttribute("", BASE_ATTR, null, "CDATA", getRef(type.getBaseType()));
348           getContentHandler().startElement(XMLSCHEMA_URI, EXTENSION_TAG, "", atts);
349         }
350
351         // complexContent, restriction
352
else if ( type.isRestriction() && type.getBaseType() != null ) {
353           getContentHandler().startElement(XMLSCHEMA_URI, COMPLEXCONTENT_TAG, "", emptyAtts);
354           atts.clear();
355           atts.addAttribute("", BASE_ATTR, null, "CDATA", getRef(type.getBaseType()));
356           getContentHandler().startElement(XMLSCHEMA_URI, RESTRICTION_TAG, "", atts);
357         }
358
359         topLevelGroup = true;
360       }
361       catch ( SAXException JavaDoc se ) {
362         throw new SchemaException(se.getMessage());
363       }
364     }
365
366     public void visit(ElementDeclaration decl) throws SchemaException {
367       // check if this element is alread visited in base type
368
if ( extensionComplexType != null ) {
369         ComplexType base = (ComplexType)extensionComplexType.getBaseType();
370         if ( base.getElementDeclaration(decl.getNamespace(), decl.getName()) != null )
371           return;
372       }
373       
374       atts.clear();
375       
376       if ( !visitor.isTopLevel() && decl.getScope().isGlobalScope() ) {
377         atts.addAttribute("", REF_ATTR, null, "CDATA", getRef(decl));
378       }
379       else {
380         Type type = decl.getType();
381         if (decl.getName() != null)
382           atts.addAttribute("", NAME_ATTR, null, "CDATA", decl.getName());
383         if (type != null && type.getName() != null)
384           atts.addAttribute("", TYPE_ATTR, null, "CDATA", getRef(type));
385         if (decl.isAbstract())
386           atts.addAttribute("", ABSTRACT_ATTR, null, "CDATA", TRUE_VALUE);
387         if (decl.isNillable())
388           atts.addAttribute("", NILLABLE_ATTR, null, "CDATA", TRUE_VALUE);
389         
390         if ( decl.getDefaultValue() != null )
391           atts.addAttribute("", DEFAULT_ATTR, null, "CDATA", decl.getDefaultValue());
392         if ( decl.getFixedValue() != null )
393           atts.addAttribute("", FIXED_ATTR, null, "CDATA", decl.getFixedValue());
394 // addForm(decl, atts, ELEMENT_FORM_DEFAULT_ATTR);
395
}
396             
397       addOccurrence(atts);
398                 
399       try {
400         getContentHandler().startElement(XMLSCHEMA_URI, ELEMENT_TAG, "", atts);
401       }
402       catch ( SAXException JavaDoc se ) {
403         throw new SchemaException(se.getMessage());
404       }
405     }
406
407     public void visit(ElementDeclarationRef decl) throws SchemaException {
408             atts.clear();
409       atts.addAttribute("", REF_ATTR, null, "CDATA", getRef(decl));
410       if (decl.isNillable())
411                 atts.addAttribute("", NILLABLE_ATTR, null, "CDATA", TRUE_VALUE);
412       if ( decl.getDefaultValue() != null )
413             atts.addAttribute("", DEFAULT_ATTR, null, "CDATA", decl.getDefaultValue());
414       if ( decl.getFixedValue() != null )
415             atts.addAttribute("", FIXED_ATTR, null, "CDATA", decl.getFixedValue());
416       addOccurrence(atts);
417       try {
418         getContentHandler().startElement(XMLSCHEMA_URI, ELEMENT_TAG, "", atts);
419       }
420       catch ( SAXException JavaDoc se ) {
421         throw new SchemaException(se.getMessage());
422       }
423     }
424
425     public void visit(AttributeDeclaration decl) throws SchemaException {
426             atts.clear();
427       Type type = decl.getType();
428       if (decl.getName() != null)
429                 atts.addAttribute("", NAME_ATTR, null, "CDATA", decl.getName());
430       if (type != null && type.getName() != null)
431                 atts.addAttribute("", TYPE_ATTR, null, "CDATA", getRef(type));
432             
433 // addForm(decl, atts, ATTRIBUTE_FORM_DEFAULT_ATTR);
434
if ( decl.getDefaultValue() != null )
435             atts.addAttribute("", DEFAULT_ATTR, null, "CDATA", decl.getDefaultValue());
436       if ( decl.getFixedValue() != null )
437             atts.addAttribute("", FIXED_ATTR, null, "CDATA", decl.getFixedValue());
438             if ( decl.getUseString() != null && !OPTIONAL_VALUE.equals(decl.getUseString()) )
439                 atts.addAttribute("", USE_ATTR, null, "CDATA", decl.getUseString());
440             
441       try {
442         getContentHandler().startElement(XMLSCHEMA_URI, ATTRIBUTE_TAG, "", atts);
443       }
444       catch ( SAXException JavaDoc se ) {
445         throw new SchemaException(se.getMessage());
446       }
447     }
448   
449     public void visit(AttributeDeclarationRef decl) throws SchemaException {
450             atts.clear();
451       atts.addAttribute("", REF_ATTR, null, "CDATA", getRef(decl));
452       if ( decl.getDefaultValue() != null )
453             atts.addAttribute("", DEFAULT_ATTR, null, "CDATA", decl.getDefaultValue());
454       if ( decl.getFixedValue() != null )
455             atts.addAttribute("", FIXED_ATTR, null, "CDATA", decl.getFixedValue());
456             if ( decl.getUseString() !=null )
457                 atts.addAttribute("", USE_ATTR, null, "CDATA", decl.getUseString());
458             
459       try {
460         getContentHandler().startElement(XMLSCHEMA_URI, ATTRIBUTE_TAG, "", atts);
461       }
462       catch ( SAXException JavaDoc se ) {
463         throw new SchemaException(se.getMessage());
464       }
465     }
466   
467     public void visit(SchemaComponentRef ref) throws SchemaException {
468       try {
469             atts.clear();
470       atts.addAttribute("", REF_ATTR, null, "CDATA", getRef(ref));
471       if (ref.getType() == Schema.MODEL_GROUP_DEFINITION) {
472                 addOccurrence(atts);
473                 getContentHandler().startElement(XMLSCHEMA_URI, GROUP_TAG, "", atts);
474       }
475             else if (ref.getType() == Schema.ATTRIBUTE_GROUP_DEFINITION) {
476                 getContentHandler().startElement(XMLSCHEMA_URI, ATTRIBUTE_GROUP_TAG, "", atts);
477       }
478       
479       }
480       catch ( SAXException JavaDoc se ) {
481         throw new SchemaException(se.getMessage());
482       }
483     }
484     
485     public void visit(ModelGroupDefinition def) throws SchemaException {
486             atts.clear();
487       atts.addAttribute("", NAME_ATTR, null, "CDATA", def.getName());
488       try {
489         getContentHandler().startElement(XMLSCHEMA_URI, GROUP_TAG, "", atts);
490       }
491       catch ( SAXException JavaDoc se ) {
492         throw new SchemaException(se.getMessage());
493       }
494       topLevelGroup = true;
495     }
496     
497     public void visit(AttributeGroupDefinition def) throws SchemaException {
498             atts.clear();
499       atts.addAttribute("", NAME_ATTR, null, "CDATA", def.getName());
500       try {
501         getContentHandler().startElement(XMLSCHEMA_URI, ATTRIBUTE_GROUP_TAG, "", atts);
502       }
503       catch ( SAXException JavaDoc se ) {
504         throw new SchemaException(se.getMessage());
505       }
506     }
507   
508     public void visit(Wildcard wildcard) throws SchemaException {
509       try {
510             atts.clear();
511       if (!visitor.isAttributeWildcard()) {
512                 addOccurrence(atts);
513                 getContentHandler().startElement(XMLSCHEMA_URI, ANY_TAG, "", atts);
514       }
515             else {
516                 getContentHandler().startElement(XMLSCHEMA_URI, ANY_ATTRIBUTE_TAG, "", atts);
517       }
518       }
519       catch ( SAXException JavaDoc se ) {
520         throw new SchemaException(se.getMessage());
521       }
522     }
523   
524     public void visit(ContentModel model) throws SchemaException {
525             atts.clear();
526       Object JavaDoc p = model.getModel();
527       if ( p != null && !(p instanceof SimpleType) ) {
528         if ( ((Particle)p).getTerm() != null && ((Particle)p).getTerm() instanceof ElementDeclaration )
529           try {
530             getContentHandler().startElement(XMLSCHEMA_URI, SEQUENCE_TAG, "", atts);
531           }
532           catch ( SAXException JavaDoc se ) {
533             throw new SchemaException(se.getMessage());
534           }
535       }
536     }
537     
538     public void visit(Particle particle) throws SchemaException {
539       min = particle.getMinOccurs();
540       max = particle.getMaxOccurs();
541       unbounded = particle.isMaxOccursUnbounded();
542     }
543     
544     public void visit(AllModelGroup group) throws SchemaException {
545             atts.clear();
546       addOccurrence(atts);
547       try {
548         getContentHandler().startElement(XMLSCHEMA_URI, ALL_TAG, "", atts);
549       }
550       catch ( SAXException JavaDoc se ) {
551         throw new SchemaException(se.getMessage());
552       }
553       depthCount++;
554       topLevelGroup = false;
555     }
556     
557     public void visit(ChoiceModelGroup group) throws SchemaException {
558             atts.clear();
559       addOccurrence(atts);
560       try {
561         getContentHandler().startElement(XMLSCHEMA_URI, CHOICE_TAG, "", atts);
562       }
563       catch ( SAXException JavaDoc se ) {
564         throw new SchemaException(se.getMessage());
565       }
566       depthCount++;
567       topLevelGroup = false;
568     }
569   
570     public void visit(SequenceModelGroup group) throws SchemaException {
571             atts.clear();
572       addOccurrence(atts);
573       try {
574         getContentHandler().startElement(XMLSCHEMA_URI, SEQUENCE_TAG, "", atts);
575       }
576       catch ( SAXException JavaDoc se ) {
577         throw new SchemaException(se.getMessage());
578       }
579       topLevelGroup = false;
580     }
581   }
582
583   class EndElementVisitor extends DefaultSchemaVisitor {
584     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
585     private static final String JavaDoc RCSName = "$Name: $";
586
587     public void visit(Schema schema) throws SchemaException {
588       try {
589         getContentHandler().endElement(XMLSCHEMA_URI, SCHEMA_TAG, "");
590         Iterator JavaDoc it = schema.getPrefixMap().entrySet().iterator();
591         while (it.hasNext()) {
592           Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
593           getContentHandler().endPrefixMapping((String JavaDoc)entry.getKey());
594         }
595       }
596       catch ( SAXException JavaDoc se ) {
597         throw new SchemaException(se.getMessage());
598       }
599     }
600     
601     public void visit(SimpleType type) throws SchemaException {
602       try {
603             // restriction, list, or union
604
switch ( type.getVariety() ) {
605                 
606                 case SimpleType.VARIETY_ATOMIC :
607           Type base = type.getBaseType();
608           if ( base != null ) {
609             getContentHandler().endElement(XMLSCHEMA_URI, RESTRICTION_TAG, "");
610           }
611           break;
612           
613                 case SimpleType.VARIETY_LIST :
614           getContentHandler().endElement(XMLSCHEMA_URI, LIST_TAG, "");
615           break;
616           
617                 case SimpleType.VARIETY_UNION :
618           if ( type.getMemberTypes() != null )
619             getContentHandler().endElement(XMLSCHEMA_URI, UNION_TAG, "");
620           break;
621       }
622
623       getContentHandler().endElement(XMLSCHEMA_URI, SIMPLE_TYPE_TAG, "");
624       }
625       catch ( SAXException JavaDoc se ) {
626         throw new SchemaException(se.getMessage());
627       }
628
629     }
630   
631     public void visit(ComplexType type) throws SchemaException {
632       try {
633       Object JavaDoc model = type.getContentModel().getModel();
634       // simpleContent
635
if ( model instanceof SimpleType ) {
636         getContentHandler().endElement(XMLSCHEMA_URI, EXTENSION_TAG, "");
637         getContentHandler().endElement(XMLSCHEMA_URI, SIMPLECONTENT_TAG, "");
638       }
639       
640       // complexContent, extension
641
else if ( type.isExtension() && type.getBaseType() != null ) {
642         extensionComplexType = null;
643         getContentHandler().endElement(XMLSCHEMA_URI, EXTENSION_TAG, "");
644         getContentHandler().endElement(XMLSCHEMA_URI, COMPLEXCONTENT_TAG, "");
645       }
646       
647       // complexContent, restriction
648
else if ( type.isRestriction() && type.getBaseType() != null ) {
649         getContentHandler().endElement(XMLSCHEMA_URI, RESTRICTION_TAG, "");
650         getContentHandler().endElement(XMLSCHEMA_URI, COMPLEXCONTENT_TAG, "");
651       }
652       
653             getContentHandler().endElement(XMLSCHEMA_URI, COMPLEX_TYPE_TAG, "");
654       }
655       catch ( SAXException JavaDoc se ) {
656         throw new SchemaException(se.getMessage());
657       }
658     }
659
660     public void visit(ElementDeclaration decl) throws SchemaException {
661       // check if this element is alread visited in base type
662
if ( extensionComplexType != null ) {
663         ComplexType base = (ComplexType)extensionComplexType.getBaseType();
664         if ( base.getElementDeclaration(decl.getNamespace(), decl.getName()) != null )
665           return;
666       }
667       
668       try {
669         getContentHandler().endElement(XMLSCHEMA_URI, ELEMENT_TAG, "");
670       }
671       catch ( SAXException JavaDoc se ) {
672         throw new SchemaException(se.getMessage());
673       }
674     }
675
676     public void visit(AttributeDeclaration decl) throws SchemaException {
677       try {
678         getContentHandler().endElement(XMLSCHEMA_URI, ATTRIBUTE_TAG, "");
679       }
680       catch ( SAXException JavaDoc se ) {
681         throw new SchemaException(se.getMessage());
682       }
683     }
684   
685     public void visit(SchemaComponentRef ref) throws SchemaException {
686       try {
687         if (ref.getType() == Schema.MODEL_GROUP_DEFINITION) {
688           getContentHandler().endElement(XMLSCHEMA_URI, GROUP_TAG, "");
689         }
690         else if (ref.getType() == Schema.ATTRIBUTE_GROUP_DEFINITION) {
691           getContentHandler().endElement(XMLSCHEMA_URI, ATTRIBUTE_GROUP_TAG, "");
692         }
693       }
694       catch ( SAXException JavaDoc se ) {
695         throw new SchemaException(se.getMessage());
696       }
697     }
698     
699     public void visit(ModelGroupDefinition def) throws SchemaException {
700       try {
701         getContentHandler().endElement(XMLSCHEMA_URI, GROUP_TAG, "");
702       }
703       catch ( SAXException JavaDoc se ) {
704         throw new SchemaException(se.getMessage());
705       }
706     }
707     
708     public void visit(AttributeGroupDefinition def) throws SchemaException {
709       try {
710         getContentHandler().endElement(XMLSCHEMA_URI, ATTRIBUTE_GROUP_TAG, "");
711       }
712       catch ( SAXException JavaDoc se ) {
713         throw new SchemaException(se.getMessage());
714       }
715     }
716   
717     public void visit(Wildcard wildcard) throws SchemaException {
718       try {
719         if (!visitor.isAttributeWildcard()) getContentHandler().endElement(XMLSCHEMA_URI, ANY_TAG, "");
720         else getContentHandler().endElement(XMLSCHEMA_URI, ANY_ATTRIBUTE_TAG, "");
721       }
722       catch ( SAXException JavaDoc se ) {
723         throw new SchemaException(se.getMessage());
724       }
725     }
726   
727     public void visit(ContentModel model) throws SchemaException {
728       try {
729         Object JavaDoc p = model.getModel();
730         if ( p != null && !(p instanceof SimpleType) ) {
731           if ( ((Particle)p).getTerm() != null && ((Particle)p).getTerm() instanceof ElementDeclaration )
732             getContentHandler().endElement(XMLSCHEMA_URI, SEQUENCE_TAG, "");
733         }
734       }
735       catch ( SAXException JavaDoc se ) {
736         throw new SchemaException(se.getMessage());
737       }
738     }
739
740     public void visit(Particle particle) throws SchemaException {
741       min = -1;
742       max = -1;
743       unbounded = false;
744     }
745     
746     public void visit(AllModelGroup group) throws SchemaException {
747       try {
748         getContentHandler().endElement(XMLSCHEMA_URI, ALL_TAG, "");
749       }
750       catch ( SAXException JavaDoc se ) {
751         throw new SchemaException(se.getMessage());
752       }
753       depthCount--;
754     }
755     
756     public void visit(ChoiceModelGroup group) throws SchemaException {
757       try {
758         getContentHandler().endElement(XMLSCHEMA_URI, CHOICE_TAG, "");
759       }
760       catch ( SAXException JavaDoc se ) {
761         throw new SchemaException(se.getMessage());
762       }
763     }
764   
765         // 26/01/2001
766
public void visit(SequenceModelGroup group) throws SchemaException {
767       try {
768         getContentHandler().endElement(XMLSCHEMA_URI, SEQUENCE_TAG, "");
769       }
770       catch ( SAXException JavaDoc se ) {
771         throw new SchemaException(se.getMessage());
772       }
773     }
774     
775   }
776   
777 }
778
Popular Tags