20px - +
wrap
zh en
-
发信人: Jove (SpringFramework), 信区: ANSI 标 题: Re: 有没有办法在源代码中表示*[m 发信站: 日月光华 (2004年03月10日17:09:45 星期三), 站内信件 【 在 Jove (SpringFramework) 的大作中提到: 】 : 抱歉,我把Fterm的彩色赋值勾上就可以用了 : 呵呵,再refine一下程序,一会儿秀一下 : 【 在 SuperSS (有心无花非本意,无心有花暗沧桑) 的大作中提到: 】 : : 你怎么用的,比如输出到一个txt文件,再复制到Term里面? 献丑了.两个文件Java2AnsiGUI.java和Java2Ansi.java,分别是GUI和内核 package jove.java2ansi; import java.awt.*; import java.awt.datatransfer.StringSelection; import java.awt.event.*; import javax.swing.*; /** * @author Jove */ public class Java2AnsiGUI extends JFrame { public static void main(String[] args) { new Java2AnsiGUI().show(); } private Java2AnsiGUI() { super("Java2Ansi Code Formatter"); setBounds(100, 100, 500, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container con = getContentPane(); con.add( new JLabel(" Press F9 to convert and save result to clipboard"), BorderLayout.NORTH); final JTextArea textSource = new JTextArea(); con.add(new JScrollPane(textSource)); final Java2Ansi java2Ubb = new Java2Ansi(); textSource.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_F9) { String code = java2Ubb.convert(textSource.getText()); Toolkit .getDefaultToolkit() .getSystemClipboard() .setContents( new StringSelection(code), null); JOptionPane.showMessageDialog( Java2AnsiGUI.this, "data have been sent to system clipboard."); } } }); } } package jove.java2ansi; import java.io.*; import java.util.*; /** * @author Jove */ public class Java2Ansi { private static class UbbStyle { String begin, end; UbbStyle(String begin, String end) { this.begin = begin; this.end = end; } String getBegin() { return begin; } String getEnd() { return end; } } public static final int COMMENT = 3; public static final int LINE_COMMENT = 2; public static final int NONTYPE = -1; public static final int QUOTE = 1; /* * 要求: * 1. 最左边显示行号(可以设置)-优先级低 * 2. 关键字用[b][color=blue] * 3. 注释(包括行注释和块注释)用[color=blue] * 4. 字符串或字符用紫色 */ static Collection reserved, classNames, methods; static final char ESC=(char)27; static final UbbStyle reservedWordsStyle = new UbbStyle(ESC+"[1;34m", ESC+"[m"); static final UbbStyle styleComment = new UbbStyle(ESC+"[32m", ESC+"[m"); static final UbbStyle styleQuote = new UbbStyle(ESC+"[35m", ESC+"[m"); static final UbbStyle sytleClassName = new UbbStyle(ESC+"[1;31m", ESC+"[m"); static final UbbStyle sytleMethod = new UbbStyle(ESC+"[32m", ESC+"[m"); public static final int WORD = 0; private static String readFile(String filename) { try { InputStream is = Java2Ansi.class.getResourceAsStream(filename); byte[] b = new byte[is.available()]; is.read(b); is.close(); return new String(b); } catch (Exception e) { return ""; } } static { StringTokenizer st = null; st = new StringTokenizer(readFile("classnames.txt")); classNames = new HashSet(); while (st.hasMoreElements()) { classNames.add(st.nextToken()); } st = new StringTokenizer(readFile("reservedWords.txt")); reserved = new HashSet(); while (st.hasMoreElements()) { reserved.add(st.nextToken()); } st = new StringTokenizer(readFile("reservedWords.txt")); methods = new HashSet(); while (st.hasMoreElements()) { methods.add(st.nextToken()); } } protected int currentType = NONTYPE; protected String convert(String source) { try { StringWriter writer = new StringWriter(source.length() * 2); PrintWriter out = new PrintWriter(writer); BufferedReader reader = new BufferedReader(new StringReader(source)); String line = null; while ((line = reader.readLine()) != null) { out.print(convertLine(line)+"\n"); } out.flush(); return writer.toString(); } catch (Exception e) { return ""; } } /** * 将一行内容转换为具有风格的内容。 * @param line 一行文本 * @return 转换后的结果 */ private String convertLine(String line) { if (line.length() == 0) { return ""; } StringBuffer lineBuffer = new StringBuffer(line.length() * 2); StringBuffer wordBuffer = new StringBuffer(); int start = 0; int end = line.length() - 1; char[] contents = line.toCharArray(); //循环判断一行中的最后一个字符前的字符状况 for (int i = start; i < end; i++) { //如果没有当前类型 if (currentType == NONTYPE) { //如果是合法的java字符开始标识符则将类型设置为单词类型并将当 前字符添加到单词缓冲区 if (Character.isJavaIdentifierStart(contents[i])) { currentType = WORD; wordBuffer.append(contents[i]); continue; } //如果是可能的注释开始 else if (contents[i] == '/') { char nextChar = contents[i + 1]; switch (nextChar) { //如果下一个字符是'*'则表明是多行注释,将类型设置为多 行字符并添加对应的字体颜色及字符 case '*' : currentType = COMMENT; lineBuffer.append(styleComment.getBegin()); lineBuffer.append(contents[i]); continue; //如果下一个字符是'/'则表明是单行注释,添加对应的 字体颜色并直接取字符串的剩余内容 case '/' : lineBuffer.append(styleComment.getBegin()); lineBuffer.append(line.substring(i)); lineBuffer.append(styleComment.getEnd()); return lineBuffer.toString(); //不是注释的开始则直接添加内容 default : lineBuffer.append(contents[i]); continue; } } //如果是双引号 else if (contents[i] == '\"') { //如果是最开始的位置或者其前一个字符不是'\'则表明是一个引 用的开始,设置类型并添加对应的字体颜色及字符 if (i == 0 || contents[i - 1] != '\\') { currentType = QUOTE; lineBuffer.append(styleQuote.getBegin()); lineBuffer.append(contents[i]); continue; } //否则不是引用的内容,添加内容 else { lineBuffer.append(contents[i]); continue; } } //如果不是以上情况则表明是其他的杂项内容,不进行处理,将字符 添加到缓冲区 else { lineBuffer.append(contents[i]); continue; } } //如果当前类型是单词 else if (currentType == WORD) { //如果是一个java标识符的合法部分表明单词没有结束,将当前字符 添加到单词缓冲区 if (Character.isJavaIdentifierPart(contents[i])) { wordBuffer.append(contents[i]); continue; } //否则表明单词结束,将单词缓冲区的内容进行转换并添加到缓冲区 并继续判断后面部分的语法成分 else { lineBuffer.append(convertWord(wordBuffer.toString())); wordBuffer = new StringBuffer(); //如果是可能的注释开始 if (contents[i] == '/') { char nextChar = contents[i + 1]; switch (nextChar) { //如果下一个字符是'*'则表明是多行注释,将类型设置 为多行字符并添加对应的字体颜色及字符 case '*' : currentType = COMMENT; lineBuffer.append(styleComment.getBegin()); lineBuffer.append(contents[i]); continue; //如果下一个字符是'/'则表明是单行注释,添加对 应的字体颜色并直接取字符串的剩余内容 case '/' : lineBuffer.append(styleComment.getBegin()); lineBuffer.append(line.substring(i)); lineBuffer.append(styleComment.getEnd()); currentType = NONTYPE; return lineBuffer.toString(); } } //如果是双引号则表明是一个引用的开始,设置类型并添加对应 的字体颜色及字符 else if (contents[i] == '\"') { currentType = QUOTE; lineBuffer.append(styleQuote.getBegin()); lineBuffer.append(contents[i]); continue; } //如果不是以上情况则表明是其他的杂项内容,不进行处理,将 字符添加到缓冲区 else { currentType = NONTYPE; lineBuffer.append(contents[i]); continue; } } } //如果当前类型是引用 else if (currentType == QUOTE) { //如果当前字符不是引号或者如果是引号但是他的前一个字符是'\'则 表明是引用的内容,添加内容 if (contents[i] != '\"' || contents[i - 1] == '\\') { lineBuffer.append(contents[i]); continue; } //否则表明是引用的结束,添加内容并结束字体设置 else { lineBuffer.append(contents[i]); lineBuffer.append(styleQuote.getEnd()); currentType = NONTYPE; continue; } } //如果当前类型是多行注释 else if (currentType == COMMENT) { //如果是第一个字符则表明是多行注释的继续的一行,添加字体设置 if (i == 0) { lineBuffer.append(styleComment.getBegin()); } //如果当前字符是'/'并且他的前一个字符是'*'则表明是注释的结束 ,添加内容并结束字体设置 if (contents[i] == '/' && contents[i - 1] == '*') { lineBuffer.append(contents[i]); lineBuffer.append(styleComment.getEnd()); currentType = NONTYPE; continue; } //否则表明不是注释的结束,添加内容 else { lineBuffer.append(contents[i]); continue; } } } //对最后一个字符进行处理并结束本行的转换 char endChar = contents[end]; //如果是没有任何类型,最后一个字符不能构成任何特殊类,直接添加内容 if (currentType == NONTYPE) { lineBuffer.append(endChar); return lineBuffer.toString(); } //如果是单词 else if (currentType == WORD) { //如果最后一个字符是标识符的一部分,添加内容并结束风格设置 if (Character.isJavaIdentifierPart(endChar)) { wordBuffer.append(endChar); lineBuffer.append(convertWord(wordBuffer.toString())); wordBuffer = new StringBuffer(); } //否则表明单词已经结束,转换单词后作为普通内容添加。 else { lineBuffer.append(convertWord(wordBuffer.toString())); wordBuffer = new StringBuffer(); lineBuffer.append(endChar); } currentType = NONTYPE; return lineBuffer.toString(); } //如果是引用,由于字符串不能跨行,因此结束字符串引用不会有问题。 else if (currentType == QUOTE) { lineBuffer.append(endChar); lineBuffer.append(styleQuote.getEnd()); currentType = NONTYPE; return lineBuffer.toString(); } else { //如果最后一个字符是'/'并且他的前一个字符是'*'则表明是注释的结束 ,添加内容并结束字体设置 if (endChar == '/' && contents[end - 1] == '*') { lineBuffer.append(endChar); lineBuffer.append(styleComment.getEnd()); currentType = NONTYPE; } //否则表明不是注释的结束,添加内容 else { lineBuffer.append(endChar); lineBuffer.append(styleComment.getEnd()); } return lineBuffer.toString(); } } /** * 将单词转换为具有风格的结果。 * @param word 原始的单词 * @return 转换后的结果 */ private String convertWord(String word) { if (reserved.contains(word)) { return reservedWordsStyle.getBegin() + word + reservedWordsStyle.getEnd(); } else if (classNames.contains(word)) { return sytleClassName.getBegin() + word + sytleClassName.getEnd(); } else if (methods.contains(word)) { return sytleMethod.getBegin() + word + sytleMethod.getEnd(); } else { return word; } } } -- More you write, better you'll know, and less mistakes you'll make in the future. So, just code away... ※ 来源:·日月光华 bbs.fudan.edu.cn·[FROM: 218.80.127.192] ※ 修改:·Jove 於 03月10日17:11:26 修改本文·[FROM: 218.80.127.192]