最近有个项目需要用程序输出代码。
由于比较复杂,边整理缩进等再输出,显得非常麻烦。
所以,想到直接不管格式输出,然后再排版。
现在把代码共享给大家。
CodeFormater.java
调用
需要用到Eclipse插件里的一些库
org.eclipse.jdt.core
org.eclipse.jdt.text
参考网站
[url]http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.isv/guide/jdt_api_codeformatter.htm[/url]
由于比较复杂,边整理缩进等再输出,显得非常麻烦。
所以,想到直接不管格式输出,然后再排版。
现在把代码共享给大家。
CodeFormater.java
package test.eclipse.plugin.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Map;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.text.edits.TextEdit;
public class CodeFormater {
//private String codeFormatFile;
private String fileEncoding;
private Map options;
private CodeFormatter codeFormatter;
public CodeFormater(String fileEncoding) {
init();
//this.codeFormatFile = codeFormatFile;
this.fileEncoding = fileEncoding;
}
public CodeFormater() {
init();
//this.codeFormatFile = codeFormatFile;
}
private void init() {
fileEncoding = "UTF-8";
options = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
// // initialize the compiler settings to be able to format 1.6 code
options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_6);
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_6);
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6);
//
// // change the option to wrap each enum constant on a new line
// options.put(
// DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS,
// DefaultCodeFormatterConstants.createAlignmentValue(
// true,
// DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE,
// DefaultCodeFormatterConstants.INDENT_ON_COLUMN));
// instanciate the default code formatter with the given options
codeFormatter = ToolFactory.createCodeFormatter(options);
}
public void formatFile(String sourceFileName) {
File file = new File(sourceFileName);
BufferedReader in = null;
BufferedWriter out = null;
String line;
String lineSeperator = System.getProperty("line.separator");
try {
// retrieve the source
in = new BufferedReader(new InputStreamReader(new FileInputStream(file), fileEncoding));
StringBuffer sb = new StringBuffer();;
while ((line = in.readLine()) != null) {
sb.append(line);
sb.append(lineSeperator);
}
in.close();
in = null;
String contents = sb.toString();
IDocument doc = new Document(contents);
// create delta
TextEdit edit = codeFormatter.format(
CodeFormatter.K_COMPILATION_UNIT, // format a compilation unit
contents, // source to format
0, // starting position
contents.length(), // length
0, // initial indentation
lineSeperator); // line separator
// apply changes to content
edit.apply(doc);
// output
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), fileEncoding));
String output = doc.get();
out.write(output);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (Exception e) {
}
}
}
}
调用
CodeFormater codeFormater = new CodeFormater();
codeFormater.formatFile("需要排版的代码的路经");
需要用到Eclipse插件里的一些库
org.eclipse.jdt.core
org.eclipse.jdt.text
参考网站
[url]http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.isv/guide/jdt_api_codeformatter.htm[/url]