1 15 package org.eclipse.jdt.core.formatter; 16 17 import java.io.BufferedInputStream ; 18 import java.io.BufferedWriter ; 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.FileWriter ; 22 import java.io.IOException ; 23 import java.text.MessageFormat ; 24 import java.util.ArrayList ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 28 import org.eclipse.equinox.app.IApplication; 29 import org.eclipse.equinox.app.IApplicationContext; 30 import org.eclipse.jdt.core.ToolFactory; 31 import org.eclipse.jdt.internal.core.util.Util; 32 import org.eclipse.jface.text.BadLocationException; 33 import org.eclipse.jface.text.Document; 34 import org.eclipse.jface.text.IDocument; 35 import org.eclipse.osgi.util.NLS; 36 import org.eclipse.text.edits.TextEdit; 37 38 51 public class CodeFormatterApplication implements IApplication { 52 53 57 private final static class Messages extends NLS { 58 private static final String BUNDLE_NAME = "org.eclipse.jdt.core.formatter.messages"; 60 public static String CommandLineConfigFile; 61 62 public static String CommandLineDone; 63 64 public static String CommandLineErrorConfig; 65 66 public static String CommandLineErrorFile; 67 68 public static String CommandLineErrorFileDir; 69 70 public static String CommandLineErrorQuietVerbose; 71 72 public static String CommandLineErrorNoConfigFile; 73 74 public static String CommandLineFormatting; 75 76 public static String CommandLineStart; 77 78 public static String CommandLineUsage; 79 80 public static String ConfigFileReadingError; 81 82 public static String FormatProblem; 83 84 public static String CaughtException; 85 86 public static String ExceptionSkip; 87 88 static { 89 NLS.initializeMessages(BUNDLE_NAME, Messages.class); 90 } 91 92 100 public static String bind(String message) { 101 return bind(message, null); 102 } 103 104 114 public static String bind(String message, Object binding) { 115 return bind(message, new Object [] { 116 binding 117 }); 118 } 119 120 132 public static String bind(String message, Object binding1, Object binding2) { 133 return bind(message, new Object [] { 134 binding1, binding2 135 }); 136 } 137 138 148 public static String bind(String message, Object [] bindings) { 149 return MessageFormat.format(message, bindings); 150 } 151 } 152 153 private static final String ARG_CONFIG = "-config"; 155 private static final String ARG_HELP = "-help"; 157 private static final String ARG_QUIET = "-quiet"; 159 private static final String ARG_VERBOSE = "-verbose"; 161 private String configName; 162 163 private Map options = null; 164 165 private static final String PDE_LAUNCH = "-pdelaunch"; 167 private boolean quiet = false; 168 169 private boolean verbose = false; 170 171 174 private void displayHelp() { 175 System.out.println(Messages.bind(Messages.CommandLineUsage)); 176 } 177 178 private void displayHelp(String message) { 179 System.err.println(message); 180 System.out.println(); 181 displayHelp(); 182 } 183 184 188 private void formatDirTree(File dir, CodeFormatter codeFormatter) { 189 190 File [] files = dir.listFiles(); 191 if (files == null) 192 return; 193 194 for (int i = 0; i < files.length; i++) { 195 File file = files[i]; 196 if (file.isDirectory()) { 197 formatDirTree(file, codeFormatter); 198 } else if (Util.isJavaLikeFileName(file.getPath())) { 199 formatFile(file, codeFormatter); 200 } 201 } 202 } 203 204 207 private void formatFile(File file, CodeFormatter codeFormatter) { 208 IDocument doc = new Document(); 209 try { 210 if (this.verbose) { 212 System.out.println(Messages.bind(Messages.CommandLineFormatting, file.getAbsolutePath())); 213 } 214 String contents = new String (org.eclipse.jdt.internal.compiler.util.Util.getFileCharContent(file, null)); 215 doc.set(contents); 217 TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, contents, 0, contents.length(), 0, null); 218 if (edit != null) { 219 edit.apply(doc); 220 } else { 221 System.err.println(Messages.bind(Messages.FormatProblem, file.getAbsolutePath())); 222 return; 223 } 224 225 final BufferedWriter out = new BufferedWriter (new FileWriter (file)); 227 try { 228 out.write(doc.get()); 229 out.flush(); 230 } finally { 231 try { 232 out.close(); 233 } catch (IOException e) { 234 235 } 236 } 237 } catch (IOException e) { 238 String errorMessage = Messages.bind(Messages.CaughtException, "IOException", e.getLocalizedMessage()); Util.log(e, errorMessage); 240 System.err.println(Messages.bind(Messages.ExceptionSkip ,errorMessage)); 241 } catch (BadLocationException e) { 242 String errorMessage = Messages.bind(Messages.CaughtException, "BadLocationException", e.getLocalizedMessage()); Util.log(e, errorMessage); 244 System.err.println(Messages.bind(Messages.ExceptionSkip ,errorMessage)); 245 } 246 } 247 248 private File [] processCommandLine(String [] argsArray) { 249 250 ArrayList args = new ArrayList (); 251 for (int i = 0, max = argsArray.length; i < max; i++) { 252 args.add(argsArray[i]); 253 } 254 int index = 0; 255 final int argCount = argsArray.length; 256 257 final int DEFAULT_MODE = 0; 258 final int CONFIG_MODE = 1; 259 260 int mode = DEFAULT_MODE; 261 final int INITIAL_SIZE = 1; 262 int fileCounter = 0; 263 264 File [] filesToFormat = new File [INITIAL_SIZE]; 265 266 loop: while (index < argCount) { 267 String currentArg = argsArray[index++]; 268 269 switch(mode) { 270 case DEFAULT_MODE : 271 if (PDE_LAUNCH.equals(currentArg)) { 272 continue loop; 273 } 274 if (ARG_HELP.equals(currentArg)) { 275 displayHelp(); 276 return null; 277 } 278 if (ARG_VERBOSE.equals(currentArg)) { 279 this.verbose = true; 280 continue loop; 281 } 282 if (ARG_QUIET.equals(currentArg)) { 283 this.quiet = true; 284 continue loop; 285 } 286 if (ARG_CONFIG.equals(currentArg)) { 287 mode = CONFIG_MODE; 288 continue loop; 289 } 290 File file = new File (currentArg); 292 if (file.exists()) { 293 if (filesToFormat.length == fileCounter) { 294 System.arraycopy(filesToFormat, 0, (filesToFormat = new File [fileCounter * 2]), 0, fileCounter); 295 } 296 filesToFormat[fileCounter++] = file; 297 } else { 298 displayHelp(Messages.bind(Messages.CommandLineErrorFile, currentArg)); 299 return null; 300 } 301 break; 302 case CONFIG_MODE : 303 this.configName = currentArg; 304 this.options = readConfig(currentArg); 305 if (this.options == null) { 306 displayHelp(Messages.bind(Messages.CommandLineErrorConfig, currentArg)); 307 return null; 308 } 309 mode = DEFAULT_MODE; 310 continue loop; 311 } 312 } 313 314 if (mode == CONFIG_MODE || this.options == null) { 315 displayHelp(Messages.bind(Messages.CommandLineErrorNoConfigFile)); 316 return null; 317 } 318 if (this.quiet && this.verbose) { 319 displayHelp( 320 Messages.bind( 321 Messages.CommandLineErrorQuietVerbose, 322 new String [] { ARG_QUIET, ARG_VERBOSE } 323 )); 324 return null; 325 } 326 if (fileCounter == 0) { 327 displayHelp(Messages.bind(Messages.CommandLineErrorFileDir)); 328 return null; 329 } 330 if (filesToFormat.length != fileCounter) { 331 System.arraycopy(filesToFormat, 0, (filesToFormat = new File [fileCounter]), 0, fileCounter); 332 } 333 return filesToFormat; 334 } 335 336 340 private Properties readConfig(String filename) { 341 BufferedInputStream stream = null; 342 try { 343 stream = new BufferedInputStream (new FileInputStream (new File (filename))); 344 final Properties formatterOptions = new Properties (); 345 formatterOptions.load(stream); 346 return formatterOptions; 347 } catch (IOException e) { 348 Util.log(e, Messages.bind(Messages.ConfigFileReadingError)); 349 } finally { 350 if (stream != null) { 351 try { 352 stream.close(); 353 } catch (IOException e) { 354 355 } 356 } 357 } 358 return null; 359 } 360 361 364 public Object start(IApplicationContext context) throws Exception { 365 File [] filesToFormat = processCommandLine((String []) context.getArguments().get(IApplicationContext.APPLICATION_ARGS)); 366 367 if (filesToFormat == null) { 368 return IApplication.EXIT_OK; 369 } 370 371 if (!this.quiet) { 372 if (this.configName != null) { 373 System.out.println(Messages.bind(Messages.CommandLineConfigFile, this.configName)); 374 } 375 System.out.println(Messages.bind(Messages.CommandLineStart)); 376 } 377 378 final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(this.options); 379 for (int i = 0, max = filesToFormat.length; i < max; i++) { 381 final File file = filesToFormat[i]; 382 if (file.isDirectory()) { 383 formatDirTree(file, codeFormatter); 384 } else if (Util.isJavaLikeFileName(file.getPath())) { 385 formatFile(file, codeFormatter); 386 } 387 } 388 if (!this.quiet) { 389 System.out.println(Messages.bind(Messages.CommandLineDone)); 390 } 391 392 return IApplication.EXIT_OK; 393 } 394 public void stop() { 395 } 397 } 398 | Popular Tags |