/* 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.framework.deploy;

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

import org.jfox.framework.util.XmlUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class ModuleDescriptor implements Descriptor {

    private List components = new ArrayList();
    private String description;
    // default priority
    private int priority = 10;

    public ModuleDescriptor(Document document) throws ComponentDescriptionException {
        this.processXML(document.getDocumentElement());
    }

    /**
     * node is the Document element
     *
     * @param node
     * @throws ComponentDescriptionException
     */
    public void processXML(Node node) throws ComponentDescriptionException {
        setDescription(XmlUtils.getChildNodeValueOf(node, "description"));
        setPriority(XmlUtils.getChildNodeValueOf(node, "priority"));
        Iterator components = XmlUtils.getElementsByTagName((Element) node, "component");
        while(components.hasNext()) {
            Element component = (Element) components.next();
            ComponentDescriptor componentDesc = new ComponentDescriptor(this);
            componentDesc.processXML(component);
            addComponent(componentDesc);
        }
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(String priority) {
        try {
            this.priority = Integer.parseInt(priority);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    private void addComponent(ComponentDescriptor component) {
        components.add(component);
    }

    public ComponentDescriptor[] getComponentDescriptors() {
        return (ComponentDescriptor[]) components.toArray(new ComponentDescriptor[components.size()]);
    }

    public static void main(String[] args) {

    }
}

