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入门到精通教程
查看: 271|回复: 0

[jsp学习]将java源文件生成html并且高亮彩色显示

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

    [LV.1]初来乍到

    发表于 2014-10-1 22:32:22 | 显示全部楼层 |阅读模式
    下面程序能将java源代码生成HTML文件并且高亮彩色显示,点击这里可以观看效果。 运行方法:只要在命令行输入java源文件名和生成的html文件名。
    如:

    C:java>java java2html GetURL.java ds1.html
    成功生成ds1.html文件 下面是这个程序的源码:

    import java.util.*;
    import java.io.*; public class java2html
    {
         private static boolean comment=false;
      
       
       
         
       

       
       
      


      
       
       
       
      
       
       
       
         private static boolean lineComment=false;
         private static boolean START = true;
         private static boolean END = false;
         private static boolean backslash=false;
         private static boolean stringlit = false;
         private static boolean stringlit2 = false;
         private static int tabWidth = 0;
         private static String tab=" ";
         private static Collection varList = new ArrayList();
         private static StringBuffer out = new StringBuffer();     private static Collection primitives = new ArrayList();
         static
         {
             String[] primString =
             {"int","Integer", "byte","Byte", "boolean","Boolean", "short", "Short", "long", "Long", "char", "Character", "float", "Float", "double", "Double", "String", "void","StringBuffer","Collection"};
             for(int i=0; i<primString.length; i++)
             primitives.add(primString);
         }     private static Collection operators = new ArrayList();
         static
         {
             String[] operString =
             {"=", ">", "<", "!", "~", "?", ":", "==", "<=", ">=", "!=", "&&", "||", "++", "--", "+", "-", "*", "/", "&", "|", "^", "%", ">>", "<<", ">>>", "+=", "-=", "*=", "/=", "&=", "|=", "^=", "%=", "<<=", ">>=", "<<<"};
             for(int i=0; i<operString.length; i++)
             operators.add(operString);
         }
         private static Collection string = new ArrayList();
         static
         {
             String[] stringString =
             {"""};
             for(int i=0; i<stringString.length; i++)
             string.add(stringString);
         }     private static Collection flow = new ArrayList();
         static
         {
             String[] flowString =
             {"true" , "false" , "null" , "import"};
             for(int i=0; i<flowString.length; i++)
             flow.add(flowString);
         }     private static Collection resWords = new ArrayList();
         static
         {
             String[] reswString =
             {"private", "static", "new", "public", "final", "class", "this",  "synchronized", "native", "package", "if", "then", "else", "finally", "switch", "do", "while", "case", "default", "return", "break", "continue", "throw", "throws", "catch", "try", "for","protected"};
             for(int i=0; i<reswString.length; i++)
             resWords.add(reswString);
         }     private static void java2html(String fileName)
         { }     public static void tabIncrease(boolean up)
         {
             //prComment(START);out.append("tabs");prComment(END);
             if(up)
             {
                 tabWidth++;
             } else
             {
                 tabWidth--;
             }
             tab=" ";
             for(int i=tabWidth; i>0; i--)
             {
                 tab = tab+"    ";
             }
         }     public static void prTitle(String str)
         {
             out.append("<TITLE>" + str + "</TITLE>");
             out.append("
    ");
         }
         public static void prPrimitive(String str)
         {
             if (comment || lineComment)
             {
                 prJComment(str);
             } else if (stringlit)
             {
                 prStringLit(str);
             } else
             {
                 out.append("<b><FONT color=red>" + str + "</FONT></b>");
                 out.append("
    ");
             }
         }     public static void prVar(String str)
         {
             if (comment || lineComment)
             {
                 prJComment(str);
             } else if (stringlit)
             {
                 prStringLit(str);
             } else
             {
                 out.append("<FONT color=red>" + str + "</FONT>");
                 out.append("
    ");
             }
         }     public static void prFlow(String str)
         {
             if (comment || lineComment)
             {
                 prJComment(str);
             } else if (stringlit)
             {
                 prStringLit(str);
             } else
             {
                 out.append("<b><FONT color=black>" + str + "</FONT></b>");
                 out.append("
    ");
             }
         }     public static void prResWord(String str)
         {
             if (comment || lineComment)
             {
                 prJComment(str);
             } else if (stringlit)
             {
                 prStringLit(str);
             } else
             {
                 out.append("<b><FONT color=black>" + str + "</FONT></b>");
                 out.append("
    ");
             }
         }     public static void prNormal(String str)
         {
             if (comment || lineComment)
             {
                 prJComment(str);
             } else if (stringlit)
             {
                 prStringLit(str);
             } else
             {
                 out.append(str);
                 out.append("
    ");
             }
         }     public static void prJComment(String str)
         {
             out.append("<FONT color=#888888>" + str + "</FONT>");
             out.append("
    ");
         }     public static void prOperator(String str)
         {
             if (comment || lineComment)
             {
                 prJComment(str);
             } else if (stringlit)
             {
                 prStringLit(str);
             } else
             {
                 out.append("<FONT color=black>" + str + "</FONT>
    ");
             }
         }     public static void prNumber(String str)
         {
             if (comment || lineComment)
             {
                 prJComment(str);
             } else if (stringlit)
             {
                 prStringLit(str);
             } else
             {
                 out.append("<FONT color=blue>" + str + "</FONT>
    ");
             }
         }     public static void prStringLit(String str)
         {
             out.append(str +"
    ");
         }     public static void prHead(boolean start)
         {
             if(start)
             {
                 out.append("<HEAD>");
                 out.append("
    ");
             } else
             {
                 out.append("</HEAD>");
                 out.append("
    ");
             }
         }     public static void prBody(boolean start)
         {
             if (start)
             {             out.append("<BODY bgcolor=white color=red>");
                 out.append("
    ");
                 out.append("<tt><FONT color=red>");
                 out.append("
    ");
             } else
             {
                 out.append("</FONT></tt>");
                 out.append("
    ");
                 out.append("</BODY>");
                 out.append("
    ");
             }
         }     public static void prComment(boolean start)
         {
             if(start)
             {
                 out.append("<!--");
                 out.append("
    ");
             } else
             {
                 out.append("-->");
                 out.append("
    ");
             }
         }     public static void prHtml(boolean start)
         {
             if(start)
             {
                 out.append("<HTML>");
                 out.append("
    ");
             } else
             {
                 out.append("</HTML>");
                 out.append("
    ");
             }
         }     public static void prBr(boolean start)
         {
             if(start)
             {
                 out.append("<BR>");
                 out.append("
    ");
             } else
             {
                 out.append("<BR>");
                 out.append("
    ");
             }
             out.append(tab);
         }     public static void prP(boolean start)
         {
             if(start)
             {
                 out.append("<P>");
                 out.append("
    ");
             } else
             {
                 out.append("</P>");
                 out.append("
    ");
             }
         }     public static void prTable(boolean start)
         {
             if(start)
             {
                 out.append("<TABLE>");
                 out.append("
    ");
             } else
             {
                 out.append("</TABLE>");
                 out.append("
    ");
             }
         }     public static void prTr(boolean start)
         {
             if(start)
             {
                 out.append("<TR>");
                 out.append("
    ");
             } else
             {
                 out.append("</TR>");
                 out.append("
    ");
             }
         }     public static void prTd(boolean start)
         {
             if(start)
             {
                 out.append("<TD>");
                 out.append("
    ");
             } else
             {
                 out.append("</TD>");
                 out.append("
    ");
             }
         }     public static void prStringOne(boolean start)
         {
             if (comment || lineComment)
             {
                 prJComment(""");
             } else if(start)
             {
                 out.append("<FONT color=green>"
    ");
                 stringlit=true;
             } else
             {
                 out.append(""</FONT>
    ");
                 stringlit=false;
             }
         }     public static void prStringTwo(boolean start)
         {
             if (comment || lineComment)
             {
                 prJComment(""");
             } else if(start)
             {
                 out.append("<FONT color=green>"
    ");
                 stringlit=true;
                 stringlit2=true;
             } else
             {
                 out.append(""</FONT>
    ");
                 stringlit=false;
                 stringlit2=false;
             }
         }     public static void main(String[] args)
         {
             if (args.length<2)
             {
                 System.out.println("
    错误:参数不足!");
                 System.out.println("java2html [源文件] [将生成文件名]");
             }
             else
             {
                 FileInputStream file;
                 StreamTokenizer st;
                 String htmlFile = args[1];
                 boolean isVar=false;
                 putHtml puthtml = new putHtml();
                 boolean firstOfComment=false;
                 int b = 8;
                 int a = 10 / b;
                 try
                 {
                     file = new FileInputStream(args[0]);
                     Reader r = new BufferedReader(new InputStreamReader(file));  
                     st = new StreamTokenizer(r);
                     st.eolIsSignificant(true);
                     st.ordinaryChar(""");
                     st.ordinaryChar("/");
                     st.ordinaryChar("\");
                     st.ordinaryChar(".");
                     st.ordinaryChar(""");
                     prHtml(START);
                     prHead(START);
                     prTitle(args[0]);
                     prHead(END);
                     prBody(START);                 while(st.nextToken() != StreamTokenizer.TT_EOF)
                     {
                         switch(st.ttype)
                         {
                             case StreamTokenizer.TT_WORD:
                             if(firstOfComment)
                             {
                                 if((!comment && !lineComment)) prOperator("/");
                             }
                             firstOfComment=false;
                             if(primitives.contains(st.sval))
                             {
                                 prPrimitive(st.sval);
                                 isVar=true;
                                 backslash=false;
                                 break;
                             }
                             if(flow.contains(st.sval))
                             {
                                 isVar=false;
                                 prNumber(st.sval);
                                 backslash=false;
                                 break;
                             }
                             if(resWords.contains(st.sval))
                             {
                                 isVar=false;
                                 prResWord(st.sval);
                                 backslash=false;
                                 break;
                             }
                             if(varList.contains(st.sval))
                             {
                                 isVar=false;
                                 prVar(st.sval);
                                 backslash=false;
                                 break;
                             }
                             if(isVar && !(varList.contains(st.sval)))
                             {
                                 varList.add(st.sval);
                                 prVar(st.sval);
                             } else
                             {
                                 prNormal(st.sval);
                             }
                             backslash=false;
                             break;
                             case StreamTokenizer.TT_EOL:
                             isVar=false;
                             if(firstOfComment)
                             {
                                 prOperator("/");
                                 firstOfComment=false;
                             }
                             firstOfComment=false;
                             lineComment=false;
                             prBr(START);
                             backslash=false;
                             break;
                             case StreamTokenizer.TT_NUMBER:
                             isVar=false;
                             if(firstOfComment)
                             {
                                 prOperator("/");
                                 firstOfComment=false;
                             }
                             firstOfComment=false;
                             if(String.valueOf((char)st.ttype).equals("."))
                             {
                                 prFlow(".");
                                 backslash=false;
                                 break;
                             }
                             prNumber(new Double(st.nval).toString());
                             backslash=false;
                             break;
                             default:
                             isVar=false;
                             if(operators.contains(String.valueOf((char)st.ttype)))
                             {
                                 if (firstOfComment)
                                 {
                                     if (comment)
                                     {
                                         //there has been a star, waiting for slash
                                         if (String.valueOf((char)st.ttype).equals("/"))
                                         {
                                             firstOfComment=false;
                                             prJComment("*/");
                                             comment=false;
                                             backslash=false;
                                             firstOfComment=false;
                                             break;
                                         } else firstOfComment=false;
                                     } else
                                     {
                                         //there has been a slash, waiting for slash or star
                                         if (String.valueOf((char)st.ttype).equals("/"))
                                         {
                                             if (!stringlit)
                                             {
                                                 lineComment=true;
                                                 prJComment("//");
                                             } else
                                             {
                                                 lineComment=false;
                                                 prNormal("//");
                                             }
                                             firstOfComment=false;
                                             comment=false;
                                             backslash=false;
                                             break;
                                         } else if (String.valueOf((char)st.ttype).equals("*"))
                                         {
                                             if (!stringlit)
                                             {
                                                 comment=true;
                                                 prJComment("/*");
                                             } else
                                             {
                                                 comment=false;
                                                 prNormal("/*");
                                             }
                                             firstOfComment=false;
                                             lineComment=false;
                                             backslash=false;
                                             break;
                                         } else
                                         {
                                             firstOfComment=false;
                                             if (!lineComment) prOperator("/");
                                             prOperator(String.valueOf((char)st.ttype));
                                             backslash=false;
                                             break;
                                         }
                                     }
                                 } else
                                 {
                                     if (comment)
                                     {
                                         //waiting for star
                                         if (String.valueOf((char)st.ttype).equals("*"))
                                         {
                                             firstOfComment=true;
                                             backslash=false;
                                             break;
                                         } else firstOfComment=false;
                                     } else
                                     {
                                         //waiting for slash
                                         if (String.valueOf((char)st.ttype).equals("/"))
                                         {
                                             firstOfComment=true;
                                             backslash=false;
                                             break;
                                         } else
                                         {
                                             firstOfComment=false;
                                             prOperator(String.valueOf((char)st.ttype));
                                             backslash=false;
                                             break;
                                         }
                                     }
                                 }
                             }
                             if(String.valueOf((char)st.ttype).equals("""))
                             {
                                 if(firstOfComment)
                                 {
                                     if (!(comment || lineComment)) prOperator("/");
                                     firstOfComment=false;
                                 }
                                 if (!backslash) prStringTwo(!stringlit);
                                 else prNormal(""");
                                 backslash=false;
                                 break;
                             }
                             if(String.valueOf((char)st.ttype).equals("\"))
                             {
                                 if(firstOfComment)
                                 {
                                     prOperator("/");
                                     firstOfComment=false;
                                 }
                                 prNormal("\");
                                 backslash=!backslash;
                                 break;
                             }
                             if(String.valueOf((char)st.ttype).equals("""))
                             {
                                 if(firstOfComment)
                                 {
                                     prOperator("/");
                                     firstOfComment=false;
                                 }
                                 if (!backslash && !stringlit2) prStringOne(!stringlit);
                                 else prNormal(""");
                                 backslash=false;
                                 break;
                             }
                             if(String.valueOf((char)st.ttype).equals(";"))
                             {
                                 if(firstOfComment)
                                 {
                                     prOperator("/");
                                     firstOfComment=false;
                                 }
                                 prFlow(";");
                                 backslash=false;
                                 break;
                             }
                             if(String.valueOf((char)st.ttype).equals(","))
                             {
                                 if(firstOfComment)
                                 {
                                     prOperator("/");
                                     firstOfComment=false;
                                 }
                                 prFlow(",");
                                 backslash=false;
                                 break;
                             }
                             if(String.valueOf((char)st.ttype).equals("."))
                             {
                                 if(firstOfComment)
                                 {
                                     prOperator("/");
                                     firstOfComment=false;
                                 }
                                 prFlow(".");
                                 backslash=false;
                                 break;
                             }
                             if(String.valueOf((char)st.ttype).equals(")"))
                             {
                                 if(firstOfComment)
                                 {
                                     prOperator("/");
                                     firstOfComment=false;
                                 }
                                 prFlow(")");
                                 backslash=false;
                                 break;
                             }
                             if(String.valueOf((char)st.ttype).equals("("))
                             {
                                 if(firstOfComment)
                                 {
                                     prOperator("/");
                                     firstOfComment=false;
                                 }
                                 prFlow("(");
                                 backslash=false;
                                 break;
                             }
                             if(String.valueOf((char)st.ttype).equals("]"))
                             {
                                 if(firstOfComment)
                                 {
                                     prOperator("/");
                                     firstOfComment=false;
                                 }
                                 prFlow("]");
                                 backslash=false;
                                 break;
                             }
                             if(String.valueOf((char)st.ttype).equals("["))
                             {
                                 if(firstOfComment)
                                 {
                                     prOperator("/");
                                     firstOfComment=false;
                                 }
                                 prFlow("[");
                                 backslash=false;
                                 break;
                             }
                             if(String.valueOf((char)st.ttype).equals("{"))
                             {
                                 if(firstOfComment)
                                 {
                                     prOperator("/");
                                     firstOfComment=false;
                                 }
                                 tabIncrease(true);
                                 prFlow("{");
                                 backslash=false;
                                 break;
                             }
                             if(String.valueOf((char)st.ttype).equals("}"))
                             {
                                 if(firstOfComment)
                                 {
                                     prOperator("/");
                                     firstOfComment=false;
                                 }
                                 tabIncrease(false);
                                 if (out.substring(out.length()-24).equals("    "))
                                 {
                                     out.delete(out.length()-24,out.length());
                                 }
                                 prFlow("}");
                                 backslash=false;
                                 break;
                             }
                             prNormal(String.valueOf((char)st.ttype));
                             backslash=false;
                             break;
                         }
                     }
                     prBody(END);
                     prHtml(END);
                 } catch (FileNotFoundException e)
                 {
                     System.out.println("Could not find the file: " + args[0]);
                 } catch (IOException e)
                 {
                     e.printStackTrace();
                 } catch (ArrayIndexOutOfBoundsException e)
                 {
                     System.out.println("There is no input file");
                 }
                 if(puthtml.put(htmlFile,out.toString()))
                 {
                     System.out.println("成功生成"+htmlFile+"文件");
                 }
                 else
                 {
                     System.out.println("生成"+htmlFile+"文件失败");
                 }         }
         }
    } class putHtml
    {
         boolean put(String fileName , String fileBody)
         {         boolean returnMessage = false;
             String file = fileName;
             try
             {
                 PrintWriter pw = new PrintWriter(new FileOutputStream(file));
                 pw.println(fileBody);
                 pw.close();
                 returnMessage = true ;
             } catch(IOException e)
             {
                 returnMessage = false;
             }
             return returnMessage;
         }
    }  
      
         
          
          
            
            

             
            

             
            
          
         

       
       
       
       
          
       
       
         
       
       
       
       
      


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

    使用道具 举报

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

    本版积分规则

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

    GMT+8, 2025-2-27 05:41 , Processed in 0.414238 second(s), 48 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

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