KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > generator > common > lib > Indentor


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Christophe Demarey.
23 Contributor(s): .
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.generator.common.lib;
28
29 /**
30  * This a utility class to indent files.
31  */

32 public class Indentor
33 {
34     // ===========================================================
35
//
36
// Constants.
37
//
38
// ===========================================================
39

40     /**
41      ** The number of spaces to print for an indentation
42      **/

43     public static final int NB_CAR_INDENT = 4;
44
45     /**
46      ** The tag used to increment indentation
47      **/

48     public static final String JavaDoc INCREMENT_TAG = new String JavaDoc("/inc");
49
50     /**
51      ** The tag used to decrement indentation
52      **/

53     public static final String JavaDoc DECREMENT_TAG = new String JavaDoc("/dec");
54
55     /**
56      ** The end-line tag
57      **/

58     public static final String JavaDoc END_LINE = new String JavaDoc("\n");
59
60     // ===========================================================
61
//
62
// Internal State.
63
//
64
// ===========================================================
65

66     /**
67      ** The number of indentation to do
68      **/

69     private int indent_;
70
71     /**
72      ** The input stream to indent
73      **/

74     private java.io.BufferedReader JavaDoc in_;
75
76     /**
77      ** The resulting output
78      **/

79     private java.util.List JavaDoc out_;
80
81     // ===========================================================
82
//
83
// Constructors.
84
//
85
// ===========================================================
86

87     /**
88      * The default constructor
89      */

90     public Indentor() {}
91
92     // ===========================================================
93
//
94
// Internal methods.
95
//
96
// ===========================================================
97

98     /**
99      * Initialize the indentor
100      *
101      * @param file_name - The file to indent.
102      */

103     private void
104     init(String JavaDoc file_name)
105     {
106         // init internal states
107
indent_ = 0;
108         in_ = null;
109         out_ = new java.util.ArrayList JavaDoc();
110
111         // init input file
112
try{
113             in_ = new java.io.BufferedReader JavaDoc( new java.io.FileReader JavaDoc(file_name) );
114         }catch(java.io.IOException JavaDoc ex){
115             System.err.println("I/O error : ");
116             ex.printStackTrace();
117         }
118     }
119
120     /**
121      * Our own little min method.
122      *
123      * @param a - An integer value.
124      * @param b - An integer value.
125      *
126      * @return The min value.
127      **/

128     private int
129     min(int a, int b)
130     {
131         if (a < b) return a;
132             return b;
133     }
134
135     /**
136      * Search the first occurence between :
137      * - INCREMENT_TAG
138      * - DECREMENT_TAG
139      * - END_LINE
140      *
141      * @param src - The string to search in.
142      *
143      * @return The index of the first occurence found, else -1.
144      **/

145     private int searchFirst(String JavaDoc src)
146     {
147         int index_inc = -1;
148         int index_dec = -1;
149         int index_end = -1;
150
151         index_inc = src.indexOf(INCREMENT_TAG);
152         index_dec = src.indexOf(DECREMENT_TAG);
153         index_end = src.indexOf(END_LINE);
154         if (index_inc != -1)
155         {
156             if (index_dec != -1)
157             {
158                 if (index_end != -1)
159                 {
160                     if (index_inc < index_dec)
161                         return min(index_inc, index_end);
162                     else
163                         return min(index_dec, index_end);
164                 }
165                 else
166                 {
167                     return min(index_inc, index_dec);
168                 }
169             }
170             else
171             {
172                 if (index_end != -1)
173                     return min(index_inc, index_end);
174                 else
175                     return index_inc;
176             }
177         }
178         else if (index_dec != -1)
179         {
180             if (index_end != -1)
181             {
182                 return min(index_dec, index_end);
183             }
184             else
185             {
186                 return index_dec;
187             }
188         }
189         else
190         {
191             return index_end;
192         }
193
194     }
195
196     /**
197      * Increment the output
198      **/

199     private void
200     increment()
201     {
202         indent_ += NB_CAR_INDENT;
203     }
204
205     /**
206      * Decrement the output
207      **/

208     private void
209     decrement()
210     {
211         indent_ -= NB_CAR_INDENT;
212         if(indent_ < 0)
213             indent_ = 0;
214     }
215
216     // ===========================================================
217
//
218
// Public methods.
219
//
220
// ===========================================================
221

222     /**
223      * Replace the first occurence found in str with the new string
224      *
225      * @param str - The string to search in.
226      * @param old_pattern - The pattern to search.
227      * @param new_pattern - The replacing pattern.
228      *
229      * @return The resulting string.
230      **/

231     public String JavaDoc
232     replaceFirst(String JavaDoc str, String JavaDoc old_pattern, String JavaDoc new_pattern)
233     {
234         int index = str.indexOf(old_pattern);
235         int l = old_pattern.length();
236         try{
237             return str.substring(0, index) + new_pattern
238                    + str.substring(index+l, str.length());
239         }catch(Exception JavaDoc e){
240             return str;
241         }
242     }
243
244     /**
245      * Management of indentation
246      *
247      * @param file_name - The file to indent.
248      **/

249     public void
250     indent(String JavaDoc file_name) throws java.io.IOException JavaDoc
251     {
252         String JavaDoc current = null;
253
254         // Init the indentor
255
init(file_name);
256
257         // Processing the file
258
while ( (current = in_.readLine() ) != null )
259         {
260             // Indent the new line
261
String JavaDoc res = new String JavaDoc("");
262
263             for(int i=0; i<indent_; i++)
264                 res += " ";
265
266             // Search incrementation / decrementation tag
267
int index = searchFirst(current);
268
269             // Generate indentation
270
if ( current.startsWith(INCREMENT_TAG, index) )
271             {
272                 increment();
273                 current = replaceFirst(current, INCREMENT_TAG, "");
274             }
275             else if( current.startsWith(DECREMENT_TAG, index) )
276             {
277                 decrement();
278                 current = replaceFirst(current, DECREMENT_TAG, "");
279                 res = new String JavaDoc("");
280                 for(int i=0; i<indent_; i++)
281                     res += " ";
282             }
283             res += current + END_LINE;
284             out_.add( res );
285         }
286
287         in_.close();
288         apply(file_name);
289     }
290
291     /**
292      * Apply changes on the file
293      *
294      * @param file_name - The file to indent.
295      **/

296     public void
297     apply(String JavaDoc file_name)
298     {
299         java.io.File JavaDoc fic = null;
300         java.io.PrintWriter JavaDoc writer = null;
301
302         // Overwrite the input file
303
try{
304             fic = new java.io.File JavaDoc(file_name);
305             fic.delete();
306
307             writer = new java.io.PrintWriter JavaDoc( new java.io.FileWriter JavaDoc(fic) );
308         }catch(java.io.IOException JavaDoc ex){
309             System.err.println("I/O error : ");
310             ex.printStackTrace();
311         }
312
313         // Print contents
314
for(int i=0; i<out_.size(); i++)
315         {
316             writer.write( (String JavaDoc)out_.get(i) );
317         }
318         writer.flush();
319         writer.close();
320     }
321
322 }
323
324
Popular Tags