KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2004 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57 package com.sun.org.apache.xerces.internal.impl.xs.traversers;
58
59 import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar;
60 import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols;
61 import com.sun.org.apache.xerces.internal.impl.xs.XSAnnotationImpl;
62 import com.sun.org.apache.xerces.internal.impl.xs.XSGroupDecl;
63 import com.sun.org.apache.xerces.internal.impl.xs.XSModelGroupImpl;
64 import com.sun.org.apache.xerces.internal.impl.xs.XSParticleDecl;
65 import com.sun.org.apache.xerces.internal.impl.xs.util.XInt;
66 import com.sun.org.apache.xerces.internal.util.DOMUtil;
67 import com.sun.org.apache.xerces.internal.util.XMLSymbols;
68 import com.sun.org.apache.xerces.internal.xni.QName;
69 import org.w3c.dom.Element JavaDoc;
70
71 /**
72  * The model group schema component traverser.
73  *
74  * <group
75  * name = NCName>
76  * Content: (annotation?, (all | choice | sequence))
77  * </group>
78  *
79  * @author Rahul Srivastava, Sun Microsystems Inc.
80  * @author Elena Litani, IBM
81  * @author Lisa Martin, IBM
82  * @version $Id: XSDGroupTraverser.java,v 1.19 2004/02/03 17:27:45 sandygao Exp $
83  */

84 class XSDGroupTraverser extends XSDAbstractParticleTraverser {
85
86     XSDGroupTraverser (XSDHandler handler,
87                        XSAttributeChecker gAttrCheck) {
88
89         super(handler, gAttrCheck);
90     }
91
92     XSParticleDecl traverseLocal(Element JavaDoc elmNode,
93                                  XSDocumentInfo schemaDoc,
94                                  SchemaGrammar grammar) {
95
96         // General Attribute Checking for elmNode declared locally
97
Object JavaDoc[] attrValues = fAttrChecker.checkAttributes(elmNode, false,
98                               schemaDoc);
99         QName refAttr = (QName) attrValues[XSAttributeChecker.ATTIDX_REF];
100         XInt minAttr = (XInt) attrValues[XSAttributeChecker.ATTIDX_MINOCCURS];
101         XInt maxAttr = (XInt) attrValues[XSAttributeChecker.ATTIDX_MAXOCCURS];
102
103         XSGroupDecl group = null;
104
105         // ref should be here.
106
if (refAttr == null) {
107             reportSchemaError("s4s-att-must-appear", new Object JavaDoc[]{"group (local)", "ref"}, elmNode);
108         } else {
109             // get global decl
110
// index is a particle index.
111
group = (XSGroupDecl)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.GROUP_TYPE, refAttr, elmNode);
112         }
113
114         // no children other than "annotation?" are allowed
115
Element JavaDoc child = DOMUtil.getFirstChildElement(elmNode);
116         if (child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
117             // REVISIT: put this somewhere
118
traverseAnnotationDecl(child, attrValues, false, schemaDoc);
119             child = DOMUtil.getNextSiblingElement(child);
120         }
121         if (child != null) {
122             reportSchemaError("s4s-elt-must-match.1", new Object JavaDoc[]{"group (local)", "(annotation?)", DOMUtil.getLocalName(elmNode)}, elmNode);
123         }
124
125         int minOccurs = minAttr.intValue();
126         int maxOccurs = maxAttr.intValue();
127
128         XSParticleDecl particle = null;
129
130         // not empty group, not empty particle
131
if (group != null && group.fModelGroup != null &&
132             !(minOccurs == 0 && maxOccurs == 0)) {
133             // create a particle to contain this model group
134
if (fSchemaHandler.fDeclPool != null) {
135                 particle = fSchemaHandler.fDeclPool.getParticleDecl();
136             } else {
137                 particle = new XSParticleDecl();
138             }
139             particle.fType = XSParticleDecl.PARTICLE_MODELGROUP;
140             particle.fValue = group.fModelGroup;
141             particle.fMinOccurs = minOccurs;
142             particle.fMaxOccurs = maxOccurs;
143         }
144
145         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
146
147         return particle;
148
149     } // traverseLocal
150

