发信人: 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]
Sender: Jove SpringFramework, message area: ANSI
Title: Re: Is there a way to express *[m in source code
Sending site: Sun and Moon Guanghua Wednesday, March 10, 2004 17:09:45, site mail
Mentioned in Jove SpringFramework's masterpiece:
: Sorry, I just checked the color assignment of Fterm and it works.
: Haha, refine the program again and show it off later.
: It is mentioned in the masterpiece of SuperSS: There is no intention but no intention, no intention is the secret of vicissitudes of life:
: : How do you use it? For example, output it to a txt file and then copy it into Term.
It’s embarrassing. Two files, Java2AnsiGUI.java and Java2Ansi.java, are GUI and kernel respectively.
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;
*
* Require:
* 1. The line number displayed on the far left can be set with low priority.
* 2. Use [b][color blue] as the keyword
* 3. Comments include line comments and block comments, use [color blue]
* 4. Use purple for strings or characters
*
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 "";
}
}
**
* Convert a line of content into styled content
* @param line A line of text
* @return converted result
*
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;
Loop to determine the character status before the last character in a line
for int i start; i < end; i {
if there is no current type
if currentType NONTYPE {
If it is a legal java character start identifier, the type is set to the word type and the current
Add the first character to the word buffer
if Character.isJavaIdentifierStart contents[i] {
currentType WORD;
wordBuffer.append contents[i];
continue;
}
If it is possible the annotation starts with
else if contents[i] ' ' {
char nextChar contents[i 1];
switch nextChar {
If the next character is '*', it indicates a multi-line comment. Set the type to multiple
line characters and add corresponding font color and characters
case '*' :
currentType COMMENT;
lineBuffer.append styleComment.getBegin;
lineBuffer.append contents[i];
continue;
If the next character is ' ', it indicates a single-line comment. Add the corresponding
Font color and directly get the remaining content of the string
case ' ' :
lineBuffer.append styleComment.getBegin;
lineBuffer.append line.substring i;
lineBuffer.append styleComment.getEnd;
return lineBuffer.toString;
If it is not the beginning of the comment, add the content directly.
default :
lineBuffer.append contents[i];
continue;
}
}
If it is double quotes
else if contents[i] ' "' {
If it is the very beginning or the character before it is not ' ', it indicates that it is a quotation
Start by setting the type and adding the corresponding font color and characters
if i 0 contents[i 1] ! ' ' {
currentType QUOTE;
lineBuffer.append styleQuote.getBegin;
lineBuffer.append contents[i];
continue;
}
Otherwise it is not the quoted content. Add content
else {
lineBuffer.append contents[i];
continue;
}
}
If it is not the above situation, it means that it is other miscellaneous content and will not be processed.
add to buffer
else {
lineBuffer.append contents[i];
continue;
}
}
If the current type is word
else if currentType WORD {
If the legal part of a Java identifier indicates that the word does not end, the current character
Add to word buffer
if Character.isJavaIdentifierPart contents[i] {
wordBuffer.append contents[i];
continue;
}
Otherwise, it indicates the end of the word. Convert the contents of the word buffer and add it to the buffer.
and continue to judge the grammatical components of the following parts
else {
lineBuffer.append convertWord wordBuffer.toString;
wordBuffer new StringBuffer;
If it is possible the annotation starts with
if contents[i] ' ' {
char nextChar contents[i 1];
switch nextChar {
If the next character is '*', it indicates a multi-line comment. Set the type
Add corresponding font color and characters to multi-line characters
case '*' :
currentType COMMENT;
lineBuffer.append styleComment.getBegin;
lineBuffer.append contents[i];
continue;
If the next character is ' ', it indicates a single line comment. Add the
The corresponding font color and directly get the remaining content of the string
case ' ' :
lineBuffer.append styleComment.getBegin;
lineBuffer.append line.substring i;
lineBuffer.append styleComment.getEnd;
currentType NONTYPE;
return lineBuffer.toString;
}
}
If it is a double quote, it indicates the beginning of a reference. Set the type and add the corresponding
font color and characters
else if contents[i] ' "' {
currentType QUOTE;
lineBuffer.append styleQuote.getBegin;
lineBuffer.append contents[i];
continue;
}
If it is not the above situation, it means that it is other miscellaneous content and will not be processed.
Characters added to buffer
else {
currentType NONTYPE;
lineBuffer.append contents[i];
continue;
}
}
}
If the current type is a reference
else if currentType QUOTE {
If the current character is not a quotation mark or if it is a quotation mark but its previous character is ' ' then
Indicate content that is quoted Add content
if contents[i] ! ' "' contents[i 1] ' ' {
lineBuffer.append contents[i];
continue;
}
Otherwise, it indicates the end of the quote. Add content and end the font setting.
else {
lineBuffer.append contents[i];
lineBuffer.append styleQuote.getEnd;
currentType NONTYPE;
continue;
}
}
If the current type is a multiline comment
else if currentType COMMENT {
If it is the first character, it indicates that it is the continuation line of the multi-line comment. Add font settings.
if i 0 {
lineBuffer.append styleComment.getBegin;
}
If the current character is ' ' and its previous character is '*', it indicates the end of the comment
Add content and end font settings
if contents[i] ' ' && contents[i 1] '*' {
lineBuffer.append contents[i];
lineBuffer.append styleComment.getEnd;
currentType NONTYPE;
continue;
}
Otherwise, it indicates that it is not the end of the comment. Add content
else {
lineBuffer.append contents[i];
continue;
}
}
}
Process the last character and end the conversion of this line
char endChar contents[end];
If there is no type, the last character cannot constitute any special class. Add the content directly.
if currentType NONTYPE {
lineBuffer.append endChar;
return lineBuffer.toString;
}
if it is a word
else if currentType WORD {
If the last character is part of the identifier add content and end styling
if Character.isJavaIdentifierPart endChar {
wordBuffer.append endChar;
lineBuffer.append convertWord wordBuffer.toString;
wordBuffer new StringBuffer;
}
Otherwise, it indicates that the word has ended and is added as normal content after converting the word.
else {
lineBuffer.append convertWord wordBuffer.toString;
wordBuffer new StringBuffer;
lineBuffer.append endChar;
}
currentType NONTYPE;
return lineBuffer.toString;
}
If it is a reference, since the string cannot cross lines, there will be no problem in ending the string reference.
else if currentType QUOTE {
lineBuffer.append endChar;
lineBuffer.append styleQuote.getEnd;
currentType NONTYPE;
return lineBuffer.toString;
} else {
If the last character is ' ' and the preceding character is '*', it indicates the end of the comment.
Add content and end font settings
if endChar ' ' && contents[end 1] '*' {
lineBuffer.append endChar;
lineBuffer.append styleComment.getEnd;
currentType NONTYPE;
}
Otherwise, it indicates that it is not the end of the comment. Add content
else {
lineBuffer.append endChar;
lineBuffer.append styleComment.getEnd;
}
return lineBuffer.toString;
}
}
**
* Convert words into stylistic results
* @param word original word
* @return converted result
*
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...
Source: Sun and Moon Guanghua bbs.fudan.edu.cn [FROM: 218.80.127.192]
Modification: Jove modified this article on March 10, 17:11:26 [FROM: 218.80.127.192]