This method is used by all code generators to create new output files. If the outputDir set by -o is not present it will be created. The final filename is sensitive to the output directory and the directory where the grammar file was found. If -o is /tmp and the original grammar file was foo/t.
(Grammar g, String fileName)
| 762 | * If outputDirectory==null then write a String. |
| 763 | */ |
| 764 | public Writer getOutputFileWriter(Grammar g, String fileName) throws IOException { |
| 765 | if (outputDirectory == null) { |
| 766 | return new StringWriter(); |
| 767 | } |
| 768 | // output directory is a function of where the grammar file lives |
| 769 | // for subdir/T.g4, you get subdir here. Well, depends on -o etc... |
| 770 | File outputDir = getOutputDirectory(g.fileName); |
| 771 | File outputFile = new File(outputDir, fileName); |
| 772 | |
| 773 | if (!outputDir.exists()) { |
| 774 | outputDir.mkdirs(); |
| 775 | } |
| 776 | FileOutputStream fos = new FileOutputStream(outputFile); |
| 777 | OutputStreamWriter osw; |
| 778 | if ( grammarEncoding!=null ) { |
| 779 | osw = new OutputStreamWriter(fos, grammarEncoding); |
| 780 | } |
| 781 | else { |
| 782 | osw = new OutputStreamWriter(fos); |
| 783 | } |
| 784 | return new BufferedWriter(osw); |
| 785 | } |
| 786 | |
| 787 | public File getImportedGrammarFile(Grammar g, String fileName) { |
| 788 | File importedFile = new File(inputDirectory, fileName); |
no test coverage detected