java实现代码高亮_别人写的Java语法高亮代码

RT。中文注释显示再一次乱码,可直接下载文件。前面的代码忘了附加对应的CSS文件,时间太久找不到原始CSS文件了,所以去网上搜了另外一个JS实现的语法高亮插件,可以参考修改它的CSS。

地址:

http://code.google.com/p/syntaxhighlighter/第一个rar包即这个JS实现的语法高亮插件,支持各种编程语言。

1.[文件] SyntaxHighlighter_1.5.1.rar ~ 39KB     下载(134)

2.[文件] JavaSyntaxHighlighter.java ~ 7KB     下载(141)

package JavaSyntaxHighterDemo;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStreamReader;

import java.util.Arrays;

import java.util.HashSet;

import java.util.Set;

/**

* @author Kevin Tse

* @version 0.1

*/

public class JavaSyntaxHighlighter {

public static int STATE_TEXT = 1; // 普通文本

public static int STATE_DOUBLE_QUOTE = 2; // 双引号

public static int STATE_SINGLE_QUOTE = 3; // 单引号

public static int STATE_MULTI_LINE_COMMENT = 4; // 多行注释

public static int STATE_LINE_COMMENT = 5; // 单行注释

private int lineNumber; // 行号

private boolean enableLineNumber = true; // 开启行号标志

public boolean isEnableLineNumber() {

return enableLineNumber;

}

// line numbers are printed by default.

// but this behavior can be disabled by invoking this method and setting the

// flag to false

public void setEnableLineNumber(boolean enableLineNumber) {

this.enableLineNumber = enableLineNumber;

}

String[] literalArray = { "null", "true", "false" };//字面常量

String[] keywordArray = { "abstract", "break", "case", "catch", "class",

"const", "continue", "default", "do", "else", "extends", "final",

"finally", "for", "goto", "if", "implements", "import",

"instanceof", "interface", "native", "new", "package", "private",

"protected", "public", "return", "static", "strictfp", "super",

"switch", "synchronized", "this", "throw", "throws", "transient",

"try", "volatile", "while" }; //关键词

String[] primitiveTypeArray = { "boolean", "char", "byte", "short", "int",

"long", "float", "double", "void" }; //原始数据类型

Set literalSet = new HashSet(Arrays.asList(literalArray));

Set keywordSet = new HashSet(Arrays.asList(keywordArray));

Set primitiveTypeSet = new HashSet(Arrays

.asList(primitiveTypeArray));

public String process(String src) {

int currentState = STATE_TEXT;

int identifierLength = 0;

int currentIndex = -1;

char currentChar = 0;

String identifier = "";

StringBuffer out = new StringBuffer();

while (++currentIndex != src.length() - 1) {

currentChar = src.charAt(currentIndex);

if (Character.isJavaIdentifierPart(currentChar)) {

out.append(currentChar);

++identifierLength;

continue;

}

if (identifierLength > 0) {

identifier = out.substring(out.length() - identifierLength);

if (currentState == STATE_TEXT) {

if (literalSet.contains(identifier)) { // identifier is a

// literal

out.insert(out.length() - identifierLength,

"

");

out.append("

");

} else if (keywordSet.contains(identifier)) { // identifier

// is a

// keyword

out.insert(out.length() - identifierLength,

"

");

out.append("

");

} else if (primitiveTypeSet.contains(identifier)) { // identifier

// is a

// primitive

// type

out.insert(out.length() - identifierLength,

"

");

out.append("

");

} else if (identifier.equals(identifier.toUpperCase())

&& !Character.isDigit(identifier.charAt(0))) { // identifier

// is

// a

// constant

out.insert(out.length() - identifierLength,

"

");

out.append("

");

} else if (Character.isUpperCase(identifier.charAt(0))) { // identifier

// is

// non-primitive

// type

out.insert(out.length() - identifierLength,

"

");

out.append("

");

}

}

}

switch (currentChar) {

// because I handle the "greater than" and "less than" marks

// somewhere else, I comment them out here

// case '

// out.append("<");

// break;

// case '>':

// out.append(">");

// break;

case '\"':

out.append('\"');

if (currentState == STATE_TEXT) {

currentState = STATE_DOUBLE_QUOTE;

out.insert(out.length() - ("\"").length(),

"

");

} else if (currentState == STATE_DOUBLE_QUOTE) {

currentState = STATE_TEXT;

out.append("

");

}

break;

case '\'':

out.append("\'");

if (currentState == STATE_TEXT) {

currentState = STATE_SINGLE_QUOTE;

out.insert(out.length() - ("\'").length(),

"

");

} else if (currentState == STATE_SINGLE_QUOTE) {

currentState = STATE_TEXT;

out.append("

");

}

break;

case '\\':

out.append("\\");

if (currentState == STATE_DOUBLE_QUOTE

|| currentState == STATE_SINGLE_QUOTE) {

// treat as a character escape sequence

out.append(src.charAt(++currentIndex));

}

break;

// if you want to translate tabs into spaces, uncomment the

// following lines

// case '\t':

// // replace tabs with tabsize number of spaces

// for (int i = 0; i < tabSize; i++)

// out.append("    ");

// break;

case '*':

out.append('*');

if (currentState == STATE_TEXT && currentIndex > 0

&& src.charAt(currentIndex - 1) == '/') {

out.insert(out.length() - ("/*").length(),

"

");

currentState = STATE_MULTI_LINE_COMMENT;

}

break;

case '/':

out.append("/");

if (currentState == STATE_TEXT && currentIndex > 0

&& src.charAt(currentIndex - 1) == '/') {

out.insert(out.length() - ("//").length(),

"

");

currentState = STATE_LINE_COMMENT;

} else if (currentState == STATE_MULTI_LINE_COMMENT) {

out.append("

");

currentState = STATE_TEXT;

}

break;

case '\r':

case '\n':

// end single line comments

if (currentState == STATE_LINE_COMMENT) {

out.insert(out.length() - 1, "

");

currentState = STATE_TEXT;

}

if (currentChar == '\r' && currentIndex < src.length() - 1) {

out.append("\r\n");

++currentIndex;

} else

out.append('\n');

if (enableLineNumber)

out.append("

"

+ (++lineNumber) + ".

");

break;

case 0:

if (currentState == STATE_LINE_COMMENT

&& currentIndex == (src.length() - 1))

out.append("

");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值