/**
*
*/
/**
* @author zhou
*
*/
package propertiesUtil;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class comapreProp {
public static void main(String[] args) {
String start = "config/start.txt";
start(start);
}
/**
* get properties file
* */
public static List<List<String>> readProp(String path) {
List<List<String>> list = new ArrayList<List<String>>();
BufferedReader bf = null;
try {
bf = new BufferedReader(new FileReader(path));
String str = null;
while ((str = bf.readLine()) != null) {
List<String> line = new ArrayList<String>();
if (str.contains("#")) {
line.add(str);
line.add("");
} else {
String key = str.substring(0, str.indexOf("="));
String value = str.substring(str.indexOf("=") + 1,
str.length());
line.add(key);
line.add(value);
}
list.add(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bf != null) {
bf.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
/**
* write properties
* */
public static void writeProp(List<List<String>> list, String path) {
OutputStream outputStream = null;
BufferedWriter bw = null;
try {
outputStream = new FileOutputStream(path);
bw = new BufferedWriter(new OutputStreamWriter(outputStream,"utf-8"));
// bw = new BufferedWriter(new FileWriter(path));
for(int i =0;i<list.size();i++){
if (list.get(i).get(0).contains("#")) {
bw.write(list.get(i).get(0));
} else {
bw.write(list.get(i).get(0) + "=" + list.get(i).get(1));
}
if(i != list.size() -1){
bw.newLine();
}
}
bw.flush();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* charge if List contain String
* */
public static boolean isContain(List<List<String>> list, List<String> line) {
boolean flag = false;
for (List<String> li : list) {
if (line.get(0).equals(li.get(0)) || line.get(0) == li.get(0)) {
flag = true;
}
}
return flag;
}
/**
* list1 need to compared list2 the base of all
* */
public static List<List<String>> compare(List<List<String>> list1, List<List<String>> list2) {
List<List<String>> listNew = new ArrayList<List<String>>();
Map<String, String> containMap = new HashMap<String, String>();
Map<String, String> noContainMap = new HashMap<String, String>();
for (List<String> list : list2) {
if (isContain(list1, list)) {
containMap.put(list.get(0), list.get(1));
} else {
noContainMap.put(list.get(0), list.get(1));
}
}
for (List<String> list : list1) {
List<String> lineNew = new ArrayList<String>();
boolean flag = false;
Iterator iterator = containMap.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, String> entry = (Entry<String, String>) iterator.next();
String key = entry.getKey();
String value = entry.getValue();
if (list.get(0).equals(key) || list.get(0) == key) {
flag = true;
lineNew.add(list.get(0));
lineNew.add(value);
}
}
if (!flag) {
lineNew.add(list.get(0));
lineNew.add(list.get(1));
}
listNew.add(lineNew);
flag = false;
}
Iterator iterator = noContainMap.entrySet().iterator();
while (iterator.hasNext()) {
List<String> lineNew = new ArrayList<String>();
Entry<String, String> entry = (Entry<String, String>) iterator.next();
String key = entry.getKey();
String value = entry.getValue();
lineNew.add(key);
lineNew.add(value);
listNew.add(lineNew);
}
return listNew;
}
/**
* start up
*/
public static void start(String path) {
String base = null;
String dire = null;
List<String> directory = new ArrayList<String>();
List<List<String>> baseProp = null;
List<List<String>> testProp = null;
List<List<String>> finalProp = null;
BufferedReader bf = null;
try {
bf = new BufferedReader(new FileReader(path));
base = bf.readLine();
dire = bf.readLine();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bf != null) {
bf.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
try {
bf = new BufferedReader(new FileReader(dire));
String line = null;
while ((line = bf.readLine()) != null) {
directory.add(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bf != null) {
try {
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
baseProp = readProp(base); // get base prop
for(String str : directory) {
testProp = readProp(str); //get test prop
finalProp = compare(testProp,baseProp);
writeProp(finalProp,str); //write back
}
System.out.println("completed...");
}
}