//
// XMLReferenceNames.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 XMLReferenceNames.java
// 
// The Initial Developer of the Original Code is David Simons.
//
// Contributor(s): Adrian Schuur
//
//
// 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: XMLReferenceNames.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
// $Log: XMLReferenceNames.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:58  karl
// import of SNIA 0_7
//
// Revision 1.12  2001/08/05 12:23:49  davidsi
// - From Klaus Olszowi - when there is nothing to return use an empty
//   enumeration not a null.
//
// Revision 1.11  2001/06/23 13:11:33  davidsi
// - Check for null keys before looking at size() (Jean Lamontagne)
//
// Revision 1.10  2001/03/19 17:40:07  davidsi
// - Ensure object path has a namespace set.
//
// Revision 1.9  2001/01/08 19:35:14  davidsi
// - Fixed using incorrect path when building enumeration.
//
// Revision 1.8  2000/11/06 19:17:32  davidsi
// - Support for other encodings.
//
// Revision 1.7  2000/08/16 06:00:00  shawn
// merge P_TOMCAT31 branch
//
// Revision 1.6.6.1  2000/08/10 00:03:36  shawn
// head merge
//
// Revision 1.6  2000/07/19 16:56:17  davidsi
// - Accept instance path elements in the response without an object path.
//
// Revision 1.5  2000/05/11 10:11:25  davidsi
// - Fixed tabs.
//
// Revision 1.4  2000/05/06 17:20:04  davidsi
// - Brought in line with SUN WBEM APIs 2.2
//
// Revision 1.3  2000/04/09 14:46:05  davidsi
// - Changed "Name" attribute to "NAME".
//
// 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.CIMNameSpace;
import org.snia.wbem.client.CIMOMHandle;
import org.snia.wbemcmd.utils.SimpleEnum;

public class XMLReferenceNames extends XMLClass {
    static String commandName="ReferenceNames";
    static Vector allowedAttrs;
    
    
    static {
	//
	// Set up valid parameter names
	//
	allowedAttrs=new Vector();
	allowedAttrs.addElement(new XMLAttribute(paramName,XMLP_RESULTROLE));
	allowedAttrs.addElement(new XMLAttribute(paramName,XMLP_ROLE));
    }
    
    
    public void execute(HTTPWriter pw,XMLNode node,CIMObjectPath path,CIMOMHandle hdl) 
	    throws CIMException,MalformedURLException {
	String className;
	boolean deepInheritance;
	Enumeration enum;
	
	String role=node.extractParameterValue(XMLP_ROLE,(String)null);
	//
	// Look for ObjectName
	//
	XMLNode tmpNode=node;
	for(;tmpNode!=null;tmpNode=node.getNext()) {
	    if (tmpNode.getToken()!=XMLNode.XML_ELEMENT_IPARAMVALUE)
		continue;
	    String attr=tmpNode.getAttribute(paramName);
	    if (attr==null || !attr.equalsIgnoreCase(XMLP_OBJECTNAME))
		continue;
	    tmpNode=tmpNode.getChild();
	    if (tmpNode==null)
		throw new CIMException(CIMException.CIM_ERR_FAILED,
				"Needed a child for <IPARAMVALUE> when handling AssociatorNames");
	    int token=tmpNode.getToken();
	    if (token==XMLNode.XML_ELEMENT_CLASSNAME)
		path=new CIMObjectPath(tmpNode.mustGetAttribute("NAME"),
				       path.getNameSpace());
	    else if (token==XMLNode.XML_ELEMENT_INSTANCENAME)
		getInstanceName(tmpNode, path);
	    break;  
	}
	String resultClass=node.extractParameterValueAttr(XMLP_RESULTCLASS,XMLNode.XML_ELEMENT_CLASSNAME,
						   paramName);
    
	enum=hdl.referenceNames(path,resultClass,role);
	//
	// If you change the below code, you may wish to make similar
	// changes to XMLAssociatorNames...
	//
	if (enum!=null) {
	    while (enum.hasMoreElements()) {
		CIMObjectPath cop=(CIMObjectPath)(enum.nextElement());
		CIMNameSpace ns=cop.getFullNameSpace();
/*
		if (ns.getHost()==null)
		    pw.println("<OBJECTPATH><LOCALINSTANCEPATH>"+
			ns.toXml(true)+
			cop.toXml()+
			"</LOCALINSTANCEPATH></OBJECTPATH>");
		else
		    pw.println("<OBJECTPATH><INSTANCEPATH>"+
			ns.toXml(false)+
			cop.toXml()+
			"</INSTANCEPATH></OBJECTPATH>");
*/
		 pw.println(cop.toXml());
	    }			
	}
	return;
    }
    

    
    //
    //
    //
    public static Enumeration getNames(HTTPOutput output,
					CIMObjectPath path,
					String resultClass,
					String role) throws CIMException {
	XMLNode result;
	Vector params=new Vector();
	String extra;
	
	output.setMethod(commandName);
	
	if (role!=null)
	    params.addElement(new Param(XMLP_ROLE,role));
	
	if( (path.getKeys() != null) && (path.getKeys().size()>0) ) {
	    //
	    // Been passed an instance path
	    //
	    try {
		extra="<IPARAMVALUE NAME=\""+XMLP_OBJECTNAME+"\">"+
		    path.toXml()+
		    "</IPARAMVALUE>";
	    } catch (MalformedURLException mue) {
		throw new CIMException(CIMException.CIM_ERR_INVALID_PARAMETER,
				    mue);
	    }
	} else {
	    //
	    // Assume a classname
	    //
	    extra="<IPARAMVALUE NAME=\""+XMLP_OBJECTNAME+"\">"+
		"<CLASSNAME NAME=\""+path.getObjectName()+
		"\"></CLASSNAME></IPARAMVALUE>";
	}
	
	if (resultClass!=null) {
	    extra=extra.concat("<IPARAMVALUE NAME=\""+XMLP_RESULTCLASS+"\">"+
		    "<CLASSNAME NAME=\""+resultClass+
		    "\"></CLASSNAME></IPARAMVALUE>");
	}
	
	result=intrinsicMethod(output,path,commandName,params,extra);
	SimpleEnum se=new SimpleEnum();
	
	if (result==null) {
	    return(se);
	}
	
	//
	// Build enumeration
	//
	XMLNode tmp;
	for(result=result;result!=null;result=result.getNext()) {
	    CIMObjectPath cop;
	    int token=result.getToken();
	    if (token==XMLNode.XML_ELEMENT_OBJECTPATH) {
		tmp=result.getChild();
		if (tmp==null)
		    continue;
		cop=new CIMObjectPath(tmp);
	    } else if (token==XMLNode.XML_ELEMENT_INSTANCEPATH) {
		//
		// The Microsoft implementation returns instance paths direct
		//
		cop=new CIMObjectPath(result);
		} else if (token==XMLNode.XML_ELEMENT_CLASSNAME) {
			cop=new CIMObjectPath(result.mustGetAttribute("NAME"),
				       path.getNameSpace());
		} else if (token==XMLNode.XML_ELEMENT_INSTANCENAME) {
			cop=new CIMObjectPath();
			cop.setNameSpace(path.getNameSpace());
			getInstanceName(result, cop);
		} else
			throw new CIMException(CIMException.XMLERROR,"XMLReferenceNames.getNames",
						"Element cannot be used to extract a path");
		se.addElement(cop);
	}
	return(se);
	
    }	    
    
}
