源码变换

来源:互联网 发布:反有关网络用语的看法 编辑:程序博客网 时间:2024/05/13 23:36

超文本标记语言(即HTML),是用于描述网页文档的一种标记语言。
HTML通过文本来描述文档显示出来应该具有的“样子”。它主要通过标签来定义对象的显示属性或行为。
如果把java的源文件直接拷贝到HTML文档中,用浏览器直接打开,会发现本来整齐有序的源文件变成了一团遭。
这是因为,文本中的许多回车和空格都被忽略了。而有些符号在html中有特殊的含义,引起了更复杂的局面。
为了源文件能正常显示,我们必须为文本加上适当的标签。对特殊的符号进行转义处理。
常用的有:
HTML 需要转义的实体:
& —>&
空格 —> 
< —><

--->>

” —> ”
此外,根据源码的特点,可以把 TAB 转为4个空格来显示。
TAB —>    
为了显示为换行,需要在行尾加
标签。
为了显示美观,对关键字加粗显示,即在关键字左右加标签。比如:
public
对单行注释文本用绿色显示,可以使用标签,形如:
//这是我的单行注释!
注意:如果“//”出现在字符串中,则注意区分,不要错误地变为绿色。
不考虑多行注释的问题 /* …. / // /* …. */

你的任务是:编写程序,把给定的源文件转化为相应的html表达。
【输入、输出格式要求】
与你的程序同一目录下,存有源文件 a.txt,其中存有标准的java源文件。要求编写程序把它转化为b.html。
例如:目前的 a.txt 文件与 b.html 文件就是对应的。可以用记事本打开b.html查看转换后的内容。用浏览器打开b.html则可以看到显示的效果。
注意:实际评测的时候使用的a.txt与示例是不同的。

import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;public class codeChange {    private String BoldComments(String tempString,String note){        return null;    }    public static void main(String[] args) throws IOException {        // TODO Auto-generated method stub      File inFile=new File("test.java");      FileInputStream fileInputStream;    try {        fileInputStream = new FileInputStream(inFile);        InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream);        BufferedReader bufferedReader=new BufferedReader(inputStreamReader);        File Outfile=new File("test.html");        FileOutputStream fileOutputStream=new FileOutputStream(Outfile);        OutputStreamWriter outStreamWriter=new OutputStreamWriter(fileOutputStream);        BufferedWriter bufferedWriter=new BufferedWriter(outStreamWriter);        outStreamWriter.write("<html>\n");        outStreamWriter.write("<body>\n");        String tempString;        while ((tempString = bufferedReader.readLine()) != null) {            tempString = tempString.replaceAll("&", "&amp;");            tempString = tempString.replaceAll(" ", "&nbsp;");            tempString = tempString.replaceAll("<", "&lt;");            tempString = tempString.replaceAll(">", "&gt;");            int index1 = tempString.lastIndexOf("//");            int index2 = tempString.indexOf("\"");            if (index1 != -1 && index2 == -1) {                String s1 = tempString.substring(0, index1);                String s2 = tempString.substring(index1);                s2 = "<font color=green>" + s2 + "</font>";                tempString = s1 + s2;            } else if (index1 != -1 && index2 != -1) {                int startMark = -1, endMark = -1;                boolean isNote = true;                for (int i = 0; i < tempString.length(); i++) {                    if ('\"' == tempString.charAt(i)) {                        if (startMark == -1) {                            startMark = i;                        } else {                            endMark = i;                            if (index1 > startMark && index1 < endMark) {                                isNote = false;                                break;                            } else {                                startMark = -1;                                endMark = -1;                            }                        }                    }                }                if (isNote == true) {                    String s1 = tempString.substring(0, index1);                    String s2 = tempString.substring(index1);                    s2 = "<font color=green>" + s2 + "</font>";                    tempString = s1 + s2;                }            }            tempString = tempString.replaceAll("\"", "&quot;");            tempString = tempString.replaceAll("\t",                    "&nbsp;&nbsp;&nbsp;&nbsp;");            tempString = tempString.replaceAll("&nbsp;public&nbsp;",                    "&nbsp;<b>public</b>&nbsp;");            tempString = tempString.replaceAll("&nbsp;class&nbsp;",                    "&nbsp;<b>class</b>&nbsp;");            tempString = tempString.replaceAll("&nbsp;static&nbsp;",                    "&nbsp;<b>static</b>&nbsp;");            tempString = tempString.replaceAll("&nbsp;void&nbsp;",                    "&nbsp;<b>void</b>&nbsp;");            outStreamWriter.write(tempString + "<br/>" + "\r\n");        }        outStreamWriter.write("\n</body>\n");        outStreamWriter.write("</html>\n");        bufferedWriter.flush();        bufferedReader.close();        bufferedWriter.close();    } catch (FileNotFoundException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }    }}
0 0
原创粉丝点击