/** * Format the given Java source {@link File} using the given Eclipse code format properties {@link File}. * * @param prefs the format properties file * @param source the file which contains the source to format * @throws FormatterException if the source coudn't be formatted * @throws IOException When the file cannot be read or written, or the preferences cannot be read. */ public static void format(File prefs, File source) throws IOException { Properties options = readConfig(prefs); if (options == null) options = readConfigInternal(); try (InputStream in = new BufferedInputStream(new FileInputStream(source)); OutputStream out = new BufferedOutputStream(new FileOutputStream(source))) { String content = Streams.toString(in); String formatted = format(options, content); Streams.write(new ByteArrayInputStream(formatted.getBytes()), out); } }
public static String format(String source) { // TODO locate user's eclipse project settings, use those if we can. Properties options = readConfig("org.eclipse.jdt.core.prefs"); final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options); return ensureCorrectNewLines(formatFile(source, codeFormatter)); }
/** * Format the given Java source {@link File} using the given Eclipse code format properties {@link File}. * * @throws IOException When the file cannot be read or written, or the preferences cannot be read. */ public static void format(File prefs, File source) throws IOException { Properties options = readConfig(prefs); if (options == null) options = readConfigInternal(); InputStream in = null; OutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(source)); String content = Streams.toString(in); String formatted = format(options, content); out = new BufferedOutputStream(new FileOutputStream(source)); Streams.write(new ByteArrayInputStream(formatted.getBytes()), out); } finally { Streams.closeQuietly(in); Streams.closeQuietly(out); } }