001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.adapter.cli;
010:
011: import net.sourceforge.chaperon.build.Automaton;
012: import net.sourceforge.chaperon.common.ConsoleLog;
013: import net.sourceforge.chaperon.model.grammar.*;
014: import net.sourceforge.chaperon.process.GeneralParserProcessor;
015:
016: import org.apache.commons.cli.CommandLine;
017: import org.apache.commons.cli.Option;
018: import org.apache.commons.cli.Options;
019: import org.apache.commons.cli.ParseException;
020: import org.apache.commons.cli.PosixParser;
021:
022: //import org.xml.sax.ContentHandler;
023: import org.xml.sax.SAXParseException;
024: import org.xml.sax.XMLReader;
025:
026: import java.io.File;
027:
028: import java.util.Properties;
029:
030: import javax.xml.parsers.SAXParserFactory;
031: import javax.xml.transform.OutputKeys;
032: import javax.xml.transform.sax.SAXTransformerFactory;
033: import javax.xml.transform.sax.TransformerHandler;
034: import javax.xml.transform.stream.StreamResult;
035:
036: public class Main {
037: public static final String LEXICON_OPT = "l";
038: public static final String LEXICON_OPT_LONG = "lexicon";
039: public static final String GRAMMAR_OPT = "g";
040: public static final String GRAMMAR_OPT_LONG = "grammar";
041: public static final String IN_OPT = "i";
042: public static final String IN_OPT_LONG = "in";
043: public static final String OUT_OPT = "o";
044: public static final String OUT_OPT_LONG = "out";
045: private File lexiconFile = null;
046: private File grammarFile = null;
047: private File cacheDir = null;
048: private String parserFactory = null;
049: private SAXParserFactory parserFactoryImpl = null;
050: private String transformerFactory = null;
051: private SAXTransformerFactory transformerFactoryImpl = null;
052: private String encoding = "ISO-8859-1";
053: private boolean indent = false;
054:
055: //private boolean flatten = false;
056: //private String inputtype = "text";
057: private ConsoleLog log = new ConsoleLog();
058:
059: /*private ParserHandlerAdapter parseradapter = null;
060: private ParserAutomaton parserautomaton = null;
061: private ParserProcessor parser = null;*/
062: /*private LexicalHandlerAdapter lexicaladapter = null;
063: private LexicalAutomaton lexicalautomaton = null;
064: private LexicalProcessor lexer = null;*/
065: private Automaton parserautomaton = null;
066: private GeneralParserProcessor parser = null;
067:
068: /**
069: * Set the lexicon, which should be used.
070: *
071: * @param lexiconFile Lexicon file.
072: */
073: public void setLexicon(File lexiconFile) {
074: this .lexiconFile = lexiconFile;
075: }
076:
077: /**
078: * Set the grammar, which should be used.
079: *
080: * @param grammarFile Grammar file.
081: */
082: public void setGrammar(File grammarFile) {
083: this .grammarFile = grammarFile;
084: }
085:
086: private void process(File inFile, File outFile) throws Exception {
087: if (!inFile.exists())
088: throw new Exception("File " + inFile + " doesn't exists");
089:
090: if (inFile.lastModified() > outFile.lastModified()) {
091: ensureDirectoryFor(outFile);
092: System.out.println("Parsing file " + inFile + " to "
093: + outFile); //info
094:
095: /*if ((this.parserautomaton!=null) && (this.parseradapter==null))
096: {
097: this.parseradapter = new ParserHandlerAdapter();
098: this.parseradapter.setFlatten(this.flatten);
099: if (inputtype.equalsIgnoreCase("xml"))
100: this.parseradapter.setEmbedded(true);
101: }
102:
103: if ((this.parserautomaton==null) && (this.lexicaladapter==null))
104: {
105: this.lexicaladapter = new LexicalHandlerAdapter();
106: }
107:
108: if ((this.parserautomaton!=null) && (this.parser==null))
109: {
110: this.parser = new ParserProcessor();
111: this.parser.setLog(log);
112: this.parser.setParserAutomaton(this.parserautomaton);
113: this.parser.setParserHandler(this.parseradapter);
114: }
115:
116: if (this.lexer==null)
117: {
118: this.lexer = new LexicalProcessor();
119: this.lexer.setLog(log);
120: this.lexer.setLexicalAutomaton(this.lexicalautomaton);
121: if (this.parserautomaton!=null)
122: this.lexer.setLexicalHandler(this.parser);
123: else
124: this.lexer.setLexicalHandler(this.lexicaladapter);
125: }*/
126: this .parser = new GeneralParserProcessor();
127: this .parser.setLog(log);
128: this .parser.setParserAutomaton(this .parserautomaton);
129:
130: Properties format = new Properties();
131:
132: format.put(OutputKeys.ENCODING, encoding);
133: if (indent)
134: format.put(OutputKeys.INDENT, "yes");
135:
136: format.put(OutputKeys.METHOD, "xml");
137:
138: SAXTransformerFactory factory = getTransformerFactory();
139:
140: TransformerHandler serializer = factory
141: .newTransformerHandler();
142: serializer.getTransformer().setOutputProperties(format);
143: serializer.setResult(new StreamResult(outFile));
144:
145: /*if (this.parserautomaton!=null)
146: this.parseradapter.setContentHandler(serializer);
147: else
148: this.lexicaladapter.setContentHandler(serializer);*/
149: this .parser.setContentHandler(serializer);
150:
151: /*if ( !inputtype.equalsIgnoreCase("xml"))
152: pushTextFile(inFile);
153: else*/
154: pushXMLFile(inFile);
155: }
156: }
157:
158: private void buildAutomata(File lexiconFile, File grammarFile)
159: throws Exception {
160: if ((cacheDir != null) && (!cacheDir.exists()))
161: throw new Exception("Cache directory " + cacheDir
162: + " doesn't exist");
163:
164: /*
165: // Lexicon
166: String filename = lexiconFile.getName();
167:
168: File cacheFile = null;
169: if (cacheDir!=null)
170: cacheFile = new File(cacheDir, filename+".obj");
171:
172: if ((cacheFile!=null) && (cacheFile.exists()) && (cacheFile.lastModified()>lexiconFile.lastModified()))
173: {
174: System.out.println("Reading lexicon from cache "+cacheFile); //debug
175: ObjectInputStream in = new ObjectInputStream(new FileInputStream(cacheFile));
176: this.lexicalautomaton = (LexicalAutomaton)in.readObject();
177: in.close();
178: }
179: else
180: {
181: System.out.println("Building lexicon from "+lexiconFile); //info
182: SAXParserFactory factory = getParserFactory();
183:
184: factory.setNamespaceAware(true);
185: XMLReader parser = factory.newSAXParser().getXMLReader();
186:
187: NamespacedSAXConfigurationHandler handler = new NamespacedSAXConfigurationHandler();
188:
189: parser.setContentHandler(handler);
190: try
191: {
192: parser.parse(lexiconFile.toString());
193: }
194: catch (SAXParseException se)
195: {
196: throw new Exception("Couldn't parse file "+lexiconFile+": "+se.getMessage());
197: }
198: Configuration lexiconconfig = handler.getConfiguration();
199: Lexicon lexicon = LexiconFactory.createLexicon(lexiconconfig);
200:
201: this.lexicalautomaton = (new LexicalAutomatonBuilder(lexicon,
202: log)).getLexicalAutomaton();
203:
204: if (cacheFile!=null)
205: {
206: ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(cacheFile));
207: out.writeObject(this.lexicalautomaton);
208: out.flush();
209: out.close();
210: }
211: }*/
212: if (grammarFile != null) {
213: // Grammar
214: //String filename = grammarFile.getName();
215:
216: /*cacheFile = null;
217: if (cacheDir!=null)
218: cacheFile = new File(cacheDir, filename+".obj");*/
219: /*if ((cacheFile!=null) && (cacheFile.exists()) && (cacheFile.lastModified()>grammarFile.lastModified()))
220: {
221: System.out.println("Reading grammar from cache "+cacheFile); //debug
222: ObjectInputStream in = new ObjectInputStream(new FileInputStream(cacheFile));
223: this.parserautomaton = (ParserAutomaton)in.readObject();
224: in.close();
225: }
226: else
227: {*/
228: System.out.println("Building grammar from " + grammarFile); //info
229:
230: SAXParserFactory factory = getParserFactory();
231:
232: factory.setNamespaceAware(true);
233:
234: XMLReader parser = factory.newSAXParser().getXMLReader();
235:
236: //NamespacedSAXConfigurationHandler handler = new NamespacedSAXConfigurationHandler();
237: GrammarFactory grammarfactory = new GrammarFactory();
238: parser.setContentHandler(grammarfactory);
239: try {
240: parser.parse(grammarFile.toString());
241: } catch (SAXParseException se) {
242: throw new Exception("Couldn't parse file "
243: + lexiconFile + ": " + se.getMessage());
244: }
245:
246: //Configuration grammarconfig = handler.getConfiguration();
247: //Grammar grammar = GrammarFactory.createGrammar(grammarconfig);
248: Grammar grammar = grammarfactory.getGrammar();
249:
250: /*this.parserautomaton = (new ParserAutomatonBuilder(grammar,
251: log)).getParserAutomaton();*/
252: this .parserautomaton = new Automaton(grammar, log);
253:
254: /*if (cacheFile!=null)
255: {
256: ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(cacheFile));
257: out.writeObject(this.parserautomaton);
258: out.flush();
259: out.close();
260: }*/
261:
262: //}
263: }
264: }
265:
266: /*private void pushTextFile(File inFile) throws Exception
267: {
268: try
269: {
270: TextLocator locator = new TextLocator();
271:
272: locator.setURI(inFile.toURL().toString());
273: locator.setLineNumber(1);
274: locator.setColumnNumber(1);
275:
276: this.lexer.handleLocator(locator);
277: this.lexer.handleStartDocument();
278:
279: LineNumberReader reader = new LineNumberReader(new InputStreamReader(new FileInputStream(inFile)));
280:
281: String line, newline = null;
282: String separator = System.getProperty("line.separator");
283:
284: while (true)
285: {
286: if (newline==null)
287: line = reader.readLine();
288: else
289: line = newline;
290:
291: if (line==null)
292: break;
293:
294: newline = reader.readLine();
295:
296: line = (newline!=null) ? line+separator : line;
297:
298: locator.setLineNumber(reader.getLineNumber());
299: locator.setColumnNumber(1);
300: this.lexer.handleText(line);
301:
302: if (newline==null)
303: break;
304: }
305:
306: reader.close();
307:
308: this.lexer.handleEndDocument();
309: }
310: catch (SAXParseException se)
311: {
312: throw new Exception("Exception occurs during parsing file "+inFile+
313: " at line "+se.getLineNumber()+" column "+
314: se.getColumnNumber()+": "+se.getMessage());
315: }
316:
317: }*/
318: private void pushXMLFile(File inFile) throws Exception {
319: SAXParserFactory parserfactory = getParserFactory();
320:
321: parserfactory.setNamespaceAware(true);
322:
323: XMLReader parser = parserfactory.newSAXParser().getXMLReader();
324:
325: /*LexicalProcessorAdapter handler = new LexicalProcessorAdapter();
326:
327: handler.setLexicalProcessor(this.lexer);
328: handler.setContentHandler(serializer);
329:
330: parser.setContentHandler(handler);*/
331: parser.setContentHandler(this .parser);
332: try {
333: parser.parse(inFile.toString());
334: } catch (SAXParseException se) {
335: throw new Exception("Exception occurs during parsing file "
336: + inFile + " at line " + se.getLineNumber()
337: + " column " + se.getColumnNumber() + ": "
338: + se.getMessage());
339: }
340: }
341:
342: private void ensureDirectoryFor(File targetFile) throws Exception {
343: File directory = new File(targetFile.getParent());
344:
345: if ((!directory.exists()) && (!directory.mkdirs()))
346: throw new Exception("Unable to create directory: "
347: + directory.getAbsolutePath());
348: }
349:
350: private SAXParserFactory getParserFactory() throws Exception {
351: if (parserFactoryImpl == null) {
352: try {
353: if (parserFactory == null)
354: parserFactoryImpl = SAXParserFactory.newInstance();
355: else
356: parserFactoryImpl = (SAXParserFactory) Class
357: .forName(parserFactory).newInstance();
358: } catch (Exception e) {
359: throw new Exception("Could not load parser factory: "
360: + e.getMessage());
361: }
362: }
363:
364: return parserFactoryImpl;
365: }
366:
367: private SAXTransformerFactory getTransformerFactory()
368: throws Exception {
369: if (transformerFactoryImpl == null) {
370: try {
371: if (transformerFactory == null)
372: transformerFactoryImpl = (SAXTransformerFactory) SAXTransformerFactory
373: .newInstance();
374: else
375: transformerFactoryImpl = (SAXTransformerFactory) Class
376: .forName(transformerFactory).newInstance();
377: } catch (Exception e) {
378: throw new Exception(
379: "Could not load transformer factory: "
380: + e.getMessage());
381: }
382: }
383:
384: return transformerFactoryImpl;
385: }
386:
387: public static void main(String[] args) {
388: Options options = new Options();
389:
390: options.addOption(new Option(LEXICON_OPT, LEXICON_OPT_LONG,
391: true, "specify lexicon, which should be used"));
392:
393: options.addOption(new Option(GRAMMAR_OPT, GRAMMAR_OPT_LONG,
394: true, "specify grammar, which should be used"));
395:
396: options.addOption(new Option(IN_OPT, IN_OPT_LONG, true,
397: "Input file"));
398:
399: options.addOption(new Option(OUT_OPT, OUT_OPT_LONG, true,
400: "Output file"));
401:
402: CommandLine line = null;
403: try {
404: line = new PosixParser().parse(options, args);
405: } catch (ParseException pe) {
406: System.err.println(pe.getMessage());
407: return;
408: }
409:
410: if (!line.hasOption(LEXICON_OPT)) {
411: System.err.println("Lexicon is not specified");
412: return;
413: }
414:
415: if (!line.hasOption(GRAMMAR_OPT)) {
416: System.err.println("Grammar is not specified");
417: return;
418: }
419:
420: if (!line.hasOption(IN_OPT)) {
421: System.err.println("Input file is not specified");
422: return;
423: }
424:
425: if (!line.hasOption(OUT_OPT)) {
426: System.err.println("Output file is not specified");
427: return;
428: }
429:
430: Main main = new Main();
431: main.setLexicon(new File(line.getOptionValue(LEXICON_OPT)));
432: main.setGrammar(new File(line.getOptionValue(GRAMMAR_OPT)));
433:
434: try {
435: main.process(new File(line.getOptionValue(IN_OPT)),
436: new File(line.getOptionValue(OUT_OPT)));
437: } catch (Exception e) {
438: System.err.println(e.getMessage());
439: }
440: }
441: }
|