KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > traversers > XSDGroupTraverser


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.xerces.impl.xs.traversers;
17
18 import org.apache.xerces.impl.xs.SchemaGrammar;
19 import org.apache.xerces.impl.xs.SchemaSymbols;
20 import org.apache.xerces.impl.xs.XSAnnotationImpl;
21 import org.apache.xerces.impl.xs.XSGroupDecl;
22 import org.apache.xerces.impl.xs.XSModelGroupImpl;
23 import org.apache.xerces.impl.xs.XSParticleDecl;
24 import org.apache.xerces.impl.xs.util.XInt;
25 import org.apache.xerces.util.DOMUtil;
26 import org.apache.xerces.util.XMLSymbols;
27 import org.apache.xerces.xni.QName;
28 import org.w3c.dom.Element JavaDoc;
29
30 /**
31  * The model group schema component traverser.
32  *
33  * <group
34  * name = NCName>
35  * Content: (annotation?, (all | choice | sequence))
36  * </group>
37  *
38  * @xerces.internal
39  *
40  * @author Rahul Srivastava, Sun Microsystems Inc.
41  * @author Elena Litani, IBM
42  * @author Lisa Martin, IBM
43  * @version $Id: XSDGroupTraverser.java,v 1.23 2004/12/20 05:43:36 mrglavas Exp $
44  */

45 class XSDGroupTraverser extends XSDAbstractParticleTraverser {
46     
47     XSDGroupTraverser (XSDHandler handler,
48             XSAttributeChecker gAttrCheck) {
49         
50         super(handler, gAttrCheck);
51     }
52     
53     XSParticleDecl traverseLocal(Element JavaDoc elmNode,
54             XSDocumentInfo schemaDoc,
55             SchemaGrammar grammar) {
56         
57         // General Attribute Checking for elmNode declared locally
58
Object JavaDoc[] attrValues = fAttrChecker.checkAttributes(elmNode, false,
59                 schemaDoc);
60         QName refAttr = (QName) attrValues[XSAttributeChecker.ATTIDX_REF];
61         XInt minAttr = (XInt) attrValues[XSAttributeChecker.ATTIDX_MINOCCURS];
62         XInt maxAttr = (XInt) attrValues[XSAttributeChecker.ATTIDX_MAXOCCURS];
63         
64         XSGroupDecl group = null;
65         
66         // ref should be here.
67
if (refAttr == null) {
68             reportSchemaError("s4s-att-must-appear", new Object JavaDoc[]{"group (local)", "ref"}, elmNode);
69         } else {
70             // get global decl
71
// index is a particle index.
72
group = (XSGroupDecl)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.GROUP_TYPE, refAttr, elmNode);
73         }
74         
75         // no children other than "annotation?" are allowed
76
Element JavaDoc child = DOMUtil.getFirstChildElement(elmNode);
77         if (child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
78             // REVISIT: put this somewhere
79
traverseAnnotationDecl(child, attrValues, false, schemaDoc);
80             child = DOMUtil.getNextSiblingElement(child);
81         }
82         
83         if (child != null) {
84             reportSchemaError("s4s-elt-must-match.1", new Object JavaDoc[]{"group (local)", "(annotation?)", DOMUtil.getLocalName(elmNode)}, elmNode);
85         }
86         
87         int minOccurs = minAttr.intValue();
88         int maxOccurs = maxAttr.intValue();
89         
90         XSParticleDecl particle = null;
91         
92         // not empty group, not empty particle
93
if (group != null && group.fModelGroup != null &&
94                 !(minOccurs == 0 && maxOccurs == 0)) {
95             // create a particle to contain this model group
96
if (fSchemaHandler.fDeclPool != null) {
97                 particle = fSchemaHandler.fDeclPool.getParticleDecl();
98             } else {
99                 particle = new XSParticleDecl();
100             }
101             particle.fType = XSParticleDecl.PARTICLE_MODELGROUP;
102             particle.fValue = group.fModelGroup;
103             particle.fMinOccurs = minOccurs;
104             particle.fMaxOccurs = maxOccurs;
105         }
106         
107         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
108         
109         return particle;
110         
111     } // traverseLocal
112

