问题场景
用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]]*$");
/**
* '<ul class="forminfo">
<li><label >样本编号 :</label>
<input type="text" name="sampleNum" id="sampleNum" class="input_txt" value="${chromosomeNew.sampleNum}" readonly="readonly"/>
</li>
</ul>'
==>
* '<tr class="info">
<th>样本编号:</th>
<td><input type="text" name="sampleNum" id="sampleNum" class="input_txt" value="${chromosomeNew.sampleNum}"/></td>
</tr>'
* @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("<li><label >") && string.contains("</label>")) {
res = "\t" + string.replaceAll("<li>", "").replaceAll(" ", "").replaceAll(":", ":").replaceAll("label", "th");
} else if (string.contains("<input ") && string.contains("readonly=\"readonly\"/>")) {
string = string.replaceAll("\t", "");
string = string.substring(string.indexOf('<'));
string = string.substring(0, string.lastIndexOf(' ') + 1);
res = "\t" + "\t" + "<td>" + string + "/></td>";
} 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("");
//read and transfer
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)) {
//write
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");
}
}