/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.deployment.web;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jfox.ejb.meta.Descriptor;
import org.jfox.ejb.meta.EJBDescriptionException;
import org.jfox.ejb.meta.EJBReferenceDescriptor;
import org.jfox.ejb.meta.EnvEntryDescriptor;
import org.jfox.ejb.meta.ResourceEnvRefDescriptor;
import org.jfox.ejb.meta.ResourceRefDescriptor;
import org.jfox.ejb.meta.XmlUtil;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * A representation of the web.xml deployment
 * contain the integration info
 *
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class WebDescriptor extends Descriptor {
    private NestedWebModule webModule;

    private List envEntries = new ArrayList();
    private List ejbReferences = new ArrayList();
    private List resourceReferences = new ArrayList();
    private List resourceEnvReferences = new ArrayList();

    public WebDescriptor(NestedWebModule webModule) {
        this.webModule = webModule;
    }

    public void processXML(Node node) throws EJBDescriptionException {
        // Parse the web-app/resource-ref elements
        Iterator iterator = XmlUtil.getElementsByTagName((Element) node, "resource-ref");
        while(iterator.hasNext()) {
            Element resRefDescriptor = (Element) iterator.next();
            ResourceRefDescriptor resRef = new ResourceRefDescriptor();
            resRef.processXML(resRefDescriptor);
            resourceReferences.add(resRef);
        }

        iterator = XmlUtil.getElementsByTagName((Element) node, "resource-env-ref");
        while(iterator.hasNext()) {
            Element resourceEnvRefDescriptor = (Element) iterator.next();
            ResourceEnvRefDescriptor resourceEnvRef = new ResourceEnvRefDescriptor();
            resourceEnvRef.processXML(resourceEnvRefDescriptor);
            resourceEnvReferences.add(resourceEnvRef);
        }


        // Parse the web-app/env-entry elements
        iterator = XmlUtil.getElementsByTagName((Element) node, "env-entry");
        while(iterator.hasNext()) {
            Element envEntry = (Element) iterator.next();
            EnvEntryDescriptor envEntryDescriptor = new EnvEntryDescriptor();
            envEntryDescriptor.processXML(envEntry);
            envEntries.add(envEntryDescriptor);
        }

        // Parse the web-app/ejb-ref elements
        iterator = XmlUtil.getElementsByTagName((Element) node, "ejb-ref");
        while(iterator.hasNext()) {
            Element ejbRef = (Element) iterator.next();
            EJBReferenceDescriptor ejbRefDescriptor = new EJBReferenceDescriptor();
            ejbRefDescriptor.processXML(ejbRef);
            ejbReferences.add(ejbRefDescriptor);
        }

    }

    public NestedWebModule getWebModule() {
        return webModule;
    }

    public EnvEntryDescriptor[] getEnvEntries() {
        return (EnvEntryDescriptor[]) envEntries.toArray(new EnvEntryDescriptor[envEntries.size()]);
    }

    public EJBReferenceDescriptor[] getEjbReferences() {
        return (EJBReferenceDescriptor[]) ejbReferences.toArray(new EJBReferenceDescriptor[ejbReferences.size()]);
    }

    public ResourceRefDescriptor[] getResourceReferences() {
        return (ResourceRefDescriptor[]) resourceReferences.toArray(new ResourceRefDescriptor[resourceReferences.size()]);
    }

    public ResourceEnvRefDescriptor[] getResourceEnvReferences() {
        return (ResourceEnvRefDescriptor[]) resourceEnvReferences.toArray(new ResourceEnvRefDescriptor[resourceEnvReferences.size()]);
    }


    public static void main(String[] args) {

    }
}

