/*
 * The Apache Software License, Version 1.1
 *
 *
 * Copyright (c) 2001 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Xerces" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation and was
 * originally based on software copyright (c) 2001, International
 * Business Machines, Inc., http://www.apache.org.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */
package org.apache.xerces.impl.v2;


import org.apache.xerces.impl.XMLErrorReporter;
import org.apache.xerces.xni.QName;
import org.apache.xerces.util.XInt;
import org.w3c.dom.Element;
import java.util.Hashtable;
import java.util.Stack;


/**
 * The wildcard schema component traverser.
 *
 * <any
 *   id = ID
 *   maxOccurs = (nonNegativeInteger | unbounded)  : 1
 *   minOccurs = nonNegativeInteger : 1
 *   namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) )  : ##any
 *   processContents = (lax | skip | strict) : strict
 *   {any attributes with non-schema namespace . . .}>
 *   Content: (annotation?)
 * </any>
 *
 *
 * <anyAttribute
 *   id = ID
 *   namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) )  : ##any
 *   processContents = (lax | skip | strict) : strict
 *   {any attributes with non-schema namespace . . .}>
 *   Content: (annotation?)
 * </anyAttribute>
 *
 * @author Rahul Srivastava, Sun Microsystems Inc.
 * @version $Id: XSDWildcardTraverser.java,v 1.2 2001/08/24 23:00:36 sandygao Exp $
 */
class  XSDWildcardTraverser extends XSDAbstractTraverser {


    /**
     * constructor
     *
     * @param  handler
     * @param  errorReporter
     * @param  gAttrCheck
     */
    XSDWildcardTraverser (XSDHandler handler,
                          XMLErrorReporter errorReporter,
                          XSAttributeChecker gAttrCheck) {
        super(handler, errorReporter, gAttrCheck);
    }


    /**
     * Traverse <any>
     *
     * @param  elmNode
     * @param  schemaDoc
     * @param  grammar
     * @return the wildcard node index
     */
    int traverseAny(Element elmNode,
                    XSDocumentInfo schemaDoc,
                    SchemaGrammar grammar) {

        // General Attribute Checking for elmNode
        Object[] attrValues = fAttrChecker.checkAttributes(elmNode, false, schemaDoc.fNamespaceSupport);
        int elemIdx = traverseWildcardDecl(elmNode, attrValues, schemaDoc, grammar, false);
        fAttrChecker.returnAttrArray(attrValues, schemaDoc.fNamespaceSupport);

        return elemIdx;
    }
    

    /**
     * Traverse <anyAttribute>
     *
     * @param  elmNode
     * @param  schemaDoc
     * @param  grammar
     * @return the wildcard node index
     */
    int traverseAnyAttribute(Element elmNode,
                             XSDocumentInfo schemaDoc,
                             SchemaGrammar grammar) {

        // General Attribute Checking for elmNode
        Object[] attrValues = fAttrChecker.checkAttributes(elmNode, false, schemaDoc.fNamespaceSupport);
        int elemIdx = traverseWildcardDecl(elmNode, attrValues, schemaDoc, grammar, true);
        fAttrChecker.returnAttrArray(attrValues, schemaDoc.fNamespaceSupport);

        return elemIdx;
    }

    
    /**
     *
     * @param  elmNode
     * @param  attrValues
     * @param  schemaDoc
     * @param  grammar
     * @param  isAnyAttribute
     * @return the wildcard node index
     */
    int traverseWildcardDecl(Element elmNode,
    			Object[] attrValues,
    			XSDocumentInfo schemaDoc,
    			SchemaGrammar grammar,
    			boolean isAnyAttribute) {
    
    	int index = -1;
    	
    	//get all attributes
    	String  namespaceAttr       = (String) attrValues[XSAttributeChecker.ATTIDX_NAMESPACE];
    	String  processContentsAttr = (String) attrValues[XSAttributeChecker.ATTIDX_PROCESSCONTENTS];
        XInt    minOccursAttr       = (XInt)   attrValues[XSAttributeChecker.ATTIDX_MINOCCURS];
        XInt    maxOccursAttr       = (XInt)   attrValues[XSAttributeChecker.ATTIDX_MAXOCCURS];
        
        //REVISIT: XSWildcardDecl class, yet to be coded.
        XSWildcardDecl wildcard = new XSWildcard();
        
        wildcard.fProcessContents = XSWildcardDecl.WILDCARD_STRICT;
        if (processContentsAttr.length() > 0) {
    		if (processContentsAttr.equals("strict")) {
    			wildcard.fProcessContents = XSWildcardDecl.WILDCARD_STRICT;
    		}
    		else if (processContentsAttr.equals("lax")) {
    			wildcard.fProcessContents = XSWildcardDecl.WILDCARD_LAX;
    		}
    		else if (processContentsAttr.equals("skip")) {
    			wildcard.fProcessContents = XSWildcardDecl.WILDCARD_SKIP;
    		}
    		//no need to check anymore else, as for any other value, XSAttributeChecker 
    		//will throw an exception.
    	}
    	
    	wildcard.fType = XSWildcardDecl.WILDCARD_ANY;
    	if (namespaceAttr.length() > 0) {
    		if (namespaceAttr.equals("##any")) {
    			wildcard.fType = XSWildcardDecl.WILDCARD_ANY;
    		}
    		else if (namespaceAttr.equals("##other")) {
    			wildcard.fType = XSWildcardDecl.WILDCARD_OTHER;
    		}
    		else {
    			StringTokenizer tokens = newStringTokenizer(namespaceAttr);
    			fNamespaceList = new String[tokens.countTokens()];
    			int i = 0;
    			String token;
    			String tempNamespace;
    			while (tokens.hasMoreTokens()) {
    				token = tokens.nextToken();
    				if (token.equals("##local")) {
    					//REVISIT: 
    					tempNamespace = SchemaGrammar.I_EMPTY_DECL;
    				}
    				else if (token.equals("##targetNamespace")) {
    					tempNamespace = schemaDoc.fTargetNamespace;
    				}
    				else {
    					// we have found namespace URI here
    					tempNamespace = token;
    				}
    				
    				//check for duplicate namespaces in the list
    				for (int j=0; j<i; j++) {
    					if (token.equals(wildcard.fNamespaceList[j]))
    						continue;
    				}
    				wildcard.fNamespaceList[i] = new String(tempNamespace);
    				i++;
    			}
    		}
    	}
    	
    	//check content
    	Element child = DOMUtil.getFirstChildElement(elmNode);
    	if (child != null)
    	{
    		if (child.equals(SchemaSymbols.ELT_ANNOTATION)) {
    			traverseAnnotationDecl(child, attrValues, false, schemaDoc);
    			child = DOMUtil.getNextSiblingElement(child);
                }
                
                if (child != null) {
                	Object[] args = new Object [] { "wildcard", child.getLocalName()};
                	fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN,
                                                   "WildcardContentRestricted",
                                                   args,
                                                   XMLErrorReporter.SEVERITY_ERROR);
                }
         }
         
         //REVISIT: method to be added in SchemaGrammar
    	 index = grammar.addWildcardDecl(wildcard);
    	 
         // take care of minOccurs and maxOccurs which may occur only with <any>
         if (!isAnyAttribute) {
         	int minOccurs = minOccursAttr.intValue();
         	int maxOccurs = maxOccursAttr.intValue();
         	//REVISIT: method to be added in SchemaGrammar
         	grammar.setWildcardMinMax(index, minOccurs, maxOccurs);
         }
    	
    	 return index;
    	 
    } // traverseWildcardDecl

} // XSDWildcardTraverser
