作者:pigone | 来源:互联网 | 2024-11-21 10:21
问题场景用Java进行web开发过程当中,当遇到很多很多个字段的实体时,最苦恼的莫过于编辑字段的查看和修改界面,发现2个页面存在很多重复信息,能不能写一遍?有没有轮子用都不如自己造。解决方式笔者根据自
问题场景 用Java进行web开发过程当中,当遇到很多很多个字段的实体时,最苦恼的莫过于编辑字段的查看和修改界面,发现2个页面存在很多重复信息,能不能写一遍?有没有轮子用都不如自己造。
解决方式 笔者根据自己页面的长期特点编写了自己的转换规则,其中采用模板模式实现,个性化转换直接继承抽象类即可。
效果 before:
after:
CODE package regular;/** * Created on 2017/11/13 * Author: youxingyang. */ abstract class BaseTransfer { abstract String transfer(String string); }package regular;import file.FileUtils;import java.io.*;import java.util.regex.Pattern;/** * Created on 2017/11/13 * Author: youxingyang. */ public class Ul2Tr extends BaseTransfer { private static Pattern blank = Pattern.compile("^[//s&&[^//n]]*$" ); /** * '' ==> * ' 样本编号: ' * @param string * @return */ @Override public String transfer (String string) { String res = "" ; if (string != null ) { if (string.contains("forminfo" )) { res = string.replaceAll("ul" , "tr" ).replaceAll("forminfo" , "info" ); } else if (string.contains("" ) && string.contains("" )) { res = "\t" + string.replaceAll("" , "" ).replaceAll(" " , "" ).replaceAll(":" , ":" ).replaceAll("label" , "th" ); } else if (string.contains(" ) && string.contains("readOnly=\"readonly\"/>" )) { string = string.replaceAll("\t" , "" ); string = string.substring(string.indexOf('<' )); string = string.substring(0 , string.lastIndexOf(' ' ) + 1 ); res = "\t" + "\t" + " " + string + "/> "
;
}
else if (string.contains(
"ul>" )) {
res = string.replaceAll(
"ul" ,
"tr" );
}
}
return res;
}
/** * 转换show -> info * @param src * @param des */ public void transferFile (String src, String des) {
File file =
new File(src);
if (!file.exists()) {
return ;
}
StringBuilder sb =
new StringBuilder(
"" );
BufferedReader reader =
null ;
try {
reader =
new BufferedReader(
new InputStreamReader(
new FileInputStream(file),
"utf-8" ));
String line;
while (reader.ready()) {
line = reader.readLine();
if (!blank.matcher(line).find()) {
String newLine = transfer(line);
if (!
"" .equals(newLine)) {
sb.append(newLine).append(
"\n" );
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (reader !=
null ) {
try {
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
File desFile =
new File(des);
if (desFile.exists()) {
if (desFile.delete()) {
System.out.println(desFile.getAbsolutePath() +
" is deleted" );
}
}
if (createFile(des)) {
BufferedWriter bw =
null ;
try {
bw =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(desFile),
"utf-8" ));
bw.write(sb.toString());
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (bw !=
null ) {
try {
bw.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/** * 创建一个文件 * @param filePath * @return */ public static boolean createFile (String filePath) {
File file =
new File(filePath);
if (file.exists()) {
System.out.println(
"目标文件已存在" + filePath);
return false ;
}
if (filePath.endsWith(File.separator)) {
System.out.println(
"目标文件不能为目录!" );
return false ;
}
if (!file.getParentFile().exists()) {
System.out.println(
"目标文件所在目录不存在,准备创建它!" );
if (!file.getParentFile().mkdirs()) {
System.out.println(
"创建目标文件所在的目录失败!" );
return false ;
}
}
try {
if (file.createNewFile()) {
System.out.println(
"创建文件成功:" + filePath);
return true ;
}
else {
System.out.println(
"创建文件失败!" );
return false ;
}
}
catch (IOException e) {
e.printStackTrace();
System.out.println(
"创建文件失败!" + e.getMessage());
return false ;
}
}
public static void main (String[] args) {
new Ul2Tr().transferFile(
"C:\\Users\\domainclient\\Desktop\\src.txt" ,
"C:\\Users\\domainclient\\Desktop\\des.txt" );
}
}