📄 color4j.java
字号:
package jove.color4j;
import java.io.*;
import java.text.NumberFormat;
import java.util.*;
/**
* @author Jove
*/
public class Color4j {
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;
static Collection reserved, classNames, methods,packages;
public static final int WORD = 0;
private static String readFile(String filename) {
try {
InputStream is = Color4j.class.getResourceAsStream(filename);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuffer buf = new StringBuffer();
while ((line = br.readLine()) != null) {
buf.append(line).append("\n");
}
is.close();
return buf.toString();
} 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("methods.txt"));
methods = new HashSet();
while (st.hasMoreElements()) {
methods.add(st.nextToken());
}
st = new StringTokenizer(readFile("packages.txt"));
packages = new HashSet();
while (st.hasMoreElements()) {
packages.add(st.nextToken());
}
}
protected int currentType = NONTYPE;
private StyleSet styleSet;
protected String convert(String source) {
try {
StringWriter writer = new StringWriter(source.length() * 2);
PrintWriter out = new PrintWriter(writer);
out.print(styleSet.getStyleOfBlock().getBegin() + "\n");
BufferedReader reader =
new BufferedReader(new StringReader(source));
String line = null;
int lineNum = 1;
NumberFormat lineNumFormat = styleSet.getLineNumberFormat();
while ((line = reader.readLine()) != null) {
if (lineNumFormat != null) {
out.print(lineNumFormat.format(lineNum++));
}
out.print(convertLine(line) + "\n");
}
out.print(styleSet.getStyleOfBlock().getEnd());
out.flush();
String code = writer.toString();
if (styleSet.needChanageBlankToNbsp()) {
//maybe only for www.cjsdn.com
code = code.replaceAll(" ", " ");
}
return code;
} 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(
styleSet.getStyleOfComment().getBegin());
lineBuffer.append(contents[i]);
continue;
case '/' :
//如果下一个字符是'/'则表明是单行注释,
//添加对应的字体颜色并直接取字符串的剩余内容
lineBuffer.append(
styleSet.getStyleOfComment().getBegin());
lineBuffer.append(line.substring(i));
lineBuffer.append(
styleSet.getStyleOfComment().getEnd());
return lineBuffer.toString();
default :
//不是注释的开始则直接添加内容
lineBuffer.append(contents[i]);
continue;
}
}
//如果是双引号
else if (contents[i] == '\"') {
//如果是最开始的位置或者其前一个字符不是'\'
//则表明是一个引用的开始,设置类型并添加对应的字体颜色及字符
if (i == 0 || contents[i - 1] != '\\') {
currentType = QUOTE;
lineBuffer.append(
styleSet.getStyleOfQuote().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(
styleSet.getStyleOfComment().getBegin());
lineBuffer.append(contents[i]);
continue;
case '/' :
//如果下一个字符是'/'则表明是单行注释,
//添加对应的字体颜色并直接取字符串的剩余内容
lineBuffer.append(
styleSet.getStyleOfComment().getBegin());
lineBuffer.append(line.substring(i));
lineBuffer.append(
styleSet.getStyleOfComment().getEnd());
currentType = NONTYPE;
return lineBuffer.toString();
}
}
//如果是双引号则表明是一个引用的开始,
//设置类型并添加对应的字体颜色及字符
else if (contents[i] == '\"') {
currentType = QUOTE;
lineBuffer.append(
styleSet.getStyleOfQuote().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(styleSet.getStyleOfQuote().getEnd());
currentType = NONTYPE;
continue;
}
}
//如果当前类型是多行注释
else if (currentType == COMMENT) {
//如果是第一个字符则表明是多行注释的继续的一行,添加字体设置
if (i == 0) {
lineBuffer.append(styleSet.getStyleOfComment().getBegin());
}
//如果当前字符是'/'并且他的前一个字符是'*'
//则表明是注释的结束,添加内容并结束字体设置
if (contents[i] == '/' && contents[i - 1] == '*') {
lineBuffer.append(contents[i]);
lineBuffer.append(styleSet.getStyleOfComment().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(styleSet.getStyleOfQuote().getEnd());
currentType = NONTYPE;
return lineBuffer.toString();
} else {
//如果最后一个字符是'/'并且他的前一个字符是'*'
//则表明是注释的结束,添加内容并结束字体设置
if (endChar == '/' && contents[end - 1] == '*') {
lineBuffer.append(endChar);
lineBuffer.append(styleSet.getStyleOfComment().getEnd());
currentType = NONTYPE;
}
//否则表明不是注释的结束,添加内容
else {
lineBuffer.append(endChar);
lineBuffer.append(styleSet.getStyleOfComment().getEnd());
}
return lineBuffer.toString();
}
}
/**
* 将单词转换为具有风格的结果。
* @param word 原始的单词
* @return 转换后的结果
*/
private String convertWord(String word) {
if (reserved.contains(word)) {
return styleSet.getStyleOfReservedWord().getBegin()
+ word
+ styleSet.getStyleOfReservedWord().getEnd();
} else if (classNames.contains(word)) {
return styleSet.getStyleOfClassName().getBegin()
+ word
+ styleSet.getStyleOfClassName().getEnd();
} else if (methods.contains(word)) {
return styleSet.getStyleOfMethod().getBegin()
+ word
+ styleSet.getStyleOfMethod().getEnd();
} else if (packages.contains(word)) {
return styleSet.getStyleOfPackage().getBegin()
+ word
+ styleSet.getStyleOfPackage().getEnd();
} else {
return word;
}
}
public void setStyleSet(StyleSet styleSet) {
this.styleSet = styleSet;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -