File eclipse-efj.patch of Package eclipse
diff -ruN org.eclipse.jdt.core.orig/formatter/org/eclipse/jdt/core/formatter/CodeFormatterApplication.java org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/CodeFormatterApplication.java
--- org.eclipse.jdt.core.orig/formatter/org/eclipse/jdt/core/formatter/CodeFormatterApplication.java 1969-12-31 19:00:00.000000000 -0500
+++ org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/CodeFormatterApplication.java 2006-02-03 22:02:39.000000000 -0500
@@ -0,0 +1,389 @@
+/*******************************************************************************
+ * Copyright (c) 2004 Ben Konrath <ben@bagu.org>
+ * Copyright (c) 2006 Red Hat Incorporated
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ben Konrath <ben@bagu.org> - initial implementation
+ * Red Hat Incorporated - improvements based on comments from JDT developers
+ * IBM Corporation - Code review and integration
+ *******************************************************************************/
+package org.eclipse.jdt.core.formatter;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.Properties;
+
+import org.eclipse.core.runtime.IPlatformRunnable;
+import org.eclipse.jdt.core.ToolFactory;
+import org.eclipse.jdt.internal.core.util.Util;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.Document;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.text.edits.TextEdit;
+
+/**
+ * Implements an Eclipse Application for org.eclipse.jdt.core.JavaCodeFormatter.
+ *
+ * There are a couple improvments that could be made: 1. Make a list of all the
+ * files first so that a file does not get formatted twice. 2. Use a text based
+ * progress monitor for output.
+ *
+ * @author Ben Konrath <bkonrath@redhat.com>
+ * @since 3.2
+ */
+public class CodeFormatterApplication implements IPlatformRunnable {
+
+ /**
+ * Deals with the messages in the properties file (cut n' pasted from a
+ * generated class).
+ */
+ private final static class Messages extends NLS {
+ private static final String BUNDLE_NAME = "org.eclipse.jdt.core.formatter.messages";//$NON-NLS-1$
+
+ public static String CommandLineConfigFile;
+
+ public static String CommandLineDone;
+
+ public static String CommandLineErrorConfig;
+
+ public static String CommandLineErrorFile;
+
+ public static String CommandLineErrorFileDir;
+
+ public static String CommandLineErrorQuietVerbose;
+
+ public static String CommandLineErrorNoConfigFile;
+
+ public static String CommandLineFormatting;
+
+ public static String CommandLineStart;
+
+ public static String CommandLineUsage;
+
+ public static String ConfigFileReadingError;
+
+ public static String FormatProblem;
+
+ public static String CaughtException;
+
+ public static String ExceptionSkip;
+
+ static {
+ NLS.initializeMessages(BUNDLE_NAME, Messages.class);
+ }
+
+ /**
+ * Bind the given message's substitution locations with the given string
+ * values.
+ *
+ * @param message
+ * the message to be manipulated
+ * @return the manipulated String
+ */
+ public static String bind(String message) {
+ return bind(message, null);
+ }
+
+ /**
+ * Bind the given message's substitution locations with the given string
+ * values.
+ *
+ * @param message
+ * the message to be manipulated
+ * @param binding
+ * the object to be inserted into the message
+ * @return the manipulated String
+ */
+ public static String bind(String message, Object binding) {
+ return bind(message, new Object[] {
+ binding
+ });
+ }
+
+ /**
+ * Bind the given message's substitution locations with the given string
+ * values.
+ *
+ * @param message
+ * the message to be manipulated
+ * @param binding1
+ * An object to be inserted into the message
+ * @param binding2
+ * A second object to be inserted into the message
+ * @return the manipulated String
+ */
+ public static String bind(String message, Object binding1, Object binding2) {
+ return bind(message, new Object[] {
+ binding1, binding2
+ });
+ }
+
+ /**
+ * Bind the given message's substitution locations with the given string
+ * values.
+ *
+ * @param message
+ * the message to be manipulated
+ * @param bindings
+ * An array of objects to be inserted into the message
+ * @return the manipulated String
+ */
+ public static String bind(String message, Object[] bindings) {
+ return MessageFormat.format(message, bindings);
+ }
+ }
+
+ private final String ARG_CONFIG = "-config"; //$NON-NLS-1$
+
+ private final String ARG_HELP = "-help"; //$NON-NLS-1$
+
+ private final String ARG_QUIET = "-quiet"; //$NON-NLS-1$
+
+ private final String ARG_VERBOSE = "-verbose"; //$NON-NLS-1$
+
+ private String configName;
+
+ private Properties options = null;
+
+ private final String PDE_LAUNCH = "-pdelaunch"; //$NON-NLS-1$
+
+ private boolean quiet = false;
+
+ private boolean verbose = false;
+
+ /**
+ * Display the command line usage message.
+ */
+ private void displayHelp() {
+ System.out.println(Messages.bind(Messages.CommandLineUsage));
+ }
+
+ private void displayHelp(String message) {
+ System.err.println(message);
+ System.out.println();
+ displayHelp();
+ }
+
+ /**
+ * Recursively format the Java source code that is contained in the
+ * directory rooted at dir.
+ */
+ private void formatDirTree(File dir, CodeFormatter codeFormatter) {
+
+ File[] files = dir.listFiles();
+ if (files == null)
+ return;
+
+ for (int i = 0; i < files.length; i++) {
+ File file = files[i];
+ if (file.isDirectory()) {
+ formatDirTree(file, codeFormatter);
+ } else if (Util.isJavaLikeFileName(file.getPath())) {
+ formatFile(file, codeFormatter);
+ }
+ }
+ }
+
+ /**
+ * Format the given Java source file.
+ */
+ private void formatFile(File file, CodeFormatter codeFormatter) {
+ IDocument doc = new Document();
+ try {
+ // read the file
+ if (this.verbose) {
+ System.out.println(Messages.bind(Messages.CommandLineFormatting, file.getAbsolutePath()));
+ }
+ String contents = new String(org.eclipse.jdt.internal.compiler.util.Util.getFileCharContent(file, null));
+ // format the file (the meat and potatoes)
+ doc.set(contents);
+ TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, contents, 0, contents.length(), 0, null);
+ if (edit != null) {
+ edit.apply(doc);
+ } else {
+ System.err.println(Messages.bind(Messages.FormatProblem, file.getAbsolutePath()));
+ return;
+ }
+
+ // write the file
+ final BufferedWriter out = new BufferedWriter(new FileWriter(file));
+ try {
+ out.write(doc.get());
+ out.flush();
+ } finally {
+ try {
+ out.close();
+ } catch (IOException e) {
+ /* ignore */
+ }
+ }
+ } catch (IOException e) {
+ String errorMessage = Messages.bind(Messages.CaughtException, "IOException", e.getLocalizedMessage()); //$NON-NLS-1$
+ Util.log(e, errorMessage);
+ System.err.println(Messages.bind(Messages.ExceptionSkip ,errorMessage));
+ } catch (BadLocationException e) {
+ String errorMessage = Messages.bind(Messages.CaughtException, "BadLocationException", e.getLocalizedMessage()); //$NON-NLS-1$
+ Util.log(e, errorMessage);
+ System.err.println(Messages.bind(Messages.ExceptionSkip ,errorMessage));
+ }
+ }
+
+ private File[] processCommandLine(String[] argsArray) {
+
+ ArrayList args = new ArrayList();
+ for (int i = 0; i < argsArray.length; i++) {
+ args.add(argsArray[i]);
+ }
+ int index = 0;
+ final int argCount = argsArray.length;
+
+ final int DEFAULT_MODE = 0;
+ final int CONFIG_MODE = 1;
+
+ int mode = DEFAULT_MODE;
+ final int INITIAL_SIZE = 1;
+ int fileCounter = 0;
+
+ File[] filesToFormat = new File[INITIAL_SIZE];
+
+ loop: while (index < argCount) {
+ String currentArg = argsArray[index++];
+
+ switch(mode) {
+ case DEFAULT_MODE :
+ if (PDE_LAUNCH.equals(currentArg)) {
+ continue loop;
+ }
+ if (ARG_HELP.equals(currentArg)) {
+ displayHelp();
+ return null;
+ }
+ if (ARG_VERBOSE.equals(currentArg)) {
+ this.verbose = true;
+ continue loop;
+ }
+ if (ARG_QUIET.equals(currentArg)) {
+ this.quiet = true;
+ continue loop;
+ }
+ if (ARG_CONFIG.equals(currentArg)) {
+ mode = CONFIG_MODE;
+ continue loop;
+ }
+ // the current arg should be a file or a directory name
+ File file = new File(currentArg);
+ if (file.exists()) {
+ if (filesToFormat.length == fileCounter) {
+ System.arraycopy(filesToFormat, 0, (filesToFormat = new File[fileCounter * 2]), 0, fileCounter);
+ }
+ filesToFormat[fileCounter++] = file;
+ } else {
+ displayHelp(Messages.bind(Messages.CommandLineErrorFile, currentArg));
+ return null;
+ }
+ break;
+ case CONFIG_MODE :
+ this.configName = currentArg;
+ this.options = readConfig(currentArg);
+ if (this.options == null) {
+ displayHelp(Messages.bind(Messages.CommandLineErrorConfig, currentArg));
+ return null;
+ }
+ mode = DEFAULT_MODE;
+ continue loop;
+ }
+ }
+
+ if (mode == CONFIG_MODE) {
+ displayHelp(Messages.bind(Messages.CommandLineErrorNoConfigFile));
+ return null;
+ }
+ if (this.quiet && this.verbose) {
+ displayHelp(
+ Messages.bind(
+ Messages.CommandLineErrorQuietVerbose,
+ new String[] { ARG_QUIET, ARG_VERBOSE }
+ ));
+ return null;
+ }
+ if (fileCounter == 0) {
+ displayHelp(Messages.bind(Messages.CommandLineErrorFileDir));
+ return null;
+ }
+ if (filesToFormat.length != fileCounter) {
+ System.arraycopy(filesToFormat, 0, (filesToFormat = new File[fileCounter]), 0, fileCounter);
+ }
+ return filesToFormat;
+ }
+
+ /**
+ * Return a Java Properties file representing the options that are in the
+ * specified config file.
+ */
+ private Properties readConfig(String filename) {
+ BufferedInputStream stream = null;
+ try {
+ stream = new BufferedInputStream(new FileInputStream(new File(filename)));
+ final Properties formatterOptions = new Properties();
+ formatterOptions.load(stream);
+ return formatterOptions;
+ } catch (IOException e) {
+ Util.log(e, Messages.bind(Messages.ConfigFileReadingError));
+ } finally {
+ if (stream != null) {
+ try {
+ stream.close();
+ } catch (IOException e) {
+ /* ignore */
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Runs the Java code formatter application
+ */
+ public Object run(Object args) throws Exception {
+ File[] filesToFormat = processCommandLine((String[]) args);
+
+ if (filesToFormat == null) {
+ return EXIT_OK;
+ }
+
+ if (!this.quiet) {
+ if (this.configName != null) {
+ System.out.println(Messages.bind(Messages.CommandLineConfigFile, this.configName));
+ }
+ System.out.println(Messages.bind(Messages.CommandLineStart));
+ }
+
+ // format the list of files and/or directories
+ final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(this.options);
+ for (int i = 0, max = filesToFormat.length; i < max; i++) {
+ final File file = filesToFormat[i];
+ if (file.isDirectory()) {
+ formatDirTree(file, codeFormatter);
+ } else if (Util.isJavaLikeFileName(file.getPath())) {
+ formatFile(file, codeFormatter);
+ }
+ }
+ if (!this.quiet) {
+ System.out.println(Messages.bind(Messages.CommandLineDone));
+ }
+
+ return EXIT_OK;
+ }
+
+}
diff -ruN org.eclipse.jdt.core.orig/formatter/org/eclipse/jdt/core/formatter/messages.properties org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/messages.properties
--- org.eclipse.jdt.core.orig/formatter/org/eclipse/jdt/core/formatter/messages.properties 1969-12-31 19:00:00.000000000 -0500
+++ org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/messages.properties 2006-02-03 22:02:36.000000000 -0500
@@ -0,0 +1,41 @@
+###############################################################################
+# Copyright (c) 2006 Ben Konrath <ben@bagu.org>
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Contributors:
+# Ben Konrath <ben@bagu.org> - initial implementation
+# IBM Corporation - Code review and integration
+###############################################################################
+CommandLineStart=Starting format job ...
+CommandLineDone=Done.
+CommandLineConfigFile=Configuration Name: {0}
+CommandLineFormatting=Formatting: {0}
+
+CommandLineUsage=Usage: eclipse -application org.eclipse.jdt.core.JavaCodeFormatter [ OPTIONS ] <files>\n\
+\n\
+\ <files> Java source files and/or directories to format.\n\
+\ Only files ending with .java will be formatted in the given directory.\n\
+\n\
+\OPTIONS:\n\
+\ -config <configFile> Use the formatting style from the specified properties file.\n\
+\ Refer to the help documentation to find out how to generate this file.\n\
+\n\
+\ -help Display this message.\n\
+\ -quiet Only print error messages.\n\
+\ -verbose Be verbose about the formatting job.
+
+CommandLineErrorFile={0} does not exist. Please specify only valid Java Source files.
+CommandLineErrorConfig=A problem occured while reading the config file {0}.
+CommandLineErrorFileDir=You must specify at least one file or directory to format.
+CommandLineErrorQuietVerbose=You cannot use the options {0} and {1} together.
+CommandLineErrorNoConfigFile=No configuration file specified.
+
+CaughtException=Caught {0} : {1}
+ExceptionSkip= {0}\nSkipping File.
+
+ConfigFileReadingError=Error Reading config file.
+
+FormatProblem=The Eclipse formatter failed to format {0}. Skip the file.
\ No newline at end of file
diff -ruN org.eclipse.jdt.core.orig/plugin.xml org.eclipse.jdt.core/plugin.xml
--- org.eclipse.jdt.core.orig/plugin.xml 2005-05-09 07:29:30.000000000 -0400
+++ org.eclipse.jdt.core/plugin.xml 2006-02-04 13:17:09.000000000 -0500
@@ -155,7 +155,17 @@
<fileTypes extension="jardesc" type="text"/>
<fileTypes extension="zip" type="binary"/>
</extension>
-
+<!-- =================================================================================== -->
+<!-- Extension: Java Code Formatter -->
+<!-- =================================================================================== -->
+<extension
+ id="JavaCodeFormatter"
+ point="org.eclipse.core.runtime.applications">
+ <application>
+ <run class="org.eclipse.jdt.core.formatter.CodeFormatterApplication" />
+ </application>
+</extension>
+
<!-- =================================================================================== -->
<!-- Extension: Java Content Types -->
<!-- =================================================================================== -->