元旦节前,写了个小程序,辅助把概设里面的动不动就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
分享到:
相关推荐
微信小程序 我厨 tab 界面设计 (源码)微信小程序 我厨 tab 界面设计 (源码)微信小程序 我厨 tab 界面设计 (源码)微信小程序 我厨 tab 界面设计 (源码)微信小程序 我厨 tab 界面设计 (源码)微信小程序 我厨 tab 界面...
小程序源码 B站首页界面设计:附详细教程 (代码+截图)小程序源码 B站首页界面设计:附详细教程 (代码+截图)小程序源码 B站首页界面设计:附详细教程 (代码+截图)小程序源码 B站首页界面设计:附详细教程 (代码+截图)...
微信小程序源码 B站首页界面设计:附详细教程(学习版)微信小程序源码 B站首页界面设计:附详细教程(学习版)微信小程序源码 B站首页界面设计:附详细教程(学习版)微信小程序源码 B站首页界面设计:附详细教程(学习版)...
微信小程序源码(含截图)我厨 tab 界面设计微信小程序源码(含截图)我厨 tab 界面设计微信小程序源码(含截图)我厨 tab 界面设计微信小程序源码(含截图)我厨 tab 界面设计微信小程序源码(含截图)我厨 tab ...
7、通过本源码,可学习到一些基本的微信小程序的布局,提升写代码的效率; 8、适用于学习微信小程序的用户; 9、可以使用于任何需要的小程序首页布局中; 10、喜欢的可以点赞、收藏加关注哦,以免找不到了哦; 11、...
微信小程序登录界面设计,适合初级学者,可以实现界面登录,简单的登录微信小程序,喜欢的可以看看,微信小程序登录界面设计(可以登录)
微信小程序登陆界面,输入账号密码即跳转到另一页面,亲测可用
微信小程序 小程序页面生成器 (源码)微信小程序 小程序页面生成器 (源码)微信小程序 小程序页面生成器 (源码)微信小程序 小程序页面生成器 (源码)微信小程序 小程序页面生成器 (源码)微信小程序 小程序页面生成器 ...
Ps改界面小程序,内置多套预制界面,基本上是曼胡风格,选择目录后直接可以改
微信小程序 用户协议 界面 四个文件,代码详细,直接添加到page中即可
1、微信小程序登录、注册切换; 2、适用于应用类小程序登录注册需求; 3、节省界面空间,只需要一个界面,切换完成登录和注册功能; 4、界面简单整洁,无需繁杂的逻辑; 5、界面中的发送验证码为示例,如需要更多...
微信小程序前端界面实现
7、通过本源码,可学习到一些基本的微信小程序的布局,提升写代码的效率; 8、适用于学习微信小程序的用户; 9、可以使用于任何需要的小程序首页布局中; 10、喜欢的可以点赞、收藏加关注哦,以免找不到了哦; 11、...
微信小程序点餐界面代码.zip
该资源包含了7个简单的android界面的小程序和小例子,简单易懂,适合初学者学习,喜欢的下载!
Transfer-Window-master WeChat小程序界面 小程序是一种新的开放能力,开发者可以快速地开发一个小程序。小程序可以在微信内被便捷地获取和传播,同时具有出色的使用体验。
微信小程序 康爱多微商城:学习界面设计 (源码)微信小程序 康爱多微商城:学习界面设计 (源码)微信小程序 康爱多微商城:学习界面设计 (源码)微信小程序 康爱多微商城:学习界面设计 (源码)微信小程序 康爱多微商城...
微信小程序——B站首页界面设计(截图+源码).zip 微信小程序——B站首页界面设计(截图+源码).zip 微信小程序——B站首页界面设计(截图+源码).zip 微信小程序——B站首页界面设计(截图+源码).zip 微信小程序...
微信小程序美化,视频教你每一步,并且有详细的步骤教程
微信小程序 面包旅行:界面设计,文本展示 (源码)微信小程序 面包旅行:界面设计,文本展示 (源码)微信小程序 面包旅行:界面设计,文本展示 (源码)微信小程序 面包旅行:界面设计,文本展示 (源码)微信小程序 面包...