class Area {
private String name;
private int id;
private int pid;
public Area(String name, int id, int pid) {
this.name = name;
this.id = id;
this.pid = pid;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
}
package net.csdn.java.regex;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import com.bao.util.io.FileUtil;
public class Test {
public static void main(String[] args) throws IOException {
String str = FileUtil.readFile2String("E:/myprog/eclipse33/csdn/src/net/csdn/java/regex/Test.java");
JavaSyntaxHighlight java = new JavaSyntaxHighlight(str);
System.out.println(java.process());
}
}
/**
* Java 语法高亮
*/
class JavaSyntaxHighlight {
/**
* 关键词
*/
private static String keywords = "abstract assert boolean break byte case catch char class const " +
"continue default do double else enum extends false final finally " +
"float for if goto implements import instanceof int interface long " +
"native new null package private protected public return short static " +
"strictfp super switch synchronized this throw throws transient true try " +
"void volatile while";
/**
* 多行注释
*/
private final static String MULTI_COMMENT = "/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/";
/**
* 单行注释
*/
private final static String SINGLE_COMMENT = "//[^\\r\\n]*+";
/**
* 字符串
*/
private final static String QUOTE_STRING = "\"[^\\\\\"]*(?:\\\\.[^\\\\\"]*)*\"";
/**
* 字符
*/
private final static String QUOTE_CHAR = "'[^\\\\']*(?:\\\\.[^\\\\']*)*'";
/**
* 高亮标签
*/
private final static String HIGHLIGHT_HTML_TAG = "";
/**
* 组合关键词表达式
*/
private final static String KEYWORDS = "@interface|\\b(?:" + keywords.replace(' ', '|') + ")\\b";
/**
* 静态常量
*/
private final static String STATIC_CONST = "(?<=\\b[A-Z_][A-Za-z0-9_]{0,50}\\.|[\\s({])[A-Z_][A-Z0-9_]*(?=\\s*[=+;,)}])";
/**
* Annotation
*/
private final static String ANNOTATION = "@[A-Z_][A-Za-z0-9_]*\\b";
private HighlightFormatter keywordFormatter = HighlightFormatter.DEFAULT_KEYWORDS_CLASS;
private HighlightFormatter multiCommentFormatter = HighlightFormatter.DEFAULT_MULTICOMMENT_CLASS;
private HighlightFormatter singleCommentFormatter = HighlightFormatter.DEFAULT_SINGLECOMMENT_CLASS;
private HighlightFormatter stringFormatter = HighlightFormatter.DEFAULT_STRING_CLASS;
private HighlightFormatter staticConstFormatter = HighlightFormatter.DEFAULT_STATIC_CONST;
private HighlightFormatter annotationFormatter = HighlightFormatter.DEFAULT_ANNOTATION;
private String text;
/**
* 匹配字符串的正则表达式
*/
private final static Pattern REGEX_QUOTE_STRING = Pattern.compile(
"(" + HIGHLIGHT_HTML_TAG + "|" + SINGLE_COMMENT + "|" + MULTI_COMMENT + ")|" +
"(" + QUOTE_STRING + ")"
);
/**
* 匹配字符的正则表达式
*/
private final static Pattern REGEX_QUOTE_CHAR = Pattern.compile(
"(" + HIGHLIGHT_HTML_TAG + "|" + SINGLE_COMMENT + "|" + MULTI_COMMENT + ")|" +
"(" + QUOTE_CHAR + ")"
);
/**
* 匹配多行注释的正则表达式
*/
private final static Pattern REGEX_MULTI_COMMENT = Pattern.compile(
"(" + HIGHLIGHT_HTML_TAG + "|" + SINGLE_COMMENT + "|" + QUOTE_STRING + ")|" +
"(" + MULTI_COMMENT + ")"
);
/**
* 匹配单行注释的正则表达式
*/
private final static Pattern REGEX_SINGLE_COMMENT = Pattern.compile(
"(" + HIGHLIGHT_HTML_TAG + "|" + MULTI_COMMENT + "|" + QUOTE_STRING + ")|" +
"(" + SINGLE_COMMENT + ")"
);
/**
* 匹配 Java 关键词的正则表达式
*/
private final static Pattern REGEX_KEYWORDS = Pattern.compile(
"(" + HIGHLIGHT_HTML_TAG + "|" + MULTI_COMMENT + "|" + QUOTE_STRING + "|" + SINGLE_COMMENT + ")|" +
"(" + KEYWORDS + ")"
);
/**
* 匹配静态常量的正则表达式
*/
private final static Pattern REGEX_STATIC = Pattern.compile(
"(" + HIGHLIGHT_HTML_TAG + "|" + MULTI_COMMENT + "|" + QUOTE_STRING + "|" + SINGLE_COMMENT + ")|" +
"(" + STATIC_CONST + ")"
);
/**
* 匹配 @ 注释的正则表达式
*/
private final static Pattern REGEX_ANNOTATION = Pattern.compile(
"(" + HIGHLIGHT_HTML_TAG + "|" + MULTI_COMMENT + "|" + QUOTE_STRING + "|" + SINGLE_COMMENT + ")|" +
"(" + ANNOTATION + ")"
);
public JavaSyntaxHighlight(String text) {
this.text = text;
}
public String process() {
text = processHtmlEscape(text);
process(REGEX_QUOTE_STRING, stringFormatter);
process(REGEX_KEYWORDS, keywordFormatter);
process(REGEX_QUOTE_CHAR, stringFormatter);
process(REGEX_MULTI_COMMENT, multiCommentFormatter);
process(REGEX_SINGLE_COMMENT, singleCommentFormatter);
process(REGEX_STATIC, staticConstFormatter);
process(REGEX_ANNOTATION, annotationFormatter);
return text;
}
private void process(Pattern regex, HighlightFormatter formatter) {
Matcher matcher = regex.matcher(text);
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
if(matcher.start(1) > -1) {
String str = matcher.group(1);
str = processSpecialChar(str);
matcher.appendReplacement(sb, str);
} else if(matcher.start(2) > -1) {
String str = matcher.group(2);
str = processSpecialChar(str);
matcher.appendReplacement(sb, formatter.getHtmlPrefix() + str + formatter.getHtmlSuffix());
}
}
matcher.appendTail(sb);
text = sb.toString();
}
private String processHtmlEscape(String str) {
return str.replace("&", "&").replace("<", "<").replace(">", ">");
}
/**
* 处理替换后的特殊字符
* @param str
* @return
*/
private String processSpecialChar(String str) {
if(str.indexOf('\\') > -1) {
str = str.replace("\\", "\\\\");
}
if(str.indexOf('$') > -1) {
str = str.replace("$", "\\$");
}
return str;
}
}
class HighlightFormatter {
private String color;
private boolean isBold;
private boolean isItalic;
private String font;
private String size;
private String styleClass;
public final static String COLOR_DEFAULT = "#000000"; // default font
public final static String FONT_DEFAULT = "Courier New"; // default color
public final static String SIZE_DEFAULT = "12pt"; // default font size
// highlight format of keywords
public final static HighlightFormatter DEFAULT_KEYWORDS_CLASS = new HighlightFormatter("keywords");
public final static HighlightFormatter DEFAULT_MULTICOMMENT_CLASS = new HighlightFormatter("multiComments");
public final static HighlightFormatter DEFAULT_SINGLECOMMENT_CLASS = new HighlightFormatter("singleComments");
public final static HighlightFormatter DEFAULT_STRING_CLASS = new HighlightFormatter("string");
public final static HighlightFormatter DEFAULT_STATIC_CONST = new HighlightFormatter("static");
public final static HighlightFormatter DEFAULT_ANNOTATION = new HighlightFormatter("annotation");
/**
* Using HTML style attribute
* @param color
* @param isBold
* @param isItalic
* @param font
* @param size
*/
public HighlightFormatter(String color, boolean isBold, boolean isItalic,
String font, String size) {
setColor(color);
setBold(isBold);
setItalic(isItalic);
setFont(font);
setSize(size);
}
/**
* Using CSS class selector
* @param styleClass
*/
public HighlightFormatter(String styleClass) {
this.styleClass = styleClass;
}
public HighlightFormatter() {
}
public String getHtmlPrefix() {
StringBuilder sb = new StringBuilder();
sb.append("<").append("span");
if(StringUtils.isNotEmpty(styleClass)) {
sb.append(" class=\"").append(styleClass).append("\">");
return sb.toString();
}
sb.append(" style=\"");
if(StringUtils.isNotEmpty(font) || isBold || isItalic || StringUtils.isNotEmpty(size)) {
sb.append("font:");
if(isItalic) {
sb.append(" italic");
}
if(isBold) {
sb.append(" bold");
}
if(StringUtils.isNotEmpty(size)) {
sb.append(" ").append(size);
}
if(StringUtils.isNotEmpty(font)) {
sb.append(" ").append(font);
}
sb.append("; ");
}
if(StringUtils.isNotEmpty(color)) {
sb.append("color: ").append(color).append("; ");
}
sb.append("\">");
return sb.toString();
}
public String getHtmlSuffix() {
return "";
}
private String processFont(String font) {
String[] fonts = font.split(",\\s*");
StringBuilder sb = new StringBuilder();
for(int i = 0; i < fonts.length; i++) {
if(i > 0) {
sb.append(",");
}
if(fonts[i].indexOf(' ') > -1) {
sb.append("\"").append(fonts[i]).append("\"");
} else {
sb.append(fonts[i]);
}
}
return sb.toString();
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isBold() {
return isBold;
}
public void setBold(boolean isBold) {
this.isBold = isBold;
}
public boolean isItalic() {
return isItalic;
}
public void setItalic(boolean isItalic) {
this.isItalic = isItalic;
}
public String getFont() {
return font;
}
public void setFont(String font) {
this.font = processFont(font);
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getStyleClass() {
return styleClass;
}
public void setStyleClass(String styleClass) {
this.styleClass = styleClass;
}
}
测试
测试
测试测试
没有评论:
发表评论