KickJava   Java API By Example, From Geeks To Geeks.

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


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.XSAttributeGroupDecl;
22 import org.apache.xerces.util.DOMUtil;
23 import org.apache.xerces.util.XMLSymbols;
24 import org.apache.xerces.xni.QName;
25 import org.w3c.dom.Element JavaDoc;
26
27 /**
28  * The attribute group definition schema component traverser.
29  *
30  * <attributeGroup
31  * id = ID
32  * name = NCName
33  * ref = QName
34  * {any attributes with non-schema namespace . . .}>
35  * Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))
36  * </attributeGroup>
37  *
38  * @xerces.internal
39  *
40  * @author Rahul Srivastava, Sun Microsystems Inc.
41  * @author Sandy Gao, IBM
42  *
43  * @version $Id: XSDAttributeGroupTraverser.java,v 1.15 2004/12/20 05:43:36 mrglavas Exp $
44  */

45 class XSDAttributeGroupTraverser extends XSDAbstractTraverser {
46     
47     XSDAttributeGroupTraverser (XSDHandler handler,
48             XSAttributeChecker gAttrCheck) {
49         
50         super(handler, gAttrCheck);
51     }
52     
53     
54     XSAttributeGroupDecl traverseLocal(Element JavaDoc elmNode,
55             XSDocumentInfo schemaDoc,
56             SchemaGrammar grammar) {
57         
58         // General Attribute Checking for elmNode declared locally
59
Object JavaDoc[] attrValues = fAttrChecker.checkAttributes(elmNode, false, schemaDoc);
60         
61         // get attribute
62
QName refAttr = (QName) attrValues[XSAttributeChecker.ATTIDX_REF];
63         
64         XSAttributeGroupDecl attrGrp = null;
65         
66         // ref should be here.
67
if (refAttr == null) {
68             reportSchemaError("s4s-att-must-appear", new Object JavaDoc[]{"attributeGroup (local)", "ref"}, elmNode);
69             fAttrChecker.returnAttrArray(attrValues, schemaDoc);
70             return null;
71         }
72         
73         // get global decl
74
attrGrp = (XSAttributeGroupDecl)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.ATTRIBUTEGROUP_TYPE, refAttr, elmNode);
75         
76         
77         // no children are allowed here except annotation, which is optional.
78
Element JavaDoc child = DOMUtil.getFirstChildElement(elmNode);
79         if (child != null) {
80             String JavaDoc childName = DOMUtil.getLocalName(child);
81             if (childName.equals(SchemaSymbols.ELT_ANNOTATION)) {
82                 traverseAnnotationDecl(child, attrValues, false, schemaDoc);
83                 child = DOMUtil.getNextSiblingElement(child);
84             }
85             
86             if (child != null) {
87                 Object JavaDoc[] args = new Object JavaDoc [] {refAttr.rawname, "(annotation?)", DOMUtil.getLocalName(child)};
88                 reportSchemaError("s4s-elt-must-match.1", args, child);
89             }
90         } // if
91

92         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
93         return attrGrp;
94         
95     } // traverseLocal
96

97     XSAttributeGroupDecl traverseGlobal(Element JavaDoc elmNode,
98             XSDocumentInfo schemaDoc,
99             SchemaGrammar grammar) {
100         
101         XSAttributeGroupDecl attrGrp = new XSAttributeGroupDecl();
102         
103         // General Attribute Checking for elmNode declared globally
104
Object JavaDoc[] attrValues = fAttrChecker.checkAttributes(elmNode, true, schemaDoc);
105         
106         String JavaDoc nameAttr = (String JavaDoc) attrValues[XSAttributeChecker.ATTIDX_NAME];
107         
108         // global declaration must have a name
109
if (nameAttr == null) {
110             reportSchemaError("s4s-att-must-appear", new Object JavaDoc[]{"attributeGroup (global)", "name"}, elmNode);
111             nameAttr = "no name";
112         }
113         
114         attrGrp.fName = nameAttr;
115         attrGrp.fTargetNamespace = schemaDoc.fTargetNamespace;
116         
117         // check the content
118
Element JavaDoc child = DOMUtil.getFirstChildElement(elmNode);
119         XSAnnotationImpl annotation = null;
120         
121         if (child!=null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
122             annotation = traverseAnnotationDecl(child, attrValues, false, schemaDoc);
123             child = DOMUtil.getNextSiblingElement(child);
124         }
125         else {
126             String JavaDoc text = DOMUtil.getSyntheticAnnotation(elmNode);
127             if (text != null) {
128                 annotation = traverseSyntheticAnnotation(elmNode, text, attrValues, false, schemaDoc);
129             }
130         }
131         
132         // Traverse the attribute and attribute group elements and fill in the
133
// attributeGroup structure
134

135         Element JavaDoc nextNode = traverseAttrsAndAttrGrps(child, attrGrp, schemaDoc, grammar, null);
136         if (nextNode!=null) {
137             // An invalid element was found...
138
Object JavaDoc[] args = new Object JavaDoc [] {nameAttr, "(annotation?, ((attribute | attributeGroup)*, anyAttribute?))", DOMUtil.getLocalName(nextNode)};
139             reportSchemaError("s4s-elt-must-match.1", args, nextNode);
140         }
141         
142         // Remove prohibited attributes from the set
143
attrGrp.removeProhibitedAttrs();
144         
145         // check for restricted redefine:
146
XSAttributeGroupDecl redefinedAttrGrp = (XSAttributeGroupDecl)fSchemaHandler.getGrpOrAttrGrpRedefinedByRestriction(
147                 XSDHandler.ATTRIBUTEGROUP_TYPE,
148                 new QName(XMLSymbols.EMPTY_STRING, nameAttr, nameAttr, schemaDoc.fTargetNamespace),
149                 schemaDoc, elmNode);
150         if(redefinedAttrGrp != null) {
151             Object JavaDoc[] errArgs = attrGrp.validRestrictionOf(nameAttr, redefinedAttrGrp);
152             if (errArgs != null) {
153                 reportSchemaError((String JavaDoc)errArgs[errArgs.length-1], errArgs, child);
154                 reportSchemaError("src-redefine.7.2.2", new Object JavaDoc [] {nameAttr, errArgs[errArgs.length-1]}, child);
155             }
156         }
157         
158         attrGrp.fAnnotation = annotation;
159         
160         // make an entry in global declarations.
161
grammar.addGlobalAttributeGroupDecl(attrGrp);
162         
163         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
164         return attrGrp;
165         
166     } // traverseGlobal
167

168 } // XSDAttributeGroupTraverser
169
Popular Tags