KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
23  * An abstract implementation of a generator.
24  * This class allows the generation of files organized with brackets ('{' and '}'),
25  * for example, the Java class files.
26  *
27  * @author Pierre Carpentier
28  */

29 public abstract class BracketGenerator extends PrintGenerator {
30
31     /**
32      * Print the output to the global PrintWriter (replace out.print(String)).
33      * @param out The string to print.
34      */

35     protected void output(String JavaDoc outputString) {
36         int index = outputString.indexOf("\n");
37         if (index != -1 && index != outputString.length() - 1) {
38             // outputString contains at least '\n' which is not at the end
39
java.util.StringTokenizer JavaDoc token = new java.util.StringTokenizer JavaDoc(outputString, "\n");
40             while (token.hasMoreTokens()) {
41                 String JavaDoc next = token.nextToken();
42                 if (!token.hasMoreTokens()) {
43                     if (outputString.endsWith("\n"))
44                         outputln(next);
45                     else
46                         output(next);
47                 } else {
48                     outputln(next);
49                 }
50             }
51         } else {
52             if (outputString.trim().startsWith("}"))
53                 level--;
54             if (newLine) {
55                 for (int j = 0; j < level; j++)
56                     out.print(TABULATION);
57             }
58             if (outputString.trim().startsWith("}")) {
59                 out.print(outputString);
60                 if (outputString.trim().endsWith("{"))
61                     level++;
62             } else {
63                 out.print(outputString);
64                 for (int i = 0; i < outputString.length(); i++) {
65                     String JavaDoc current = String.valueOf(outputString.charAt(i));
66                     if (current.equals("{"))
67                         level++;
68                     else if (current.equals("}"))
69                         level--;
70                 }
71             }
72             if (outputString.trim().endsWith("\n"))
73                 newLine = true;
74             else
75                 newLine = false;
76         }
77     }
78     
79 }
80
Popular Tags