151     XSGroupDecl traverseGlobal(Element JavaDoc elmNode,
152                                XSDocumentInfo schemaDoc,
153                                SchemaGrammar grammar) {
154
155         // General Attribute Checking for elmNode declared globally
156
Object JavaDoc[] attrValues = fAttrChecker.checkAttributes(elmNode, true,
157                               schemaDoc);
158         String JavaDoc strNameAttr = (String JavaDoc) attrValues[XSAttributeChecker.ATTIDX_NAME];
159
160         // must have a name
161
if (strNameAttr == null) {
162             reportSchemaError("s4s-att-must-appear", new Object JavaDoc[]{"group (global)", "name"}, elmNode);
163         }
164
165         XSGroupDecl group = null;
166         XSParticleDecl particle = null;
167
168         // must have at least one child
169
Element JavaDoc l_elmChild = DOMUtil.getFirstChildElement(elmNode);
170         XSAnnotationImpl annotation = null;
171         if (l_elmChild == null) {
172             reportSchemaError("s4s-elt-must-match.2",
173                               new Object JavaDoc[]{"group (global)", "(annotation?, (all | choice | sequence))"},
174                               elmNode);
175         } else {
176             // Create the group defi up-front, so it can be passed
177
// to the traversal methods
178
group = new XSGroupDecl();
179
180             String JavaDoc childName = l_elmChild.getLocalName();
181             if (childName.equals(SchemaSymbols.ELT_ANNOTATION)) {
182                 annotation = traverseAnnotationDecl(l_elmChild, attrValues, true, schemaDoc);
183                 l_elmChild = DOMUtil.getNextSiblingElement(l_elmChild);
184                 if (l_elmChild != null)
185                     childName = l_elmChild.getLocalName();
186             }
187
188             if (l_elmChild == null) {
189                 reportSchemaError("s4s-elt-must-match.2",
190                                   new Object JavaDoc[]{"group (global)", "(annotation?, (all | choice | sequence))"},
191                                   elmNode);
192             } else if (childName.equals(SchemaSymbols.ELT_ALL)) {
193                 particle = traverseAll(l_elmChild, schemaDoc, grammar, CHILD_OF_GROUP, group);
194             } else if (childName.equals(SchemaSymbols.ELT_CHOICE)) {
195                 particle = traverseChoice(l_elmChild, schemaDoc, grammar, CHILD_OF_GROUP, group);
196             } else if (childName.equals(SchemaSymbols.ELT_SEQUENCE)) {
197                 particle = traverseSequence(l_elmChild, schemaDoc, grammar, CHILD_OF_GROUP, group);
198             } else {
199                 reportSchemaError("s4s-elt-must-match.1",
200                                   new Object JavaDoc[]{"group (global)", "(annotation?, (all | choice | sequence))", DOMUtil.getLocalName(l_elmChild)},
201                                   l_elmChild);
202             }
203
204             if (l_elmChild != null &&
205                 DOMUtil.getNextSiblingElement(l_elmChild) != null) {
206                 reportSchemaError("s4s-elt-must-match.1",
207                                   new Object JavaDoc[]{"group (global)", "(annotation?, (all | choice | sequence))",
208                                                DOMUtil.getLocalName(DOMUtil.getNextSiblingElement(l_elmChild))},
209                                   DOMUtil.getNextSiblingElement(l_elmChild));
210             }
211
212             // add global group declaration to the grammar
213
if (strNameAttr != null) {
214                 group.fName = strNameAttr;
215                 group.fTargetNamespace = schemaDoc.fTargetNamespace;
216                 if (particle != null)
217                     group.fModelGroup = (XSModelGroupImpl)particle.fValue;
218                 group.fAnnotation = annotation;
219                 grammar.addGlobalGroupDecl(group);
220             }
221             else {
222                 // name attribute is not there, don't return this group.
223
group = null;
224             }
225         }
226         if(group != null) {
227             // store groups redefined by restriction in the grammar so
228
// that we can get at them at full-schema-checking time.
229
Object JavaDoc redefinedGrp = fSchemaHandler.getGrpOrAttrGrpRedefinedByRestriction(XSDHandler.GROUP_TYPE,
230                 new QName(XMLSymbols.EMPTY_STRING, strNameAttr, strNameAttr, schemaDoc.fTargetNamespace),
231                 schemaDoc, elmNode);
232             if(redefinedGrp != null) {
233                 // store in grammar
234
grammar.addRedefinedGroupDecl(group, (XSGroupDecl)redefinedGrp,
235                                               fSchemaHandler.element2Locator(elmNode));
236             }
237         }
238
239         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
240
241         return group;
242
243     } // traverseGlobal
244
}
245
Popular Tags