001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.xerces.impl.xs.traversers;
018:
019: import org.apache.xerces.impl.xs.SchemaGrammar;
020: import org.apache.xerces.impl.xs.SchemaSymbols;
021: import org.apache.xerces.impl.xs.XSAnnotationImpl;
022: import org.apache.xerces.impl.xs.XSAttributeGroupDecl;
023: import org.apache.xerces.impl.xs.util.XSObjectListImpl;
024: import org.apache.xerces.util.DOMUtil;
025: import org.apache.xerces.util.XMLSymbols;
026: import org.apache.xerces.xni.QName;
027: import org.apache.xerces.xs.XSObjectList;
028: import org.w3c.dom.Element;
029:
030: /**
031: * The attribute group definition schema component traverser.
032: *
033: * <attributeGroup
034: * id = ID
035: * name = NCName
036: * ref = QName
037: * {any attributes with non-schema namespace . . .}>
038: * Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))
039: * </attributeGroup>
040: *
041: * @xerces.internal
042: *
043: * @author Rahul Srivastava, Sun Microsystems Inc.
044: * @author Sandy Gao, IBM
045: *
046: * @version $Id: XSDAttributeGroupTraverser.java 449424 2006-09-24 16:22:30Z mrglavas $
047: */
048: class XSDAttributeGroupTraverser extends XSDAbstractTraverser {
049:
050: XSDAttributeGroupTraverser(XSDHandler handler,
051: XSAttributeChecker gAttrCheck) {
052:
053: super (handler, gAttrCheck);
054: }
055:
056: XSAttributeGroupDecl traverseLocal(Element elmNode,
057: XSDocumentInfo schemaDoc, SchemaGrammar grammar) {
058:
059: // General Attribute Checking for elmNode declared locally
060: Object[] attrValues = fAttrChecker.checkAttributes(elmNode,
061: false, schemaDoc);
062:
063: // get attribute
064: QName refAttr = (QName) attrValues[XSAttributeChecker.ATTIDX_REF];
065:
066: XSAttributeGroupDecl attrGrp = null;
067:
068: // ref should be here.
069: if (refAttr == null) {
070: reportSchemaError("s4s-att-must-appear", new Object[] {
071: "attributeGroup (local)", "ref" }, elmNode);
072: fAttrChecker.returnAttrArray(attrValues, schemaDoc);
073: return null;
074: }
075:
076: // get global decl
077: attrGrp = (XSAttributeGroupDecl) fSchemaHandler.getGlobalDecl(
078: schemaDoc, XSDHandler.ATTRIBUTEGROUP_TYPE, refAttr,
079: elmNode);
080:
081: // no children are allowed here except annotation, which is optional.
082: Element child = DOMUtil.getFirstChildElement(elmNode);
083: if (child != null) {
084: String childName = DOMUtil.getLocalName(child);
085: if (childName.equals(SchemaSymbols.ELT_ANNOTATION)) {
086: traverseAnnotationDecl(child, attrValues, false,
087: schemaDoc);
088: child = DOMUtil.getNextSiblingElement(child);
089: } else {
090: String text = DOMUtil.getSyntheticAnnotation(child);
091: if (text != null) {
092: traverseSyntheticAnnotation(child, text,
093: attrValues, false, schemaDoc);
094: }
095: }
096:
097: if (child != null) {
098: Object[] args = new Object[] { refAttr.rawname,
099: "(annotation?)", DOMUtil.getLocalName(child) };
100: reportSchemaError("s4s-elt-must-match.1", args, child);
101: }
102: } // if
103:
104: fAttrChecker.returnAttrArray(attrValues, schemaDoc);
105: return attrGrp;
106:
107: } // traverseLocal
108:
109: XSAttributeGroupDecl traverseGlobal(Element elmNode,
110: XSDocumentInfo schemaDoc, SchemaGrammar grammar) {
111:
112: XSAttributeGroupDecl attrGrp = new XSAttributeGroupDecl();
113:
114: // General Attribute Checking for elmNode declared globally
115: Object[] attrValues = fAttrChecker.checkAttributes(elmNode,
116: true, schemaDoc);
117:
118: String nameAttr = (String) attrValues[XSAttributeChecker.ATTIDX_NAME];
119:
120: // global declaration must have a name
121: if (nameAttr == null) {
122: reportSchemaError("s4s-att-must-appear", new Object[] {
123: "attributeGroup (global)", "name" }, elmNode);
124: nameAttr = "no name";
125: }
126:
127: attrGrp.fName = nameAttr;
128: attrGrp.fTargetNamespace = schemaDoc.fTargetNamespace;
129:
130: // check the content
131: Element child = DOMUtil.getFirstChildElement(elmNode);
132: XSAnnotationImpl annotation = null;
133:
134: if (child != null
135: && DOMUtil.getLocalName(child).equals(
136: SchemaSymbols.ELT_ANNOTATION)) {
137: annotation = traverseAnnotationDecl(child, attrValues,
138: false, schemaDoc);
139: child = DOMUtil.getNextSiblingElement(child);
140: } else {
141: String text = DOMUtil.getSyntheticAnnotation(elmNode);
142: if (text != null) {
143: annotation = traverseSyntheticAnnotation(elmNode, text,
144: attrValues, false, schemaDoc);
145: }
146: }
147:
148: // Traverse the attribute and attribute group elements and fill in the
149: // attributeGroup structure
150:
151: Element nextNode = traverseAttrsAndAttrGrps(child, attrGrp,
152: schemaDoc, grammar, null);
153: if (nextNode != null) {
154: // An invalid element was found...
155: Object[] args = new Object[] {
156: nameAttr,
157: "(annotation?, ((attribute | attributeGroup)*, anyAttribute?))",
158: DOMUtil.getLocalName(nextNode) };
159: reportSchemaError("s4s-elt-must-match.1", args, nextNode);
160: }
161:
162: // Remove prohibited attributes from the set
163: attrGrp.removeProhibitedAttrs();
164:
165: // check for restricted redefine:
166: XSAttributeGroupDecl redefinedAttrGrp = (XSAttributeGroupDecl) fSchemaHandler
167: .getGrpOrAttrGrpRedefinedByRestriction(
168: XSDHandler.ATTRIBUTEGROUP_TYPE, new QName(
169: XMLSymbols.EMPTY_STRING, nameAttr,
170: nameAttr, schemaDoc.fTargetNamespace),
171: schemaDoc, elmNode);
172: if (redefinedAttrGrp != null) {
173: Object[] errArgs = attrGrp.validRestrictionOf(nameAttr,
174: redefinedAttrGrp);
175: if (errArgs != null) {
176: reportSchemaError((String) errArgs[errArgs.length - 1],
177: errArgs, child);
178: reportSchemaError("src-redefine.7.2.2", new Object[] {
179: nameAttr, errArgs[errArgs.length - 1] }, child);
180: }
181: }
182:
183: XSObjectList annotations;
184: if (annotation != null) {
185: annotations = new XSObjectListImpl();
186: ((XSObjectListImpl) annotations).add(annotation);
187: } else {
188: annotations = XSObjectListImpl.EMPTY_LIST;
189: }
190:
191: attrGrp.fAnnotations = annotations;
192:
193: // make an entry in global declarations.
194: grammar.addGlobalAttributeGroupDecl(attrGrp);
195:
196: fAttrChecker.returnAttrArray(attrValues, schemaDoc);
197: return attrGrp;
198:
199: } // traverseGlobal
200:
201: } // XSDAttributeGroupTraverser
|