1 8 9 package net.sourceforge.chaperon.common; 10 11 import net.sourceforge.chaperon.build.LexicalAutomatonBuilder; 12 import net.sourceforge.chaperon.build.ParserAutomatonBuilder; 13 import net.sourceforge.chaperon.model.grammar.Grammar; 14 import net.sourceforge.chaperon.model.grammar.GrammarFactory; 15 import net.sourceforge.chaperon.model.lexicon.Lexicon; 16 import net.sourceforge.chaperon.model.lexicon.LexiconFactory; 17 import net.sourceforge.chaperon.process.LexicalAutomaton; 18 import net.sourceforge.chaperon.process.LexicalProcessor; 19 import net.sourceforge.chaperon.process.ParserAutomaton; 20 import net.sourceforge.chaperon.process.ParserProcessor; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.impl.SimpleLog; 24 25 import org.xml.sax.XMLReader ; 26 import org.xml.sax.helpers.AttributesImpl ; 27 import org.xml.sax.helpers.LocatorImpl ; 28 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.InputStreamReader ; 32 import java.io.LineNumberReader ; 33 34 import javax.xml.parsers.SAXParserFactory ; 35 import javax.xml.transform.sax.SAXTransformerFactory ; 36 import javax.xml.transform.sax.TransformerHandler ; 37 import javax.xml.transform.stream.StreamResult ; 38 39 45 public class SimpleParser 46 { 47 public static void process(File lexiconFile, File grammarFile, File inFile, File outFile) 48 throws Exception 49 { 50 Log log = new SimpleLog("log"); 52 53 SAXParserFactory parserFactoryImpl = SAXParserFactory.newInstance(); 55 parserFactoryImpl.setNamespaceAware(true); 56 57 XMLReader xmlparser = parserFactoryImpl.newSAXParser().getXMLReader(); 59 60 LexiconFactory lexiconfactory = new LexiconFactory(); 62 xmlparser.setContentHandler(lexiconfactory); 63 xmlparser.parse(lexiconFile.toString()); 64 65 Lexicon lexicon = lexiconfactory.getLexicon(); 66 67 LexicalAutomaton lexicalautomaton = 69 (new LexicalAutomatonBuilder(lexicon, log)).getLexicalAutomaton(); 70 71 LexicalProcessor lexer = new LexicalProcessor(); 73 lexer.setLog(log); 74 lexer.setLexicalAutomaton(lexicalautomaton); 75 76 xmlparser = parserFactoryImpl.newSAXParser().getXMLReader(); 78 79 GrammarFactory grammarfactory = new GrammarFactory(); 81 xmlparser.setContentHandler(grammarfactory); 82 xmlparser.parse(grammarFile.toString()); 83 84 Grammar grammar = grammarfactory.getGrammar(); 85 86 ParserAutomaton parserautomaton = 88 (new ParserAutomatonBuilder(grammar, log)).getParserAutomaton(); 89 90 ParserProcessor parser = new ParserProcessor(); 92 parser.setLog(log); 93 parser.setParserAutomaton(parserautomaton); 94 95 SAXTransformerFactory transformerFactoryImpl = 97 (SAXTransformerFactory )SAXTransformerFactory.newInstance(); 98 99 TransformerHandler serializer = transformerFactoryImpl.newTransformerHandler(); 101 serializer.setResult(new StreamResult (outFile)); 102 103 lexer.setContentHandler(parser); 105 parser.setContentHandler(serializer); 106 107 LocatorImpl locator = new LocatorImpl (); 110 locator.setSystemId(inFile.toURL().toString()); 111 locator.setLineNumber(1); 112 locator.setColumnNumber(1); 113 lexer.setDocumentLocator(locator); 114 115 lexer.startDocument(); 117 118 lexer.startElement("http://chaperon.sourceforge.net/schema/text/1.0", "text", "text", 120 new AttributesImpl ()); 121 122 LineNumberReader reader = 123 new LineNumberReader (new InputStreamReader (new FileInputStream (inFile))); 124 125 String line; 126 String newline = null; 127 String separator = System.getProperty("line.separator"); 128 129 while (true) 131 { 132 if (newline==null) 133 line = reader.readLine(); 134 else 135 line = newline; 136 137 if (line==null) 138 break; 139 140 newline = reader.readLine(); 141 142 line = (newline!=null) ? (line+separator) : line; 143 144 locator.setLineNumber(reader.getLineNumber()); 145 locator.setColumnNumber(1); 146 lexer.characters(line.toCharArray(), 0, line.length()); 147 148 if (newline==null) 149 break; 150 } 151 152 reader.close(); 153 154 lexer.endElement("http://chaperon.sourceforge.net/schema/text/1.0", "text", "text"); 156 157 lexer.endDocument(); 159 } 160 161 public static void main(String [] args) 162 { 163 File lexiconFile = new File (args[0]); 164 File grammarFile = new File (args[1]); 165 File inFile = new File (args[2]); 166 File outFile = new File (args[3]); 167 168 try 169 { 170 process(lexiconFile, grammarFile, inFile, outFile); 171 } 172 catch (Exception e) 173 { 174 e.printStackTrace(); 175 } 176 } 177 } 178 | Popular Tags |