KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * copyright 2002 2004 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.jmi.generator;
21
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25
26 /**
27  * @author Xavier Blanc - Pierre Carpentier
28  *
29  */

30 public abstract class PrintGenerator implements Generator {
31
32     /** The output PrintWriter. */
33     protected PrintWriter JavaDoc out;
34     
35     /** The indentation level. */
36     protected int level;
37     
38     /** The tabulation in space characters. */
39     protected String JavaDoc TABULATION = " ";
40     
41     /** Flag for a new line. */
42     protected boolean newLine = true;
43     
44     /**
45      * Default Constructor.
46      */

47     public PrintGenerator() {
48         level = 0;
49     }
50     
51     /**
52      * Set the output stream.
53      * @param stream The output stream.
54      */

55     public void setOutput(OutputStream JavaDoc stream) throws IOException JavaDoc {
56         out = new PrintWriter JavaDoc(stream);
57     }
58     
59     /**
60      * Terminate the current line by writing the line separator string.
61      */

62     public void outputln()
63     {
64         out.println();
65         newLine = true;
66     }
67     
68     /**
69      * Print a String and then terminate the line.
70      * @param output The String to output.
71      */

72     public void outputln(String JavaDoc output)
73     {
74         output(output);
75         outputln();
76     }
77     
78     /**
79      * Print the output to the global PrintWriter (replace out.print(String)).
80      * @param out The string to print.
81      */

82     protected void output(String JavaDoc outputString) {
83         int index = outputString.indexOf("\n");
84         if (index != -1 && index != outputString.length() - 1) {
85             // outputString contains at least '\n' which is not at the end
86
java.util.StringTokenizer JavaDoc token = new java.util.StringTokenizer JavaDoc(outputString, "\n");
87             while (token.hasMoreTokens()) {
88                 String JavaDoc next = token.nextToken();
89                 if (!token.hasMoreTokens()) {
90                     if (outputString.endsWith("\n"))
91                         out.println(next);
92                     else
93                         out.print(next);
94                 } else {
95                     out.println(next);
96                 }
97             }
98         } else {
99             if (outputString.trim().startsWith("}"))
100                 level--;
101             if (newLine) {
102                 for (int j = 0; j < level; j++)
103                     out.print(TABULATION);
104             }
105             if (outputString.trim().startsWith("}")) {
106                 out.print(outputString);
107                 if (outputString.trim().endsWith("{"))
108                     level++;
109             } else {
110                 out.print(outputString);
111                 for (int i = 0; i < outputString.length(); i++) {
112                     String JavaDoc current = String.valueOf(outputString.charAt(i));
113                     if (current.equals("{"))
114                         level++;
115                     else if (current.equals("}"))
116                         level--;
117                 }
118             }
119             if (outputString.trim().endsWith("\n"))
120                 newLine = true;
121             else
122                 newLine = false;
123         }
124     }
125     
126     /**
127      * Flush the output file.
128      */

129     public void flushFile() {
130         out.flush();
131     }
132     
133     /**
134      * Annotation processing.
135      * @param annotation_ The annotation to print.
136      */

137     public void annotationTemplate(String JavaDoc annotation_) {
138         if (annotation_.trim().length() > 0) {
139             String JavaDoc annotation = "/** \n" + replaceAll(annotation_.trim(), "\\\\n", "\n") + "\n/";
140             annotation = replaceAll(annotation, "\\\\s", " ");
141             StringBuffer JavaDoc margin = new StringBuffer JavaDoc();
142             for (int i = 0; i < level; i++)
143                 margin.append(TABULATION);
144             annotation = replaceAll(annotation, "\n", "\n" + margin.toString() + " *");
145             outputln(annotation);
146         }
147     }
148     
149
150     /**
151      * Replace all the occurences of the given string with the given replacement.
152      * @param original The original String to modify.
153      * @param to_replace The substring of the string to search and replace.
154      * @param new_value The replacement String.
155      * @return The modified String.
156      */

157     public static String JavaDoc replaceAll(String JavaDoc original, String JavaDoc to_replace, String JavaDoc new_value) {
158         StringBuffer JavaDoc newString = new StringBuffer JavaDoc();
159         int indexBegin = 0;
160         int index = original.indexOf(to_replace, indexBegin);
161         while (index != -1) {
162             newString.append(original.substring(indexBegin, index));
163             newString.append(new_value);
164             indexBegin = index + to_replace.length();
165             index = original.indexOf(to_replace, indexBegin);
166         }
167         newString.append(original.substring(indexBegin));
168         return newString.toString();
169     }
170     
171 }
172
Popular Tags