1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 package com.sun.org.apache.xerces.internal.impl.xs.traversers;
23 
24 import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar;
25 import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols;
26 import com.sun.org.apache.xerces.internal.impl.xs.XSAnnotationImpl;
27 import com.sun.org.apache.xerces.internal.impl.xs.XSAttributeGroupDecl;
28 import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;
29 import com.sun.org.apache.xerces.internal.util.DOMUtil;
30 import com.sun.org.apache.xerces.internal.util.XMLSymbols;
31 import com.sun.org.apache.xerces.internal.xni.QName;
32 import com.sun.org.apache.xerces.internal.xs.XSObjectList;
33 import org.w3c.dom.Element;
34 
35 /**
36  * The attribute group definition schema component traverser.
37  *
38  * <attributeGroup
39  *   id = ID
40  *   name = NCName
41  *   ref = QName
42  *   {any attributes with non-schema namespace . . .}>
43  *   Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))
44  * </attributeGroup>
45  *
46  * @xerces.internal
47  *
48  * @author Rahul Srivastava, Sun Microsystems Inc.
49  * @author Sandy Gao, IBM
50  *
51  */
52 class XSDAttributeGroupTraverser extends XSDAbstractTraverser {
53 
XSDAttributeGroupTraverser(XSDHandler handler, XSAttributeChecker gAttrCheck)54     XSDAttributeGroupTraverser (XSDHandler handler,
55             XSAttributeChecker gAttrCheck) {
56 
57         super(handler, gAttrCheck);
58     }
59 
60 
traverseLocal(Element elmNode, XSDocumentInfo schemaDoc, SchemaGrammar grammar)61     XSAttributeGroupDecl traverseLocal(Element elmNode,
62             XSDocumentInfo schemaDoc,
63             SchemaGrammar grammar) {
64 
65         // General Attribute Checking for elmNode declared locally
66         Object[] attrValues = fAttrChecker.checkAttributes(elmNode, false, schemaDoc);
67 
68         // get attribute
69         QName   refAttr = (QName)   attrValues[XSAttributeChecker.ATTIDX_REF];
70 
71         XSAttributeGroupDecl attrGrp = null;
72 
73         // ref should be here.
74         if (refAttr == null) {
75             reportSchemaError("s4s-att-must-appear", new Object[]{"attributeGroup (local)", "ref"}, elmNode);
76             fAttrChecker.returnAttrArray(attrValues, schemaDoc);
77             return null;
78         }
79 
80         // get global decl
81         attrGrp = (XSAttributeGroupDecl)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.ATTRIBUTEGROUP_TYPE, refAttr, elmNode);
82 
83         // no children are allowed here except annotation, which is optional.
84         Element child = DOMUtil.getFirstChildElement(elmNode);
85         if (child != null) {
86             String childName = DOMUtil.getLocalName(child);
87             if (childName.equals(SchemaSymbols.ELT_ANNOTATION)) {
88                 traverseAnnotationDecl(child, attrValues, false, schemaDoc);
89                 child = DOMUtil.getNextSiblingElement(child);
90             } else {
91                 String text = DOMUtil.getSyntheticAnnotation(child);
92                 if (text != null) {
93                     traverseSyntheticAnnotation(child, text, attrValues, false, schemaDoc);
94                 }
95             }
96 
97             if (child != null) {
98                 Object[] args = new Object [] {refAttr.rawname, "(annotation?)", DOMUtil.getLocalName(child)};
99                 reportSchemaError("s4s-elt-must-match.1", args, child);
100             }
101         } // if
102 
103         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
104         return attrGrp;
105 
106     } // traverseLocal
107 
traverseGlobal(Element elmNode, XSDocumentInfo schemaDoc, SchemaGrammar grammar)108     XSAttributeGroupDecl traverseGlobal(Element elmNode,
109             XSDocumentInfo schemaDoc,
110             SchemaGrammar grammar) {
111 
112         XSAttributeGroupDecl attrGrp = new XSAttributeGroupDecl();
113 
114         // General Attribute Checking for elmNode declared globally
115         Object[] attrValues = fAttrChecker.checkAttributes(elmNode, true, schemaDoc);
116 
117         String  nameAttr   = (String) attrValues[XSAttributeChecker.ATTIDX_NAME];
118 
119         // global declaration must have a name
120         if (nameAttr == null) {
121             reportSchemaError("s4s-att-must-appear", new Object[]{"attributeGroup (global)", "name"}, elmNode);
122             nameAttr = NO_NAME;
123         }
124 
125         attrGrp.fName = nameAttr;
126         attrGrp.fTargetNamespace = schemaDoc.fTargetNamespace;
127 
128         // check the content
129         Element child = DOMUtil.getFirstChildElement(elmNode);
130         XSAnnotationImpl annotation = null;
131 
132         if (child!=null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
133             annotation = traverseAnnotationDecl(child, attrValues, false, schemaDoc);
134             child = DOMUtil.getNextSiblingElement(child);
135         }
136         else {
137             String text = DOMUtil.getSyntheticAnnotation(elmNode);
138             if (text != null) {
139                 annotation = traverseSyntheticAnnotation(elmNode, text, attrValues, false, schemaDoc);
140             }
141         }
142 
143         // Traverse the attribute and attribute group elements and fill in the
144         // attributeGroup structure
145 
146         Element nextNode = traverseAttrsAndAttrGrps(child, attrGrp, schemaDoc, grammar, null);
147         if (nextNode!=null) {
148             // An invalid element was found...
149             Object[] args = new Object [] {nameAttr, "(annotation?, ((attribute | attributeGroup)*, anyAttribute?))", DOMUtil.getLocalName(nextNode)};
150             reportSchemaError("s4s-elt-must-match.1", args, nextNode);
151         }
152 
153         if (nameAttr.equals(NO_NAME)) {
154             // if a global group doesn't have a name, then don't add it.
155             fAttrChecker.returnAttrArray(attrValues, schemaDoc);
156             return null;
157         }
158 
159         // Remove prohibited attributes from the set
160         attrGrp.removeProhibitedAttrs();
161 
162         // check for restricted redefine:
163         XSAttributeGroupDecl redefinedAttrGrp = (XSAttributeGroupDecl)fSchemaHandler.getGrpOrAttrGrpRedefinedByRestriction(
164                 XSDHandler.ATTRIBUTEGROUP_TYPE,
165                 new QName(XMLSymbols.EMPTY_STRING, nameAttr, nameAttr, schemaDoc.fTargetNamespace),
166                 schemaDoc, elmNode);
167         if(redefinedAttrGrp != null) {
168             Object[] errArgs = attrGrp.validRestrictionOf(nameAttr, redefinedAttrGrp);
169             if (errArgs != null) {
170                 reportSchemaError((String)errArgs[errArgs.length-1], errArgs, child);
171                 reportSchemaError("src-redefine.7.2.2", new Object [] {nameAttr, errArgs[errArgs.length-1]}, child);
172             }
173         }
174 
175         XSObjectList annotations;
176         if (annotation != null) {
177             annotations = new XSObjectListImpl();
178             ((XSObjectListImpl)annotations).addXSObject (annotation);
179         } else {
180             annotations = XSObjectListImpl.EMPTY_LIST;
181         }
182 
183         attrGrp.fAnnotations = annotations;
184 
185         // make an entry in global declarations.
186         if (grammar.getGlobalAttributeGroupDecl(attrGrp.fName) == null) {
187             grammar.addGlobalAttributeGroupDecl(attrGrp);
188         }
189 
190         // also add it to extended map
191         final String loc = fSchemaHandler.schemaDocument2SystemId(schemaDoc);
192         final XSAttributeGroupDecl attrGrp2 = grammar.getGlobalAttributeGroupDecl(attrGrp.fName, loc);
193         if (attrGrp2 == null) {
194             grammar.addGlobalAttributeGroupDecl(attrGrp, loc);
195         }
196 
197         // handle duplicates
198         if (fSchemaHandler.fTolerateDuplicates) {
199             if (attrGrp2 != null) {
200                 attrGrp = attrGrp2;
201             }
202             fSchemaHandler.addGlobalAttributeGroupDecl(attrGrp);
203         }
204 
205         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
206         return attrGrp;
207 
208     } // traverseGlobal
209 
210 } // XSDAttributeGroupTraverser
211