import org.apache.xerces.parsers.DOMParser;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.DocumentType;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


import java.awt.TextArea;
import java.awt.Label;
import java.awt.Font;
import java.applet.Applet;
import java.util.Vector;


public class testxml extends Applet {
    TextArea text2;
    Label label2;
    Vector vec;
    public Document doc;
    private Vector moduleNode;

    public void init() {
	moduleNode = new Vector();
	label2 = new Label("XML DOM Parser Output");
	text2 = new TextArea();
	text2.setColumns(80);
	text2.setRows(80);
        vec = new Vector();
	int tempi=0;
	int max=0;
	int diff=0;

	// Adding XML code here
	String uri = "http://www.sdsc.edu/~damartin/bionome/guixml/home.xml";
        System.out.println("Parsing XML File: "+ uri + "\n\n");

        DOMParser parser = new DOMParser();
        try {
            parser.parse(uri);
            doc = parser.getDocument();
        } catch (Exception e) {
            System.out.println("Error in parsing: " + e.getMessage());
        }


	String title, name, url;
	NodeList sections = doc.getElementsByTagName("Section");
	int numSections = sections.getLength();
	System.err.println("numSections = "+numSections);
	
	text2.append("Section in Main Page:\n");
	for (int i=0; i<numSections; i++) {
	    Element section = (Element) sections.item(i);
	    text2.append("Section\n");
            // 1 Step
            name = getModuleName(section, "Name");
            if (name!="") text2.append("    Name = "+name+"\n");
	}




/*
	// Go thru all Module nodes by getting elements by tag name
	int numParams;
	String moduletype, step, input, output;
	String elementName = "Module";
	NodeList modules = doc.getElementsByTagName(elementName);
	int moduleCount = modules.getLength();

	for (int i=0; i<moduleCount; i++) {
	    Element module = (Element) modules.item(i);

	    text2.append(module.getNodeName()+"\n");

	    // Type always exists
	    moduletype = getModuleName(module, "Type");
	    text2.append("    Type = "+moduletype+"\n");

	    // 0 or 1 Steps
	    step = getModuleName(module, "Step");
	    if (step!="") text2.append("    Step = "+step+"\n");

	    // 0 or 1 Inputs
	    input = getModuleName(module, "Input");
	    if (input!="") text2.append("    Input = "+input+"\n");

	    // 0 or 1 Outputs
	    output = getModuleName(module, "Output");
	    if (output!="") text2.append("    Output = "+output+"\n");

	    // Go through the 1 or more Parameters
	    NodeList parameters = module.getElementsByTagName("Parameter");
	    numParams = parameters.getLength();

	    for (int j=0; j<numParams; j++) {
		Element parameter = (Element) parameters.item(j);
		NodeList names = parameter.getElementsByTagName("Name");
	        text2.append("     Parameter\n");
	        text2.append("         Name = "+getParameterName(names)+"\n");
		NodeList values = parameter.getElementsByTagName("Value");
	        text2.append("         Value = "+getParameterName(values)+"\n");
	    }
*/
/*
	    // Go through the 1 or more Input
	    NodeList inputs = element.getElementsByTagName("Input");

	    int numInputs = inputs.getLength();
            for (int j=0; j<numInputs; j++) {
                Element input = (Element) inputs.item(j);
                NodeList descs = input.getElementsByTagName("Desc");
                text2.append("     Input\n");
                text2.append("         Desc = "+getParameterName(descs)+"\n");
            }


	    // Go through the 1 or more Output
	    NodeList outputs = element.getElementsByTagName("Output");

	    int numOutputs = outputs.getLength();
            for (int j=0; j<numOutputs; j++) {
                Element output = (Element) outputs.item(j);
                NodeList descs = output.getElementsByTagName("Desc");
                text2.append("     Output\n");
                text2.append("         Desc = "+getParameterName(descs)+"\n");
            }
*/

	//}
	add(label2);
	add(text2);
    }

    private String getModuleName(Element element, String Name) {
        String temp="";
        // Go thru the elements
        NodeList types = element.getElementsByTagName(Name);
        //int typeCount = types.getLength();
        Element type = (Element) types.item(0);
        NodeList children = type.getChildNodes();
        Node child = children.item(0);
        temp = child.getNodeValue();
/*
        for (int j=0; j<typeCount; j++) {
            Element type = (Element) types.item(j);

            NodeList children = type.getChildNodes();
            int childCount = children.getLength();
            for (int k=0; k<childCount; k++) {
                 Node child = children.item(k);
                 temp = child.getNodeValue();
            }
        }
*/
        return(temp);
    }

    private String getParameterName(NodeList types) {
        String temp="";
        // Go thru the elements
        int typeCount = types.getLength();
        for (int j=0; j<typeCount; j++) {
            Element type = (Element) types.item(j);

            NodeList children = type.getChildNodes();
            int childCount = children.getLength();
            for (int k=0; k<childCount; k++) {
                 Node child = children.item(k);
                 temp = child.getNodeValue();
            }
        }
        return(temp);
    }

}
	
