Package net.sourceforge.chaperon.cmdline

Source Code of net.sourceforge.chaperon.cmdline.CmdLineParser

/*
*  Simple command-line driver for the chaperon parser.
*  See http://www.sourceforge.net/projects/chaperon
*  Copyright (C) Bertrand Delacretaz, www.codeconsult.ch.
*  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.cmdline;

import java.io.OutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.OutputStreamWriter;

import org.apache.xml.serialize.Method;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;

import org.apache.xerces.parsers.SAXParser;

import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource;

import net.sourceforge.chaperon.grammar.Grammar;
import net.sourceforge.chaperon.grammar.SyntaxErrorException;
import net.sourceforge.chaperon.grammar.generator.SAXGrammarGenerator;
import net.sourceforge.chaperon.parser.generator.ParserTableGenerator;
import net.sourceforge.chaperon.parser.ParserTable;
import net.sourceforge.chaperon.parser.Parser;
//import net.sourceforge.chaperon.parser.CompressedDocument;
import net.sourceforge.chaperon.parser.output.EventQueue;
import net.sourceforge.chaperon.parser.output.SAXEventAdapter;

/**
* Simple command-line driver for the chaperon parser
*
* @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
* @version $Revision: 1.4 $
*/
public class CmdLineParser
{

  /**
   * Parse grammarFile and use its grammar to parse inputFile.
   * Write the result to os.
   * Does not store the compiled grammar, recompiles it every time.
   *
   * @param grammarFile Grammar file
   * @param inputFile File to parse
   * @param os Output stream for the output
   *
   * @throws Exception Exception
   */
  public CmdLineParser(File grammarFile, File inputFile,
                       OutputStream os) throws Exception
  {
    final ParserTable pt = parseGrammar(grammarFile);
    final EventQueue queue = parseInput(pt, inputFile);

    dumpDocument(queue, os);
  }

  /**
   * Parse supplied grammarFile.
   *
   * @param grammarFile Grammar file
   *
   * @return Parser table
   *
   * @throws Exception Exception, if an error occured while
   *                   generating the parser table
   */
  private ParserTable parseGrammar(File grammarFile) throws Exception
  {
    final SAXParser parser = new SAXParser();
    final SAXGrammarGenerator gg = new SAXGrammarGenerator();

    parser.setContentHandler(gg);

    info("parsing grammar file " + grammarFile.getName() + "...");
    parser.parse(grammarFile.getAbsolutePath());

    info("building parser table...");
    final Grammar g = gg.getGrammar();

    if (g == null)
    {
      throw new Exception("no Grammar was generated while parsing grammar file");
    }
    final ParserTableGenerator ptg = new ParserTableGenerator(g);

    return ptg.getParserTable();
  }

  /**
   * Parse supplied inputFile using supplied ParserTable
   * (compiled grammar)
   *
   * @param pt Parser table
   * @param inputFile File to parse
   *
   * @return The output document from the parser
   *
   * @throws Exception Exception, if an error occured while
   *                   parsing the text
   */
  private EventQueue parseInput(ParserTable pt,
                                File inputFile) throws Exception
  {
    info("parsing input file " + inputFile.getName() + "...");
    final Parser p = new Parser();

    return p.parse(pt, new FileInputStream(inputFile));
  }

  /**
   * Dump supplied Events to supplied OutputStream
   *
   * @param queue Events, which the parser generates
   * @param os Output stream for the output
   *
   * @throws Exception Exception, if an error occured while
   *                   generating the dump
   */
  private void dumpDocument(EventQueue queue,
                            OutputStream os) throws Exception
  {
    info("dumping parsed XML document...");
    final String encoding = "iso-8859-1";
    final OutputFormat format = new OutputFormat(Method.XML, encoding, true);

    format.setIndenting(true);
    format.setIndent(1);

    final OutputStreamWriter osw = new OutputStreamWriter(os);
    final XMLSerializer xmls = new XMLSerializer(osw, format);

    SAXEventAdapter adapter = new SAXEventAdapter(xmls.asContentHandler());
    queue.fireEvents(adapter);
    os.flush();
  }

  /**
   * Trivial logging mechanism
   *
   * @param msg Message
   */
  protected static void warn(String msg)
  {
    System.err.println("Chaperon CmdLineParser WARNING : " + msg);
  }

  /**
   * Trivial info mechanism
   *
   * @param msg Message
   */
  protected static void info(String msg)
  {
    System.err.println("Chaperon CmdLineParser: " + msg);
  }

  /**
   * Entry point to parser from the command-line.
   * Compiles the given grammar file and runs the parser on given
   * input file. Output goes to stdout unless an output filename
   * is specified
   *
   * @param args Cmdline arguments
   *
   * @throws Exception Exception
   */
  public static void main(String args[]) throws Exception
  {
    if (args.length < 2)
    {
      warn("usage: CmdLineParser <grammarFile> <inputFile> [outputFile]");
      System.exit(1);
    }

    final String grammarFile = args[0];
    final String inputFile = args[1];
    final String outputFile = (args.length > 2) ? args[2] : null;

    info("using grammar file: " + grammarFile);
    info("using input file: " + grammarFile);
    info("using output file: "
         + ((outputFile == null) ? "stdout" : outputFile));

    OutputStream os = System.out;

    if (outputFile != null)
    {
      os = new FileOutputStream(outputFile);
    }

    new CmdLineParser(new File(grammarFile), new File(inputFile), os);
    info("all done.");
  }
}
TOP

Related Classes of net.sourceforge.chaperon.cmdline.CmdLineParser

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.