package com.haohaosh.common.util.web;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;
/**
*
* @项目名 ssh
* @功能 根据相应的静态页面模板,替换相应的内容;
* @类名 TextToHtml
* @作者 javalearns.com
* @日期 Aug 30, 20113:44:55 PM
* @版本 1.0
*/
@SuppressWarnings("unchecked")
public class TextToHtml {
/**
* 根据相应的模板替换相应的内容;
*
* @param filePath
* 模板地址;
* @param htmlFile
* 生成的静态页的路径;
* @param replace
* 替换的内容;Map<替换正则,替换后的内容>
* @return
*/
public static boolean TextToHtmlFile(String filePath, String htmlFile,
Map<String, String> replace) {
String str = "";
try {
String tempStr = "";
FileInputStream is = new FileInputStream(filePath);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while ((tempStr = br.readLine()) != null)
str = str + tempStr;
is.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
try {
Iterator it = replace.entrySet().iterator();
while (it.hasNext()) {
Map.Entry context = (Map.Entry) it.next();
String key = context.getKey().toString();
String value = context.getValue().toString();
str = str.replaceAll(key, value);
}
File f = new File(htmlFile);
BufferedWriter o = new BufferedWriter(new FileWriter(f));
o.write(str);
o.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
}