Package net.sourceforge.chaperon.adapter.cli

Source Code of net.sourceforge.chaperon.adapter.cli.Main

/*
*  Copyright (C) Chaperon. All rights reserved.
*  -------------------------------------------------------------------------
*  This software is published under the terms of the Apache Software License
*  version 1.1, a copy of which has been included  with this distribution in
*  the LICENSE file.
*/

package net.sourceforge.chaperon.adapter.cli;

import net.sourceforge.chaperon.build.Automaton;
import net.sourceforge.chaperon.common.ConsoleLog;
import net.sourceforge.chaperon.model.grammar.*;
import net.sourceforge.chaperon.process.GeneralParserProcessor;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

//import org.xml.sax.ContentHandler;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;

import java.io.File;

import java.util.Properties;

import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

public class Main
{
  public static final String LEXICON_OPT = "l";
  public static final String LEXICON_OPT_LONG = "lexicon";
  public static final String GRAMMAR_OPT = "g";
  public static final String GRAMMAR_OPT_LONG = "grammar";
  public static final String IN_OPT = "i";
  public static final String IN_OPT_LONG = "in";
  public static final String OUT_OPT = "o";
  public static final String OUT_OPT_LONG = "out";
  private File lexiconFile = null;
  private File grammarFile = null;
  private File cacheDir = null;
  private String parserFactory = null;
  private SAXParserFactory parserFactoryImpl = null;
  private String transformerFactory = null;
  private SAXTransformerFactory transformerFactoryImpl = null;
  private String encoding = "ISO-8859-1";
  private boolean indent = false;

  //private boolean flatten = false;
  //private String inputtype = "text";
  private ConsoleLog log = new ConsoleLog();

  /*private ParserHandlerAdapter parseradapter = null;
  private ParserAutomaton parserautomaton = null;
  private ParserProcessor parser = null;*/
  /*private LexicalHandlerAdapter lexicaladapter = null;
  private LexicalAutomaton lexicalautomaton = null;
  private LexicalProcessor lexer = null;*/
  private Automaton parserautomaton = null;
  private GeneralParserProcessor parser = null;

  /**
   * Set the lexicon, which should be used.
   *
   * @param lexiconFile Lexicon file.
   */
  public void setLexicon(File lexiconFile)
  {
    this.lexiconFile = lexiconFile;
  }

  /**
   * Set the grammar, which should be used.
   *
   * @param grammarFile Grammar file.
   */
  public void setGrammar(File grammarFile)
  {
    this.grammarFile = grammarFile;
  }

  private void process(File inFile, File outFile) throws Exception
  {
    if (!inFile.exists())
      throw new Exception("File "+inFile+" doesn't exists");

    if (inFile.lastModified()>outFile.lastModified())
    {
      ensureDirectoryFor(outFile);
      System.out.println("Parsing file "+inFile+" to "+outFile)//info

      /*if ((this.parserautomaton!=null) && (this.parseradapter==null))
      {
        this.parseradapter = new ParserHandlerAdapter();
        this.parseradapter.setFlatten(this.flatten);
        if (inputtype.equalsIgnoreCase("xml"))
          this.parseradapter.setEmbedded(true);
      }

      if ((this.parserautomaton==null) && (this.lexicaladapter==null))
      {
        this.lexicaladapter = new LexicalHandlerAdapter();
      }

      if ((this.parserautomaton!=null) && (this.parser==null))
      {
        this.parser = new ParserProcessor();
        this.parser.setLog(log);
        this.parser.setParserAutomaton(this.parserautomaton);
        this.parser.setParserHandler(this.parseradapter);
      }

      if (this.lexer==null)
      {
        this.lexer = new LexicalProcessor();
        this.lexer.setLog(log);
        this.lexer.setLexicalAutomaton(this.lexicalautomaton);
        if (this.parserautomaton!=null)
          this.lexer.setLexicalHandler(this.parser);
        else
          this.lexer.setLexicalHandler(this.lexicaladapter);
      }*/
      this.parser = new GeneralParserProcessor();
      this.parser.setLog(log);
      this.parser.setParserAutomaton(this.parserautomaton);

      Properties format = new Properties();

      format.put(OutputKeys.ENCODING, encoding);
      if (indent)
        format.put(OutputKeys.INDENT, "yes");

      format.put(OutputKeys.METHOD, "xml");

      SAXTransformerFactory factory = getTransformerFactory();

      TransformerHandler serializer = factory.newTransformerHandler();
      serializer.getTransformer().setOutputProperties(format);
      serializer.setResult(new StreamResult(outFile));

      /*if (this.parserautomaton!=null)
        this.parseradapter.setContentHandler(serializer);
      else
        this.lexicaladapter.setContentHandler(serializer);*/
      this.parser.setContentHandler(serializer);

      /*if ( !inputtype.equalsIgnoreCase("xml"))
        pushTextFile(inFile);
      else*/
      pushXMLFile(inFile);
    }
  }

