/* Generated by Together */

package cobalt.deployment.descriptor.xml;

import java.util.ArrayList;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.apache.xerces.dom.NodeImpl;
import org.apache.xerces.dom.ElementImpl;
import org.apache.xerces.dom.DocumentImpl;

import cobalt.deployment.descriptor.*;
import cobalt.deployment.util.XmlUtil;

/**
 * Title:        method xml description
 * Description:  J2EE standard method xml description
 * Copyright:    Copyright (c) 2001
 * Company:      ETRI
 * @author YeonJun Choi
 * @version 1.0
 */

public class MethodNode extends ElementImpl {
	public MethodNode(Document doc) {
        super((DocumentImpl)doc, "method");
    }
    static public MethodDescriptor getMethodDescriptor(EjbBundleDescriptor bundle, Node node) {
        NodeList nodelist = node.getChildNodes();
        MethodDescriptor method = null;
        String ejbname = null, mtd_intf = null, mtd_name = null;
        ArrayList mtd_params = new ArrayList();
        Node child = null;
        for ( int i=0 ; i<nodelist.getLength(); i++) {
            child = nodelist.item(i);
            if ( !(child instanceof Element) ) continue;
            if (((Element)child).getTagName().equals("ejb-name"))
                ejbname = XmlUtil.getNodeTextValue(child);
            else if (((Element)child).getTagName().equals("method-intf"))
                mtd_intf = XmlUtil.getNodeTextValue(child);
            else if (((Element)child).getTagName().equals("method-name"))
                mtd_name = XmlUtil.getNodeTextValue(child);
            else if (((Element)child).getTagName().equals("method-param"))
                mtd_params.add(XmlUtil.getNodeTextValue(child));
        }
        EjbDescriptor ejb = bundle.getEjbDescriptor(ejbname);
        method = ejb.getMethod(mtd_intf, mtd_name, mtd_params);
        return method;
    }
    public void setMethodDescriptor(MethodDescriptor descriptor) {
        Document doc = getOwnerDocument();
        XmlUtil.addNode(this, XmlUtil.getNewTextNode((DocumentImpl)doc, "description", descriptor.getDescription()));
        XmlUtil.addNode(this, XmlUtil.getNewTextNode((DocumentImpl)doc, "ejb-name", descriptor.getEjbName()));
        XmlUtil.addNode(this, XmlUtil.getNewTextNode((DocumentImpl)doc, "method-intf", descriptor.getMethodIntf()));
        XmlUtil.addNode(this, XmlUtil.getNewTextNode((DocumentImpl)doc, "method-name", descriptor.getMethodName()));
        //if ( descriptor.getParams() != null) {
        if ( !descriptor.getParams().isEmpty()) {
            Node node = new ElementImpl((DocumentImpl)doc, "method-params");
            this.appendChild(node);
            XmlUtil.appendMethodParamNode((DocumentImpl)doc, node, descriptor.getParams());
        }
    }
}
