将Java文件转换为HTML文件

项目中碰到的一个小问题,现将解决方法开源,主要由三步组成。废话不多上代码。

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 exception
			e.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];
		      }
		    }
		  }

3 代码高亮功能实现与按照HTML格式进行封装
 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) {
		      }
		    }
		  }





  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值