KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javacc > jjtree > IO


1 /*
2  * Copyright © 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
3  * California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has
4  * intellectual property rights relating to technology embodied in the product
5  * that is described in this document. In particular, and without limitation,
6  * these intellectual property rights may include one or more of the U.S.
7  * patents listed at http://www.sun.com/patents and one or more additional
8  * patents or pending patent applications in the U.S. and in other countries.
9  * U.S. Government Rights - Commercial software. Government users are subject
10  * to the Sun Microsystems, Inc. standard license agreement and applicable
11  * provisions of the FAR and its supplements. Use is subject to license terms.
12  * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
13  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries. This
14  * product is covered and controlled by U.S. Export Control laws and may be
15  * subject to the export or import laws in other countries. Nuclear, missile,
16  * chemical biological weapons or nuclear maritime end uses or end users,
17  * whether direct or indirect, are strictly prohibited. Export or reexport
18  * to countries subject to U.S. embargo or to entities identified on U.S.
19  * export exclusion lists, including, but not limited to, the denied persons
20  * and specially designated nationals lists is strictly prohibited.
21  */

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 JavaDoc ifn;
32   private String JavaDoc 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 JavaDoc getInputFileName()
46   {
47     return ifn;
48   }
49
50   Reader getIn()
51   {
52     return in;
53   }
54
55   String JavaDoc 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 JavaDoc s)
77   {
78     out.print(s);
79   }
80
81   void println(String JavaDoc 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 JavaDoc create_output_file_name(String JavaDoc i) {
103     String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc ne) { // Should never happen
146
throw new JJTreeIOException(ne.toString());
147     } catch (SecurityException JavaDoc 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 /*end*/
168
Popular Tags