Package net.sourceforge.chaperon.common

Source Code of net.sourceforge.chaperon.common.SimpleParser

/*
*  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.common;

import net.sourceforge.chaperon.build.LexicalAutomatonBuilder;
import net.sourceforge.chaperon.build.ParserAutomatonBuilder;
import net.sourceforge.chaperon.model.grammar.Grammar;
import net.sourceforge.chaperon.model.grammar.GrammarFactory;
import net.sourceforge.chaperon.model.lexicon.Lexicon;
import net.sourceforge.chaperon.model.lexicon.LexiconFactory;
import net.sourceforge.chaperon.process.LexicalAutomaton;
import net.sourceforge.chaperon.process.LexicalProcessor;
import net.sourceforge.chaperon.process.ParserAutomaton;
import net.sourceforge.chaperon.process.ParserProcessor;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.SimpleLog;

import org.xml.sax.XMLReader;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.LocatorImpl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

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

/**
* Simple example for the using of the Chaperon parser.
*
* @author <a href="mailto:stephan@apache.org">Stephan Michels </a>
* @version CVS $Id: SimpleParser.java,v 1.7 2003/12/09 19:55:52 benedikta Exp $
*/
public class SimpleParser
{
  public static void process(File lexiconFile, File grammarFile, File inFile, File outFile)
    throws Exception
  {
    // Create log
    Log log = new SimpleLog("log");

    // Create factory for SAX parser
    SAXParserFactory parserFactoryImpl = SAXParserFactory.newInstance();
    parserFactoryImpl.setNamespaceAware(true);

    // Get a SAX parser
    XMLReader xmlparser = parserFactoryImpl.newSAXParser().getXMLReader();

    // Create a lexicon model for a given lexicon file
    LexiconFactory lexiconfactory = new LexiconFactory();
    xmlparser.setContentHandler(lexiconfactory);
    xmlparser.parse(lexiconFile.toString());

    Lexicon lexicon = lexiconfactory.getLexicon();

    // Build a automaton from the lexicon model
    LexicalAutomaton lexicalautomaton =
      (new LexicalAutomatonBuilder(lexicon, log)).getLexicalAutomaton();

    // Create a processor for the lexicon
    LexicalProcessor lexer = new LexicalProcessor();
    lexer.setLog(log);
    lexer.setLexicalAutomaton(lexicalautomaton);

    // Get a SAX parser
    xmlparser = parserFactoryImpl.newSAXParser().getXMLReader();

    // Create a grammar model for a given grammar file
    GrammarFactory grammarfactory = new GrammarFactory();
    xmlparser.setContentHandler(grammarfactory);
    xmlparser.parse(grammarFile.toString());

    Grammar grammar = grammarfactory.getGrammar();

    // Build a automaton from the grammar model
    ParserAutomaton parserautomaton =
      (new ParserAutomatonBuilder(grammar, log)).getParserAutomaton();

    // Create a processor for the grammar
    ParserProcessor parser = new ParserProcessor();
    parser.setLog(log);
    parser.setParserAutomaton(parserautomaton);

    // Create factory for SAX transformer
    SAXTransformerFactory transformerFactoryImpl =
      (SAXTransformerFactory)SAXTransformerFactory.newInstance();

    // Create serializer to write the SAX stream into a file
    TransformerHandler serializer = transformerFactoryImpl.newTransformerHandler();
    serializer.setResult(new StreamResult(outFile));

    // Connect components into a pipeline
    lexer.setContentHandler(parser);
    parser.setContentHandler(serializer);

    // Push text into this pipeline
    // Create locator, which help to find possible syntax errors
    LocatorImpl locator = new LocatorImpl();
    locator.setSystemId(inFile.toURL().toString());
    locator.setLineNumber(1);
    locator.setColumnNumber(1);
    lexer.setDocumentLocator(locator);

    // Start document
    lexer.startDocument();

    // Start 'text' element, which the parser dispatch
    lexer.startElement("http://chaperon.sourceforge.net/schema/text/1.0", "text", "text",
                       new AttributesImpl());

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

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

    // Push text
    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);
      lexer.characters(line.toCharArray(), 0, line.length());

      if (newline==null)
        break;
    }

    reader.close();

    // End 'text' element
    lexer.endElement("http://chaperon.sourceforge.net/schema/text/1.0", "text", "text");

    // End document
    lexer.endDocument();
  }

  public static void main(String[] args)
  {
    File lexiconFile = new File(args[0]);
    File grammarFile = new File(args[1]);
    File inFile = new File(args[2]);
    File outFile = new File(args[3]);

    try
    {
      process(lexiconFile, grammarFile, inFile, outFile);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
}
TOP

Related Classes of net.sourceforge.chaperon.common.SimpleParser

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.