📄 htmutility.java
字号:
package g2w.app.gchm.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
/**
* A utility to handle html files.
*
* @author GreatGhoul
* @version 007 2009-3-21 8:00:58
*/
public class HTMUtility {
public static String getHTMLTitle(String path) {
String[] encodings = {"gb2312", "gbk", "utf-8", "big5"};
String title = null;
// Try to get the title.
for (int i = 0; i < encodings.length; i++) {
try {
Parser parser = new Parser(path);
parser.setEncoding(encodings[i]);
NodeList list = parser.parse(new TagNameFilter("title"));
title = list.elementAt(0).toPlainTextString().replaceAll(" ", " ");
break;
} catch (ParserException e) {
continue;
} catch (NullPointerException e) {
continue;
}
}
if (title != null && !((title = title.trim()).isEmpty()))
return title;
else
return "未命名";
}
public static String getHTMLContent(String path) {
String[] encodings = {"gb2312", "gbk", "utf-8", "big5"};
for (int i = 0; i < encodings.length; i++) {
try {
Parser parser = new Parser(path);
parser.setEncoding(encodings[i]);
NodeList list = parser.parse(null);
return list.toHtml(true);
} catch (ParserException e) {
continue;
}
}
return null;
}
public static File createBlankHtmlDocument(String title, String path) throws IOException {
File file = new File(path);
file.createNewFile();
BufferedWriter writer = new BufferedWriter(new FileWriter(path));
String html = "<html>\n<head>\n<title>" + title + "</title>\n</head>\n</body>\n</html>";
writer.write(html);
writer.close();
return file;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -