之前在网上有一些现成的转换工具的,不过有些是Javascript,只限于Web环境下使用,有些是本地程序,但是没有编码的选项,只是单纯的提供输入及输出,工作的时候需要处理些特殊编码的文件的时候就没法子了。没办法,只有自已写一个转换工具,满足自已的需要。因为时间关系,暂时写了一个在控制台下运行的程序,谁有兴趣,帮忙做个界面出来也不错。呵,下面是所有代码。可惜,我机子上没用Java2Html的插件,要不代码就没这么难看了。。。不过将就下了。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.nio.charset.Charset;
public class Html2Js {
private String source;
private String dist;
private String encoding;
public static String html2js(String html) {
StringBuffer sb = new StringBuffer(html);
StringBuffer js = new StringBuffer();
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == '"') {
sb.replace(i, i, "//");
i++;
}
}
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == '/n') {
sb.replace(i, i, "/"+");
i = i + 2;
}
}
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == '/n') {
sb.replace(i + 1, i + 1, "/"");
i = i + 2;
}
}
js.append("document.write(/"");
js.append(sb);
js.append("/");");
return js.toString();
}
public static void main(String[] arg) {
if (arg.length == 0) {
System.out
.println("useage: java Html2Js <sourcePath> [distPath] [encoding]/n"
+ " -sourcePath: specify your source HTML file/n"
+ " -distPath: specify your expect path of the file generate by program,defaul the same as sourcePath/n"
+ " -encoding: specify the encoding of the source file,the output use the same encoding,if not specify,use the system file encoding");
return;
}
// prepare
Html2Js tool = new Html2Js();
tool.source = arg[0];
if (arg.length > 1)
tool.dist = arg[1];
else
tool.dist = tool.source.substring(0, tool.source.lastIndexOf('.'))
+ ".js";
if (arg.length > 2)
tool.encoding = arg[2];
else
tool.encoding = System.getProperty("file.encoding");
// read the source
try {
String html = tool.getSource().replaceAll("/r", "");
tool.genFile(tool.dist, html2js(html), tool.encoding);
System.out.println("html2js finish!");
} catch (FileNotFoundException e) {
System.err.println("Fatal error! source file : " + tool.source
+ "not found!");
throw new RuntimeException(e.getMessage());
} catch (IOException e) {
System.err.println("Fatal error!");
throw new RuntimeException(e.getMessage());
}
}
/**
* 从源文件读进文本
*
* @throws IOException
*/
public String getSource() throws IOException {
StringBuffer sb = new StringBuffer();
Reader reader = new InputStreamReader(new FileInputStream(source),
Charset.forName(encoding));
int read = 0;
char[] data = new char[1024];
while ((read = reader.read(data, 0, 1024)) != -1) {
sb.append(data, 0, read);
}
return sb.toString();
}
public void genFile(String fullFileName, String body, String encoding)
throws IOException {
File file = new File(fullFileName);
FileOutputStream fos = null;
fos = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(fos, Charset
.forName(encoding));
if (body != null && !"".equals(body)) {
writer.write(body);
writer.close();
}
}
public void setDist(String dist) {
this.dist = dist;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public void setSource(String source) {
this.source = source;
}
}