113     XSGroupDecl traverseGlobal(Element JavaDoc elmNode,
114             XSDocumentInfo schemaDoc,
115             SchemaGrammar grammar) {
116         
117         // General Attribute Checking for elmNode declared globally
118
Object JavaDoc[] attrValues = fAttrChecker.checkAttributes(elmNode, true,
119                 schemaDoc);
120         String JavaDoc strNameAttr = (String JavaDoc) attrValues[XSAttributeChecker.ATTIDX_NAME];
121         
122         // must have a name
123
if (strNameAttr == null) {
124             reportSchemaError("s4s-att-must-appear", new Object JavaDoc[]{"group (global)", "name"}, elmNode);
125         }
126         
127         XSGroupDecl group = null;
128         XSParticleDecl particle = null;
129         
130         // must have at least one child
131
Element JavaDoc l_elmChild = DOMUtil.getFirstChildElement(elmNode);
132         XSAnnotationImpl annotation = null;
133         if (l_elmChild == null) {
134             reportSchemaError("s4s-elt-must-match.2",
135                     new Object JavaDoc[]{"group (global)", "(annotation?, (all | choice | sequence))"},
136                     elmNode);
137         } else {
138             // Create the group defi up-front, so it can be passed
139
// to the traversal methods
140
group = new XSGroupDecl();
141             
142             String JavaDoc childName = l_elmChild.getLocalName();
143             if (childName.equals(SchemaSymbols.ELT_ANNOTATION)) {
144                 annotation = traverseAnnotationDecl(l_elmChild, attrValues, true, schemaDoc);
145                 l_elmChild = DOMUtil.getNextSiblingElement(l_elmChild);
146                 if (l_elmChild != null)
147                     childName = l_elmChild.getLocalName();
148             }
149             else {
150                 String JavaDoc text = DOMUtil.getSyntheticAnnotation(elmNode);
151                 if (text != null) {
152                     annotation = traverseSyntheticAnnotation(elmNode, text, attrValues, false, schemaDoc);
153                 }
154             }
155             
156             if (l_elmChild == null) {
157                 reportSchemaError("s4s-elt-must-match.2",
158                         new Object JavaDoc[]{"group (global)", "(annotation?, (all | choice | sequence))"},
159                         elmNode);
160             } else if (childName.equals(SchemaSymbols.ELT_ALL)) {
161                 particle = traverseAll(l_elmChild, schemaDoc, grammar, CHILD_OF_GROUP, group);
162             } else if (childName.equals(SchemaSymbols.ELT_CHOICE)) {
163                 particle = traverseChoice(l_elmChild, schemaDoc, grammar, CHILD_OF_GROUP, group);
164             } else if (childName.equals(SchemaSymbols.ELT_SEQUENCE)) {
165                 particle = traverseSequence(l_elmChild, schemaDoc, grammar, CHILD_OF_GROUP, group);
166             } else {
167                 reportSchemaError("s4s-elt-must-match.1",
168                         new Object JavaDoc[]{"group (global)", "(annotation?, (all | choice | sequence))", DOMUtil.getLocalName(l_elmChild)},
169                         l_elmChild);
170             }
171             
172             if (l_elmChild != null &&
173                     DOMUtil.getNextSiblingElement(l_elmChild) != null) {
174                 reportSchemaError("s4s-elt-must-match.1",
175                         new Object JavaDoc[]{"group (global)", "(annotation?, (all | choice | sequence))",
176                         DOMUtil.getLocalName(DOMUtil.getNextSiblingElement(l_elmChild))},
177                         DOMUtil.getNextSiblingElement(l_elmChild));
178             }
179             
180             // add global group declaration to the grammar
181
if (strNameAttr != null) {
182                 group.fName = strNameAttr;
183                 group.fTargetNamespace = schemaDoc.fTargetNamespace;
184                 if (particle != null)
185                     group.fModelGroup = (XSModelGroupImpl)particle.fValue;
186                 group.fAnnotation = annotation;
187                 grammar.addGlobalGroupDecl(group);
188             }
189             else {
190                 // name attribute is not there, don't return this group.
191
group = null;
192             }
193         }
194         if(group != null) {
195             // store groups redefined by restriction in the grammar so
196
// that we can get at them at full-schema-checking time.
197
Object JavaDoc redefinedGrp = fSchemaHandler.getGrpOrAttrGrpRedefinedByRestriction(XSDHandler.GROUP_TYPE,
198                     new QName(XMLSymbols.EMPTY_STRING, strNameAttr, strNameAttr, schemaDoc.fTargetNamespace),
199                     schemaDoc, elmNode);
200             if(redefinedGrp != null) {
201                 // store in grammar
202
grammar.addRedefinedGroupDecl(group, (XSGroupDecl)redefinedGrp,
203                         fSchemaHandler.element2Locator(elmNode));
204             }
205         }
206         
207         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
208         
209         return group;
210         
211     } // traverseGlobal
212
}
213
Popular Tags