KickJava   Java API By Example, From Geeks To Geeks.

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


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 RedefineHandler 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
37     RedefineHandler(Loader loader) {
38         this.loader = loader;
39     }
40   
41     public ElementHandler startElement(String JavaDoc namespaceURI, String JavaDoc localName, Attributes JavaDoc atts)
42     throws SAXException JavaDoc
43     {
44         if ( namespaceURI.equals(XMLSCHEMA_URI) ) {
45         String JavaDoc redefinedName = atts.getValue("", NAME_ATTR);
46         // redefineName is verified by Schema of Schemas
47

48         // case 1, 2 : SimpleType, ComplexType
49
if ( localName.equals(SIMPLE_TYPE_TAG) || localName.equals(COMPLEX_TYPE_TAG) ) {
50           // find Type whose name is typeName
51
Type oldType = loader.getSchema().getType(redefinedName);
52           if ( oldType == null ) {
53             String JavaDoc errCode = "src-redefine.4.2";
54             String JavaDoc errMsg = "Error while processing localName -> " + localName;
55             loader.reportLoadingError(errMsg, new SchemaException(errCode));
56           }
57           oldType.setName(redefinedName + ".redefine");
58
59           // construct new type
60
if ( localName.equals(SIMPLE_TYPE_TAG) ) {
61             SimpleType newType = null;
62             try {
63               newType = loader.buildSimpleType(atts);
64               newType.setBaseType(oldType);
65               loader.getSchema().removeRegisteredComponent(newType);
66               loader.getSchema().register(newType);
67             }
68             catch ( SchemaException se ) {
69               String JavaDoc errMsg = "Error while processing localName -> " + localName;
70               loader.reportLoadingError(errMsg, se);
71             }
72             return new SimpleTypeHandler(loader, newType, false, true);
73           }
74           else {
75             ComplexType newType = null;
76             try {
77               newType = loader.buildComplexType(atts);
78               newType.setBaseType(oldType);
79               loader.getSchema().removeRegisteredComponent(newType);
80               loader.getSchema().register(newType);
81             }
82             catch ( SchemaException se ) {
83               String JavaDoc errMsg = "Error while processing localName -> " + localName;
84               loader.reportLoadingError(errMsg, se);
85             }
86             return new ComplexTypeHandler(loader, newType, false, true);
87           }
88         }
89
90         // case 3 : attribute group
91
else if ( localName.equals(ATTRIBUTE_GROUP_TAG) ) {
92           // find Type whose name is typeName
93
AttributeGroupDefinition oldAttGroup = loader.getSchema().getAttributeGroupDefinition(redefinedName);
94           if ( oldAttGroup == null ) {
95             String JavaDoc errCode = "src-redefine.4.2";
96             String JavaDoc errMsg = "Error while processing localName -> " + localName;
97             loader.reportLoadingError(errMsg, new SchemaException(errCode));
98           }
99           oldAttGroup.setName(redefinedName + ".redefine");
100
101           // construct new AttributeGroupDefinition
102
AttributeGroupDefinition def = loader.buildAttributeGroupDefinition(atts, loader.getSchema());
103           loader.getSchema().removeRegisteredComponent(def);
104           try {
105             loader.getSchema().register(def);
106           } catch ( SchemaException se ) {
107             // it's vide
108
}
109           return new AttributeGroupHandler(loader, def, oldAttGroup);
110         }
111
112         // case 4 : group
113
else if ( localName.equals(GROUP_TAG) ) {
114           ModelGroupDefinition oldGroup = loader.getSchema().getModelGroupDefinition(redefinedName);
115           if ( oldGroup == null ) {
116             String JavaDoc errCode = "src-redefine.4.2";
117             String JavaDoc errMsg = "Error while processing localName -> " + localName;
118             loader.reportLoadingError(errMsg, new SchemaException(errCode));
119           }
120           oldGroup.setName(redefinedName + ".redefine");
121           try {
122             loader.getSchema().register(oldGroup);
123           } catch ( SchemaException se ) {
124             // it's vide
125
}
126
127           ModelGroupDefinition def = loader.buildModelGroupDefinition(atts, loader.getSchema());
128           loader.getSchema().removeRegisteredComponent(def);
129           try {
130             loader.getSchema().register(def);
131           } catch ( SchemaException se ) {
132             // it's vide
133
}
134           return new ContentModelHandler(loader, def, true);
135         }
136
137         else if (localName.equals(ANNOTATION_TAG)) {
138           return new AnnotationHandler();
139         }
140         }
141         loader.notifyUnknownElement(namespaceURI, localName);
142         return this;
143     }
144 }
145     
146
Popular Tags