用程序整理代码 (Format Code)

最近有个项目需要用程序输出代码。
由于比较复杂,边整理缩进等再输出,显得非常麻烦。
所以,想到直接不管格式输出,然后再排版。

现在把代码共享给大家。

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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值