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

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

primitives.add(primstring[i]);

}

private static collection operators = new arraylist();

static

{

string[] operstring =

{“=”, “>”, “=”, “!=”, “&&”, “||”, “++”, “–“, “+”, “-“, “*”, “/”, “&”, “|”, “^”, “%”, “>>”, “<>>”, “+=”, “-=”, “*=”, “/=”, “&=”, “|=”, “^=”, “%=”, “<<=”, “>>=”, “<<

for(int i=0; i

operators.add(operstring[i]);

}

private static collection string = new arraylist();

static

{

string[] stringstring =

{“\””};

for(int i=0; i

string.add(stringstring[i]);

}

private static collection flow = new arraylist();

static

{

string[] flowstring =

{“true” , “false” , “null” , “import”};

for(int i=0; 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

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+”    ”;

}

}

public static void prtitle(string str)

{

out.append(“

” + str + “”);

out.append(“\n”);

}

public static void prprimitive(string str)

{

if (comment || linecomment)

{

prjcomment(str);

} else if (stringlit)

{

prstringlit(str);

} else

{

out.append(“” + str + “”);

out.append(“\n”);

}

}

public static void prvar(string str)

{

if (comment || linecomment)

{

prjcomment(str);

} else if (stringlit)

{

prstringlit(str);

} else

{

out.append(“” + str + “”);

out.append(“\n”);

}

}

public static void prflow(string str)

{

if (comment || linecomment)

{

prjcomment(str);

} else if (stringlit)

{

prstringlit(str);

} else

{

out.append(“” + str + “”);

out.append(“\n”);

}

}

public static void prresword(string str)

{

if (comment || linecomment)

{

prjcomment(str);

} else if (stringlit)

{

prstringlit(str);

} else

{

out.append(“” + str + “”);

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(“” + str + “”);

out.append(“\n”);

}

public static void properator(string str)

{

if (comment || linecomment)

{

prjcomment(str);

} else if (stringlit)

{

prstringlit(str);

} else

{

out.append(“” + str + “\n”);

}

}

public static void prnumber(string str)

{

if (comment || linecomment)

{

prjcomment(str);

} else if (stringlit)

{

prstringlit(str);

} else

{

out.append(“” + str + “\n”);

}

}

public static void prstringlit(string str)

{

out.append(str +”\n”);

}

public static void prhead(boolean start)

{

if(start)

{

out.append(“

”);

out.append(“\n”);

} else

{

out.append(“”);

out.append(“\n”);

}

}

public static void prbody(boolean start)

{

if (start)

{

out.append(“

”);

out.append(“\n”);

out.append(“”);

out.append(“\n”);

} else

{

out.append(“”);

out.append(“\n”);

out.append(“”);

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

out.append(“\n”);

} else

{

out.append(“”);

out.append(“\n”);

}

}

public static void prbr(boolean start)

{

if(start)

{

out.append(“
”);

out.append(“\n”);

} else

{

out.append(“
”);

out.append(“\n”);

}

out.append(tab);

}

public static void prp(boolean start)

{

if(start)

{

out.append(“

”);

out.append(“\n”);

} else

{

out.append(“

”);

out.append(“\n”);

}

}

public static void prtable(boolean start)

{

if(start)

{

out.append(“

out.append(“\n”);

} else

{

out.append(“

”);

out.append(“\n”);

}

}

public static void prtr(boolean start)

{

if(start)

{

out.append(“

”);

out.append(“\n”);

} else

{

out.append(“

”);

out.append(“\n”);

}

}

public static void prtd(boolean start)

{

if(start)

{

out.append(“

”);

out.append(“\n”);

} else

{

out.append(“

”);

out.append(“\n”);

}

}

public static void prstringone(boolean start)

{

if (comment || linecomment)

{

prjcomment(“\”);

} else if(start)

{

out.append(“\\n”);

stringlit=true;

} else

{

out.append(“\\n”);

stringlit=false;

}

}

public static void prstringtwo(boolean start)

{

if (comment || linecomment)

{

prjcomment(“\””);

} else if(start)

{

out.append(“\”\n”);

stringlit=true;

stringlit2=true;

} else

{

out.append(“\”\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(“    ”))

{

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值