// TestingMobyParser.java
//
//    Created: August 2005
//
// This file is a component of the BioMoby project.
// Copyright Martin Senger (martin.senger@gmail.com).
//

import org.biomoby.client.CmdLineHelper;
import org.biomoby.shared.parser.MobyPackage;

import org.tulsoft.tools.BaseCmdLine;
import java.io.File;
import java.util.Map;
import java.util.HashMap;

/**
 * A command-line client with the only purpose - to test parser of the
 * Biomoby XML data. It takes an XML file, parses it and prints all
 * its component as hierarchical strings. <p>
 *
 * Or, it re-creates back the same XML and prints it. The XML result in
 * this case is not identical as the original - it may have different
 * formatting, it probably has more XML namespace prefixes (they are
 * everywhere), but more importantly, it may not reflect all data that
 * was in the original input. This is because it ignores all values
 * that are not carried by, and only by, the Biomoby primitives types
 * (as it was allowed before big change in the summer 2005). <p>
 *
 * Start it with <tt>-h</tt> option. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: TestingMobyParser.java,v 1.3 2008/02/28 05:21:48 senger Exp $
 */
public class TestingMobyParser
    extends CmdLineHelper {

    /*************************************************************************
     *
     *  An entry point...
     *
     *************************************************************************/
    public static void main (String [] args) {
	try {

	    BaseCmdLine cmd = getCmdLine (args, TestingMobyParser.class);

	    boolean reverse = cmd.hasOption ("-r");
	    boolean noprint = cmd.hasOption ("-n");
	    String backup = cmd.getParam ("-b");

	    String infile = null;
	    Map<String,String> backups = new HashMap<String,String>();
	    int i = 0;
	    while (i < cmd.params.length) {
		if (cmd.params[i].startsWith ("-") && i < cmd.params.length - 1) {
		    backups.put (cmd.params[i].substring (1),
				 cmd.params[i+1]);
		    i += 2;
		    continue;
		} else {
		    infile = cmd.params[i];
		    i++;
		    continue;
		}
	    }

	    if (infile == null) return;

	    MobyPackage moby = null;
	    if (backups.isEmpty()) {
		moby = MobyPackage.createFromXML (new File (cmd.params[0]), backup);
	    } else {
		moby = MobyPackage.createFromXML (new File (cmd.params[0]), backups);
	    }

	    if (! noprint)
		msgln (moby.toString());

	    if (reverse)
		msg (moby.toXML());

	    exit (0);

	} catch (Throwable e) {
	    processErrorAndExit (e);
	}
    }
}
