源文件变成了一团遭。这是因为,文本中的许多回车和空格都被忽略了。而有些符号在html中有特殊的含义,引起了更复杂的局面。
为了源文件能正常显示,我们必须为文本加上适当的标签。对特殊的符号进行转义处理。
常用的有:
HTML 需要转义的实体:
& ---> &
空格 --->
< ---> <
> ---> >
" ---> "
此外,根据源码的特点,可以把 TAB 转为4个空格来显示。
TAB --->
为了显示为换行,需要在行尾加<br/>标签。
为了显示美观,对关键字加粗显示,即在关键字左右加<b>标签。比如:
<b>public</b>
对单行注释文本用绿色显示,可以使用<font>标签,形如:
<font color=green>//这是我的单行注释!</font>
注意:如果“//”出现在字符串中,则注意区分,不要错误地变为绿色。
不考虑多行注释的问题(/* … / 或 /* … */)
你的任务是:编写程序,把给定的源文件转化为相应的html表达。
【输入、输出格式要求】
与你的程序同一目录下,存有源文件 a.txt,其中存有标准的java源文件。要求编写程序把它转化为b.html。
a.txt
// 我的工具类
public class MyTool
{
public static void main(String[] args)
{
int a = 100;
int b = 20;
if(a>b && true)
System.out.println(a);
else
System.out.println("this! //aaa//kkk"); // 测试注释显示是否正确
}
}
b.html
<html><body>
<br/>
<font color=green>// 我的工具类</font><br/>
<b>public</b> <b>class</b> MyTool<br/>
{<br/>
<b>public</b> <b>static</b> <b>void</b> main(String[] args)<br/>
{<br/>
int a = 100;<br/>
int b = 20;<br/>
if(a>b && true)<br/>
System.out.println(a);<br/>
else<br/>
System.out.println("this! //aaa//kkk"); <font color=green>// 测试注释显示是否正确</font><br/>
}<br/>
}<br/>
</body></html>
例如:目前的 a.txt 文件与 b.html 文件就是对应的。可以用记事本打开b.html查看转换后的内容。用浏览器打开b.html则可以看到显示的效果。
注意:实际评测的时候使用的a.txt与示例是不同的。
public class Main {
private static FileWriter fileWriter;
public static void main(String[] args) {
File readFile = new File("a.txt");
File writeFile = new File("b.html");
String in;
try {
FileReader fileReader = new FileReader(readFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
fileWriter = new FileWriter(writeFile);
fileWriter.write("<html><body>\n");
while ((in = bufferedReader.readLine()) != null) {
int pstartflag = -1;
int pendflag = -1;
int sstartflag = -1;
int sendflag = -1;
int cstartflag = -1;
int cendflag = -1;
int vstartflag = -1;
int vendflag = -1;
for (int i = 0; i < in.length(); i++) {
char is = in.charAt(i);
if (in.contains("public")) {
pstartflag = in.indexOf("public");
pendflag = pstartflag + 5;
}
if (in.contains("static")) {
sstartflag = in.indexOf("static");
sendflag = sstartflag + 5;
}
if (in.contains("class")) {
cstartflag = in.indexOf("class");
cendflag = cstartflag + 4;
}
if (in.contains("void")) {
vstartflag = in.indexOf("void");
vendflag = vstartflag + 3;
}
if (i == pstartflag || i == sstartflag || i == cstartflag
|| i == vstartflag) {
fileWriter.write("<b>");
}
if (is == ' ') {
fileWriter.write(" ");
} else if (is == ' ') {
fileWriter.write(" ");
} else if (is == '&') {
fileWriter.write("&");
} else if (is == '<') {
fileWriter.write("<");
} else if (is == '>') {
fileWriter.write(">");
} else if (is == '"') {
fileWriter.write(""");
} else
if (is == '/'
&& in.charAt(i + 1) == '/'
&& !(in.substring(i,in.length()).contains("\"") && in
.substring(0, i).contains("\""))) {
fileWriter.write("<font color=green>");
fileWriter.write(is);
} else {
fileWriter.write(is);
}
if (i == sendflag || i == vendflag || i == pendflag
|| i == cendflag) {
fileWriter.write("</b>");
}
}
if (in.contains("//")) {
fileWriter.write("</font>");
}
fileWriter.write("<br/>\n");
}
fileWriter.write("</body></html>\n");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}