1 19 package gcc.generator; 20 21 import gcc.generator.CodeWriter; 22 import gcc.generator.GenOptions; 23 24 import java.io.File; 25 import java.io.FileOutputStream; 26 import java.io.OutputStream; 27 import java.io.OutputStreamWriter; 28 import java.io.PrintWriter; 29 30 public class JavaWriter extends CodeWriter 31 { 32 protected GenOptions _genOptions; 33 34 protected PrintWriter _pw; 35 36 protected boolean _needIndent = true; 37 protected int _indentPos = 0; 38 protected String _indentStr = ""; 39 protected String _spaces = " "; 40 41 public JavaWriter( GenOptions genOptions, String fileName, String ext ) 42 { 43 super( genOptions, fileName, ext ); 44 } 45 46 protected File getFile() 47 throws GenException 48 { 49 File file = null; 50 GenOptions go = getGenOptions(); 51 String fileName = getFileName() + getFileExt(); 52 53 try 54 { 55 file = new File( go.getGenDir(), fileName ); 56 57 if (file.exists() && !go.isOverwrite ()) 58 { 59 fileName = fileName + ".new"; 60 61 file = new File( go.getGenDir(), fileName ); 62 } 63 } 64 catch( Exception ex ) 65 { 66 throw new GenException( "Error: Unable to open output dir: " + go.getGenDir() + ", file: " + fileName, ex ); 67 } 68 69 return file; 70 } 71 72 public void openFile() 73 throws GenException 74 { 75 OutputStream os = null; 76 77 if (_file != null) 78 { 79 return; 81 } 82 83 _file = getFile(); 84 85 if (_file == null) 86 { 87 throw new GenException( "Error: Unable to obtain output file." ); 88 } 89 90 if (getGenOptions().isVerbose()) 91 { 92 System.out.println( "Generating: " + _file ); 93 } 94 95 os = null; 96 97 _file.getParentFile().mkdirs(); 100 102 if (_file.exists() && !_file.canWrite()) 103 { 104 throw new GenException( "Error: Unable to write to file: " + _file ); 105 } 106 107 if (!_file.exists() && !_file.getParentFile().canWrite()) 108 { 109 throw new GenException( "Error: Unable to write to directory: " + _file.getParentFile() ); 110 } 111 112 try 113 { 114 os = new FileOutputStream( _file ); 115 } 116 catch( Exception ex ) 117 { 118 throw new GenException( "Error: Unable to init output file: " + _file, ex ); 119 } 120 121 try 122 { 123 _pw = new PrintWriter(new OutputStreamWriter(os)); 124 } 125 catch( Exception ex ) 126 { 127 throw new GenException( "Error: Unable to init output file: " + _file, ex ); 128 } 129 } 130 131 public void closeFile() 132 throws GenException 133 { 134 if (_pw != null) 135 { 136 try 137 { 138 _pw.flush(); 139 _pw.close(); 140 } 141 catch( Exception e ) 142 { 143 throw new GenException( "Error: Unable to close output file: " + _file, e ); 144 } 145 146 _pw = null; 147 } 148 149 _file = null; 150 } 151 152 public void indent() 153 { 154 _indentPos += 4; 155 if (_indentPos > _spaces.length()) 156 { 157 _indentPos -= 4; 158 } 159 _indentStr = _spaces.substring( 0, _indentPos ); 160 } 161 162 public void outdent() 163 { 164 _indentPos -= 4; 165 if (_indentPos < 0) 166 { 167 _indentPos = 0; 168 } 169 _indentStr = _spaces.substring( 0, _indentPos ); 170 } 171 172 public void begin() 173 { 174 _needIndent = true; 175 println( "{" ); 176 indent(); 177 } 178 179 public void end() 180 { 181 outdent(); 182 _needIndent = true; 183 println( "}" ); 184 } 185 186 public void newln() 187 { 188 println( "" ); 189 _needIndent = true; 190 } 191 192 public void comment( String msg ) 193 { 194 println( "// " + msg ); 195 } 196 197 public void println( String line ) 198 { 199 if (_needIndent) 200 { 201 _needIndent = false; 202 _pw.print( _indentStr ); 203 } 204 205 _pw.println( line ); 206 _needIndent = true; 207 } 208 209 public void print( String line ) 210 { 211 if (_needIndent) 212 { 213 _needIndent = false; 214 _pw.print( _indentStr ); 215 } 216 217 _pw.print( line ); 218 } 219 } 220 | Popular Tags |