01: package antlr;
02:
03: import java.io.IOException;
04: import java.io.PrintWriter;
05: import java.util.HashMap;
06: import java.util.Map;
07:
08: public class DefaultJavaCodeGeneratorPrintWriterManager implements
09: JavaCodeGeneratorPrintWriterManager {
10: private Grammar grammar;
11: private PrintWriterWithSMAP smapOutput;
12: private PrintWriter currentOutput;
13: private Tool tool;
14: private Map sourceMaps = new HashMap();
15: private String currentFileName;
16:
17: public PrintWriter setupOutput(Tool tool, Grammar grammar)
18: throws IOException {
19: return setupOutput(tool, grammar, null);
20: }
21:
22: public PrintWriter setupOutput(Tool tool, String fileName)
23: throws IOException {
24: return setupOutput(tool, null, fileName);
25: }
26:
27: public PrintWriter setupOutput(Tool tool, Grammar grammar,
28: String fileName) throws IOException {
29: this .tool = tool;
30: this .grammar = grammar;
31:
32: if (fileName == null)
33: fileName = grammar.getClassName();
34:
35: smapOutput = new PrintWriterWithSMAP(tool
36: .openOutputFile(fileName + ".java"));
37: currentFileName = fileName + ".java";
38: currentOutput = smapOutput;
39: return currentOutput;
40: }
41:
42: public void startMapping(int sourceLine) {
43: smapOutput.startMapping(sourceLine);
44: }
45:
46: public void startSingleSourceLineMapping(int sourceLine) {
47: smapOutput.startSingleSourceLineMapping(sourceLine);
48: }
49:
50: public void endMapping() {
51: smapOutput.endMapping();
52: }
53:
54: public void finishOutput() throws IOException {
55: currentOutput.close();
56: if (grammar != null) {
57: PrintWriter smapWriter;
58: smapWriter = tool.openOutputFile(grammar.getClassName()
59: + ".smap");
60: String grammarFile = grammar.getFilename();
61: grammarFile = grammarFile.replace('\\', '/');
62: int lastSlash = grammarFile.lastIndexOf('/');
63: if (lastSlash != -1)
64: grammarFile = grammarFile.substring(lastSlash + 1);
65: smapOutput.dump(smapWriter, grammar.getClassName(),
66: grammarFile);
67: sourceMaps.put(currentFileName, smapOutput.getSourceMap());
68: }
69: currentOutput = null;
70: }
71:
72: public Map getSourceMaps() {
73: return sourceMaps;
74: }
75:
76: public int getCurrentOutputLine() {
77: return smapOutput.getCurrentOutputLine();
78: }
79: }
|