1 33 34 package edu.rice.cs.drjava; 35 36 import java.io.*; 37 import java.util.Vector ; 38 import edu.rice.cs.plt.io.IOUtil; 41 import edu.rice.cs.drjava.model.definitions.indent.Indenter; 42 import edu.rice.cs.drjava.model.definitions.DefinitionsDocument; 43 import edu.rice.cs.drjava.model.GlobalEventNotifier; 44 45 50 public class IndentFiles { 51 52 60 public static void main(String [] args) { 61 Vector <String > fileNames = new Vector <String >(); 62 int indentLevel = 2; 63 boolean silent = false; 64 if (args.length < 1) _displayUsage(); 65 else { 66 for (int i = 0; i < args.length; i++) { 67 String arg = args[i]; 68 if (arg.equals("-indent")) { 69 i++; 70 try { indentLevel = Integer.parseInt(args[i]); } 71 catch (Exception e) { 72 _displayUsage(); 73 System.exit(-1); 74 } 75 } 76 else if (arg.equals("-silent")) silent = true; 77 else fileNames.add(arg); 78 } 79 indentFiles(fileNames, indentLevel, silent); 80 } 81 } 82 83 86 private static void _displayUsage() { 87 System.out.println( 88 "Usage:" + 89 " java edu.rice.cs.drjava.IndentFile [-indent N] [-silent] [filenames]\n" + 90 " Where N is the number of spaces in an indentation level"); 91 } 92 93 98 public static void indentFiles(Vector <String > fileNames, int indentLevel, boolean silent) { 99 Indenter indenter = new Indenter(indentLevel); 102 103 if (!silent) System.out.println("DrJava - Indenting files:"); 104 for (int i = 0; i < fileNames.size(); i++) { 105 String fname = fileNames.get(i); 106 File file = new File(fname); 107 if (!silent) { 108 System.out.print(" " + fname + " ... "); 109 System.out.flush(); 110 } 111 try { 112 String fileContents = IOUtil.toString(file); 113 DefinitionsDocument doc = new DefinitionsDocument(indenter, new GlobalEventNotifier()); 114 doc.insertString(0, fileContents, null); int docLen = doc.getLength(); 116 doc.indentLines(0, docLen); 117 fileContents = doc.getText(); 118 IOUtil.writeStringToFile(file, fileContents); 119 if (!silent) System.out.println("done."); 120 } 121 catch (Exception e) { 122 if (!silent) { 123 System.out.println("ERROR!"); 124 System.out.println(" Exception: " + e.toString()); 125 e.printStackTrace(System.out); 126 System.out.println(); 127 } 128 } 129 } 131 if (!silent) System.out.println(); 132 } 133 134 147 } 148 | Popular Tags |