1 22 23 package org.javacc.jjtree; 24 25 import java.io.*; 26 27 import org.javacc.parser.JavaCCGlobals; 28 29 final class IO 30 { 31 private String ifn; 32 private String ofn; 33 private Reader in; 34 private PrintWriter out; 35 private PrintStream msg; 36 private PrintStream err; 37 38 IO() 39 { 40 ifn = "<uninitialized input>"; 41 msg = System.out; 42 err = System.err; 43 } 44 45 String getInputFileName() 46 { 47 return ifn; 48 } 49 50 Reader getIn() 51 { 52 return in; 53 } 54 55 String getOutputFileName() 56 { 57 return ofn; 58 } 59 60 PrintWriter getOut() 61 { 62 return out; 63 } 64 65 PrintStream getMsg() 66 { 67 return msg; 68 } 69 70 PrintStream getErr() 71 { 72 return err; 73 } 74 75 76 void print(String s) 77 { 78 out.print(s); 79 } 80 81 void println(String s) 82 { 83 out.println(s); 84 } 85 86 void println() 87 { 88 out.println(); 89 } 90 91 92 void closeAll() 93 { 94 if (out != null) out.close(); 95 if (msg != null) msg.flush(); 96 if (err != null) err.flush(); 97 } 98 99 100 101 102 private String create_output_file_name(String i) { 103 String o = JJTreeOptions.getOutputFile(); 104 105 if (o.equals("")) { 106 int s = i.lastIndexOf(File.separatorChar); 107 if (s >= 0) { 108 i = i.substring(s + 1); 109 } 110 111 int di = i.lastIndexOf('.'); 112 if (di == -1) { 113 o = i + ".jj"; 114 } else { 115 String suffix = i.substring(di); 116 if (suffix.equals(".jj")) { 117 o = i + ".jj"; 118 } else { 119 o = i.substring(0, di) + ".jj"; 120 } 121 } 122 } 123 124 return o; 125 } 126 127 128 void setInput(String fn) throws JJTreeIOException 129 { 130 try { 131 File fp = new File(fn); 132 if (!fp.exists()) { 133 throw new JJTreeIOException("File " + fn + " not found."); 134 } 135 if (fp.isDirectory()) { 136 throw new JJTreeIOException(fn + " is a directory. Please use a valid file name."); 137 } 138 if (org.javacc.parser.JavaCCGlobals.isGeneratedBy("JJTree", fn)) { 139 throw new JJTreeIOException(fn + " was generated by jjtree. Cannot run jjtree again."); 140 } 141 ifn = fp.getPath(); 142 143 in = new FileReader(ifn); 144 145 } catch (NullPointerException ne) { throw new JJTreeIOException(ne.toString()); 147 } catch (SecurityException se) { 148 throw new JJTreeIOException("Security violation while trying to open " + fn); 149 } catch (FileNotFoundException e) { 150 throw new JJTreeIOException("File " + fn + " not found."); 151 } catch (IOException ioe) { 152 throw new JJTreeIOException(ioe.toString()); 153 } 154 155 try { 156 JavaCCGlobals.createOutputDir(JJTreeOptions.getJJTreeOutputDirectory()); 157 File ofile = new File(JJTreeOptions.getJJTreeOutputDirectory(), create_output_file_name(ifn)); 158 ofn = ofile.toString(); 159 out = new PrintWriter(new FileWriter(ofile)); 160 } catch (IOException fnf) { 161 throw new JJTreeIOException("Can't create output file " + ofn); 162 } 163 } 164 165 } 166 167 168 | Popular Tags |