1 65 66 67 package org.hsqldb.util; 68 69 import java.io.File ; 70 import java.io.FileReader ; 71 import java.io.FileWriter ; 72 import java.io.IOException ; 73 import java.io.LineNumberReader ; 74 import java.util.Vector ; 75 76 85 110 public class CodeSwitcher { 111 112 private static final String ls = System.getProperty("line.separator", 113 "\n"); 114 private Vector vList; 115 private Vector vSwitchOn; 116 private Vector vSwitchOff; 117 private Vector vSwitches; 118 private static final int MAX_LINELENGTH = 82; 119 120 126 public static void main(String [] a) { 127 128 CodeSwitcher s = new CodeSwitcher(); 129 130 if (a.length == 0) { 131 showUsage(); 132 133 return; 134 } 135 136 boolean path = false; 137 138 for (int i = 0; i < a.length; i++) { 139 String p = a[i]; 140 141 if (p.startsWith("+")) { 142 s.vSwitchOn.addElement(p.substring(1)); 143 } else if (p.startsWith("-")) { 144 s.vSwitchOff.addElement(p.substring(1)); 145 } else { 146 s.addDir(p); 147 148 path = true; 149 } 150 } 151 152 if (!path) { 153 printError("no path specified"); 154 showUsage(); 155 } 156 157 s.process(); 158 159 if (s.vSwitchOff.size() == 0 && s.vSwitchOn.size() == 0) { 160 s.printSwitches(); 161 } 162 } 163 164 168 static void showUsage() { 169 170 System.out.print("Usage: java CodeSwitcher [paths] [labels] [+][-]\n" 171 + "If no labels are specified then all used\n" 172 + "labels in the source code are shown.\n" 173 + "Use +MODE to switch on the things labeld MODE\n" 174 + "Use -MODE to switch off the things labeld MODE\n" 175 + "Path: Any number of path or files may be\n" 176 + "specified. Use . for the current directory\n" 177 + "(including sub-directories).\n" 178 + "Example: java CodeSwitcher +JAVA2 .\n" 179 + "This example switches on code labeled JAVA2\n" 180 + "in all *.java files in the current directory\n" 181 + "and all subdirectories.\n"); 182 } 183 184 188 CodeSwitcher() { 189 190 vList = new Vector (); 191 vSwitchOn = new Vector (); 192 vSwitchOff = new Vector (); 193 vSwitches = new Vector (); 194 } 195 196 200 void process() { 201 202 int len = vList.size(); 203 204 for (int i = 0; i < len; i++) { 205 System.out.print("."); 206 207 String file = (String ) vList.elementAt(i); 208 209 if (!processFile(file)) { 210 System.out.println("in file " + file + " !"); 211 } 212 } 213 214 System.out.println(""); 215 } 216 217 221 void printSwitches() { 222 223 System.out.println("Used labels:"); 224 225 for (int i = 0; i < vSwitches.size(); i++) { 226 System.out.println((String ) (vSwitches.elementAt(i))); 227 } 228 } 229 230 236 void addDir(String path) { 237 238 File f = new File (path); 239 240 if (f.isFile() && path.endsWith(".java")) { 241 vList.addElement(path); 242 } else if (f.isDirectory()) { 243 String [] list = f.list(); 244 245 for (int i = 0; i < list.length; i++) { 246 addDir(path + File.separatorChar + list[i]); 247 } 248 } 249 } 250 251 259 boolean processFile(String name) { 260 261 File f = new File (name); 262 File fnew = new File (name + ".new"); 263 int state = 0; boolean switchoff = false; 265 boolean working = false; 266 267 try { 268 Vector v = getFileLines(f); 269 Vector v1 = new Vector (v.size()); 270 271 for (int i = 0; i < v.size(); i++) { 272 v1.addElement(v.elementAt(i)); 273 } 274 275 for (int i = 0; i < v.size(); i++) { 276 String line = (String ) v.elementAt(i); 277 278 if (line == null) { 279 break; 280 } 281 282 if (working) { 283 if (line.equals("/*") || line.equals("*/")) { 284 v.removeElementAt(i--); 285 286 continue; 287 } 288 } 289 290 if (line.startsWith("//#")) { 291 if (line.startsWith("//#ifdef ")) { 292 if (state != 0) { 293 printError( 294 "'#ifdef' not allowed inside '#ifdef'"); 295 296 return false; 297 } 298 299 state = 1; 300 301 String s = line.substring(9); 302 303 if (vSwitchOn.indexOf(s) != -1) { 304 working = true; 305 switchoff = false; 306 } else if (vSwitchOff.indexOf(s) != -1) { 307 working = true; 308 309 v.insertElementAt("/*", ++i); 310 311 switchoff = true; 312 } 313 314 if (vSwitches.indexOf(s) == -1) { 315 vSwitches.addElement(s); 316 } 317 } else if (line.startsWith("//#else")) { 318 if (state != 1) { 319 printError("'#else' without '#ifdef'"); 320 321 return false; 322 } 323 324 state = 2; 325 326 if (!working) {} 327 else if (switchoff) { 328 if (v.elementAt(i - 1).equals("")) { 329 v.insertElementAt("*/", i - 1); 330 331 i++; 332 } else { 333 v.insertElementAt("*/", i++); 334 } 335 336 switchoff = false; 337 } else { 338 v.insertElementAt("/*", ++i); 339 340 switchoff = true; 341 } 342 } else if (line.startsWith("//#endif")) { 343 if (state == 0) { 344 printError("'#endif' without '#ifdef'"); 345 346 return false; 347 } 348 349 state = 0; 350 351 if (working && switchoff) { 352 if (v.elementAt(i - 1).equals("")) { 353 v.insertElementAt("*/", i - 1); 354 355 i++; 356 } else { 357 v.insertElementAt("*/", i++); 358 } 359 } 360 361 working = false; 362 } else {} 363 } 364 } 365 366 if (state != 0) { 367 printError("'#endif' missing"); 368 369 return false; 370 } 371 372 boolean filechanged = false; 373 374 for (int i = 0; i < v.size(); i++) { 375 if (!v1.elementAt(i).equals(v.elementAt(i))) { 376 filechanged = true; 377 378 break; 379 } 380 } 381 382 if (!filechanged) { 383 return true; 384 } 385 386 writeFileLines(v, fnew); 387 388 File fbak = new File (name + ".bak"); 389 390 fbak.delete(); 391 f.renameTo(fbak); 392 393 File fcopy = new File (name); 394 395 fnew.renameTo(fcopy); 396 fbak.delete(); 397 398 return true; 399 } catch (Exception e) { 400 printError(e.toString()); 401 402 return false; 403 } 404 } 405 406 static Vector getFileLines(File f) throws IOException { 407 408 LineNumberReader read = new LineNumberReader (new FileReader (f)); 409 Vector v = new Vector (); 410 411 for (;;) { 412 String line = read.readLine(); 413 414 if (line == null) { 415 break; 416 } 417 418 v.addElement(line); 419 } 420 421 read.close(); 422 423 return v; 424 } 425 426 static void writeFileLines(Vector v, File f) throws IOException { 427 428 FileWriter write = new FileWriter (f); 429 430 for (int i = 0; i < v.size(); i++) { 431 write.write((String ) v.elementAt(i)); 432 write.write(ls); 433 } 434 435 write.flush(); 436 write.close(); 437 } 438 439 445 static void printError(String error) { 446 System.out.println(""); 447 System.out.println("ERROR: " + error); 448 } 449 } 450 | Popular Tags |