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://172.20.150.251/uhtss/xmltest/roses.xml";
	String uri = "http://www.sdsc.edu/~damartin/bionome/xmldave/tulips.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 step, channel, input, output;
	NodeList mappings = doc.getElementsByTagName("Mapping");
	int numMapping = mappings.getLength();
	System.err.println("numMappings = "+numMapping);
	
	Element mapping = (Element) mappings.item(0);
	text2.append("First Mapping\n");
        // 1 Step
        step = getModuleName(mapping, "Step");
        if (step!="") text2.append("    Step = "+step+"\n");

        // 1 Channel
        channel = getModuleName(mapping, "Channel");
        if (channel!="") text2.append("    Channel = "+channel+"\n");

        // 1 Output
        output = getModuleName(mapping, "Output");
        if (output!="") text2.append("    Output = "+output+"\n");

	text2.append("\n\n");
	NodeList corrections = doc.getElementsByTagName("Correction");
	Element correction = (Element) corrections.item(1);
	text2.append("Second Correction\n");

        // 1 Step
        step = getModuleName(correction, "Step");
        if (step!="") text2.append("    Step = "+step+"\n");

        // 1 Input
        input = getModuleName(correction, "Input");
        if (input!="") text2.append("    Input = "+input+"\n");

        // 1 Output
        output = getModuleName(correction, "Output");
        if (output!="") text2.append("    Output = "+output+"\n");

        // 1 BackgroundWellType
        String bwt = getModuleName(correction, "BackgroundWellType");
        if (bwt!="") text2.append("    BackgroundWellType = "+bwt+"\n");

        // 1 CorrectionMethod
        String cm = getModuleName(correction, "CorrectionMethod");
        if (cm!="") text2.append("    CorrectionMethod = "+cm+"\n");

        // 1 CorrectionValue
        String cv = getModuleName(correction, "CorrectionValue");
        if (cv!="") text2.append("    CorrectionValue = "+cv+"\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();
        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);
    }

}
	
