Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 272|回复: 0

[Java基础知识]一个给代码作高亮度彩色处理的类

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-2 13:01:01 | 显示全部楼层 |阅读模式
    下面是源码,经它本身处理后的效果。
    import java.util.HashMap;
    import java.util.Hashtable;
    import java.io.*;
    public class CodeViewer {
         private static HashMap reservedWords = new HashMap(150);               

         private boolean inMultiLineComment = false;

         private String backgroundColor = "#ffffff";

         private String commentStart = "<font color="#aa0000"><i>";

         private String commentEnd = "</i></font>";

         private String stringStart = "<font color="#000099">";

         private String stringEnd = "</font>";

         private String reservedWordStart = "<b>";

         private String reservedWordEnd = "</b>";

         /**
          * Load all keywords at class loading time.
          */

         static {
             loadKeywords();
         }

         /**
          * Gets the HTML for the start of a comment block.
          */

         public String getCommentStart() {
             return commentStart;
         }

         /**
          * Sets the html for the start of a comment block.
          */

         public void setCommentStart(String commentStart) {
             this.commentStart = commentStart;
         }

         /**
          * Gets the html for the end of a comment block.
          */

         public String getCommentEnd() {
             return commentEnd;
         }

         /**
          * Sets the html for the end of a comment block.
          */

         public void setCommentEnd(String commentEnd) {
             this.commentEnd = commentEnd;
         }

         /**
          * Gets the html for the start of a String.
          */

         public String getStringStart() {
             return stringStart;
         }

         /**
          * Sets the html for the start of a String.
          */

         public void setStringStart(String stringStart) {
             this.stringStart = stringStart;
         }

         /**
          * Gets the html for the end of a String.
          */

         public String getStringEnd() {
             return stringEnd;
         }

         /**
          * Sets the html for the end of a String.
          */

         public void setStringEnd(String stringEnd) {
             this.stringEnd = stringEnd;
         }

         /**
          * Gets the html for the start of a reserved word.
          */

         public String getReservedWordStart() {
             return reservedWordStart;
         }

         /**
          * Sets the html for the start of a reserved word.
          */

         public void setReservedWordStart(String reservedWordStart) {
             this.reservedWordStart = reservedWordStart;
         }

         /**
          * Gets the html for the end of a reserved word.
          */

         public String getReservedWordEnd() {
             return reservedWordEnd;
         }

         /**
          * Sets the html for the end of a reserved word.
          */

         public void setReservedWordEnd(String reservedWordEnd) {
             this.reservedWordEnd = reservedWordEnd;
         }

         /**
          * Passes off each line to the first filter.
          *
          * @param line
          *            The line of Java code to be highlighted.
          */

         public String syntaxHighlight(String line) {
             return htmlFilter(line);
         }

         /*
          * Filter html tags that appear in the java source into more benign text
          * that won"t disrupt the output.
          */

         private String htmlFilter(String line) {
             if (line == null || line.equals("")) {
                 return "";
             }
             // replace ampersands with HTML escape sequence for ampersand;
                     line = replace(line, "&", "&#38;");
                     line=replace(line," "," ");
                     line=replace(line,"        ","    ");
             
             // replace " sequences with HTML escape sequences;
             line = replace(line, "\"", "&#92;&#34");
             // replace the \ with HTML escape sequences. fixes a problem when
             // backslashes preceed quotes.
             line = replace(line, "\\", "&#92;&#92;");
             // replace less-than signs which might be confused
             // by HTML as tag angle-brackets;
             line = replace(line, "<", "&#60;");
             // replace greater-than signs which might be confused
             // by HTML as tag angle-brackets;
             line = replace(line, ">", "&#62;");
             return multiLineCommentFilter(line);
         }

         /*
          * Filter out multiLine comments. State is kept with a private boolean
          * variable.
          */

         private String multiLineCommentFilter(String line) {
             if (line == null || line.equals("")) {
                 return "";
             }
             StringBuffer buf = new StringBuffer();
             int index;
             // First, check for the end of a multi-line comment.
             if (inMultiLineComment && (index = line.indexOf("*/")) > -1
                     && !isInsideString(line, index)) {
                 inMultiLineComment = false;
                 buf.append(line.substring(0, index));
                 buf.append("*/").append(commentEnd);
                 if (line.length() > index + 2) {
                     buf.append(inlineCommentFilter(line.substring(index + 2)));
                 }
                 return buf.toString();
             }
             // If there was no end detected and we"re currently in a multi-line
             // comment, we don"t want to do anymore work, so return line.
             else if (inMultiLineComment) {
                 return line;
             }
             // We"re not currently in a comment, so check to see if the start
             // of a multi-line comment is in this line.
             else if ((index = line.indexOf("/*")) > -1
                     && !isInsideString(line, index)) {
                 inMultiLineComment = true;
                 // Return result of other filters + everything after the start
                 // of the multiline comment. We need to pass the through the
                 // to the multiLineComment filter again in case the comment ends
                 // on the same line.
                 buf.append(inlineCommentFilter(line.substring(0, index)));
                 buf.append(commentStart).append("/*");
                 buf.append(multiLineCommentFilter(line.substring(index + 2)));
                 return buf.toString();
             }
             // Otherwise, no useful multi-line comment information was found so
             // pass the line down to the next filter for processesing.
             else {
                 return inlineCommentFilter(line);
             }
         }

         /*
          * Filter inline comments from a line and formats them properly.
          */

         private String inlineCommentFilter(String line) {
             if (line == null || line.equals("")) {
                 return "";
             }
             StringBuffer buf = new StringBuffer();
             int index;
             if ((index = line.indexOf("//")) > -1 && !isInsideString(line, index)) {
                 buf.append(stringFilter(line.substring(0, index)));
                 buf.append(commentStart);
                 buf.append(line.substring(index));
                 buf.append(commentEnd);
             } else {
                 buf.append(stringFilter(line));
             }
             return buf.toString();
         }

         /*
          * Filters strings from a line of text and formats them properly.
          */

         private String stringFilter(String line) {
             if (line == null || line.equals("")) {
                 return "";
             }
             StringBuffer buf = new StringBuffer();
             if (line.indexOf(""") <= -1) {
                 return keywordFilter(line);
             }
             int start = 0;
             int startStringIndex = -1;
             int endStringIndex = -1;
             int tempIndex;
             // Keep moving through String characters until we want to stop...
             while ((tempIndex = line.indexOf(""")) > -1) {
                 // We found the beginning of a string
                 if (startStringIndex == -1) {
                     startStringIndex = 0;
                     buf.append(stringFilter(line.substring(start, tempIndex)));
                     buf.append(stringStart).append(""");
                     line = line.substring(tempIndex + 1);
                 }
                 // Must be at the end
                 else {
                     startStringIndex = -1;
                     endStringIndex = tempIndex;
                     buf.append(line.substring(0, endStringIndex + 1));
                     buf.append(stringEnd);
                     line = line.substring(endStringIndex + 1);
                 }
             }
             buf.append(keywordFilter(line));
             return buf.toString();
         }

         /*
          * Filters keywords from a line of text and formats them properly.
          */

         private String keywordFilter(String line) {
             if (line == null || line.equals("")) {
                 return "";
             }
             StringBuffer buf = new StringBuffer();
             // HashMap usedReservedWords = new HashMap(); // >= Java2 only (not
             // thread-safe)
             Hashtable usedReservedWords = new Hashtable(); // < Java2 (thread-safe)
             int i = 0, startAt = 0;
             char ch;
             StringBuffer temp = new StringBuffer();
             while (i < line.length()) {
                 temp.setLength(0);
                 ch = line.charAt(i);
                 startAt = i;
                 // 65-90, uppercase letters
                 // 97-122, lowercase letters
                 while (i < line.length()
                         && ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122))) {
                     temp.append(ch);
                     i++;
                     if (i < line.length()) {
                         ch = line.charAt(i);
                     }
                 }
                 String tempString = temp.toString();
                 if (reservedWords.containsKey(tempString)
                         && !usedReservedWords.containsKey(tempString)) {
                     usedReservedWords.put(tempString, tempString);
                     line = replace(line, tempString, (reservedWordStart
                             + tempString + reservedWordEnd));
                     i += (reservedWordStart.length() + reservedWordEnd.length());
                 } else {
                     i++;
                 }
             }
             buf.append(line);
             return buf.toString();
         }

         /**
          * Replaces all instances of oldString with newString in line.
          */

         private static final String replace(String line, String oldString,
                 String newString) {
             int i = 0;
             if ((i = line.indexOf(oldString, i)) >= 0) {
                 char[] line2 = line.toCharArray();
                 char[] newString2 = newString.toCharArray();
                 int oLength = oldString.length();
                 StringBuffer buf = new StringBuffer(line2.length);
                 buf.append(line2, 0, i).append(newString2);
                 i += oLength;
                 int j = i;
                 while ((i = line.indexOf(oldString, i)) > 0) {
                     buf.append(line2, j, i - j).append(newString2);
                     i += oLength;
                     j = i;
                 }
                 buf.append(line2, j, line2.length - j);
                 return buf.toString();
             }
             return line;
         }

         /*
          * Checks to see if some position in a line is between String start and
          * ending characters. Not yet used in code or fully working :)
          */

         private boolean isInsideString(String line, int position) {
             if (line.indexOf(""") < 0) {
                 return false;
             }
             int index;
             String left = line.substring(0, position);
             String right = line.substring(position);
             int leftCount = 0;
             int rightCount = 0;
             while ((index = left.indexOf(""")) > -1) {
                 leftCount++;
                 left = left.substring(index + 1);
             }
             while ((index = right.indexOf(""")) > -1) {
                 rightCount++;
                 right = right.substring(index + 1);
             }
             if (rightCount % 2 != 0 && leftCount % 2 != 0) {
                 return true;
             } else {
                 return false;
             }
         }


         /*
          * Load Hashtable (or HashMap) with Java reserved words. Improved list in
          * version 1.1 taken directly from Java language spec.
          */

         private static void loadKeywords() {
             reservedWords.put("abstract", "abstract");
             reservedWords.put("boolean", "boolean");
             reservedWords.put("break", "break");
             reservedWords.put("byte", "byte");
             reservedWords.put("case", "case");
             reservedWords.put("catch", "catch");
             reservedWords.put("char", "char");
             reservedWords.put("class", "class");
             reservedWords.put("const", "const");
             reservedWords.put("continue", "continue");
             reservedWords.put("default", "default");
             reservedWords.put("do", "do");
             reservedWords.put("double", "double");
             reservedWords.put("else", "else");
             reservedWords.put("extends", "extends");
             reservedWords.put("false", "false");
             reservedWords.put("final", "final");
             reservedWords.put("finally", "finally");
             reservedWords.put("float", "float");
             reservedWords.put("for", "for");
             reservedWords.put("goto", "goto");
             reservedWords.put("if", "if");
             reservedWords.put("implements", "implements");
             reservedWords.put("import", "import");
             reservedWords.put("instanceof", "instanceof");
             reservedWords.put("int", "int");
             reservedWords.put("interface", "interface");
             reservedWords.put("long", "long");
             reservedWords.put("native", "native");
             reservedWords.put("new", "new");
             reservedWords.put("package", "package");
             reservedWords.put("private", "private");
             reservedWords.put("protected", "protected");
             reservedWords.put("public", "public");
             reservedWords.put("return", "return");
             reservedWords.put("short", "short");
             reservedWords.put("static", "static");
             reservedWords.put("strictfp", "strictfp");
             reservedWords.put("super", "super");
             reservedWords.put("switch", "switch");
             reservedWords.put("synchronized", "synchronized");
             reservedWords.put("this", "this");
             reservedWords.put("throw", "throw");
             reservedWords.put("throws", "throws");
             reservedWords.put("transient", "transient");
             reservedWords.put("true", "true");
             reservedWords.put("try", "try");
             reservedWords.put("void", "void");
             reservedWords.put("volatile", "volatile");
             reservedWords.put("while", "while");
         }

             public static void main(String args[]) throws IOException {
                 CodeViewer c=new CodeViewer();
                 if (args.length<2){
                   System.out.println("
    错误:参数不足!");
                   System.out.println("使用方法java CodeViewer  [源文件名] [生成文件名(html格式)]");
                }else{
                  FileReader fr=new FileReader(args[0]);
                  BufferedReader br=new BufferedReader(fr);
                  FileWriter fw=new FileWriter(args[1],true);
                  BufferedWriter bw=new BufferedWriter(fw);

                  String s;
                  while((s=br.readLine())!=null){
                    s=c.syntaxHighlight(s);
                    bw.write(s);
                    bw.write("<br>");
                    bw.newLine();
                  }
                  bw.flush();
                  br.close();
                  fr.close();
                  bw.close();
                  fw.close();
                        
               }

         }
    }



       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/10/2/130100312.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-6-14 00:21 , Processed in 0.434350 second(s), 48 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表