//
// XMLEnumerateClasses.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 XMLEnumerateClasses.java
// 
// The Initial Developer of the Original Code is David Simons.
//
// Contributor(s): Angela Lee
//
//
// 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: XMLEnumerateClasses.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
// $Log: XMLEnumerateClasses.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:39  karl
// import of SNIA 0_7
//
// Revision 1.12  2001/07/07 16:28:32  davidsi
// - Allow null object paths to be specified for the classnames.
//
// Revision 1.11  2001/07/07 16:24:51  davidsi
// - Need to now specially detect the case where the classname has not
//   been set since it will now not been null but rather the special
//   unset marker in the object path.
//
// Revision 1.10  2001/01/07 16:54:37  davidsi
// - Fixed parameter ordering (deep/localOnly were reversed).
// - Added missing getNext() call.
// - Set origin to false as per spec.
//
// Revision 1.9  2000/11/12 15:01:40  davidsi
// - Added missing brace.
//
// Revision 1.8  2000/11/12 14:47:51  davidsi
// - Added support for <VALUE.OBJECT>
//
// Revision 1.7  2000/11/06 19:34:00  davidsi
// - Added support for <VALUE_NAMEDOBJECT>
//
// Revision 1.6  2000/08/16 05:59:59  shawn
// merge P_TOMCAT31 branch
//
// Revision 1.5.8.1  2000/08/10 00:03:36  shawn
// head merge
//
// Revision 1.5  2000/05/11 10:11:24  davidsi
// - Fixed tabs.
//
// Revision 1.4  2000/05/06 17:20:03  davidsi
// - Brought in line with SUN WBEM APIs 2.2
//
// Revision 1.3  2000/04/09 14:39:19  davidsi
// - Fixed extraction and generation of classnames.
// - Removed VALUE.NAMEDOBJECT elements.
//
// Revision 1.2  2000/02/13 15:10:05  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.CIMClass;
import org.snia.wbemcmd.utils.SimpleEnum;
import org.snia.wbem.client.CIMOMHandle;

public class XMLEnumerateClasses extends XMLClass {
    static String commandName="EnumerateClasses";

    public void execute(HTTPWriter pw,XMLNode node,CIMObjectPath path,CIMOMHandle hdl)
        throws CIMException, MalformedURLException {
    String className;
    boolean localOnly;
    boolean deep;
    boolean includeQualifiers;
    boolean includeClassOrigin;
    Enumeration enum;
    String[] propertyList;

    
    className=node.extractParameterValueAttr(XMLP_CLASSNAME,XMLNode.XML_ELEMENT_CLASSNAME,
                           paramName);
    
    propertyList=node.extractParameterStringArray(XMLP_PROPERTYLIST);
    localOnly=node.extractParameterValue(XMLP_LOCAL,true);
    deep=node.extractParameterValue(XMLP_DEEP,false);
    includeQualifiers=node.extractParameterValue(XMLP_QUAL,true);
    includeClassOrigin=node.extractParameterValue(XMLP_ORIGIN,false);

    path.setObjectName(className);
    
    //enum=hdl.enumClass(path,deep,localOnly);
    enum=hdl.enumerateClasses(path, deep, localOnly, 
        includeQualifiers, includeClassOrigin);
    if (enum==null) 
        throw new CIMException(CIMException.CIM_ERR_NOT_FOUND+"Path="+path);
    
    //
    // Build result
    //
    while (enum.hasMoreElements()) {
        CIMClass cimClass=(CIMClass)(enum.nextElement());
        pw.println( 
                 cimClass.toXml(localOnly,includeQualifiers,
                         includeClassOrigin,propertyList)
                ); 
    }

    return;
    }
    
    
    public static Enumeration enumClass(HTTPOutput output,
           CIMObjectPath path,
           boolean deep,
           boolean localOnly) throws CIMException {
        return(XMLEnumerateClasses.enumerateClasses(output, path, 
            deep, localOnly, true, false));
    }

    public static Enumeration enumerateClasses(
        HTTPOutput output,
        CIMObjectPath path,
        boolean deepInheritance,
        boolean localOnly,
        boolean includeQualifiers,
        boolean includeClassOrigin) throws CIMException {
    XMLNode result;
    Vector params=new Vector();
    XMLAttribute attr;
    String className=null;

    if (path!=null) {
        className=path.getObjectName();
        //
        // Map unset classname back to null
        //
        if (className.equals(CIMObjectPath.NOT_SET))
        className=null;
    }

    SimpleEnum se=new SimpleEnum();
    
    
    output.setMethod(commandName);
    
    if (className!=null)
        params.addElement(new Param(XMLP_CLASSNAME,Param.VALUESET,
            "<CLASSNAME NAME=\""+className+"\"/>"));
    params.addElement(new Param(XMLP_LOCAL,localOnly));
    params.addElement(new Param(XMLP_DEEP, deepInheritance));
    params.addElement(new Param(XMLP_QUAL, includeQualifiers));
    params.addElement(new Param(XMLP_ORIGIN, includeClassOrigin));


    result=intrinsicMethod(output,path,commandName,params);
    
    if (result==null) {
        return(se);
    }

    for(;result!=null;) {
        // If there is a VALUE.NAMEDOBJECT node, then assume the class
        // is embedded within it - needed because some implementations
        // are still using this in the returns.
        if (result.getToken()==XMLNode.XML_ELEMENT_VALUE_NAMEDOBJECT) {
        XMLNode lresult=result.getChild();
        lresult=lresult.findElement(XMLNode.XML_ELEMENT_CLASS);
        if (lresult==null)
            break;
        CIMClass cimClass=new CIMClass(lresult);
        se.addElement(cimClass);
        result=result.getNext();
        } else if (result.getToken()==XMLNode.XML_ELEMENT_VALUE_OBJECT) {
        XMLNode lresult=result.getChild();
        lresult=lresult.findElement(XMLNode.XML_ELEMENT_CLASS);
        if (lresult==null)
            break;
        se.addElement( new CIMClass(lresult) );
        result=result.getNext();
        } else {
        result=result.findElement(XMLNode.XML_ELEMENT_CLASS);
        if (result==null)
            break;
        se.addElement( new CIMClass(result) );
        result=result.getNext();
        }
    }
    return(se);
    }    
}
