1 18 package org.apache.geronimo.interop.generator; 19 20 import java.io.File ; 21 import java.io.FileOutputStream ; 22 import java.io.OutputStream ; 23 import java.io.OutputStreamWriter ; 24 import java.io.PrintWriter ; 25 26 public class JavaWriter extends CodeWriter { 27 private GenOptions genOptions; 28 private PrintWriter pw; 29 private boolean needIndent = true; 30 private int indentPos = 0; 31 private String indentStr = ""; 32 private String spaces = " "; 33 34 public JavaWriter(GenOptions genOptions, String fileName, String ext) { 35 super(genOptions, fileName, ext); 36 } 37 38 protected File getFile() 39 throws GenException { 40 File file = null; 41 GenOptions go = getGenOptions(); 42 String fileName = getFileName() + getFileExt(); 43 44 try { 45 file = new File (go.getGenSrcDir(), fileName); 46 47 if (file.exists() && !go.isOverwrite()) { 48 fileName = fileName + ".new"; 49 50 file = new File (go.getGenSrcDir(), fileName); 51 } 52 } catch (Exception ex) { 53 throw new GenException("Error: Unable to open output dir: " + go.getGenSrcDir() + ", file: " + fileName, ex); 54 } 55 56 return file; 57 } 58 59 public void openFile() 60 throws GenException { 61 OutputStream os = null; 62 63 if (file != null) { 64 return; 66 } 67 68 file = getFile(); 69 70 if (file == null) { 71 throw new GenException("Error: Unable to obtain output file."); 72 } 73 74 if (getGenOptions().isVerbose()) { 75 System.out.println("Generating: " + file); 76 } 77 78 os = null; 79 80 file.getParentFile().mkdirs(); 83 85 if (file.exists() && !file.canWrite()) { 86 throw new GenException("Error: Unable to write to file: " + file); 87 } 88 89 if (!file.exists() && !file.getParentFile().canWrite()) { 90 throw new GenException("Error: Unable to write to directory: " + file.getParentFile()); 91 } 92 93 try { 94 os = new FileOutputStream (file); 95 } catch (Exception ex) { 96 throw new GenException("Error: Unable to init output file: " + file, ex); 97 } 98 99 try { 100 pw = new PrintWriter (new OutputStreamWriter (os)); 101 } catch (Exception ex) { 102 throw new GenException("Error: Unable to init output file: " + file, ex); 103 } 104 } 105 106 public void closeFile() 107 throws GenException { 108 if (pw != null) { 109 try { 110 pw.flush(); 111 pw.close(); 112 } catch (Exception e) { 113 throw new GenException("Error: Unable to close output file: " + file, e); 114 } 115 116 pw = null; 117 } 118 119 file = null; 120 } 121 122 public void indent() { 123 indentPos += 4; 124 if (indentPos > spaces.length()) { 125 indentPos -= 4; 126 } 127 indentStr = spaces.substring(0, indentPos); 128 } 129 130 public void outdent() { 131 indentPos -= 4; 132 if (indentPos < 0) { 133 indentPos = 0; 134 } 135 indentStr = spaces.substring(0, indentPos); 136 } 137 138 public void begin() { 139 needIndent = true; 140 println("{"); 141 indent(); 142 } 143 144 public void end() { 145 outdent(); 146 needIndent = true; 147 println("}"); 148 } 149 150 public void newln() { 151 println(""); 152 needIndent = true; 153 } 154 155 public void comment(String msg) { 156 println("// " + msg); 157 } 158 159 public void println(String line) { 160 if (needIndent) { 161 needIndent = false; 162 pw.print(indentStr); 163 } 164 165 pw.println(line); 166 needIndent = true; 167 } 168 169 public void print(String line) { 170 if (needIndent) { 171 needIndent = false; 172 pw.print(indentStr); 173 } 174 175 pw.print(line); 176 } 177 } 178 | Popular Tags |