/*
 * (C) Copyright IBM Corp. 1997-1998  All rights reserved.
 *
 * US Government Users Restricted Rights Use, duplication or
 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *
 * The program is provided "as is" without any warranty express or
 * implied, including the warranty of non-infringement and the implied
 * warranties of merchantibility and fitness for a particular purpose.
 * IBM will not be liable for any damages suffered by you as a result
 * of using the Program. In no event will IBM be liable for any
 * special, indirect or consequential damages or lost profits even if
 * IBM has been advised of the possibility of their occurrence. IBM
 * will not be liable for any third party claims against you.
 */

package com.ibm.xml.sample;

import com.ibm.xml.parser.*;
import java.io.PrintWriter;

/**
 * A sample of MD5.
 *
 * @version 1.1
 * @author TAMURA Kent &lt;kent@trl.ibm.co.jp&gt;
 */
public class Digest {
    public static void main(String[] argv) {
        ElementFactory factory = new DefaultElementFactory();
        TXDocument doc = factory.createDocument();
        doc.setVersion("1.0");

        TXElement root = factory.createElement("PERSON");

                                                //  Make <NAME>John Doe</NAME>
        TXElement item = factory.createElement("NAME");
        item.addElement(factory.createText("John Doe"));
        root.addElement(item);
                                                // Make <AGE>35</AGE>
        item = factory.createElement("AGE");
        item.addElement(factory.createText("35"));
        root.addElement(item);
                                                // Make <EMAIL>John.Doe@foo.com</EMAIL>
        item = factory.createElement("EMAIL");
        item.addElement(factory.createText("John.Doe@foo.com"));
        root.addElement(item);
                                                // Make <URL HREF="http:...."/>
        item = factory.createElement("URL");
        item.setAttribute("HREF", "http://www.foo.com/%7EJohn.Doe/");
        root.addElement(item);

                                                // Now root has NAME,AGE,EMAIL,URL.
        doc.addElement(root);

        PrintWriter out = new PrintWriter(System.out);
        Format.print(doc, out);
        out.println("================================================================");
        printMD5Tree(doc, out, 0);

        out.println("================================================================");
        item.addElement(factory.createText("hoge?"));   // Modify the document
        Format.print(doc, out);
        out.println("================================================================");
        printMD5Tree(doc, out, 0);
        out.flush();
    }

    static void printByteArray(PrintWriter pw, byte[] ab) {
        if (null == ab) {
            pw.print("NULL");
        } else {
            for (int i = 0;  i < ab.length;  i ++) {
                printByte(pw, ab[i]);
            }
        }
        pw.println("");
    }
    static final String tab = "0123456789ABCDEF";
    static void printByte(PrintWriter pw, byte b) {
        
        pw.print(tab.charAt((b>>4)&0x0f));
        pw.print(tab.charAt(b&0x0f));
    }
    static void printMD5Tree(Parent par, PrintWriter pw, int indent) {
        org.w3c.dom.NodeIterator ne = par.getChildNodes();
        Child ch;
        while (null != (ch = (Child)ne.toNextNode())) {
            Format.printSpace(pw, indent);
            pw.print(com.ibm.xml.sample.trlx.getLastName(ch.getClass().toString())+" ");
            printByteArray(pw, ch.getDigest());
            if (ch instanceof Parent) {
                printMD5Tree((Parent)ch, pw, indent+2);
            }
        }
        pw.flush();
    }
}

