欢迎光临
我们一直在努力

将java代码生成html并且高亮显示-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

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[i]);
    }

    private static collection operators = new arraylist();
    static
    {
        string[] operstring =
        {“=”, “>”, “<“, “!”, “~”, “?”, “:”, “==”, “<=”, “>=”, “!=”, “&&”, “||”, “++”, “–“, “+”, “-“, “*”, “/”, “&”, “|”, “^”, “%”, “>>”, “<<“, “>>>”, “+=”, “-=”, “*=”, “/=”, “&=”, “|=”, “^=”, “%=”, “<<=”, “>>=”, “<<<“};
        for(int i=0; i<operstring.length; i++)
        operators.add(operstring[i]);
    }

    private static collection string = new arraylist();
    static
    {
        string[] stringstring =
        {“\””};
        for(int i=0; i<stringstring.length; i++)
        string.add(stringstring[i]);
    }

    private static collection flow = new arraylist();
    static
    {
        string[] flowstring =
        {“true” , “false” , “null” , “import”};
        for(int i=0; i<flowstring.length; i++)
        flow.add(flowstring[i]);
    }

    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[i]);
    }

    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+”&nbsp;&nbsp;&nbsp;&nbsp;”;
        }
    }

    public static void prtitle(string str)
    {
        out.append(“<title>” + str + “</title>”);
        out.append(“\n”);
    }

    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(“\n”);
        }
    }

    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(“\n”);
        }
    }

    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(“\n”);
        }
    }

    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(“\n”);
        }
    }

    public static void prnormal(string str)
    {
        if (comment || linecomment)
        {
            prjcomment(str);
        } else if (stringlit)
        {
            prstringlit(str);
        } else
        {
            out.append(str);
            out.append(“\n”);
        }
    }

    public static void prjcomment(string str)
    {
        out.append(“<font color=#888888>” + str + “</font>”);
        out.append(“\n”);
    }

    public static void properator(string str)
    {
        if (comment || linecomment)
        {
            prjcomment(str);
        } else if (stringlit)
        {
            prstringlit(str);
        } else
        {
            out.append(“<font color=black>” + str + “</font>\n”);
        }
    }

    public static void prnumber(string str)
    {
        if (comment || linecomment)
        {
            prjcomment(str);
        } else if (stringlit)
        {
            prstringlit(str);
        } else
        {
            out.append(“<font color=blue>” + str + “</font>\n”);
        }
    }

    public static void prstringlit(string str)
    {
        out.append(str +”\n”);
    }

    public static void prhead(boolean start)
    {
        if(start)
        {
            out.append(“<head>”);
            out.append(“\n”);
        } else
        {
            out.append(“</head>”);
            out.append(“\n”);
        }
    }

    public static void prbody(boolean start)
    {
        if (start)
        {

            out.append(“<body bgcolor=white color=red>”);
            out.append(“\n”);
            out.append(“<tt><font color=red>”);
            out.append(“\n”);
        } else
        {
            out.append(“</font></tt>”);
            out.append(“\n”);
            out.append(“</body>”);
            out.append(“\n”);
        }
    }

    public static void prcomment(boolean start)
    {
        if(start)
        {
            out.append(“<!–“);
            out.append(“\n”);
        } else
        {
            out.append(“–>”);
            out.append(“\n”);
        }
    }

    public static void prhtml(boolean start)
    {
        if(start)
        {
            out.append(“<html>”);
            out.append(“\n”);
        } else
        {
            out.append(“</html>”);
            out.append(“\n”);
        }
    }

    public static void prbr(boolean start)
    {
        if(start)
        {
            out.append(“<br>”);
            out.append(“\n”);
        } else
        {
            out.append(“<br>”);
            out.append(“\n”);
        }
        out.append(tab);
    }

    public static void prp(boolean start)
    {
        if(start)
        {
            out.append(“<p>”);
            out.append(“\n”);
        } else
        {
            out.append(“</p>”);
            out.append(“\n”);
        }
    }

    public static void prtable(boolean start)
    {
        if(start)
        {
            out.append(“<table>”);
            out.append(“\n”);
        } else
        {
            out.append(“</table>”);
            out.append(“\n”);
        }
    }

    public static void prtr(boolean start)
    {
        if(start)
        {
            out.append(“<tr>”);
            out.append(“\n”);
        } else
        {
            out.append(“</tr>”);
            out.append(“\n”);
        }
    }

    public static void prtd(boolean start)
    {
        if(start)
        {
            out.append(“<td>”);
            out.append(“\n”);
        } else
        {
            out.append(“</td>”);
            out.append(“\n”);
        }
    }

    public static void prstringone(boolean start)
    {
        if (comment || linecomment)
        {
            prjcomment(“\”);
        } else if(start)
        {
            out.append(“<font color=green>\\n”);
            stringlit=true;
        } else
        {
            out.append(“\</font>\n”);
            stringlit=false;
        }
    }

    public static void prstringtwo(boolean start)
    {
        if (comment || linecomment)
        {
            prjcomment(“\””);
        } else if(start)
        {
            out.append(“<font color=green>\”\n”);
            stringlit=true;
            stringlit2=true;
        } else
        {
            out.append(“\”</font>\n”);
            stringlit=false;
            stringlit2=false;
        }
    }

    public static void main(string[] args)
    {
        if (args.length<2)
        {
            system.out.println(“\n错误:参数不足!”);
            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(“&nbsp;&nbsp;&nbsp;&nbsp;”))
                            {
                                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;
    }
}

 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 将java代码生成html并且高亮显示-JSP教程,Java技巧及代码
分享到: 更多 (0)

相关推荐

  • 暂无文章