  private void buildAutomata(File lexiconFile, File grammarFile)
    throws Exception
  {
    if ((cacheDir!=null) && (!cacheDir.exists()))
      throw new Exception("Cache directory "+cacheDir+" doesn't exist");

    /*
    // Lexicon
    String filename = lexiconFile.getName();

    File cacheFile = null;
    if (cacheDir!=null)
      cacheFile = new File(cacheDir, filename+".obj");

    if ((cacheFile!=null) && (cacheFile.exists()) && (cacheFile.lastModified()>lexiconFile.lastModified()))
    {
      System.out.println("Reading lexicon from cache "+cacheFile); //debug
      ObjectInputStream in = new ObjectInputStream(new FileInputStream(cacheFile));
      this.lexicalautomaton = (LexicalAutomaton)in.readObject();
      in.close();
    }
    else
    {
      System.out.println("Building lexicon from "+lexiconFile); //info
      SAXParserFactory factory = getParserFactory();

      factory.setNamespaceAware(true);
      XMLReader parser = factory.newSAXParser().getXMLReader();

      NamespacedSAXConfigurationHandler handler = new NamespacedSAXConfigurationHandler();

      parser.setContentHandler(handler);
      try
      {
        parser.parse(lexiconFile.toString());
      }
      catch (SAXParseException se)
      {
        throw new Exception("Couldn't parse file "+lexiconFile+": "+se.getMessage());
      }
      Configuration lexiconconfig = handler.getConfiguration();
      Lexicon lexicon = LexiconFactory.createLexicon(lexiconconfig);

      this.lexicalautomaton = (new LexicalAutomatonBuilder(lexicon,
          log)).getLexicalAutomaton();

      if (cacheFile!=null)
      {
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(cacheFile));
        out.writeObject(this.lexicalautomaton);
        out.flush();
        out.close();
      }
    }*/
    if (grammarFile!=null)
    {
      // Grammar
      //String filename = grammarFile.getName();

      /*cacheFile = null;
      if (cacheDir!=null)
        cacheFile = new File(cacheDir, filename+".obj");*/
      /*if ((cacheFile!=null) && (cacheFile.exists()) && (cacheFile.lastModified()>grammarFile.lastModified()))
      {
        System.out.println("Reading grammar from cache "+cacheFile); //debug
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(cacheFile));
        this.parserautomaton = (ParserAutomaton)in.readObject();
        in.close();
      }
      else
      {*/
      System.out.println("Building grammar from "+grammarFile)//info

      SAXParserFactory factory = getParserFactory();

      factory.setNamespaceAware(true);

      XMLReader parser = factory.newSAXParser().getXMLReader();

      //NamespacedSAXConfigurationHandler handler = new NamespacedSAXConfigurationHandler();
      GrammarFactory grammarfactory = new GrammarFactory();
      parser.setContentHandler(grammarfactory);
      try
      {
        parser.parse(grammarFile.toString());
      }
      catch (SAXParseException se)
      {
        throw new Exception("Couldn't parse file "+lexiconFile+": "+se.getMessage());
      }

      //Configuration grammarconfig = handler.getConfiguration();
      //Grammar grammar = GrammarFactory.createGrammar(grammarconfig);
      Grammar grammar = grammarfactory.getGrammar();

      /*this.parserautomaton = (new ParserAutomatonBuilder(grammar,
          log)).getParserAutomaton();*/
      this.parserautomaton = new Automaton(grammar, log);

      /*if (cacheFile!=null)
      {
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(cacheFile));
        out.writeObject(this.parserautomaton);
        out.flush();
        out.close();
      }*/

      //}
    }
  }

