KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > corba > generator > PrintGenerator


1 /**
2  * copyright 2002 2003 Laboratoire d'Informatique Paris 6 (LIP6)
3  *
4  * This file is part of ModFact.
5  *
6  * ModFact is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * at your option) any later version.
10  *
11  * ModFact is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with ModFact; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20 package org.objectweb.modfact.corba.generator;
21
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26
27 /**
28  * An abstract implementation of a generator.
29  * This class allows the generation of any document types (Java, XML, ...).
30  * It manage the output file and the indentation into the generated file.
31  *
32  * @author Xavier Blanc, Pierre Carpentier
33  */

34 public abstract class PrintGenerator implements Generator {
35     
36     /** The output file name. */
37     protected PrintWriter JavaDoc out;
38     
39     /** The output file name. */
40     protected String JavaDoc filename = new String JavaDoc();
41
42     /** The tabulation. */
43     protected final String JavaDoc TABULATION = new String JavaDoc(" ");
44
45     /** The identation level. */
46     protected int level = 0;
47
48     /** New Line indicator. */
49     protected boolean newLine = true;
50     
51
52     /**
53      * Set the output file name.
54      * @param file The file name.
55      * @throws java.io.IOException if the file exists but is a directory rather than a regular file,
56      * does not exist but cannot be created, or cannot be opened for any other reason.
57      */

58     public void setOutput(String JavaDoc file) throws IOException JavaDoc {
59         out = new PrintWriter JavaDoc(new FileOutputStream JavaDoc(file), true);
60         filename = file;
61     }
62
63     /**
64      * Set the output file.
65      * @param file The file.
66      * @throws java.io.IOException if the file exists but is a directory rather than a regular file,
67      * does not exist but cannot be created, or cannot be opened for any other reason.
68      */

69     public void setOutput(java.io.File JavaDoc file) throws IOException JavaDoc {
70         out = new PrintWriter JavaDoc(new FileOutputStream JavaDoc(file), true);
71         filename = file.getName();
72     }
73
74     /**
75      * Set the output.
76      * @param stream The output stream.
77      */

78     public void setOutput(OutputStream JavaDoc stream) throws IOException JavaDoc {
79         out = new PrintWriter JavaDoc(stream);
80     }
81
82     /**
83      * Print the output to the global PrintWriter (replace out.print(String)).
84      * This default implementation generates no indentation.
85      * @param outputString The string to print.
86      */

87     protected void output(String JavaDoc outputString) {
88         int index = outputString.indexOf("\n");
89         if (index != -1 && index != outputString.length() - 1) {
90             // outputString contains at least '\n' which is not at the end
91
java.util.StringTokenizer JavaDoc token = new java.util.StringTokenizer JavaDoc(outputString, "\n");
92             while (token.hasMoreTokens()) {
93                 String JavaDoc next = token.nextToken();
94                 if (!token.hasMoreTokens()) {
95                     if (outputString.endsWith("\n"))
96                         outputln(next);
97                     else
98                         output(next);
99                 } else {
100                     outputln(next);
101                 }
102             }
103         } else {
104             out.print(outputString);
105             if (outputString.trim().endsWith("\n"))
106                 newLine = true;
107             else
108                 newLine = false;
109         }
110     }
111
112     /**
113      * Print a new line to the global PrintWriter (replace out.println()).
114      */

115     protected void outputln() {
116         out.println();
117         newLine = true;
118     }
119
120     /**
121      * Print the output with a new line to the global PrintWriter (replace out.println(String)).
122      * @param outputString The string to output.
123      */

124     protected void outputln(String JavaDoc outputString) {
125         output(outputString);
126         outputln();
127     }
128     
129     /**
130      * Flush the output file.
131      */

132     public void flushFile() {
133         out.flush();
134     }
135     
136 }
137
Popular Tags