KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.StringTokenizer JavaDoc;
26
27 import org.xml.sax.Attributes JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xquark.schema.*;
30 import org.xquark.util.DefaultElementHandler;
31 import org.xquark.util.ElementHandler;
32
33 class SimpleTypeHandler extends DefaultElementHandler implements SchemaConstants
34 {
35 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
36 private static final String JavaDoc RCSName = "$Name: $";
37   private Loader loader = null;
38   private Type type = null;
39   private boolean simpleContent;
40   private boolean redefine;
41   private boolean alreadyRedefined = false;
42
43   SimpleTypeHandler(Loader loader, Type type, boolean simpleContent, boolean redefine)
44     throws SAXException JavaDoc
45   {
46             this.loader = loader;
47             this.type = type;
48       this.simpleContent = simpleContent;
49       this.redefine = redefine;
50     }
51         
52     public ElementHandler startElement(String JavaDoc strNamespaceURI, String JavaDoc localName, Attributes JavaDoc atts)
53     throws SAXException JavaDoc
54     {
55       if ( strNamespaceURI.equals(XMLSCHEMA_URI) ) {
56         // restriction
57
if ( localName.equals(RESTRICTION_TAG) ) {
58           if ( !redefine ) {
59             // set base type
60
Type baseType = null;
61             try {
62               baseType = loader.getType(atts.getValue("", BASE_ATTR));
63             }
64             catch ( SchemaException se ) {
65               String JavaDoc errMsg = "Error while processing localName -> " + localName;
66               loader.reportLoadingError(errMsg, se);
67             }
68             type.setBaseType(baseType);
69             if ( simpleContent ) ((ComplexType)type).setExtension(false);
70           }
71           else {
72             // redefine simpel Type : special treating
73
alreadyRedefined = true;
74             ((SimpleType)type).setVariety(SimpleType.VARIETY_ATOMIC);
75             // get base type name
76
String JavaDoc baseTypeName = atts.getValue("", BASE_ATTR);
77             if ( !this.type.getName().equals(baseTypeName) ) {
78               String JavaDoc errCode = "src-redefine.5";
79               String JavaDoc errMsg = "Error while processing localName -> " + localName;
80               loader.reportLoadingError(errMsg, new SchemaException(errCode));
81             }
82           }
83           return this;
84         }
85
86         // in a redefinition, simpleContent is always false
87
// facet
88
Facet facet = loader.buildFacet(localName, atts);
89         if (facet != null) {
90           if ( simpleContent ) {
91             SimpleType contentType = null;
92             if (((ComplexType)type).getContentModel().getModel() == null) {
93               contentType = new SimpleType(type.getSchema(), null, new SimpleBaseTypeRef(((ComplexType)type)));
94               ((ComplexType)type).getContentModel().setModel(contentType);
95             } else {
96               contentType = (SimpleType)((ComplexType)type).getContentModel().getModel();
97             }
98             try {
99               contentType.setFacet(facet);
100             } catch ( SchemaException e ) {
101               String JavaDoc errMsg = "Error while processing localName -> " + localName;
102               loader.reportLoadingError(errMsg, e);
103             }
104           }
105           else {
106             if ( redefine && !alreadyRedefined ) {
107               String JavaDoc errCode = "src-redefine.5";
108               String JavaDoc errMsg = "Error while processing localName -> " + localName;
109               loader.reportLoadingError(errMsg, new SchemaException(errCode));
110             }
111             try {
112               ((SimpleType)type).setFacet(facet);
113             } catch ( SchemaException e ) {
114               String JavaDoc errMsg = "Error while processing localName -> " + localName;
115               loader.reportLoadingError(errMsg, e);
116             }
117           }
118
119           return this;
120         }
121
122         // redefined simpleType can't have the following tags
123
if ( redefine ) {
124           String JavaDoc errCode = "src-redefine.5";
125           String JavaDoc errMsg = "Error while processing localName -> " + localName;
126           loader.reportLoadingError(errMsg, new SchemaException(errCode));
127         }
128
129         // this is only when simpleContent is true
130
// in a redefinition, simpleContent is always false
131
if ( simpleContent && localName.equals(EXTENSION_TAG) ) {
132           // get base type
133
Type baseType = null;
134           try {
135             baseType = loader.getType(atts.getValue("", BASE_ATTR));
136           }
137           catch ( SchemaException se ) {
138             String JavaDoc errMsg = "Error while processing localName -> " + localName;
139             loader.reportLoadingError(errMsg, se);
140           }
141           type.setBaseType(baseType);
142           ((ComplexType)type).setExtension(true);
143           return this;
144         }
145
146         else if (localName.equals(SIMPLE_TYPE_TAG)) {
147           // inline simple type definition
148
SimpleType inlineType = null;
149           try {
150             inlineType = loader.buildSimpleType(atts);
151           }
152           catch ( SchemaException se ) {
153             String JavaDoc errMsg = "Error while processing localName -> " + localName;
154             loader.reportLoadingError(errMsg, se);
155           }
156           if ( simpleContent ) {
157             ((ComplexType)type).getContentModel().setModel(inlineType);
158             return new SimpleTypeHandler(loader, inlineType, false, false);
159           }
160           else {
161             switch ( ((SimpleType)type).getVariety() ) {
162               case SimpleType.VARIETY_ATOMIC:
163               // chack for restriction
164
if ( type.getBaseType() != null ) {
165                 String JavaDoc errCode = "src-simple-type.2";
166                 String JavaDoc errMsg = "Error while processing localName -> " + localName;
167                 loader.reportLoadingError(errMsg, new SchemaException(errCode));
168               }
169               type.setBaseType(inlineType);
170
171               case SimpleType.VARIETY_LIST:
172               if ( ((SimpleType)type).getItemType() != null ) {
173                 String JavaDoc errCode = "src-simple-type.3";
174                 String JavaDoc errMsg = "Error while processing localName -> " + localName;
175                 loader.reportLoadingError(errMsg, new SchemaException(errCode));
176               }
177               ((SimpleType)type).setItemType(inlineType);
178
179               case SimpleType.VARIETY_UNION:
180               ((SimpleType)type).addMemberType(inlineType);
181             }
182             return new SimpleTypeHandler(loader, inlineType, false, false);
183           }
184         }
185
186         // list, only simpleContent is false
187
// it's verificated by Schema of Schemas
188
else if ( localName.equals(LIST_TAG) ) {
189           ((SimpleType)type).setVariety(SimpleType.VARIETY_LIST);
190           // check itemType attribute
191
String JavaDoc itemTypeName = atts.getValue("", ITEM_TYPE_ATTR);
192           if ( itemTypeName != null ) {
193             try {
194               ((SimpleType)type).setItemType(loader.getType(itemTypeName));
195             }
196             catch ( SchemaException se ) {
197               String JavaDoc errMsg = "Error while processing localName -> " + localName;
198               loader.reportLoadingError(errMsg, se);
199             }
200           }
201           return this;
202         }
203
204         // union, only simpleContent is false
205
// it's verificated by Schema of Schemas
206
else if ( localName.equals(UNION_TAG) ) {
207           ((SimpleType)type).setVariety(SimpleType.VARIETY_UNION);
208           // check memberTypes attribute
209
String JavaDoc memberTypes = atts.getValue("", MEMBER_TYPES_ATTR);
210           if ( memberTypes != null ) {
211             StringTokenizer JavaDoc stMemberTypes = new StringTokenizer JavaDoc(memberTypes);
212             while (stMemberTypes.hasMoreTokens()) {
213               String JavaDoc memberTypeName = stMemberTypes.nextToken();
214               try {
215                 ((SimpleType)type).addMemberType(loader.getType(memberTypeName));
216               }
217               catch ( SchemaException se ) {
218                 String JavaDoc errMsg = "Error while processing localName -> " + localName;
219                 loader.reportLoadingError(errMsg, se);
220               }
221             }
222           }
223           return this;
224         }
225
226         // attribute tag, only when simpleContent is true
227
// it's verificated by Schema of Schemas
228
else if ( localName.equals(ATTRIBUTE_TAG) ) {
229           // case 3: attribute declaration
230
AttributeDeclaration decl = null;
231           try {
232             decl = loader.buildAttributeDeclaration(atts, ((ComplexType)type));
233             ((ComplexType)type).register(decl);
234           } catch ( SchemaException e ) {
235             String JavaDoc errMsg = "Error while processing localName -> " + localName;
236             loader.reportLoadingError(errMsg, e);
237           }
238           return new AttributeTypeHandler(loader, decl);
239         }
240
241         // attribute group, only when simpleContent is true
242
// it's verificated by Schema of Schemas
243
else if ( localName.equals(ATTRIBUTE_GROUP_TAG) ) {
244           // case 4: attribute group reference
245
String JavaDoc refName = atts.getValue("", REF_ATTR);
246           // refName is verified by Schema of Schemas
247
try {
248             SchemaComponent ref = loader.getRef(refName, Schema.ATTRIBUTE_GROUP_DEFINITION);
249             ((ComplexType)type).register(ref);
250           } catch ( SchemaException e ) {
251             String JavaDoc errMsg = "Error while processing localName -> " + localName;
252             loader.reportLoadingError(errMsg, e);
253           }
254           return this;
255         }
256
257         // anyAttribute, only when simpleContent is true
258
// it's verificated by Schema of Schemas
259
else if (localName.equals(ANY_ATTRIBUTE_TAG)) {
260           // case 5: any attribute reference
261
Wildcard wildcard = null;
262           try {
263             wildcard = loader.buildWildcard(atts);
264           } catch ( SchemaException e ) {
265             String JavaDoc errMsg = "Error while processing localName -> " + localName;
266             loader.reportLoadingError(errMsg, e);
267           }
268           ((ComplexType)type).setAttributeWildcard(wildcard);
269           return this;
270         }
271
272         else if ( localName.equals(ANNOTATION_TAG) ) {
273           return new AnnotationHandler();
274         }
275       }
276         
277       loader.notifyUnknownElement(strNamespaceURI, localName);
278       return this;
279     }
280     
281 }
282
Popular Tags