KickJava   Java API By Example, From Geeks To Geeks.

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


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 AttributeGroupHandler extends DefaultElementHandler implements SchemaConstants
32 {
33     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
34     private static final String JavaDoc RCSName = "$Name: $";
35     
36     private Loader loader = null;
37     private AttributeGroupDefinition def;
38     private boolean redefined;
39     private AttributeGroupDefinition oldGroup;
40
41     AttributeGroupHandler(Loader loader, AttributeGroupDefinition def) {
42         this.loader = loader;
43         this.def = def;
44         this.redefined = false;
45     }
46     
47     AttributeGroupHandler(Loader loader, AttributeGroupDefinition def, AttributeGroupDefinition oldGroup) {
48         this(loader, def);
49         this.redefined = true;
50         this.oldGroup = oldGroup;
51     }
52
53     public ElementHandler startElement(String JavaDoc namespaceURI, String JavaDoc localName, Attributes JavaDoc atts)
54     throws SAXException JavaDoc
55     {
56         if ( namespaceURI.equals(XMLSCHEMA_URI) ) {
57         if (localName.equals(ATTRIBUTE_GROUP_TAG)) {
58           // redefined attribute group definition : special treating
59
String JavaDoc refName = atts.getValue("", REF_ATTR);
60           // refName == null is verificated by Schema of Schemas
61
if ( redefined && refName.equals(def.getName()) ) {
62             // self reference
63
def.add(oldGroup);
64               redefined = false;
65           }
66           else {
67             SchemaComponent ref = null;
68             try {
69                ref = loader.getRef(refName, Schema.ATTRIBUTE_GROUP_DEFINITION);
70             }
71             catch ( SchemaException se ) {
72               String JavaDoc errMsg = "Error while processing localName -> " + localName;
73               loader.reportLoadingError(errMsg, se);
74             }
75             def.add(ref);
76           }
77           return this;
78         }
79
80         else if (localName.equals(ATTRIBUTE_TAG)) {
81           AttributeDeclaration decl = null;
82           try {
83             decl = loader.buildAttributeDeclaration(atts, null);
84           }
85           catch ( SchemaException se ) {
86             String JavaDoc errMsg = "Error while processing localName -> " + localName;
87             loader.reportLoadingError(errMsg, se);
88           }
89           if ( redefined ) {
90               try {
91                 checkAttributeValidRestriction(decl);
92               }
93               catch ( SchemaException e) {
94                 String JavaDoc errMsg = "Error while processing localName -> " + localName;
95                 loader.reportLoadingError(errMsg, e);
96               }
97           }
98           if ( def.attributeFind(decl) ) {
99             String JavaDoc errCode = "ag-props-correct.2";
100             String JavaDoc errMsg = "Error while processing localName -> " + localName;
101             loader.reportLoadingError(errMsg, new SchemaException(errCode));
102           }
103           def.add(decl);
104           return new AttributeTypeHandler(loader, decl);
105         }
106
107         else if (localName.equals(ANY_ATTRIBUTE_TAG)) {
108           Wildcard wildcard = null;
109           try {
110             wildcard = loader.buildWildcard(atts);
111           }
112           catch ( SchemaException se ) {
113             String JavaDoc errMsg = "Error while processing localName -> " + localName;
114             loader.reportLoadingError(errMsg, se);
115           }
116           if ( redefined ) {
117               try {
118                 checkAttributeValidRestriction(wildcard);
119               }
120               catch ( SchemaException e) {
121                 String JavaDoc errMsg = "Error while processing localName -> " + localName;
122                 loader.reportLoadingError(errMsg, e);
123               }
124           }
125           def.setAttributeWildcard(wildcard);
126           return this;
127         }
128
129         else if (localName.equals(ANNOTATION_TAG)) {
130           return new AnnotationHandler();
131         }
132         }
133         
134         loader.notifyUnknownElement(namespaceURI, localName);
135         return this;
136     }
137     
138     private void checkAttributeValidRestriction(AttributeDeclaration decl)
139     throws SchemaException {
140         java.util.Iterator JavaDoc it = this.oldGroup.getAttributeDeclarations().iterator();
141         AttributeDeclaration baseDecl = null;
142         while ( it.hasNext() ) {
143             AttributeDeclaration attr = (AttributeDeclaration)it.next();
144             if ( decl.getName().equals(attr.getName()) &&
145                  ( ( decl.getNamespace() == null && attr.getNamespace() == null ) ||
146                    ( decl.getNamespace().equals(attr.getNamespace()) ) ) ) {
147                baseDecl = attr;
148                break;
149             }
150         }
151         
152         if ( baseDecl != null ) {
153             if ( !(baseDecl.getUse() != REQUIRED || decl.getUse() == REQUIRED) ) {
154             // "Attribute <use> isn't reqired, but attribute <use> in base type is required.";
155
throw new SchemaException("derivation-ok-restriction.2.1.1", decl);
156             }
157             String JavaDoc errCode02 = decl.getType().checkTypeDerivationOK(baseDecl.getType(), baseDecl.getType().getFinal(), false);
158             if ( errCode02 != null ) {
159             // "Attribute's type definition must be validly derivded from that of the base type's attribute declaration.";
160
throw new SchemaException("derivation-ok-restriction.2.1.2", decl, new SchemaException(errCode02, decl));
161             }
162             if ( !( baseDecl.getFixedValue() == null || baseDecl.getFixedValue().equals(decl.getFixedValue()) ) ) {
163             // "Attribute declaration's effective value constraint doesn't match with that of base type's attribute declaration.";
164
throw new SchemaException("derivation-ok-restriction.2.1.3", decl);
165             }
166         }
167         else if ( this.oldGroup.getAttributeWildcard() == null ||
168                   this.oldGroup.getAttributeWildcard().isAllowed(decl.getNamespace()) == false )
169         {
170         // "Attribute declaration, or a valid attribute wildcard, must be defined in base type.";
171
throw new SchemaException("derivation-ok-restriction.2.2", decl);
172         }
173     }
174     
175     private void checkAttributeValidRestriction(Wildcard wild)
176     throws SchemaException {
177       Wildcard baseW = this.oldGroup.getAttributeWildcard();
178       if ( baseW == null ) {
179 // "Base type must have an attribute wildcard.";
180
throw new SchemaException("derivation-ok-restriction.4.1", this);
181       }
182       else {
183         String JavaDoc errCode02 = wild.validWildcardSubset(baseW);
184         if ( errCode02 != null ) {
185 // "Attribute wildcard must be a valid subset of base type's.";
186
throw new SchemaException("derivation-ok-restriction.4.2", this, new SchemaException(errCode02, wild));
187         }
188       }
189     }
190 }
191     
192
Popular Tags