1 26 27 package org.objectweb.openccm.generator.common.lib; 28 29 32 public class Indentor 33 { 34 40 43 public static final int NB_CAR_INDENT = 4; 44 45 48 public static final String INCREMENT_TAG = new String ("/inc"); 49 50 53 public static final String DECREMENT_TAG = new String ("/dec"); 54 55 58 public static final String END_LINE = new String ("\n"); 59 60 66 69 private int indent_; 70 71 74 private java.io.BufferedReader in_; 75 76 79 private java.util.List out_; 80 81 87 90 public Indentor() {} 91 92 98 103 private void 104 init(String file_name) 105 { 106 indent_ = 0; 108 in_ = null; 109 out_ = new java.util.ArrayList (); 110 111 try{ 113 in_ = new java.io.BufferedReader ( new java.io.FileReader (file_name) ); 114 }catch(java.io.IOException ex){ 115 System.err.println("I/O error : "); 116 ex.printStackTrace(); 117 } 118 } 119 120 128 private int 129 min(int a, int b) 130 { 131 if (a < b) return a; 132 return b; 133 } 134 135 145 private int searchFirst(String 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 199 private void 200 increment() 201 { 202 indent_ += NB_CAR_INDENT; 203 } 204 205 208 private void 209 decrement() 210 { 211 indent_ -= NB_CAR_INDENT; 212 if(indent_ < 0) 213 indent_ = 0; 214 } 215 216 222 231 public String 232 replaceFirst(String str, String old_pattern, String 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 e){ 240 return str; 241 } 242 } 243 244 249 public void 250 indent(String file_name) throws java.io.IOException 251 { 252 String current = null; 253 254 init(file_name); 256 257 while ( (current = in_.readLine() ) != null ) 259 { 260 String res = new String (""); 262 263 for(int i=0; i<indent_; i++) 264 res += " "; 265 266 int index = searchFirst(current); 268 269 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 (""); 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 296 public void 297 apply(String file_name) 298 { 299 java.io.File fic = null; 300 java.io.PrintWriter writer = null; 301 302 try{ 304 fic = new java.io.File (file_name); 305 fic.delete(); 306 307 writer = new java.io.PrintWriter ( new java.io.FileWriter (fic) ); 308 }catch(java.io.IOException ex){ 309 System.err.println("I/O error : "); 310 ex.printStackTrace(); 311 } 312 313 for(int i=0; i<out_.size(); i++) 315 { 316 writer.write( (String )out_.get(i) ); 317 } 318 writer.flush(); 319 writer.close(); 320 } 321 322 } 323 324 | Popular Tags |