/*
 * class: CmdLineMain
 *
 * Version $Id: CmdLineMain.txt,v 1.1 2003/08/01 19:44:57 atlee Exp $
 *
 * Date: May 20 2003
 *
 * (c) 2003 IceCube Collaboration
 */

package icecube.testdaq.generator;

import icecube.testdaq.domrawio.LeCroy2DomRawSpecs;
import icecube.testdaq.domrawio.TransformerFailure;
import icecube.testdaq.domrawio.OutputWriteException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.Validator;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * This class reads in an xml file describing the transformation settings,
 * checks their validity and if the input is readable and the output writeable
 * in addition, leCroy type data is transformed into DomRaw data.
 *
 * @version $Id: CmdLineMain.txt,v 1.1 2003/08/01 19:44:57 atlee Exp $
 * @author mka
 */
public class CmdLineMain
{

    // public static final member data

    // protected static final member data

    // static final member data

    // private static final member data

    // private static member data
    private static LeCroy2DomRawSpecs lc2DrSpecs;
    private static JAXBContext jc;

    // private instance member data

    // constructors

    /**
     * Create an instance of this class.
     * Default constructor is declared, but private, to stop accidental
     * creation of an instance of the class.
     */
    private CmdLineMain()
    {
    }

    // instance member method (alphabetic)

    // static member methods (alphabetic)

    /**
     * Reads in an xml file and throws exceptions whenever something goes wrong.
     * The contents of the xml are available through the lc2DrSpecs.
     *
     * @param xmlFileName
     * @throws JAXBException
     * @throws FileNotFoundException
     */
    private static void readInXml( String xmlFileName )
            throws JAXBException, FileNotFoundException {

            jc = JAXBContext.newInstance(
                    "icecube.testdaq.domrawio" );
            Unmarshaller u = jc.createUnmarshaller();
            lc2DrSpecs = (LeCroy2DomRawSpecs) u.unmarshal( new FileInputStream(
                    xmlFileName ));
    }

    /**
     * Validates the entries that have been read in from the xml file.
     *
     * @return true if valid, false otherwise.
     */
    /**
     *
     * @param xmlFileName name of file containing the transformation parameters
     * @return
     */
    private static boolean validate( String xmlFileName ) {

        boolean valid = false;
        Validator validator = null;

        try {
            validator = jc.createValidator();
        } catch ( JAXBException e ) {

            System.out.println("The JAXBContext does not allow to validate.");
            System.exit( -6 );
        }

        try{
            valid = validator.validate( lc2DrSpecs );

        } catch ( JAXBException e ) {

            System.out.println("The xml file " + xmlFileName + " could not be " +
                    "validated.");
            System.exit( -5 );
        }

        return (valid);
    }



    // Description of this object.
    // public String toString() {}

    /**
     * Main class that can be called from the command line to transfer LeCroy
     * type to DomRaw data, passing the xml file with the trf. information as
     * first argument.
     *
     * @param args
     */
    public static void main(String args[]) {


        // Read in the xml file, passed as first argument:
        if ( args.length >= 1 ) {

            try {

                readInXml(args[0]);
            } catch ( JAXBException e ) {

                e.printStackTrace();
                System.out.println("The xml file " + args[0] + " could not be " +
                        "parsed properly.");
                System.exit( -3 );
            } catch ( FileNotFoundException e ) {

                e.printStackTrace();
                System.out.println("The xml file " + args[0] + " could not be " +
                        "found on this system.");
                System.exit( -2 );
            }

        } else {

            System.out.println("Usage: call this transfer program with the name " +
                    "of an xml file as first argument. ");
            System.exit( -1 );
        }


        // If this xml file has valid entries continue and transform according
        // to it, otherwise exit:
            if ( validate( args[0] ) == true ) {

                // Everything is happening here:
                try {

                    Transformer transformer = new Transformer(lc2DrSpecs);
                    transformer.transform();
                } catch ( TransformerFailure transformerFailure ) {

                    System.out.println(transformerFailure.getMessage() +
                            "\nThe transformation failed.");
                    System.exit( -8 );
                } catch ( OutputWriteException domRawWriteException ) {

                    System.out.println(domRawWriteException.getMessage() +
                            "\nThe transformation failed.");
                    System.exit( -8 );
                }
            } else {

                System.out.println("The xml file " + args[0] + " is not valid. " +
                        "It cannot be used for transformation.");
                System.exit( -4 );
            }

        // Fine. That's it.
        System.out.println("Everything done.");
        System.exit( 0 );

    }

}
