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  */
  61 class XSDWildcardTraverser extends XSDAbstractTraverser {
  62 
  63     /**
  64      * constructor
  65      *
  66      * @param  handler
  67      * @param  errorReporter
  68      * @param  gAttrCheck
  69      */
  70     XSDWildcardTraverser (XSDHandler handler,
  71             XSAttributeChecker gAttrCheck) {
  72         super(handler, gAttrCheck);
  73     }
  74 
  75 
  76     /**
  77      * Traverse <any>
  78      *
  79      * @param  elmNode
  80      * @param  schemaDoc
  81      * @param  grammar
  82      * @return the wildcard node index
  83      */
  84     XSParticleDecl traverseAny(Element elmNode,
  85             XSDocumentInfo schemaDoc,
  86             SchemaGrammar grammar) {
  87 
  88         // General Attribute Checking for elmNode
  89         Object[] attrValues = fAttrChecker.checkAttributes(elmNode, false, schemaDoc);
  90         XSWildcardDecl wildcard = traverseWildcardDecl(elmNode, attrValues, schemaDoc, grammar);
  91 
  92         // for <any>, need to create a new particle to reflect the min/max values
  93         XSParticleDecl particle = null;
  94         if (wildcard != null) {
  95             int min = ((XInt)attrValues[XSAttributeChecker.ATTIDX_MINOCCURS]).intValue();
  96             int max = ((XInt)attrValues[XSAttributeChecker.ATTIDX_MAXOCCURS]).intValue();
  97             if (max != 0) {
  98                 if (fSchemaHandler.fDeclPool !=null) {
  99                     particle = fSchemaHandler.fDeclPool.getParticleDecl();
 100                 } else {
 101                     particle = new XSParticleDecl();
 102                 }
 103                 particle.fType = XSParticleDecl.PARTICLE_WILDCARD;
 104                 particle.fValue = wildcard;
 105                 particle.fMinOccurs = min;
 106                 particle.fMaxOccurs = max;
 107                 particle.fAnnotations = wildcard.fAnnotations;
 108             }
 109         }
 110 
 111         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
 112 
 113         return particle;
 114     }
 115 
 116 
 117     /**
 118      * Traverse &lt;anyAttribute&gt;
 119      *
 120      * @param  elmNode
 121      * @param  schemaDoc
 122      * @param  grammar
 123      * @return the wildcard node index
 124      */
 125     XSWildcardDecl traverseAnyAttribute(Element elmNode,
 126             XSDocumentInfo schemaDoc,
 127             SchemaGrammar grammar) {
 128 
 129         // General Attribute Checking for elmNode
 130         Object[] attrValues = fAttrChecker.checkAttributes(elmNode, false, schemaDoc);
 131         XSWildcardDecl wildcard = traverseWildcardDecl(elmNode, attrValues, schemaDoc, grammar);
 132         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
 133 
 134         return wildcard;
 135     }
 136 
 137 
 138     /**
 139      *
 140      * @param  elmNode
 141      * @param  attrValues
 142      * @param  schemaDoc
 143      * @param  grammar
 144      * @return the wildcard node index
 145      */
 146     XSWildcardDecl traverseWildcardDecl(Element elmNode,
 147             Object[] attrValues,
 148             XSDocumentInfo schemaDoc,
 149             SchemaGrammar grammar) {
 150 
 151         //get all attributes
 152         XSWildcardDecl wildcard = new XSWildcardDecl();
 153         // namespace type
 154         XInt namespaceTypeAttr = (XInt) attrValues[XSAttributeChecker.ATTIDX_NAMESPACE];
 155         wildcard.fType = namespaceTypeAttr.shortValue();
 156         // namespace list
 157         wildcard.fNamespaceList = (String[])attrValues[XSAttributeChecker.ATTIDX_NAMESPACE_LIST];
 158         // process contents
 159         XInt processContentsAttr = (XInt) attrValues[XSAttributeChecker.ATTIDX_PROCESSCONTENTS];
 160         wildcard.fProcessContents = processContentsAttr.shortValue();
 161 
 162         //check content
 163         Element child = DOMUtil.getFirstChildElement(elmNode);
 164         XSAnnotationImpl annotation = null;
 165         if (child != null)
 166         {
 167             if (DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
 168                 annotation = traverseAnnotationDecl(child, attrValues, false, schemaDoc);
 169                 child = DOMUtil.getNextSiblingElement(child);
 170             }
 171             else {
 172                 String text = DOMUtil.getSyntheticAnnotation(elmNode);
 173                 if (text != null) {
 174                     annotation = traverseSyntheticAnnotation(elmNode, text, attrValues, false, schemaDoc);
 175                 }
 176             }
 177 
 178             if (child != null) {
 179                 reportSchemaError("s4s-elt-must-match.1", new Object[]{"wildcard", "(annotation?)", DOMUtil.getLocalName(child)}, elmNode);
 180             }
 181         }
 182         else {
 183             String text = DOMUtil.getSyntheticAnnotation(elmNode);
 184             if (text != null) {
 185                 annotation = traverseSyntheticAnnotation(elmNode, text, attrValues, false, schemaDoc);
 186             }
 187         }
 188         XSObjectList annotations;
 189         if (annotation != null) {
 190             annotations = new XSObjectListImpl();
 191             ((XSObjectListImpl) annotations).addXSObject(annotation);
 192         } else {
 193             annotations = XSObjectListImpl.EMPTY_LIST;
 194         }
 195         wildcard.fAnnotations = annotations;
 196 
 197         return wildcard;
 198 
 199     } // traverseWildcardDecl
 200 
 201 } // XSDWildcardTraverser