// 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();
}