/*
 * Approved for Public Release; Distribution Unlimited. Case Number 14-4275
 * Copyright 2014 The MITRE Corporation.  All rights reserved.
 */
package org.mitre.deluge;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import javax.xml.transform.TransformerException;

/**
 * A command line program for transforming an XML with an XSLT that includes the <xsl:stream> command.<p>
 * The transformer send the results to a SAXResult, thus enabling it to handle streaming XSLT.<p>
 * @author James Garriss
 */
public final class Deluge
{
    private static PrintStream outputPrintStream;
    
    /**
     * The main class for the CLI program <p>
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // Make sure we have the correct number of arguments.
        if (args.length != 3)
        {
            printStandardMessage();
            return;
        }
        
        // Run sanity checks on the arguments.
        File xmlFile = null;
        try
        {
            xmlFile = new File(args[0]);
        }
        catch (Exception ex)
        {
            System.out.println("It appears that your first argument is not a valid file.");
            printStandardMessage();
            return;
        }
        
        File xslFile = null;
        try
        {
            xslFile = new File(args[1]);
        }
        catch (Exception ex)
        {
            System.out.println("It appears that your second argument is not a valid file.");
            printStandardMessage();
            return;
        }
        
        File outputFile = null;
        try
        {
            outputFile = new File(args[2]);
        }
        catch (Exception ex)
        {
            System.out.println("It appears that your third argument is not a valid file.");
            printStandardMessage();
            return;
        }
        
        // Be prepared to handle the ^C shutdown
        Runtime.getRuntime().addShutdownHook(new Thread()
        {
            @Override
            public void run()
            { 
                // Close the printstream, if it was opened.
                if (outputPrintStream != null)
                {
                    outputPrintStream.close();
                }
            }
        });

        // Call the transformation class
        try
        {
            DelugeTransformer transformer = new DelugeTransformer();
            transformer.transformStreaming(xmlFile, xslFile, outputFile);
        }
        catch (IOException ex)
        {
            System.out.println("A file cannot be found: " + ex.getMessage());
            printStandardMessage();
        }
        catch (TransformerException ex)
        {
            System.out.println("There was a problem with the tranformation: " + ex.getMessage());
            printStandardMessage();
        }
    }

    /**
     * Prints a standard message explaining how to use the software
     */
    private static void printStandardMessage()
    {
        System.out.println("To run: java -jar deluge-0.5.jar xmlFile.xml xslFile.xsl outputFile.html");
        System.out.println("Replace '0.5' with the current version and '.html' with the correct output type.");
        System.out.println("To quit: Ctrl-c");
    }
}
