在产品开发中,需要用到使用模板来生成.java文件,这就需要使用Jalopy等Format工具来实现代码的格式设置,Jalopy就是一种强大的Java code Format工具,下面简单介绍一下他的是用:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import de.hunsicker.jalopy.Jalopy;
public class FormatJava {
static URL convention = null;
static Jalopy jalopy = null;
private static void init()throws FormatException{
try {
convention = FormatJava.class.getResource("/META-INF/convention.xml");
Jalopy.setConvention(convention);
jalopy = new Jalopy();
} catch (IOException e) {
// TODO Auto-generated catch block
throw new FormatException(e.getMessage());
}
}
public static void format(File file)throws FormatException{
if(jalopy == null)
init();
try {
String name = file.getName();
if(name.endsWith(".java")){
jalopy.setInput(file);
jalopy.setOutput(file);
jalopy.format();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
new FormatException(e.getMessage());
}
}
public static void format(String fileName)throws FormatException{
if(jalopy == null)
init();
if(fileName == null)
return;
if(!fileName.endsWith(".java"))
return;
File file = new File(fileName);
if(!file.exists())
return;
try {
jalopy.setInput(file);
jalopy.setOutput(file);
jalopy.format();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
new FormatException(e.getMessage());
}
}
}
这里的convertion.xml中定义了关于java文件中的相关格式,里边的子项主要是包括方法参数是否现实,是否缩进等,下面是一个基本的例子:
[color=blue]
<?xml version="1.0" encoding="UTF-8"?>
<jalopy>
<internal>
<version>6</version>
</internal>
<printer>
<alignment>
<parameterMethodDeclaration>true</parameterMethodDeclaration>
</alignment>
<blanklines>
<after>
<header>2</header>
<lastImport>1</lastImport>
<method>1</method>
</after>
</blanklines>
<braces>
<insert>
<dowhile>true</dowhile>
<for>true</for>
<ifelse>true</ifelse>
<while>true</while>
</insert>
</braces>
<imports>
<grouping>
<defaultDepth>1</defaultDepth>
<packages>java:1|javax:1|org:1|gnu:1|*:0</packages>
</grouping>
<policy>disabled</policy>
<sort>true</sort>
</imports>
<indentation>
<policy>
<deep>true</deep>
</policy>
<sizes>
<extends>8</extends>
<implements>8</implements>
</sizes>
</indentation>
<wrapping>
<always>
<before>
<extends>true</extends>
<implements>true</implements>
</before>
</always>
<general>
<enable>true</enable>
</general>
</wrapping>
</printer>
</jalopy>
分享到:
相关推荐
jalopy排版 xml是别人的 .rar
JALOPY2-MAIN:Java的源代码格式化程序
jalopy-eclipse eclipse 插件
jalopy_html, 中文文档,
maven-jalopy-plugin-1.5.1.jar
maven-jalopy-plugin-1.3.1.jar
检测代码是否规范所使用的xml文件及使用说明
jalopy-eclipse-0.2-1.5rc3jalopy-eclipse-0.2-1.5rc3jalopy-eclipse-0.2-1.5rc3jalopy-eclipse-0.2-1.5rc3jalopy-eclipse-0.2-1.5rc3jalopy-eclipse-0.2-1.5rc3jalopy-eclipse-0.2-1.5rc3jalopy-eclipse-0.2-1.5rc3...
maven-jalopy-plugin-1.5.jar
The Eclipse plugin for Jalopy.
maven-jalopy-plugin-1.4.jar
maven-jalopy-plugin-1.3.jar
maven-jalopy-plugin-1.2.jar
maven-jalopy-plugin-1.5.1-sources.jar
相当于以前c 的indent顺便写一下其他语言的c, c++ c# ==> indent Astylejava ==> astyle Jalopy Jacobe ImportScrbberphp ==> phpCodeBeautifierperl ==> perlTidypython ==> Pydentasp ==> VBSBeaut 工具名称 ...
Checkstyle是一款检查Java程序代码样式的工具,可以帮助我们自动化代码规范检查过程,从而使得开发人员从这项重要但枯燥的任务中解脱出来。Checkstyle提供高可配置性,以便适用于各种代码规范。它通过指定modules来...
这是利用axis2开发webservice所需的jar包,
包含easyexplore、jalopy、propertiesEditor、run-jetty-run、spket、tomcatv3,经测试,eclipse3.4,eclipse3.5及以上版本可用。下载后直接解压覆盖dropins文件夹即可。