/*
	Copyright © 2004-2005 Gossamer Group, LLC.  All Rights Reserved.


*/


/***************************************************************************
 * Major Revision Log:
 *
 * Programmer ID  |    Date    |      Major Revision/ Modification
 * -------------------------------------------------------------------------
 * Skip          Mar 29, 2005        created file
 *
 *
 ***************************************************************************/


import org.apache.xml.security.Init;
import org.apache.xml.security.c14n.Canonicalizer;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.FileOutputStream;


/**
 * 
 * 
 *
 * @author <a href="mailto:skip@gossamer-group.com">Skip Walker</a>
 */
public class TestCanonicalizer
{
    public static final String TEST_MESSAGE_FILE = "TestSoapMessage.xml";
    public static final String TEST_MESSAGE_FILE_NO_WS =
            "TestSoapMessage-nowhitespace.xml";

    // -------------------------------------------------------------- Constants


    // ------------------------------------------------------- Static Variables


    // ------------------------------------------------------ Static Init Block

    static
    {
        Init.init();
    }

    // ----------------------------------------------------- Instance Variables
	
	
    // ----------------------------------------------------------- Constructors


    // --------------------------------------------------------- Static Methods

    public static void testCanonicalizerWithJAXP(String[] args)
            throws Exception
    {
        String file = TEST_MESSAGE_FILE;

        if (args.length > 0)
            file = args[0];

        File f = new File(file);

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();


        factory.setValidating(true);
        factory.setNamespaceAware(true);

        DocumentBuilder builder = factory.newInstance().
                newDocumentBuilder();

        System.out.println("\n---------\nParsing file with JAXP: " + file);
        Document doc = builder.parse(f);

        System.out.println("\nSerializing Doc before Canonicalization:\n");
        XMLSerializer ser = new XMLSerializer();
        ser.setOutputByteStream(System.out);
        ser.serialize(doc);


        System.out.println("\n");

        Canonicalizer c14n = Canonicalizer.getInstance(
                Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS);

        byte[] bytes = c14n.canonicalizeSubtree(doc.getDocumentElement());

        String canonicalizedXML = new String(bytes);
        String outputFile = "jaxp-c14n.xml";
        if (args.length > 1)
            outputFile = args[1];

        System.out.println("Canonicalized XML written to file: " + outputFile
                           + "\n\n" + canonicalizedXML + "\n\n-------");

        FileOutputStream fos = new FileOutputStream(new File(outputFile));
        fos.write(bytes);

    }

    /**
     * Args
     * @param args
     * @throws Exception
     */
    public static void testCanonicalizerWithElectricXML(String[] args)
            throws Exception
    {
        String file = TEST_MESSAGE_FILE;

        if (args.length > 0)
            file = args[0];

        electric.xml.Document doc = new electric.xml.Document(new File(file));
        System.out.println("\n----------\nParsing File with ElectricXML: " +
                           file);
        System.out.println("\nDocument before Canonicalization:\n\n" +
                           doc.toString());


        Canonicalizer c14n = Canonicalizer.getInstance(
                Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS);


        byte[] bytes = c14n.canonicalizeSubtree(doc.getRoot());


        String canonicalizedXML = new String(bytes);
        String outputFile = "electric-c14n.xml";
        if (args.length > 1)
            outputFile = args[1];

        FileOutputStream fos = new FileOutputStream(new File(outputFile));
        fos.write(bytes);

        System.out.println("\nCanonicalized XML written to file: " + outputFile
                           + "\n\n" + canonicalizedXML + "\n\n-------");



    }




    private static void testFiles()
            throws Exception
    {
        String[] exArgs1 =
                {TEST_MESSAGE_FILE,
                 "c14n-electric.xml"};

        testCanonicalizerWithElectricXML(exArgs1);

        String[] exArgs2 =
                {TEST_MESSAGE_FILE_NO_WS,
                 "c14n-electric-nowhitespace.xml"};

        testCanonicalizerWithElectricXML(exArgs2);

        String[] ndArgs1 =
                {TEST_MESSAGE_FILE,
                 "c14n-jaxp.xml"};

        testCanonicalizerWithJAXP(ndArgs1);

        String[] ndArgs2 =
                {TEST_MESSAGE_FILE_NO_WS,
                 "c14n-jaxp-nowhitespace.xml"};

        testCanonicalizerWithJAXP(ndArgs2);


    }

    public static void printUsage()
    {
        System.out.println("3 ways to run:  ");
        System.out.println("Test JAXP and ElectricXML on both Test files");
        System.out.println("\tTestCanonicalizer files");
        System.out.println("- The test for JAXP and ElectricXML are run on " +
                           "both test files.");
        System.out.println("\nTest ElectricXML on a test file");
        System.out.println("\tTestCanonicalizer electric [inputFile] [outputFile]");
        System.out.println("\t-inputFile defaults to " + TEST_MESSAGE_FILE);
        System.out.println("\t-outputFile defaults to elctric-c14n.xml");
        System.out.println("\nTest JAXP on a test file");
        System.out.println("\tTestCanonicalizer jaxp [inputFile] [outputFile]");
        System.out.println("\t-inputFile defaults to " + TEST_MESSAGE_FILE);
        System.out.println("\t-outputFile defaults to jaxp-c14n.xml");
    }

    // --------------------------------------------------------- Public Methods


    // -------------------------------------------------- Getter/Setter Methods
    
    
    // ------------------------------------------------------ Canonical Methods
    
    
    // ------------------------------------------------------ Interface Methods
    
    

    // -------------------------------------------------------- Package Methods

    // ------------------------------------------------------ Protected Methods


    // -------------------------------------------------------- Private Methods

    // ---------------------------------------------------------- Inner Classes

    // ------------------------------------------------------------ Main Method






    /**
     * Arguments run
     *
     * files
     * electric [inputFile (default=TestSoapMessage.xml)] [outputFile(default=electric-c14n.xml)]
     * jaxp [inputFiledefault=TestSoapMessage.xml] [outputFile(default=jaxp-c14n.xml)]
     *
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception
    {


        if (args.length == 0)
        {
            printUsage();
            System.exit(-1);
        }

        String testToRun = args[0];
        String[] methodArgs = new String[0];

        methodArgs = new String[args.length-1];
        System.arraycopy(args,1,methodArgs,0,args.length-1);



        if ("electric".equals(testToRun))
            testCanonicalizerWithElectricXML(methodArgs);
        else if ("jaxp".equals(testToRun))
            testCanonicalizerWithJAXP(methodArgs);
        else
            testFiles();




    }



}