/**
 * @(#)Grammar.java	18.03.2004
 * 
 * @author kst
 */
package commons.chartparser;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
/**
 * Einzige ausfuehrbare Klasse des Projekts
 */
@SuppressWarnings("unchecked")
public class Main
{

static final String      PROPERTIES     = ".properties";
static String            RULES          = "rules.txt";
static String            LEX            = "lex.txt";
static String            TEXT           = "text.txt";
static String            DEFAULT_PARSER = "Chart";
static String            path           = ".";
static Text              text           = null;
public static Properties setup          = null;
static String            FILE_SEP       = null;
static public boolean    IGNCASE        = true;

// ignore case fuer terminale (lex+text): Sie==sie
public static void main (String[] argv)
{
   FILE_SEP = System.getProperty("file.separator");
   if (argv.length == 0 || (argv.length > 0 && (argv[0].equalsIgnoreCase("/help") || argv[0].endsWith("?"))))
   {
      useage();
   }
   // ----------------------------------------------------------------------
   // Setting aus Property File:
   // normalerweise: ".properties"
   setup(argv);
   // Parser P = new ChartParser (new Grammar(path+FILE_SEP+RULES), new
   // Lex(path+FILE_SEP+LEX));
   // Grammatik, Lexikon und ParserName bestimmen
   Grammar G = new Grammar(setup.getProperty("path") + FILE_SEP + setup.getProperty("rules"));
   Lex L = new Lex(setup.getProperty("path") + FILE_SEP + setup.getProperty("lex"));
   String parserName = setup.getProperty("parser");
   // Grammatik, Lexikon und ParserName
   // an Parserfactory-Methode geben:
   // Parser wird dann gesucht und geladen
   Parser P = Parser.getParser(G, L, parserName);
   // Parser geladen? dann noch ein paar Einstellungen und satzweise parsen:
   if (P != null)
   {
      P.quiet = setup.getProperty("quiet").equalsIgnoreCase("true");
      IGNCASE = setup.getProperty("ignorecase").equalsIgnoreCase("true");
      Iterator S = text.satzIterator();
      while (S != null && S.hasNext())
      {
         // Einen Satz als String lesen:
         String satz = (String) S.next();
         System.out.print("\"" + satz + "\" ---> ");
         // Satz in Vector von Woertern umwandeln:
         List woerter = (ArrayList) Text.wortVector(satz, true);
         // String r = P.checkResult(T);
         // Parsing, Ergebnis ist ein STree-Objekt, das einen Syntaxbaum
         // repraesentiert:
         STree T = P.parse(woerter);
         // String r = P.checkResult(T);
         if (T == null)
         {
            debug("{" + satz + "} " + P.checkResult());
            // gibt die bereinigte chart aus
         }
         else
         {
            // Ausgabe-Format aus den Properties holen
            // (Vollform||Semantik||Syntax):
            int outputformat = Integer.parseInt((String) setup.getProperty("output"));
            // Ausgabe des Baums:
            debug(T.toString(outputformat));
         }
      } // naechster Satz...
   }
   // ----------------------------------------------------------------------
}

private static void setup (String[] argv)
{
   setup = new Properties();
   File f = new File(PROPERTIES);
   if (f.exists())
   {
      try
      {
         setup.load(new FileInputStream(f));
      }
      catch (Exception e)
      {
         debug(PROPERTIES + " not found or not readable.");
      }
   }
   if (setup.getProperty("parser") == null) setup.setProperty("parser", DEFAULT_PARSER);
   if (setup.getProperty("path") == null) setup.setProperty("path", ".");
   if (setup.getProperty("text") == null) setup.setProperty("text", TEXT);
   if (setup.getProperty("lex") == null) setup.setProperty("lex", LEX);
   if (setup.getProperty("rules") == null) setup.setProperty("rules", RULES);
   if (setup.getProperty("output") == null) setup.setProperty("output", "0");
   if (setup.getProperty("quiet") == null) setup.setProperty("quiet", "false");
   if (setup.getProperty("ignorecase") == null) setup.setProperty("ignorecase", "false");
   switch (argv.length)
   {
   case 0:
      text = new Text();
      text.readFromFile(setup.getProperty("path") + FILE_SEP + setup.getProperty("text"));
      break;
   case 1:
      setup.setProperty("path", argv[0]);
      text = new Text();
      text.readFromFile(setup.getProperty("path") + FILE_SEP + setup.getProperty("text"));
      break;
   case 2:
      setup.setProperty("path", argv[0]);
      text = new Text();
      text.readFromFile(argv[1]);
      break;
   default:
      useage();
   }
}

public static void useage ()
{
   debug("Main [<Folder>] [<Textfile>]");
   debug(" Folder  : Path or relative mountpoint for text.txt, lex.txt and rules.txt.");
   debug(" Textfile: use absolute path description here.");
   System.exit(-1);
}

public static void debug (String v)
{
   System.out.println(v);
}

public static String format (String f, Object[] o)
{
   String s = "";
   for (int i = 0; i < o.length; i++)
   {
      s += o[i];
      s += " ";
   }
   return s;
}
}