/*
 * 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 java.util.Vector;

/**
 * To store and validate information about substitutionGroup
 *
 * @author Sandy Gao, IBM
 * @author Rahul Srivastava, Sun Microsystems Inc.
 *
 * @version $Id: SubstitutionGroupHandler.java,v 1.3 2001/09/19 16:25:35 sandygao Exp $
 */
class SubstitutionGroupHandler {

    // grammar resolver
    // XSGrammarResolver fGrammarResolver;

    // Substitution group registry
    Vector fSubstitutionGroup;

    /**
     * Default constructor
     */
    SubstitutionGroupHandler() {
        fSubstitutionGroup = new Vector(10,5);
    }

    /*
    SubstitutionGroupHandler(XSGrammarResolver grammarResolver) {
        fGrammarResolver = grammarResolver;
        fSubstitutionGroup = new Vector(10,5);
    }
    */

    /**
     * clear the internal registry of substitutionGroup information
     */
    void reset() {
        fSubstitutionGroup.clear();
    }

    /**
     * add one substitution group pair
     */
    void addSubstitutionGroup(XSElementDecl element) {
        fSubstitutionGroup.addElement(element);
    }

    /**
     * get all elements that can substitute the given element,
     * according to the spec, we shouldn't consider the {block} constraints.
     */
    XSElementDecl[] getSubstitutionGroup(String elementUri, String elementName) {
        Vector tempSubstitutionGroups = new Vector(5,5);
        XSElementDecl element = null;
        
        for (int i=0, size=fSubstitutionGroup.size(); i<size; i++) {
        	element = (XSElementDecl)fSubstitutionGroup.elementAt(i);
        	if (elementUri.equals(element.fTargetNamespace) && 
        	    elementName.equals(element.fSubGroup.fName)) {
        		tempSubstitutionGroups.addElement(element);
        	}
        }
        
        // copy elements from vector to array
        XSElementDecl[] elements;
        Object[] objects = tempSubstitutionGroups.toArray();
        int size = objects.length;
        if (size > 0) {
        	elements = new XSElementDecl[size];
		System.arraycopy(objects,0,elements,0,size);
        }
        
        return elements;
    }

    /**
     * check whether an element with the given type can have the other element
     * as its {substitution group affiliation}.
     */
    static boolean checkSubstitutionGroupOK(XSElementDecl element, XSDocumentInfo docInfo) {

        XSElementDecl headElement = element.fSubGroup;
        
	// if block attr. of head element is #all or substitution then, no substitution allowed.
	if (headElement.fBlock != SchemaSymbols.EMPTY_SET)
		if ((headElement.fBlock & SchemaSymbols.SUBSTITUTION) != 0)
			return false;
	else {
	//REVISIT: maybe something is to changed here, depending on change in XSDocumentInfo
	// if blockDefault attr. of schema is #all or substitution then, no substitution allowed.
	//	if ((docInfo.fBlockDefault & SchemaSymbols.SUBSTITUTION) != 0)
	//		return false;
	}
	
        // type should be same or derived from head element's type.
        String headTypeName = headElement.fType.getXSTypeName();
        XSTypeDecl subType = element.fType;
        String subTypeName = subType.getXSTypeName();
        
        while (!headTypeName.equals(subTypeName)) {
        	//REVISIT: getBaseType method is not yet implemented in XSTypeDecl
        	subType = subType.getBaseType();
        	if (subType == null)
        		break;
        	else
        		subTypeName = subType.getXSTypeName();
        }
        
        if (!headTypeName.equals(subTypeName))
        	return false;
        
        // substitution allowed.
        return true;
    }

} // class SubstitutionGroupHandler
