KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.xml.sax.Attributes JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27 import org.xquark.schema.*;
28 import org.xquark.util.DefaultElementHandler;
29 import org.xquark.util.ElementHandler;
30
31 class ComplexTypeHandler extends DefaultElementHandler implements SchemaConstants
32 {
33 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
34 private static final String JavaDoc RCSName = "$Name: $";
35     private Loader loader = null;
36     private ComplexType type = null;
37   private boolean complexContent;
38   private boolean redefine;
39
40     ComplexTypeHandler(Loader loader, ComplexType type, boolean complexContent, boolean redefine)
41     throws SAXException JavaDoc
42   {
43             this.loader = loader;
44             this.type = type;
45       this.complexContent = complexContent;
46       this.redefine = redefine;
47     }
48     
49     public ElementHandler startElement(String JavaDoc namespaceURI, String JavaDoc localName, Attributes JavaDoc atts)
50     throws SAXException JavaDoc
51     {
52         if ( namespaceURI.equals(XMLSCHEMA_URI) ) {
53         // in Redefinition, complexContent is always false
54
if ( complexContent ) {
55           if ( localName.equals(RESTRICTION_TAG) || localName.equals(EXTENSION_TAG) ) {
56                 Type baseType = null;
57                 try {
58                   baseType = loader.getType(atts.getValue("", BASE_ATTR));
59                 }
60                 catch ( SchemaException se ) {
61                   String JavaDoc errMsg = "Error while processing localName -> " + localName;
62                   loader.reportLoadingError(errMsg, se);
63                 }
64                 if ( redefine ) {
65                   redefine = false;
66                   if ( type.getName() == null || baseType == null ||
67                        !type.getName().equals(baseType.getName()) ) {
68                       String JavaDoc errCode = "src-redefine.5";
69                       String JavaDoc errMsg = "Error while processing localName -> " + localName;
70                       loader.reportLoadingError(errMsg, new SchemaException(errCode));
71                   }
72                 }
73                 else
74                     type.setBaseType(baseType);
75               
76             // extention or restriction ?
77
if ( localName.equals(RESTRICTION_TAG) ) type.setExtension(false);
78             else type.setExtension(true);
79
80             return this;
81           }
82         }
83         else {
84           // case 1 : simpleContent
85
if ( localName.equals(SIMPLECONTENT_TAG) ) {
86             if ( !redefine ) {
87               type.setContentModel(new ContentModel(TEXT_ONLY));
88             }
89             return new SimpleTypeHandler(loader, type, true, redefine);
90           }
91
92           // case 2 : complexContent
93
if ( localName.equals(COMPLEXCONTENT_TAG) ) {
94             String JavaDoc mixed = atts.getValue("", MIXED_VALUE);
95             if ( TRUE_VALUE.equals(mixed)) {
96               type.setContentModel(new ContentModel(MIXED));
97             }
98             return new ComplexTypeHandler(loader, type, true, redefine);
99           }
100         }
101
102         // redefined complexType can't have the following tags
103
if ( redefine ) {
104           String JavaDoc errCode = "src-redefine.5";
105           String JavaDoc errMsg = "Error while processing localName -> " + localName;
106           loader.reportLoadingError(errMsg, new SchemaException(errCode));
107         }
108
109         // case 3 : sequence | choice | all | group
110
if (localName.equals(GROUP_TAG) || localName.equals(SEQUENCE_TAG)
111             || localName.equals(CHOICE_TAG) || localName.equals(ALL_TAG)) {
112           if ( !complexContent ) type.setExtension(false);
113           Particle particle = null;
114           try {
115             particle = loader.buildParticle(localName, type, atts);
116           }
117           catch ( SchemaException se ) {
118             String JavaDoc errMsg = "Error while processing localName -> " + localName;
119             loader.reportLoadingError(errMsg, se);
120           }
121           if (type.getContentModel() == null) {
122             type.setContentModel(new ContentModel(particle));
123           }
124           else if (type.getContentModel().getModel() == null) {
125             type.getContentModel().setModel(particle);
126           }
127           Object JavaDoc term = particle.getTerm();
128           if (term instanceof ModelGroup) {
129             return new ContentModelHandler(loader, (ModelGroup)term, type);
130           }
131           else return this;
132         }
133
134         // case 4 : attribute
135
if ( localName.equals(ATTRIBUTE_TAG) ) {
136           if ( !complexContent ) type.setExtension(false);
137           AttributeDeclaration decl = null;
138           try {
139             decl = loader.buildAttributeDeclaration(atts, type);
140             type.register(decl);
141           } catch ( SchemaException e ) {
142             String JavaDoc errMsg = "Error while processing localName -> " + localName;
143             loader.reportLoadingError(errMsg, e);
144           }
145           return new AttributeTypeHandler(loader, decl);
146         }
147
148         // case 5 : attribute group
149
if ( localName.equals(ATTRIBUTE_GROUP_TAG) ) {
150           if ( !complexContent ) type.setExtension(false);
151           String JavaDoc refName = atts.getValue("", REF_ATTR); // refName is verified by Schema of Schemas
152
SchemaComponent ref = null;
153           try {
154             ref = loader.getRef(refName, Schema.ATTRIBUTE_GROUP_DEFINITION);
155             type.register(ref);
156           } catch ( SchemaException e ) {
157             String JavaDoc errMsg = "Error while processing localName -> " + localName;
158             loader.reportLoadingError(errMsg, e);
159           }
160           return this;
161         }
162
163         // case 6 : anyAttribute
164
if (localName.equals(ANY_ATTRIBUTE_TAG)) {
165           if ( !complexContent ) type.setExtension(false);
166           Wildcard wildcard = null;
167           try {
168             wildcard = loader.buildWildcard(atts);
169           } catch ( SchemaException e ) {
170             String JavaDoc errMsg = "Error while processing localName -> " + localName;
171             loader.reportLoadingError(errMsg, e);
172           }
173           type.setAttributeWildcard(wildcard);
174           return this;
175         }
176
177         // case 7 : annotation
178
if ( localName.equals(ANNOTATION_TAG) ) return new AnnotationHandler();
179         }
180         loader.notifyUnknownElement(namespaceURI, localName);
181         return this;
182     }
183 }
184
Popular Tags