/*
 * 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.util.DOMUtil;
import  org.w3c.dom.Element;
import  java.util.Stack;
import  java.util.Hashtable;

/**
 * The model group schema component traverser.
 *
 * <group
 *   name = NCName>
 *   Content: (annotation?, (all | choice | sequence))
 * </group>
 * @version $Id: XSDGroupTraverser.java,v 1.5 2001/09/07 16:46:14 sandygao Exp $
 * @author Rahul Srivastava, Sun Microsystems Inc.
 */
class  XSDGroupTraverser extends XSDAbstractParticleTraverser{

    private Stack fCurrentGroupNameStack;
    private Hashtable fGroupNameRegistry;

    XSDGroupTraverser (XSDHandler handler,
                       XMLErrorReporter errorReporter,
                       XSAttributeChecker gAttrCheck) {

        super(handler, errorReporter, gAttrCheck);
    }

    int traverseLocal(Element elmNode,
                      XSDocumentInfo schemaDoc,
                      SchemaGrammar grammar)
    {
    
        // General Attribute Checking for elmNode declared locally
        Object[] attrValues = fAttrChecker.checkAttributes(elmNode, false, schemaDoc.fNamespaceSupport);
        int elemIdx = traverseGroupDecl(elmNode, attrValues, schemaDoc, grammar, true);
        fAttrChecker.returnAttrArray(attrValues, schemaDoc.fNamespaceSupport);

        return elemIdx;
    }

    int traverseGlobal(Element elmNode,
                 XSDocumentInfo schemaDoc,
                 SchemaGrammar grammar)
    {
                 
        // General Attribute Checking for elmNode declared globally
        Object[] attrValues = fAttrChecker.checkAttributes(elmNode, true, schemaDoc.fNamespaceSupport);
        int elemIdx = traverseGroupDecl(elmNode, attrValues, schemaDoc, grammar, true);
        fAttrChecker.returnAttrArray(attrValues, schemaDoc.fNamespaceSupport);

        return elemIdx;
    }
    
    /**
     * Traverse the group element.
     *
     * @param  elmNode
     * @param  attrValues
     * @param  schemaDoc
     * @param  grammar
     * @param  isGlobal
     * @return the element declaration index
     */
    int traverseGroupDecl(Element elmNode,
                             Object[] attrValues,
                             XSDocumentInfo schemaDoc,
                             SchemaGrammar grammar,
                             boolean isGlobal)
    {

        String  l_strNameAttr	= (String)  attrValues[XSAttributeChecker.ATTIDX_NAME];
        String  l_strRefAttr	= (String)  attrValues[XSAttributeChecker.ATTIDX_REF];
        Integer l_nMinAttr	= (Integer) attrValues[XSAttributeChecker.ATTIDX_MINOCCURS];
        Integer l_nMaxAttr	= (Integer) attrValues[XSAttributeChecker.ATTIDX_MAXOCCURS];
        
        int l_nGroupIndex = -1;

        // get targetNamespace
        String l_strNamespace = XSDHandler.EMPTY_STRING;
        if (isGlobal)
        {
        	l_strNamespace = schemaDoc.fTargetNamespace;
        }
        else if (schemaDoc.fAreLocalElementsQualified)
        {
        	l_strNamespace = schemaDoc.fTargetNamespace;
        }

        // check for the constraints on the group element.
        // REVISIT: when parent of group is <redifine>
        // REVISIT: check for circular references
        if (isGlobal)
        {
        	// must have a name
        	if (l_strNameAttr.length() == 0)
        	{
        		reportGenericSchemaError("Global group declaration must have a name.");
        	}
        	
        	// must have at least one child
        	Element l_elmChild = DOMUtil.getFirstChildElement(elmNode);
        	if (l_elmChild == null)
        	{
        		reportGenericSchemaError("Global group declaration must have a child.");
        	}
        	else
        	{
        		//REVISIT: add group information to SchemaGrammar - fGlobalGroupDecls
        		// l_nGroupIndex = grammar.addGroupDecl(groupInfo, isGlobal); //No such method in SchemaGrammar
        		//NOTE: The type of groupInfo will decide what information to gather here and populate that object
        		
    			if (l_elmChild.getLocalName().equals(SchemaSymbols.ELT_ANNOTATION))
    			{
    				traverseAnnotationDecl(l_elmChild, attrValues, isGlobal, schemaDoc);
    				l_elmChild = DOMUtil.getNextSiblingElement(l_elmChild);
    			}

    			if (l_elmChild == null)
    			{
    				reportGenericSchemaError("Global group element must have a child <all>, <choice> or <sequence>.");
    			}
    			else if (l_elmChild.getLocalName().equals(SchemaSymbols.ELT_ALL))
    			{
    				traverseAll(l_elmChild, schemaDoc, grammar);
    			}
    			else if (l_elmChild.getLocalName().equals(SchemaSymbols.ELT_CHOICE))
    			{
    				traverseChoice(l_elmChild, schemaDoc, grammar);
    			}
    			else if (l_elmChild.getLocalName().equals(SchemaSymbols.ELT_SEQUENCE))
    			{
    				traverseSequence(l_elmChild, schemaDoc, grammar);
    			}
    			else
    			{
    				reportGenericSchemaError("Invalid child for the group element.");
    			}
    		} //if
    	}
        else
        // local declaration. Corresponds to a Particle component.
        {
        	// ref should be here.
        	if (l_strRefAttr.length() == 0)
        	{
        		reportGenericSchemaError("Local group declaration should have ref.");
        	}
        	
        	//check whether what this refers to, exists or not!!
        	l_nGroupIndex = grammar.getGroupIndex(l_strRefAttr);
        	if (l_nGroupIndex == -1)
        	{
        		reportGenericSchemaError("Reference made to non-existent group element");
        	}
        	
        	//If minOccurs==maxOccurs==0, then its a non-existent element
        	if ((l_nMinAttr.intValue() == 0) && (l_nMaxAttr.intValue() == 0))
        	{
        		reportGenericSchemaError("Non-existing element, making reference to group");
        	}
        	
        	// Now, if its referencing, what's the fun of having childs.
        	if (DOMUtil.getFirstChildElement(elmNode) != null)
        	{
        		reportGenericSchemaError("Local group declaration cannot have child.");
        	}
        	
        } // if
        
        return l_nGroupIndex;
        
    } // traverseGroupDecl()

}
