1 | package desmoj.core.report; |
2 | |
3 | import java.io.BufferedWriter; |
4 | import java.io.FileWriter; |
5 | import java.io.IOException; |
6 | import java.io.Writer; |
7 | |
8 | /** |
9 | * FileOutput is the class all other filebased Output classes used within the |
10 | * DESMO-j framework are derived from. The basic functionality needed for all |
11 | * FileOutput objects are methods to open and close the stream used for output |
12 | * and set the two necessary types of separators for column output. Two methods |
13 | * for writing Strings into the file are supported. The name is needed for |
14 | * opening a file to identify the output channel in terms of window titles or |
15 | * file names, depending on the type of output the specific subclass relies on. |
16 | * Note that each object of this class supports the creation of several files in |
17 | * a row. You can open and close as many files as needed with one object. |
18 | * |
19 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
20 | * @author Tim Lechler |
21 | * |
22 | * Licensed under the Apache License, Version 2.0 (the "License"); |
23 | * you may not use this file except in compliance with the License. You |
24 | * may obtain a copy of the License at |
25 | * http://www.apache.org/licenses/LICENSE-2.0 |
26 | * |
27 | * Unless required by applicable law or agreed to in writing, software |
28 | * distributed under the License is distributed on an "AS IS" |
29 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
30 | * or implied. See the License for the specific language governing |
31 | * permissions and limitations under the License. |
32 | * |
33 | */ |
34 | public class FileOutput { |
35 | |
36 | /** |
37 | * The character used to indicate the end of a line. |
38 | */ |
39 | private static String eol = System.getProperty("line.separator"); // standard OS eol |
40 | |
41 | /** |
42 | * The character used to separate individual entries within a line. |
43 | */ |
44 | private static String sep = ";"; // standard String to separate entries |
45 | |
46 | /** |
47 | * The name of the file produced by this FileOutput. |
48 | */ |
49 | protected String fileName; |
50 | |
51 | /** |
52 | * The FileWriter used to open, close and write file and data. |
53 | */ |
54 | protected Writer file; |
55 | |
56 | /** |
57 | * The status of the current file. |
58 | */ |
59 | protected boolean fileOpen; |
60 | |
61 | /** |
62 | * Remembers if any data has been written to it. If not, it might as well be |
63 | * deleted. |
64 | */ |
65 | protected boolean empty; |
66 | |
67 | /** |
68 | * Constructs a FileOutput object. A FileOuput object is capable of opening |
69 | * a file using method <code>open(String fileName)</code>. Note that if |
70 | * the output stream used cannot be opened, the whole error handling is |
71 | * printed to the system's standard output stream. The 'endOfLine' String is |
72 | * set to the underlying system's String used to separate lines as given by |
73 | * the system property <code>line.separator</code>. |
74 | */ |
75 | public FileOutput() { |
76 | |
77 | fileOpen = false; // no file open yet |
78 | empty = true; // and nothing written to it either |
79 | fileName = null; // name can only be given with method open(String) |
80 | } |
81 | |
82 | /** |
83 | * Flushes the buffer and closes the file. note that if another file with |
84 | * the same name is opened and written to, it will overwrite the previous |
85 | * file on the disc without notice. All users of Output objects have to call |
86 | * <code>close()</code> to properly shut down the ouput stream after the |
87 | * last call to the printing methods. Classes overriding this method should |
88 | * put all necessary actions to properly close their file into the |
89 | * <code>close()</code> method and make a call to |
90 | * <code>super.close()</code> as their last command to finally close |
91 | * buffers and files. |
92 | */ |
93 | public void close() { |
94 | |
95 | if (!fileOpen) |
96 | return; // file not yet openend |
97 | |
98 | try { |
99 | file.flush(); |
100 | file.close(); |
101 | fileOpen = false; |
102 | empty = true; |
103 | } catch (IOException ioEx) { |
104 | System.out.println("IOException thrown : " + ioEx); |
105 | System.out |
106 | .println("description: Can't flush and close " + fileName); |
107 | System.out.println("origin : Experiment auxiliaries"); |
108 | System.out.println("location : FileOutput.close()"); |
109 | System.out.println("hint : Check access to the file and" |
110 | + " that it is not in use by some other application."); |
111 | System.out |
112 | .println("The System will not be shut down. But the file " |
113 | + fileName |
114 | + " can not be closed and may contain no data!"); |
115 | /* |
116 | * the system will not be shut down, because this may disrupt other |
117 | * programs like CoSim from Ralf Bachmann (Universtiy of Hamburg, |
118 | * germany). |
119 | */ |
120 | // System.exit(-1); // radical but no time for fileselectors now |
121 | } |
122 | |
123 | } |
124 | |
125 | /** |
126 | * Returns the currently valid String for separating lines of output. This |
127 | * is a platform dependent String and is set to the underlying system's |
128 | * String by default. |
129 | * |
130 | * @return String : The String used to separate lines |
131 | */ |
132 | public static String getEndOfLine() { |
133 | |
134 | return eol; |
135 | |
136 | } |
137 | |
138 | /** |
139 | * Returns the current fileName of this FileOutput or <code>null</code> if |
140 | * it has not been opened and named yet. |
141 | * |
142 | * @return java.lang.String : The name of the file or <code>null</code> if |
143 | * file has not been opened and named yet |
144 | */ |
145 | public String getFileName() { |
146 | |
147 | return fileName; |
148 | |
149 | } |
150 | |
151 | /** |
152 | * Returns the currently active String for separating individual entries in |
153 | * a line of output. This comes in handy, when trying to write files in a |
154 | * certain format i.e. dbf format. Data from files complying to that format |
155 | * can easily be read and imported to a variety of standard statistics |
156 | * packages for further manipulation and analysis. |
157 | * |
158 | * @return String : The String used to separate entries within a line |
159 | */ |
160 | public static String getSeparator() { |
161 | |
162 | return sep; |
163 | |
164 | } |
165 | |
166 | /** |
167 | * Returns a boolean value showing if relevant data has been written to the |
168 | * file. If only file headings have been written, this method will return |
169 | * <code>true</code> to indicate that it does not contain information and |
170 | * thus might just as well be deleted. If data has been written to it, this |
171 | * method will return <code>false</code> instead. |
172 | * |
173 | * @return boolean : Is <code>true</code> if no relevant data but the |
174 | * heading has been written to this file, <code>false</code> |
175 | * otherwise |
176 | */ |
177 | public boolean isEmpty() { |
178 | return empty; |
179 | } |
180 | |
181 | /** |
182 | * Returns the current state of the FileOutput. <code>True</code> is |
183 | * returned, if the file has been opened successfully and can be written to. |
184 | * If the file has been closed or has not been opened yet, |
185 | * <code>false</code> is returned. |
186 | * |
187 | * @return boolean : Is <code>true</code> if the file is currently open, |
188 | * <code>false</code> if not |
189 | */ |
190 | public boolean isOpen() { |
191 | |
192 | return fileOpen; |
193 | |
194 | } |
195 | |
196 | /** |
197 | * Opens a new file with the given fileName for writing data to. If no |
198 | * String is given, the default filename "unnamed_DESMOJ_file" is used. Note |
199 | * that opening a file with a name that already exists in the user's target |
200 | * directory will overwrite that existing file without notice! So be careful |
201 | * when choosing file names and make sure to use unique names. |
202 | * |
203 | * @param name |
204 | * java.lang.String : The name of the file to be created |
205 | */ |
206 | public void open(String name) { |
207 | |
208 | if (fileOpen) |
209 | return; // file already opened |
210 | |
211 | if (name != null) |
212 | fileName = name;// create the named object |
213 | else |
214 | fileName = "unnamed_DESMOJ_File"; |
215 | |
216 | // now try to create a new file in the user's standard directory |
217 | try { |
218 | file = new BufferedWriter(new FileWriter(fileName)); |
219 | |
220 | fileOpen = true; |
221 | empty = true; |
222 | } catch (IOException ioEx) { |
223 | System.out.println("IOException thrown : " + ioEx); |
224 | System.out.println("description: Can't create file " + fileName); |
225 | System.out.println("origin : While creating the Experiment " |
226 | + "auxiliaries."); |
227 | System.out |
228 | .println("location : method open() in class FileOutput."); |
229 | System.out |
230 | .println("hint : Check access to the default path and " |
231 | + "that no file of thesame name exists"); |
232 | System.out |
233 | .println("The System will not be shut down. But the file " |
234 | + fileName |
235 | + " can not be opened and may not exist as " |
236 | + "expected!"); |
237 | /* |
238 | * the system will not be shut down, because this may disrupt other |
239 | * programs like CoSim from Ralf Bachmann (Universtiy of Hamburg, |
240 | * germany). |
241 | */ |
242 | // System.exit(-1); // radical but no time for fileselectors now |
243 | } |
244 | |
245 | } |
246 | |
247 | /** |
248 | * Sets the end-of-line separator String to the given parameter value. The |
249 | * default value is the end-of-line String of the underlying platform. Use |
250 | * this method to create files of specific formats. Data from files |
251 | * complying to that format can easily be read and imported to a variety of |
252 | * standard statistics packages for further manipulation and analysis. |
253 | * |
254 | * @param eolString |
255 | * String : The String for separating lines |
256 | */ |
257 | public static void setEndOfLine(String eolString) { |
258 | |
259 | eol = eolString; |
260 | |
261 | } |
262 | |
263 | /** |
264 | * Sets the entry separator String to the given parameter value. The default |
265 | * value is ';'. This is useful, when trying to write files in a certain |
266 | * format i.e. dbf format. Data from files complying to that format can |
267 | * easily be read and imported to a variety of standard statistics packages |
268 | * for further manipulation and analysis. |
269 | * |
270 | * @param sepString |
271 | * String : The String for separating entries within a line |
272 | */ |
273 | public static void setSeparator(String sepString) { |
274 | |
275 | sep = sepString; |
276 | |
277 | } |
278 | |
279 | /** |
280 | * Writes the given String to the open file. If the given String is empty, |
281 | * the method will simply return without action. Override this method if you |
282 | * want your data to be written with specific tags as used in the |
283 | * <code>HTMLFileOutput</code> class. |
284 | * |
285 | * @param s |
286 | * java.lang.String : The String to write to the file |
287 | * @see HTMLFileOutput |
288 | */ |
289 | public void write(String s) { |
290 | if (s == null) |
291 | return; // again nulls |
292 | if (!fileOpen) |
293 | return; // file not yet openend |
294 | empty = false; // remember that something's written to file |
295 | |
296 | // try to write and handle Exception if needed |
297 | try { |
298 | file.write(s); |
299 | } catch (IOException ioEx) { |
300 | System.out.println("IOException thrown : " + ioEx); |
301 | System.out.println("description: Can't write to file " + fileName); |
302 | System.out.println("origin : Experiment auxiliaries"); |
303 | System.out.println("location : class FileOutput, method write()"); |
304 | System.out.println("hint : Check access to the file and" |
305 | + " that it is not in use by some other application."); |
306 | System.out |
307 | .println("The System will not be shut down. But it can not be " |
308 | + "written to the file " |
309 | + fileName |
310 | + ". The file may " |
311 | + "not contain all the important data!"); |
312 | /* |
313 | * the system will not be shut down, because this may disrupt other |
314 | * programs like CoSim from Ralf Bachmann (Universtiy of Hamburg, |
315 | * germany). |
316 | */ |
317 | // System.exit(-1); // radical but no time for fileselectors now |
318 | } |
319 | |
320 | } |
321 | |
322 | /** |
323 | * Writes the given String to the open file, adding a line separator to the |
324 | * end of the String written. If the string given is null or "" just a new |
325 | * line character will be written to the file. This method simply adds the |
326 | * end-of-line String to the String given and calls the |
327 | * <code>void write(String s)</code> method. |
328 | * |
329 | * @param s |
330 | * java.lang.String : The String to write to the file |
331 | */ |
332 | public void writeln(String s) { |
333 | |
334 | if ((s == null) || (s.length() == 0)) { |
335 | write(eol); |
336 | return; |
337 | } |
338 | |
339 | write(s + eol); |
340 | |
341 | } |
342 | |
343 | /** |
344 | * Writes the given String to the open file, adding a separator to the end |
345 | * of the String written. If the string given is null or "" just a new line |
346 | * character will be written to the file. This method simply adds the |
347 | * end-of-line String to the String given and calls the |
348 | * <code>void write(String s)</code> method. |
349 | * |
350 | * @param s |
351 | * java.lang.String : The String to write to the file |
352 | */ |
353 | public void writeSep(String s) { |
354 | |
355 | if ((s == null) || (s.length() == 0)) { |
356 | write(sep); |
357 | return; |
358 | } |
359 | |
360 | write(s + sep); |
361 | } |
362 | } |