1 8 9 package net.sourceforge.chaperon.adapter.cli; 10 11 import net.sourceforge.chaperon.build.Automaton; 12 import net.sourceforge.chaperon.common.ConsoleLog; 13 import net.sourceforge.chaperon.model.grammar.*; 14 import net.sourceforge.chaperon.process.GeneralParserProcessor; 15 16 import org.apache.commons.cli.CommandLine; 17 import org.apache.commons.cli.Option; 18 import org.apache.commons.cli.Options; 19 import org.apache.commons.cli.ParseException; 20 import org.apache.commons.cli.PosixParser; 21 22 import org.xml.sax.SAXParseException ; 24 import org.xml.sax.XMLReader ; 25 26 import java.io.File ; 27 28 import java.util.Properties ; 29 30 import javax.xml.parsers.SAXParserFactory ; 31 import javax.xml.transform.OutputKeys ; 32 import javax.xml.transform.sax.SAXTransformerFactory ; 33 import javax.xml.transform.sax.TransformerHandler ; 34 import javax.xml.transform.stream.StreamResult ; 35 36 public class Main 37 { 38 public static final String LEXICON_OPT = "l"; 39 public static final String LEXICON_OPT_LONG = "lexicon"; 40 public static final String GRAMMAR_OPT = "g"; 41 public static final String GRAMMAR_OPT_LONG = "grammar"; 42 public static final String IN_OPT = "i"; 43 public static final String IN_OPT_LONG = "in"; 44 public static final String OUT_OPT = "o"; 45 public static final String OUT_OPT_LONG = "out"; 46 private File lexiconFile = null; 47 private File grammarFile = null; 48 private File cacheDir = null; 49 private String parserFactory = null; 50 private SAXParserFactory parserFactoryImpl = null; 51 private String transformerFactory = null; 52 private SAXTransformerFactory transformerFactoryImpl = null; 53 private String encoding = "ISO-8859-1"; 54 private boolean indent = false; 55 56 private ConsoleLog log = new ConsoleLog(); 59 60 63 66 private Automaton parserautomaton = null; 67 private GeneralParserProcessor parser = null; 68 69 74 public void setLexicon(File lexiconFile) 75 { 76 this.lexiconFile = lexiconFile; 77 } 78 79 84 public void setGrammar(File grammarFile) 85 { 86 this.grammarFile = grammarFile; 87 } 88 89 private void process(File inFile, File outFile) throws Exception 90 { 91 if (!inFile.exists()) 92 throw new Exception ("File "+inFile+" doesn't exists"); 93 94 if (inFile.lastModified()>outFile.lastModified()) 95 { 96 ensureDirectoryFor(outFile); 97 System.out.println("Parsing file "+inFile+" to "+outFile); 99 130 this.parser = new GeneralParserProcessor(); 131 this.parser.setLog(log); 132 this.parser.setParserAutomaton(this.parserautomaton); 133 134 Properties format = new Properties (); 135 136 format.put(OutputKeys.ENCODING, encoding); 137 if (indent) 138 format.put(OutputKeys.INDENT, "yes"); 139 140 format.put(OutputKeys.METHOD, "xml"); 141 142 SAXTransformerFactory factory = getTransformerFactory(); 143 144 TransformerHandler serializer = factory.newTransformerHandler(); 145 serializer.getTransformer().setOutputProperties(format); 146 serializer.setResult(new StreamResult (outFile)); 147 148 152 this.parser.setContentHandler(serializer); 153 154 157 pushXMLFile(inFile); 158 } 159 } 160 161 private void buildAutomata(File lexiconFile, File grammarFile) 162 throws Exception 163 { 164 if ((cacheDir!=null) && (!cacheDir.exists())) 165 throw new Exception ("Cache directory "+cacheDir+" doesn't exist"); 166 167 215 if (grammarFile!=null) 216 { 217 220 223 232 System.out.println("Building grammar from "+grammarFile); 234 SAXParserFactory factory = getParserFactory(); 235 236 factory.setNamespaceAware(true); 237 238 XMLReader parser = factory.newSAXParser().getXMLReader(); 239 240 GrammarFactory grammarfactory = new GrammarFactory(); 242 parser.setContentHandler(grammarfactory); 243 try 244 { 245 parser.parse(grammarFile.toString()); 246 } 247 catch (SAXParseException se) 248 { 249 throw new Exception ("Couldn't parse file "+lexiconFile+": "+se.getMessage()); 250 } 251 252 Grammar grammar = grammarfactory.getGrammar(); 255 256 258 this.parserautomaton = new Automaton(grammar, log); 259 260 267 268 } 270 } 271 272 324 private void pushXMLFile(File inFile) throws Exception 325 { 326 SAXParserFactory parserfactory = getParserFactory(); 327 328 parserfactory.setNamespaceAware(true); 329 330 XMLReader parser = parserfactory.newSAXParser().getXMLReader(); 331 332 338 parser.setContentHandler(this.parser); 339 try 340 { 341 parser.parse(inFile.toString()); 342 } 343 catch (SAXParseException se) 344 { 345 throw new Exception ("Exception occurs during parsing file "+inFile+" at line "+ 346 se.getLineNumber()+" column "+se.getColumnNumber()+": "+se.getMessage()); 347 } 348 } 349 350 private void ensureDirectoryFor(File targetFile) throws Exception 351 { 352 File directory = new File (targetFile.getParent()); 353 354 if ((!directory.exists()) && (!directory.mkdirs())) 355 throw new Exception ("Unable to create directory: "+directory.getAbsolutePath()); 356 } 357 358 private SAXParserFactory getParserFactory() throws Exception 359 { 360 if (parserFactoryImpl==null) 361 { 362 try 363 { 364 if (parserFactory==null) 365 parserFactoryImpl = SAXParserFactory.newInstance(); 366 else 367 parserFactoryImpl = (SAXParserFactory )Class.forName(parserFactory).newInstance(); 368 } 369 catch (Exception e) 370 { 371 throw new Exception ("Could not load parser factory: "+e.getMessage()); 372 } 373 } 374 375 return parserFactoryImpl; 376 } 377 378 private SAXTransformerFactory getTransformerFactory() 379 throws Exception 380 { 381 if (transformerFactoryImpl==null) 382 { 383 try 384 { 385 if (transformerFactory==null) 386 transformerFactoryImpl = (SAXTransformerFactory )SAXTransformerFactory.newInstance(); 387 else 388 transformerFactoryImpl = 389 (SAXTransformerFactory )Class.forName(transformerFactory).newInstance(); 390 } 391 catch (Exception e) 392 { 393 throw new Exception ("Could not load transformer factory: "+e.getMessage()); 394 } 395 } 396 397 return transformerFactoryImpl; 398 } 399 400 public static void main(String [] args) 401 { 402 Options options = new Options(); 403 404 options.addOption(new Option(LEXICON_OPT, LEXICON_OPT_LONG, true, 405 "specify lexicon, which should be used")); 406 407 options.addOption(new Option(GRAMMAR_OPT, GRAMMAR_OPT_LONG, true, 408 "specify grammar, which should be used")); 409 410 options.addOption(new Option(IN_OPT, IN_OPT_LONG, true, "Input file")); 411 412 options.addOption(new Option(OUT_OPT, OUT_OPT_LONG, true, "Output file")); 413 414 CommandLine line = null; 415 try 416 { 417 line = new PosixParser().parse(options, args); 418 } 419 catch (ParseException pe) 420 { 421 System.err.println(pe.getMessage()); 422 return; 423 } 424 425 if (!line.hasOption(LEXICON_OPT)) 426 { 427 System.err.println("Lexicon is not specified"); 428 return; 429 } 430 431 if (!line.hasOption(GRAMMAR_OPT)) 432 { 433 System.err.println("Grammar is not specified"); 434 return; 435 } 436 437 if (!line.hasOption(IN_OPT)) 438 { 439 System.err.println("Input file is not specified"); 440 return; 441 } 442 443 if (!line.hasOption(OUT_OPT)) 444 { 445 System.err.println("Output file is not specified"); 446 return; 447 } 448 449 Main main = new Main(); 450 main.setLexicon(new File (line.getOptionValue(LEXICON_OPT))); 451 main.setGrammar(new File (line.getOptionValue(GRAMMAR_OPT))); 452 453 try 454 { 455 main.process(new File (line.getOptionValue(IN_OPT)), new File (line.getOptionValue(OUT_OPT))); 456 } 457 catch (Exception e) 458 { 459 System.err.println(e.getMessage()); 460 } 461 } 462 } 463 | Popular Tags |