分享
 
 
 

将java代码生成html并且高亮显示

王朝java/jsp·作者佚名  2005-11-24
窄屏简体版  字體: |||超大  

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;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有