View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2001-2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package com.sun.org.apache.xerces.internal.impl.xs.traversers;
22  
23  import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar;
24  import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols;
25  import com.sun.org.apache.xerces.internal.impl.xs.XSAnnotationImpl;
26  import com.sun.org.apache.xerces.internal.impl.xs.XSParticleDecl;
27  import com.sun.org.apache.xerces.internal.impl.xs.XSWildcardDecl;
28  import com.sun.org.apache.xerces.internal.impl.xs.util.XInt;
29  import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;
30  import com.sun.org.apache.xerces.internal.util.DOMUtil;
31  import com.sun.org.apache.xerces.internal.xs.XSObjectList;
32  import org.w3c.dom.Element;
33  
34  /**
35   * The wildcard schema component traverser.
36   *
37   * <any
38   *   id = ID
39   *   maxOccurs = (nonNegativeInteger | unbounded)  : 1
40   *   minOccurs = nonNegativeInteger : 1
41   *   namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) )  : ##any
42   *   processContents = (lax | skip | strict) : strict
43   *   {any attributes with non-schema namespace . . .}>
44   *   Content: (annotation?)
45   * </any>
46   *
47   * <anyAttribute
48   *   id = ID
49   *   namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) )  : ##any
50   *   processContents = (lax | skip | strict) : strict
51   *   {any attributes with non-schema namespace . . .}>
52   *   Content: (annotation?)
53   * </anyAttribute>
54   *
55   * @xerces.internal
56   *
57   * @author Rahul Srivastava, Sun Microsystems Inc.
58   * @author Sandy Gao, IBM
59   *
60   * @version $Id: XSDWildcardTraverser.java,v 1.7 2010-11-01 04:40:02 joehw Exp $
61   */
62  class XSDWildcardTraverser extends XSDAbstractTraverser {
63  
64      /**
65       * constructor
66       *
67       * @param  handler
68       * @param  errorReporter
69       * @param  gAttrCheck
70       */
71      XSDWildcardTraverser (XSDHandler handler,
72              XSAttributeChecker gAttrCheck) {
73          super(handler, gAttrCheck);
74      }
75  
76  
77      /**
78       * Traverse <any>
79       *
80       * @param  elmNode
81       * @param  schemaDoc
82       * @param  grammar
83       * @return the wildcard node index
84       */
85      XSParticleDecl traverseAny(Element elmNode,
86              XSDocumentInfo schemaDoc,
87              SchemaGrammar grammar) {
88  
89          // General Attribute Checking for elmNode
90          Object[] attrValues = fAttrChecker.checkAttributes(elmNode, false, schemaDoc);
91          XSWildcardDecl wildcard = traverseWildcardDecl(elmNode, attrValues, schemaDoc, grammar);
92  
93          // for <any>, need to create a new particle to reflect the min/max values
94          XSParticleDecl particle = null;
95          if (wildcard != null) {
96              int min = ((XInt)attrValues[XSAttributeChecker.ATTIDX_MINOCCURS]).intValue();
97              int max = ((XInt)attrValues[XSAttributeChecker.ATTIDX_MAXOCCURS]).intValue();
98              if (max != 0) {
99                  if (fSchemaHandler.fDeclPool !=null) {
100                     particle = fSchemaHandler.fDeclPool.getParticleDecl();
101                 } else {
102                     particle = new XSParticleDecl();
103                 }
104                 particle.fType = XSParticleDecl.PARTICLE_WILDCARD;
105                 particle.fValue = wildcard;
106                 particle.fMinOccurs = min;
107                 particle.fMaxOccurs = max;
108                 particle.fAnnotations = wildcard.fAnnotations;
109             }
110         }
111 
112         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
113 
114         return particle;
115     }
116 
117 
118     /**
119      * Traverse &lt;anyAttribute&gt;
120      *
121      * @param  elmNode
122      * @param  schemaDoc
123      * @param  grammar
124      * @return the wildcard node index
125      */
126     XSWildcardDecl traverseAnyAttribute(Element elmNode,
127             XSDocumentInfo schemaDoc,
128             SchemaGrammar grammar) {
129 
130         // General Attribute Checking for elmNode
131         Object[] attrValues = fAttrChecker.checkAttributes(elmNode, false, schemaDoc);
132         XSWildcardDecl wildcard = traverseWildcardDecl(elmNode, attrValues, schemaDoc, grammar);
133         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
134 
135         return wildcard;
136     }
137 
138 
139     /**
140      *
141      * @param  elmNode
142      * @param  attrValues
143      * @param  schemaDoc
144      * @param  grammar
145      * @return the wildcard node index
146      */
147     XSWildcardDecl traverseWildcardDecl(Element elmNode,
148             Object[] attrValues,
149             XSDocumentInfo schemaDoc,
150             SchemaGrammar grammar) {
151 
152         //get all attributes
153         XSWildcardDecl wildcard = new XSWildcardDecl();
154         // namespace type
155         XInt namespaceTypeAttr = (XInt) attrValues[XSAttributeChecker.ATTIDX_NAMESPACE];
156         wildcard.fType = namespaceTypeAttr.shortValue();
157         // namespace list
158         wildcard.fNamespaceList = (String[])attrValues[XSAttributeChecker.ATTIDX_NAMESPACE_LIST];
159         // process contents
160         XInt processContentsAttr = (XInt) attrValues[XSAttributeChecker.ATTIDX_PROCESSCONTENTS];
161         wildcard.fProcessContents = processContentsAttr.shortValue();
162 
163         //check content
164         Element child = DOMUtil.getFirstChildElement(elmNode);
165         XSAnnotationImpl annotation = null;
166         if (child != null)
167         {
168             if (DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
169                 annotation = traverseAnnotationDecl(child, attrValues, false, schemaDoc);
170                 child = DOMUtil.getNextSiblingElement(child);
171             }
172             else {
173                 String text = DOMUtil.getSyntheticAnnotation(elmNode);
174                 if (text != null) {
175                     annotation = traverseSyntheticAnnotation(elmNode, text, attrValues, false, schemaDoc);
176                 }
177             }
178 
179             if (child != null) {
180                 reportSchemaError("s4s-elt-must-match.1", new Object[]{"wildcard", "(annotation?)", DOMUtil.getLocalName(child)}, elmNode);
181             }
182         }
183         else {
184             String text = DOMUtil.getSyntheticAnnotation(elmNode);
185             if (text != null) {
186                 annotation = traverseSyntheticAnnotation(elmNode, text, attrValues, false, schemaDoc);
187             }
188         }
189         XSObjectList annotations;
190         if (annotation != null) {
191             annotations = new XSObjectListImpl();
192             ((XSObjectListImpl) annotations).addXSObject(annotation);
193         } else {
194             annotations = XSObjectListImpl.EMPTY_LIST;
195         }
196         wildcard.fAnnotations = annotations;
197 
198         return wildcard;
199 
200     } // traverseWildcardDecl
201 
202 } // XSDWildcardTraverser