现在又一个这样的需求, 一个形如这样的文件:
test.txt 写道
/* ----------------- name1 ----------------- */
flag1: name1 type: b
description: "hello"
condition: a(name2)
/* ----------------- name2 ----------------- */
flag2: name2 type: c
box_name: name3
description: "world"
condition: b(name3)
/* ----------------- name3 ----------------- */
flag1: name3 type: b
description: "hello world"
flag1: name1 type: b
description: "hello"
condition: a(name2)
/* ----------------- name2 ----------------- */
flag2: name2 type: c
box_name: name3
description: "world"
condition: b(name3)
/* ----------------- name3 ----------------- */
flag1: name3 type: b
description: "hello world"
现在要求将里面的name进行替换,比如将name1换成newname1,name2换成newname2,替换的内容可以用properties文件,例如properties文件的内容可以如下所示:
properties文件 写道
name1=
newname1
name2= newname2
name3= newname3
name2= newname2
name3= newname3
与形如out.txt文件的内容比较,结果是这两个文件的逻辑内容一致, 还要验证test2的文件编写规则是否正确,对于某些name块中有condition属性,里面的name要定义在此name之上
test2.txt 写道
/* ----------------- newname3 ----------------- */
flag1: newname3 type: c
description: "world"
/* ----------------- newname2 ----------------- */
flag2: newname2 type: b
box_name: newname3
description: "hello world"
condition: b(newname3)
/* ----------------- newname1 ----------------- */
flag1: newname1 type: b
description: "hello"
condition:a(newname2)
flag1: newname3 type: c
description: "world"
/* ----------------- newname2 ----------------- */
flag2: newname2 type: b
box_name: newname3
description: "hello world"
condition: b(newname3)
/* ----------------- newname1 ----------------- */
flag1: newname1 type: b
description: "hello"
condition:a(newname2)
给出我的实现方法,实现的方法比较粗糙(因为注释的话是不影响的,所以我都是将注释去掉之后在比较的),我先创建一个类,把文件的内容进行抽象。
import java.util.*;
public class JobDefine {
private int flag;
private String name;
private String boxname;
private List<String> conditionList;
private Map<String, String> map;
public List<String> getConditionList() {
return conditionList;
}
public void setConditionList(List<String> conditionList) {
this.conditionList = conditionList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBoxname() {
return boxname;
}
public void setBoxname(String boxname) {
this.boxname = boxname;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
@Override
public int hashCode() {
// 这里没有编写
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof JobDefine)) {
return false;
}
JobDefine define = (JobDefine) obj;
Map<String, String> map1 = define.getMap();
Set<String> keyset = map1.keySet();
int i = 0;
if (define.getName().equals(name) && map1.size() == map.size()) {
for (String s : keyset) {
if (map.containsKey(s) && map.get(s).equals(map1.get(s))) {
i++;
} else {
return false;
}
}
}
return (map1.size() == i);
}
}
之后编写比较以及验证方法:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SequenceDef {
public static void main(String[] args) throws IOException {
replacedFile();
compareFiles();
verifyFile();
}
private static void replacedFile() throws IOException {
String fileContext = readFile("test.txt");
System.out.println("********************************");
Properties prop = new Properties();
InputStream in = SequenceDef.class.getClassLoader()
.getResourceAsStream("map2.properties");
prop.load(in);
in.close();
Set<Object> keyValue = prop.keySet();
for (Object o : keyValue) {
String key = (String) o;
System.out.println(key);
System.out.println(prop.getProperty(key));
fileContext = fileContext.replaceAll(key, prop.getProperty(key));
}
System.out.println("********************************");
writeFile("out.txt", fileContext);
}
/**
* 将文件读取成字符串
*
* @param sourceFile
* @return
*/
private static String readFile(String sourceFile) {
StringBuilder sb = new StringBuilder();
BufferedReader br;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(
sourceFile)));
char[] buf = new char[64];
int count = 0;
try {
while ((count = br.read(buf)) != -1) {
sb.append(buf, 0, count);
}
} finally {
if (br != null) {
br.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
String fileContext = sb.toString();
return fileContext;
}
private static void writeFile(String toFileName, String fileContext) {
PrintWriter out = null;
try {
out = new PrintWriter(
new BufferedWriter(new FileWriter(toFileName)));
out.print(fileContext);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.flush();
out.close();
}
}
}
/**
* 首先根据注释/*加上*'/'来划分字符串,每一个字符串当做一个jobclass来处理,并处理字符串放进类的相应属性中
*
* @param sourceFile
* @return
*/
private static List<JobDefine> splitContent(String sourceFile) {
String fileContent = readFile(sourceFile);
String[] str = fileContent.split("\\/*(.*)\\*/");
List<JobDefine> list = new ArrayList<JobDefine>();
for (int i = 1; i < str.length; i++) {
String splittext = str[i].trim();
JobDefine perjob = new JobDefine();
perjob.setFlag(i);
Map<String, String> map = new HashMap<String, String>();
String[] allline = splittext.split("\r\n");
// You can add condition here.
String[] names = { "flag1", "flag2" };
for (int index = 0; index < allline.length; index++) {
String line = allline[index];
if (index == 0) {
for (String prefixjob : names) {
if (line.startsWith(prefixjob)) {
int end = line.indexOf("job_type") - 1;
int from = line.indexOf(":") + 2;
String name = line.substring(from, end);
perjob.setName(name);
map.put(prefixjob, name);
// s.replaceAll("\\s*$", "") means 去掉s末尾处的空格
map.put("job_type",
line.substring(line.lastIndexOf(":") + 2)
.replaceAll("\\s*$", ""));
break;
}
}
} else {
if (line.startsWith("box_name")) {
String boxname = line.substring(line.indexOf(":") + 2)
.trim();
perjob.setBoxname(boxname);
map.put("box_name", boxname);
} else if (line.startsWith("condition")) {
List<String> conditionList = new ArrayList<String>();
String condition = line
.substring(line.indexOf(":") + 2).replaceAll(
"\\s*$", "");
map.put("condition", condition);
// 将一个字符串中的圆括号的内容放入list中
Matcher m = Pattern.compile("\\(.+?\\)").matcher(
condition);
while (m.find()) {
conditionList.add(m.group().substring(1,
m.group().length() - 1));
}
perjob.setConditionList(conditionList);
} else if (line.startsWith("#")) {
String owner = line.substring(line.indexOf(":") + 1)
.replaceAll("\\s*$", "");
map.put("owner", owner);
} else {
map.put(line.substring(0, line.indexOf(":")),
line.substring(line.indexOf(":") + 1));
}
}
}
perjob.setMap(map);
list.add(perjob);
}
return list;
}
/**
* 比较两个文件的逻辑内容是否一致,分成一个一个job来比较 当他们的name一致时,比较有相同name的job
*
*/
private static void compareFiles() {
List<JobDefine> autoList = splitContent("out.txt");
List<JobDefine> writeList = splitContent("test2.txt");
int i = 0;
for (JobDefine define1 : autoList) {
for (JobDefine define2 : writeList) {
if (define1.getName().equals(define2.getName())) {
if (!define1.equals(define2)) {
System.out.println("the content for the name ["
+ define1.getName() + "] is not equal.");
} else {
i++;
}
break;
}
}
}
if (autoList.size() == i) {
System.out.println("the two files content are consistent.");
}
}
/**
* 如果boxname和condition有值,并且在nameMap中的,那么放入conditionMap中
* 在conditionMap中,如果某个name匹配的value值比他的jobname的value大。说明这个conditionMap中的
* jobclass定义与jobname之后,这样是不对的。
*/
private static void verifyFile() {
List<JobDefine> list = splitContent("test2.txt");
Map<String, Integer> nameMap = new HashMap<String, Integer>();
int i = 0;
for (JobDefine perclass : list) {
i++;
nameMap.put(perclass.getName(), i);
}
int j = 0;
int tag = 0;
for (JobDefine perclass : list) {
j++;
String boxname = perclass.getBoxname();
List<String> conditionList = perclass.getConditionList();
if (null != boxname || null != conditionList) {
Map<String, Integer> conditionMap = new HashMap<String, Integer>();
if (null != boxname && nameMap.containsKey(boxname)) {
conditionMap.put(boxname, nameMap.get(boxname));
}
if (null != conditionList) {
for (String percondition : conditionList) {
if (nameMap.containsKey(percondition)) {
conditionMap.put(percondition,
nameMap.get(percondition));
}
}
}
if (null != conditionMap) {
Set<Map.Entry<String, Integer>> set = conditionMap
.entrySet();
for (Map.Entry<String, Integer> entry : set) {
if (entry.getValue().intValue() > j) {
tag++;
System.out.println("This job name [ "
+ perclass.getName()
+ " ] definition location is not correct."
+ " It should list after name [ "
+ entry.getKey() + " ]");
}
}
// for (Integer integer : conditionMap.values()) {
// if (integer.intValue() > j) {
// tag++;
// System.out.println("This job name [ "
// + perclass.getName()
// + " ] definition location is not correct."
// }
// }
}
}
}
if (tag == 0) {
System.out.println("This file's content is CORRECT.");
} else {
System.out.println("This file's job definition is INCORRECT.");
}
}
}