将Java文件转换为HTML文件
来源:互联网 发布:网络的安全机制有哪些 编辑:程序博客网 时间:2024/05/16 17:39
项目中碰到的一个小问题,现将解决方法开源,主要由三步组成。废话不多上代码。
1 主函数
static String[] keywordString = { "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "for", "final", "finally", "float", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while", "true", "false", "null"};static HashSet<String> keywordSet=new HashSet<String>(Arrays.asList(keywordString));public static void main(String[] args) {String filename="C:/Users/Ethan/workspace/Text/tx2.txt";String filename2="C:/Users/Ethan/workspace/Text/javaH.html";try { Scanner input = new Scanner(new File(args[0])); PrintWriter output = new PrintWriter(new File(args[1])); JavaToHTML(input, output);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}
2 将Java源代码转为HTML
static void translateToHTML(String text, Scanner input, PrintWriter output) throws Exception { text = text.replaceAll("// ", "LINECOMMENT"); text = text.replaceAll("/\\*", "BLOCKCOMMENT"); String token; while (text != null && text.length() > 0) { // * and / are in conflict with /* and // String[] parts = text.split("[%\\+\\-\\*/\r\n\t \\[\\].;(){},]", 2); token = parts[0]; if (token.length() > 1 && token.startsWith("LINECOMMENT")) { output.format("%s", "<span class = \"comment\">"); parts = text.split("\r\n", 2); text = parts[1]; output.format("%s", parts[0].replaceAll("LINECOMMENT", "// ")); output.format("%s", "</span>\r\n"); continue; } else if (token.length() > 1 && token.startsWith("BLOCKCOMMENT")) { output.format("%s", "<span class = \"comment\">"); parts = text.split("\\*/", 2); text = parts[1]; output.format("%s", parts[0].replaceAll("BLOCKCOMMENT", "/*") + "*/"); output.format("%s", "</span>"); continue; } else if (token.length() > 1 && token.matches("'\\w'*")) { output.format("%s", "<span class = \"literal\">"); output.format("%s", token); output.format("%s", "</span>"); } else if (token.startsWith("\"") && token.endsWith("\"") && (token.length() > 1)) { output.format("%s", "<span class = \"literal\">" + token + "</span>"); } else if (token.startsWith("'") && token.endsWith("'") && (token.length() > 1)) { output.format("%s", "<span class = \"literal\">" + token + "</span>"); } else if (token.equals("' '")) { output.format("%s", "<span class = \"literal\">" + token + "</span>"); } else if (token.startsWith("\"") && token.endsWith("\"") && (token.length() == 1)) { if (stringToken) { output.format("%s", token + "</span>"); stringToken = false; } else { output.format("%s", "<span class = \"literal\">" + token); stringToken = true; } } else if (token.startsWith("\"")) { output.format("%s", "<span class = \"literal\">" + token); stringToken = true; } else if (token.endsWith("\"") && (!token.endsWith("\\\""))) { output.format("%s", token); output.format("%s", "</span>"); stringToken = false; } else if (token.matches("\\d+")) { // Check if numeric output.format("%s", "<span class = \"literal\">" + token + "</span>"); } else if (!stringToken && keywordSet.contains(token)) { output.format("%s", "<span class = \"keyword\">" + token + "</span>"); } else { output.format("%s", token); } if (token.length() < text.length()) { if (text.charAt(token.length()) == '<') output.format("%s", "<"); else if (text.charAt(token.length()) == '>') output.format("%s", ">"); else output.format("%s", text.charAt(token.length())); } if (parts.length == 2) { text = parts[1]; } } }
static boolean stringToken = false; static String inputFileName; public static void JavaToHTML(Scanner input, PrintWriter output) { try { output.format("%s\r\n", "<html>"); output.format("%s\r\n", "<head>"); output.format("%s\r\n", "<title>Intro to Java Programming, 6E - " + inputFileName + "</title>"); output.format("%s\r\n", "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">"); output.format("%s\r\n", "<style type = \"text/css\">"); output.format("%s\r\n", "body {font-family: \"Courier New\", sans-serif; font-size: 100%; color: black}"); output.format("%s\r\n", ".keyword {color: #000080; font-weight: bold}"); output.format("%s\r\n", ".comment {color: #008000}"); output.format("%s\r\n", ".literal {color: #0000ff}"); output.format("%s\r\n", ".keyword {color: black; font-weight: bold}"); output.format("%s\r\n", ".comment {color: #77797C}"); output.format("%s\r\n", ".literal {color: #007346; font-weight: bold}"); output.format("%s\r\n", "</style>"); output.format("%s\r\n", "</head>"); output.format("%s\r\n", "<body>"); output.format("%s\r\n", "<pre>"); String text = ""; String temp; // Read all lines while (input.hasNext()) { temp = input.nextLine(); text += temp + "\r\n"; } text = text.replaceAll(">", ">"); text = text.replaceAll("<", "<"); translateToHTML(text, input, output); output.format("%s\r\n", "</pre>"); output.format("%s\r\n", "</body>"); output.format("%s\r\n", "</html>"); } catch (Exception ex) { System.out.println(ex); } finally { try { input.close(); output.close(); } catch (Exception ex) { } } }
阅读全文
0 0
- 将Java文件转换为HTML文件
- 将CHM文件转换为HTML文件
- 将Excel文件转换为Html
- 将Excel文件转换为Html
- 将Schema文件转换为Java文件
- 将html文件转换为pdf格式的文件
- Html文件转CHM文件类--------用C#实现将html文件转换为chm文件
- Html文件转CHM文件类--------用C#实现将html文件转换为chm文件
- java编程将HTML文件转换成PDF文件
- java编程将HTML文件转换成PDF文件
- java编程将HTML文件转换成PDF文件
- 将CHM文件转换为HTML或PDF
- 将RTF文件转换为HTML的方法(借助Office2003)
- 将 Word 文件 转换 为 HTML 工具类
- Linux下批量将md文件批量转换为html文件
- linux shell将文本文件转换为html文件
- 将WORD文件转换成HTML文件
- 将PPT文件转换成HTML文件
- thinkphp表现层中的路径问题
- 第七章 常用类
- linux磁盘分区管理
- RedisTemplate 的hash结构测试类, 使用场景 购物车
- LeetCode 167. Two Sum II
- 将Java文件转换为HTML文件
- JavaScrip的数据类型及变量
- 第八章 集合
- Javaweb题
- ReactNative之FlatList组件使用方法总结
- web性能优化之DOM篇
- jdbc在模糊搜索时候报错:Parameter index out of range
- 字符串与字符数组的区别
- 可编辑的select下拉框
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
均金念什么
全球均温或升高7度
均意思
均的组词
诸葛均
满丰风均女人
慕梓均顾清欢结局
均成语
满丰风均女人照片
均胜电子股票
苏雅薄元均免费阅读
均方差
均怎么读
长沙房价均价
李广均枪毙现场
李广均事件
移动支付月均消费
河太均
2100年全球均温或升高7摄氏度
iphone11均为英特尔基带
苦乐不均
新乐房价现在均价在多少啊
乐上
有乐
杭州房价均价
房子均价
深圳房价均价
房价均价是什么意思
房子均价是什么意思
均价是什么意思
股票均价是什么意思
房子均价4500是什么意思
深圳平均房价
不再公布楼市均价
唐山二手房本月均价
新2019年新余各个楼盘均价
均值不等式
均值
样本均值
均值的方差
均值怎么算