//
// XMLQualifier.java
//
// The contents of this file are subject to the SNIA Public License Version 1.0
// (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
//	http://www.snia.org/resources/openSource.html
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the 
// License for the specific language governing rights and limitations
// under the License.
//
// The Original Code is XMLQualifier.java
// 
// The Initial Developer of the Original Code is David Simons.
//
// Contributor(s):
//
// This source uses the Solaris WBEM APIs version 2.2. 
// Synchronization will be maintained with the Solaris WBEM APIS,
// which are being standardized through the Java Community Process.
//
//
// $Id: XMLQualifier.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
// $Log: XMLQualifier.java,v $
// Revision 1.1  2003/07/17 18:32:55  kumpf
// HP-BAPU: PEP-64 CIM Client and CIM Listener Java Interface
//
// Revision 1.1.1.1  2001/10/31 18:17:57  karl
// import of SNIA 0_7
//
// Revision 1.5  2000/08/16 06:00:00  shawn
// merge P_TOMCAT31 branch
//
// Revision 1.4.8.1  2000/08/10 00:03:36  shawn
// head merge
//
// Revision 1.4  2000/05/11 10:11:25  davidsi
// - Fixed tabs.
//
// Revision 1.3  2000/05/06 17:20:04  davidsi
// - Brought in line with SUN WBEM APIs 2.2
//
// Revision 1.2  2000/02/13 15:10:07  davidsi
// - Added text to cover re-use of Solaris WBEM APIs.
//
// Revision 1.1.1.1  2000/02/06 16:41:02  davidsi
// Initial version.
//
//
//
package org.snia.wbemcmd.xml;

import java.util.Enumeration;
import java.util.Vector;
import java.net.MalformedURLException;
import org.snia.wbem.cim.CIMException;
import org.snia.wbem.cim.CIMObjectPath;
import org.snia.wbem.cim.CIMDataType;
import org.snia.wbem.cim.CIMQualifierType;
import org.snia.wbem.cim.CIMFlavor;
import org.snia.wbem.cim.CIMScope;


public abstract class XMLQualifier extends XMLClass {
    
    static String XMLP_QUALIFIERNAME="QualifierName";
    static String XMLP_QUALIFIERDECL="QualifierDeclaration";
    static String paramISARRAY="ISARRAY";
    static String paramQualifierFlavor="QualifierFlavor";
    static String paramArraySize="ArraySize";
    
    protected static XMLNode processQualifierDecl(XMLNode result,CIMQualifierType cimQualifier) 
		throws CIMException {
	String superClassName;
	String inClassName;
	XMLNode next;

	//
	// Assumed to already be on a <QUALIFIER.DECLARATION> element
	//
	
	//
	// Get parameters
	//
	String qualName=result.getAttribute(paramName);
	if (qualName==null)
	    throw new CIMException(CIMException.CIM_ERR_INVALID_PARAMETER,
		"No qualifier name");

	cimQualifier.setName(qualName);
	
	//
	// Process qualifier type information
	//
	String qualType=result.getAttribute(paramTypeAssign);
	if (qualType==null)
	    throw new CIMException(CIMException.CIM_ERR_INVALID_PARAMETER,
		"No type for qualifier");
	    
	CIMDataType dt=CIMDataType.getDataType(qualType);
	
	String qualISARRAY=result.getAttribute(paramISARRAY);
	if (qualISARRAY!=null && qualISARRAY.equalsIgnoreCase("true")) {
	    String qualArraySize=result.getAttribute(paramArraySize);
	    
	    
	    if (qualArraySize!=null)
		dt=new CIMDataType(new Integer(qualArraySize).intValue(),
				   dt.findArrayType(dt.getType()));
	    else
		dt=new CIMDataType(dt.findArrayType(dt.getType()));
	}
	cimQualifier.setType(dt);
		
	//
	// Process flavors
	//  
	processFlavor(result,cimQualifier,paramOverridable,true,CIMFlavor.ENABLEOVERRIDE,CIMFlavor.DISABLEOVERRIDE );
	processFlavor(result,cimQualifier,paramToSubClass,true,CIMFlavor.TOSUBCLASS,CIMFlavor.RESTRICTED );
	processFlavor(result,cimQualifier,paramTranslatable,false,CIMFlavor.TRANSLATE,-1);
    
	XMLNode scope=result.getChild();
	if (scope==null)
	    throw new CIMException(CIMException.CIM_ERR_INVALID_PARAMETER,
		"Could not find child element for scope");
	
	XMLNode scopeNode=scope.findElement(XMLNode.XML_ELEMENT_SCOPE);

	if (scopeNode==null)
	    throw new CIMException(CIMException.CIM_ERR_INVALID_PARAMETER,
		"Could not find scope");

	processScope(scope,cimQualifier,"CLASS",CIMScope.CLASS);
	processScope(scope,cimQualifier,"INSTANCE",CIMScope.INSTANCE);
	processScope(scope,cimQualifier,"ASSOCIATION",CIMScope.ASSOCIATION);
	processScope(scope,cimQualifier,"REFERENCE",CIMScope.REFERENCE);
	processScope(scope,cimQualifier,"PROPERTY",CIMScope.PROPERTY);
	processScope(scope,cimQualifier,"METHOD",CIMScope.METHOD);
	processScope(scope,cimQualifier,"PARAMETER",CIMScope.PARAMETER);
	processScope(scope,cimQualifier,"INDICATION",CIMScope.INDICATION);
	
	XMLNode valueNode=scope.findElement(XMLNode.XML_ELEMENT_VALUE);
	if (scopeNode==null) {
	    valueNode=scope.findElement(XMLNode.XML_ELEMENT_VALUE_ARRAY);
	    if (scopeNode==null) 
		throw new CIMException(CIMException.CIM_ERR_INVALID_PARAMETER,
			"Could not find value or array of values");
	}
	return(result.getNext());
    }
    
    private static void processFlavor(XMLNode result,
				      CIMQualifierType cqt,
				      String attrName,
				      boolean defaultValue,
				      int trueFlavor,
				      int falseFlavor) 
	throws CIMException {
	boolean flag;
	
	String qualFlavor=result.getAttribute(attrName);
	if (qualFlavor==null)
	    flag=defaultValue;
	else if (qualFlavor.equalsIgnoreCase("true"))
	    flag=true;
	else  if (qualFlavor.equalsIgnoreCase("false"))
	    flag=false;
	else
	    throw new CIMException(CIMException.CIM_ERR_FAILED);
	
	if (flag)
	    cqt.addFlavor(CIMFlavor.getFlavor(trueFlavor));
	else {
	    if (falseFlavor!=-1)
		cqt.addFlavor(CIMFlavor.getFlavor(trueFlavor));
	}
	    
    }
    private static void processScope(XMLNode result,
				     CIMQualifierType cqt,
				     String attrName,
				     int scopeValue)
			throws CIMException {
	String scope=result.getAttribute(attrName);
	if (scope==null)
	    return;
	    
	if (scope.equalsIgnoreCase("true"))
	    cqt.addScope(CIMScope.getScope(scopeValue));
	else if (!scope.equalsIgnoreCase("false"))
	    throw new CIMException(CIMException.CIM_ERR_FAILED);	
    }
	
	
    protected static String getQualifierName(XMLNode node) throws CIMException {
	    String qualifierName;

	//
	// It is assumed we are on a IPARAMVALUE node
	//
	if (node.getToken()!=XMLNode.XML_ELEMENT_IPARAMVALUE)
	    throw new CIMException(CIMException.CIM_ERR_INVALID_PARAMETER,
				    "Expected IPARAMVALUE to lead into QualifierName");
	
	String propertyName=node.getAttribute(paramName);
	if (propertyName==null)
	    throw new CIMException(CIMException.CIM_ERR_INVALID_PARAMETER,
		"Cannot find property Name");

	if (!propertyName.equalsIgnoreCase(XMLP_QUALIFIERNAME))
	    throw new CIMException(CIMException.CIM_ERR_INVALID_PARAMETER,
		"Cannot find qualifier name");
	
	node=node.getChild();
	node=node.findElement(XMLNode.XML_ELEMENT_VALUE);
	if (node==null) {
	    throw new CIMException(CIMException.CIM_ERR_INVALID_PARAMETER,
		"Cannot find value for property name "+propertyName);
	}
	return(node.getUnescapeText());
    }
}