  /*private void pushTextFile(File inFile) throws Exception
  {
    try
    {
      TextLocator locator = new TextLocator();

      locator.setURI(inFile.toURL().toString());
      locator.setLineNumber(1);
      locator.setColumnNumber(1);

      this.lexer.handleLocator(locator);
      this.lexer.handleStartDocument();

      LineNumberReader reader = new LineNumberReader(new InputStreamReader(new FileInputStream(inFile)));

      String line, newline = null;
      String separator = System.getProperty("line.separator");

      while (true)
      {
        if (newline==null)
          line = reader.readLine();
        else
          line = newline;

        if (line==null)
          break;

        newline = reader.readLine();

        line = (newline!=null) ? line+separator : line;

        locator.setLineNumber(reader.getLineNumber());
        locator.setColumnNumber(1);
        this.lexer.handleText(line);

        if (newline==null)
          break;
      }

      reader.close();

      this.lexer.handleEndDocument();
    }
    catch (SAXParseException se)
    {
      throw new Exception("Exception occurs during parsing file "+inFile+
                               " at line "+se.getLineNumber()+" column "+
                               se.getColumnNumber()+": "+se.getMessage());
    }

  }*/
  private void pushXMLFile(File inFile) throws Exception
  {
    SAXParserFactory parserfactory = getParserFactory();

    parserfactory.setNamespaceAware(true);

    XMLReader parser = parserfactory.newSAXParser().getXMLReader();

    /*LexicalProcessorAdapter handler = new LexicalProcessorAdapter();

    handler.setLexicalProcessor(this.lexer);
    handler.setContentHandler(serializer);

    parser.setContentHandler(handler);*/
    parser.setContentHandler(this.parser);
    try
    {
      parser.parse(inFile.toString());
    }
    catch (SAXParseException se)
    {
      throw new Exception("Exception occurs during parsing file "+inFile+" at line "+
                          se.getLineNumber()+" column "+se.getColumnNumber()+": "+se.getMessage());
    }
  }

  private void ensureDirectoryFor(File targetFile) throws Exception
  {
    File directory = new File(targetFile.getParent());

    if ((!directory.exists()) && (!directory.mkdirs()))
      throw new Exception("Unable to create directory: "+directory.getAbsolutePath());
  }

  private SAXParserFactory getParserFactory() throws Exception
  {
    if (parserFactoryImpl==null)
    {
      try
      {
        if (parserFactory==null)
          parserFactoryImpl = SAXParserFactory.newInstance();
        else
          parserFactoryImpl = (SAXParserFactory)Class.forName(parserFactory).newInstance();
      }
      catch (Exception e)
      {
        throw new Exception("Could not load parser factory: "+e.getMessage());
      }
    }

    return parserFactoryImpl;
  }

  private SAXTransformerFactory getTransformerFactory()
    throws Exception
  {
    if (transformerFactoryImpl==null)
    {
      try
      {
        if (transformerFactory==null)
          transformerFactoryImpl = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
        else
          transformerFactoryImpl =
            (SAXTransformerFactory)Class.forName(transformerFactory).newInstance();
      }
      catch (Exception e)
      {
        throw new Exception("Could not load transformer factory: "+e.getMessage());
      }
    }

    return transformerFactoryImpl;
  }

  public static void main(String[] args)
  {
    Options options = new Options();

    options.addOption(new Option(LEXICON_OPT, LEXICON_OPT_LONG, true,
                                 "specify lexicon, which should be used"));

    options.addOption(new Option(GRAMMAR_OPT, GRAMMAR_OPT_LONG, true,
                                 "specify grammar, which should be used"));

    options.addOption(new Option(IN_OPT, IN_OPT_LONG, true, "Input file"));

    options.addOption(new Option(OUT_OPT, OUT_OPT_LONG, true, "Output file"));

    CommandLine line = null;
    try
    {
      line = new PosixParser().parse(options, args);
    }
    catch (ParseException pe)
    {
      System.err.println(pe.getMessage());
      return;
    }

    if (!line.hasOption(LEXICON_OPT))
    {
      System.err.println("Lexicon is not specified");
      return;
    }

    if (!line.hasOption(GRAMMAR_OPT))
    {
      System.err.println("Grammar is not specified");
      return;
    }

    if (!line.hasOption(IN_OPT))
    {
      System.err.println("Input file is not specified");
      return;
    }

    if (!line.hasOption(OUT_OPT))
    {
      System.err.println("Output file is not specified");
      return;
    }

    Main main = new Main();
    main.setLexicon(new File(line.getOptionValue(LEXICON_OPT)));
    main.setGrammar(new File(line.getOptionValue(GRAMMAR_OPT)));

    try
    {
      main.process(new File(line.getOptionValue(IN_OPT)), new File(line.getOptionValue(OUT_OPT)));
    }
    catch (Exception e)
    {
      System.err.println(e.getMessage());
    }
  }
}
TOP

Related Classes of net.sourceforge.chaperon.adapter.cli.Main

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.