元旦节前,写了个小程序,辅助把概设里面的动不动就20多个字段的table自动生成成html脚本。今天改了下,也许以后还可以用来自动生成除了table外的其他的html脚本呢~。
概设里面的:
String productTypeName 产品类型
String brandName 品牌
跑下程序,自动转成html语句:
<ig:BoundDataField DataFieldName="productTypeName" Key="productTypeName" Width="80px"><Header Text="产品类型" /></ig:BoundDataField>
<ig:BoundDataField DataFieldName="brandName" Key="brandName" Width="80px"><Header Text="品牌" /></ig:BoundDataField>
开始干活~~
Step1:一个接口,将一个String格式化为html格式的String,可能是table,可能是input~~
package com.zyp.util.file.format;
public interface IFormater{
public String formatToHtml(String var);
}
Step2:实现该接口的类,转成table语句
package com.zyp.util.file.format;
import java.util.StringTokenizer;
public class WebDataGridFormater implements IFormater{
public String formatToHtml(String var) {
return fromatToHTML(var);
}
//将字符串格式化为html语句。
public String fromatToHTML(String s){
int index = 0;
String rtnValue = null;
String[] tokens = new String[4];
if(s==null){
return null;
}
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens()) {
String value = st.nextToken();
int i = value.indexOf("(");
if(i>0){
value = value.substring(0, i);
}
int k = value.indexOf("(");
if(k>0){
value = value.substring(0, k);
}
tokens[index] = value;
index++;
}
//根据显示table的字数,设置宽度
int px = countPx(tokens[2]);
//默认不隐藏
if(index==3){
rtnValue = "<ig:BoundDataField DataFieldName=\""+tokens[1]+"\" Key=\""+tokens[1]+"\" Width=\""+px+"px\"><Header Text=\""+tokens[2]+"\" /></ig:BoundDataField>";
}
//隐藏该列
else if(index == 4){
rtnValue = "<ig:BoundDataField DataFieldName=\""+tokens[1]+"\" Key=\""+tokens[1]+"\" Hidden=\""+tokens[3]+"\" ><Header Text=\""+tokens[1]+"\" /></ig:BoundDataField>";
}
return rtnValue;
}
//根据显示的字符长度,设置该列宽度
public int countPx(String pxString){
int rtnValue = 0;
//<4: 80px
//<6:100px
//<10:120
//>10:150
int len = pxString.length();
if(len<=4){
rtnValue = 80;
}else if(len<=6){
rtnValue = 100;
}else if(len<=8){
rtnValue = 120;
}else if(len<=10){
rtnValue = 150;
}else{
rtnValue = 200;
}
return rtnValue;
}
}
Step3:找一个Helper来根据指定的类名,返回一个formater:
package com.zyp.util.file.format;
public class FormatHelper {
private static FormatHelper instance = new FormatHelper();
public static FormatHelper getInstance() {
return instance;
}
public IFormater createFormater(String foramterClassName) {
IFormater formater = null;
try {
Class classType = Class.forName(foramterClassName);
formater = (IFormater) classType.newInstance();
return formater;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return formater;
}
}
Step4:主方法,读取配置文件,得到当前输入的字符串和输出字符串的文件。逐行遍历输入文件,通过上面的Helper得到一个Formater,转换成对应格式的html语句,写入输出文件:
package com.zyp.util.file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Properties;
import com.zyp.util.file.format.FormatHelper;
import com.zyp.util.file.format.IFormater;
public class Util {
public static String DATA_GRID_NAME = "<ig:BoundDataField DataFieldName=";
private static String inPath = ".\\temp.txt";
private static String outPath = ".\\out.txt";
private static String configName = "config.properties";
private static Properties p;
private static IFormater formater;
public static void init() {
try {
Util util = new Util();
InputStream is = util.getClass().getResourceAsStream(
"/" + configName);
p = new Properties();
p.load(is);
String input = (String) p.get("input");
String output = (String) p.get("output");
String formaterName = p.getProperty("classname");
if (input != null) {
inPath = input;
}
if (output != null) {
outPath = output;
}
if ( formaterName != null) {
formater = FormatHelper.getInstance().createFormater(formaterName);
}
System.out.println("input file = " + inPath);
System.out.println("output file = " + outPath);
System.out.println("formater name = " + formater.getClass().getName());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 将字符串格式化为html语句。
public static String fromatToHTML(String s){
return formater.formatToHtml(s);
}
public static void main(String[] args) {
try {
init();
File inFile = new File(inPath);
File outFile = new File(outPath);
if (!outFile.exists()) {
String dir = outPath.substring(0, outPath.lastIndexOf("\\"));
File dirFile = new File(dir);
if (!dirFile.exists()) {
if (!dirFile.mkdir())
System.out.println("create directory failed!");
return;
}
if (!outFile.createNewFile()) {
System.out.println("create file failed");
return;
}
}
BufferedReader reader = null;
OutputStreamWriter writer = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(inFile), "UTF-8"));
writer = new OutputStreamWriter(new FileOutputStream(outPath),
"UTF-8");
String tempString = null;
int line = 0;
while ((tempString = reader.readLine()) != null) {
String html = fromatToHTML(tempString);
System.out.println(html);
writer.write(html + "\n");
writer.flush();
line++;
}
System.out.println("共处理数据:" + line);
reader.close();
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Step5:最后一个ant脚本,帮忙打包:
<?xml version="1.0" encoding="UTF-8" ?> <project name="UtilProject" basedir="." default="run"> <property name="projectName" value="Util" /> <property name="src" value="src" /> <property name="classes" value="classes" /> <property name="jarDir" value="dist"/> <path id="classpath"> <fileset dir="${jarDir}"> <include name="*.jar"/> <include name="config.properties"/> </fileset> </path> <target name="init"> <mkdir dir="${classes}"/> <mkdir dir="${jarFile}"/> </target> <target name="compile" depends="init" > <javac srcdir="${src}" destdir="${classes}" encoding="UTF-8"/> </target> <target name="build" depends="compile"> <jar jarfile="${jarDir}/${projectName}.jar" basedir="${classes}" includes="**/*.class"/> </target> <target name="run" depends="build"> <java classname="com.zyp.util.file.Util" > <classpath> <pathelement path="${jarDir}"/> <fileset dir="${jarDir}" > <include name="*.jar" /> <include name="*.properties" /> <include name="*.txt" /> </fileset> </classpath> </java> </target> </project>
Step6:打出一个jar包来后,写一个bat,执行main方法,就可以给大家用了
@echo off set classpath=%CLASS_PATH%;.;./util.jar;./config.properties echo %classpath% java com.zyp.util.file.Util pause
Step7:config.properties里面指定输入和输出文件的路径,及转换的类,运行上面的bat文件就完了。
#读取文件路径: input=.\\temp.txt #输出文件路径: output=.\\out.txt #格式化的类名: classname=com.zyp.util.file.format.WebDataGridFormater