java中JEditorPane中的语法高亮显示

java中JEditorPane中的语法高亮显示,java,regex,swing,syntax-highlighting,Java,Regex,Swing,Syntax Highlighting,我想在jEditorPane中执行语法高亮显示。它允许我执行单行语法高亮显示,但如果XML标记被拆分为两行或多行,则无法工作。下面是我用来突出显示语法的代码。帮我解决这个问题。谢谢 public class XmlView extends PlainView { private static HashMap<Pattern, Color> patternColors; private static String TAG_PATTERN = "(</?[A-Za

我想在jEditorPane中执行语法高亮显示。它允许我执行单行语法高亮显示,但如果XML标记被拆分为两行或多行,则无法工作。下面是我用来突出显示语法的代码。帮我解决这个问题。谢谢

public class XmlView extends PlainView {

    private static HashMap<Pattern, Color> patternColors;
    private static String TAG_PATTERN = "(</?[A-Za-z\\-_0-9]*)\\s?>?";
    private static String TAG_END_PATTERN = "(/>)";
    private static String TAG_ATTRIBUTE_PATTERN = "\\s(\\w*)\\=";
    private static String TAG_ATTRIBUTE_VALUE = "[a-z\\-]*\\=(\"[^\"]*\")";
    private static String TAG_COMMENT = "(<\\!--[\\w * \\S]*-->)";
    private static String TAG_CDATA = "(<\\!\\[CDATA\\[.*\\]\\]>)";

    static {
        // NOTE: the order is important!
        patternColors = new LinkedHashMap<Pattern, Color>();
        patternColors.put(Pattern.compile(TAG_PATTERN), new Color(163, 21, 21));
        patternColors.put(Pattern.compile(TAG_CDATA), Color.GRAY);
        patternColors.put(Pattern.compile(TAG_ATTRIBUTE_PATTERN), new Color(127, 0, 127));
        patternColors.put(Pattern.compile(TAG_END_PATTERN), new Color(63, 127, 127));
        patternColors.put(Pattern.compile(TAG_ATTRIBUTE_VALUE), new Color(42, 0, 255));
        patternColors.put(Pattern.compile(TAG_COMMENT), new Color(0, 128, 0));
    }

    public XmlView(Element element) {

        super(element);

        // Set tabsize to 4 (instead of the default 8)
        getDocument().putProperty(PlainDocument.tabSizeAttribute, 4);
    }

    @Override
    protected int drawUnselectedText(Graphics graphics, int x, int y, int p0,
            int p1) throws BadLocationException {

        Document doc = getDocument();
        String text = doc.getText(p0, p1 - p0);

        Segment segment = getLineBuffer();

        SortedMap<Integer, Integer> startMap = new TreeMap<Integer, Integer>();
        SortedMap<Integer, Color> colorMap = new TreeMap<Integer, Color>();

        // Match all regexes on this snippet, store positions
        for (Map.Entry<Pattern, Color> entry : patternColors.entrySet()) {

            Matcher matcher = entry.getKey().matcher(text);

            while (matcher.find()) {
                startMap.put(matcher.start(1), matcher.end());
                colorMap.put(matcher.start(1), entry.getValue());
            }
        }

        // TODO: check the map for overlapping parts

        int i = 0;

        // Colour the parts
        for (Map.Entry<Integer, Integer> entry : startMap.entrySet()) {
            int start = entry.getKey();
            int end = entry.getValue();

            if (i < start) {
                graphics.setColor(Color.black);
                doc.getText(p0 + i, start - i, segment);
                x = Utilities.drawTabbedText(segment, x, y, graphics, this, i);
            }

            graphics.setColor(colorMap.get(start));
            i = end;
            doc.getText(p0 + start, i - start, segment);
            x = Utilities.drawTabbedText(segment, x, y, graphics, this, start);
        }

        // Paint possible remaining text black
        if (i < text.length()) {
            graphics.setColor(Color.black);
            doc.getText(p0 + i, text.length() - i, segment);
            x = Utilities.drawTabbedText(segment, x, y, graphics, this, i);
        }

        return x;
    }

}
公共类XmlView扩展了PlainView{
私有静态HashMap模式颜色;
私有静态字符串TAG_PATTERN=“(?”;
私有静态字符串标记_END_PATTERN=“(/>”);
私有静态字符串标记\u属性\u模式=“\\s(\\w*)\\=”;
私有静态字符串标记\u属性\u值=“[a-z\\-]*\\=(\“[^\“]*\”);
私有静态字符串标记_COMMENT=“()”;
私有静态字符串标记\u CDATA=“()”;
静止的{
//注意:订单很重要!
patternColors=新建LinkedHashMap();
patternColors.put(Pattern.compile(TAG_Pattern),新颜色(163,21,21));
patternColors.put(Pattern.compile(TAG\u CDATA),Color.GRAY);
patternColors.put(Pattern.compile(TAG_ATTRIBUTE_Pattern),new Color(127,0,127));
patternColors.put(Pattern.compile(TAG_END_Pattern)),新颜色(63127127));
patternColors.put(Pattern.compile(TAG_属性_值),新颜色(42,0255));
patternColors.put(Pattern.compile(TAG_COMMENT),新颜色(0,128,0));
}
公共XmlView(元素){
超级(元素);
//将tabsize设置为4(而不是默认的8)
getDocument().putProperty(PlainDocument.tabSizeAttribute,4);
}
@凌驾
受保护的int drawUnselectedText(图形图形、int x、int y、int p0、,
int p1)引发BadLocationException{
Document doc=getDocument();
String text=doc.getText(p0,p1-p0);
段段=getLineBuffer();
SortedMap startMap=新树映射();
SortedMap colorMap=新树映射();
//匹配此代码段上的所有正则表达式,存储位置
对于(Map.Entry:patternColors.entrySet()){
Matcher Matcher=entry.getKey().Matcher(文本);
while(matcher.find()){
startMap.put(matcher.start(1),matcher.end());
colorMap.put(matcher.start(1),entry.getValue());
}
}
//TODO:检查地图中的重叠部分
int i=0;
//给零件上色
对于(Map.Entry:startMap.entrySet()){
int start=entry.getKey();
int end=entry.getValue();
如果(i<开始){
图形.设置颜色(颜色.黑色);
doc.getText(p0+i,start-i,段);
x=实用程序.drawTabbedText(段,x,y,图形,this,i);
}
graphics.setColor(colorMap.get(start));
i=结束;
doc.getText(p0+start,i-start,段);
x=实用程序.drawTabbedText(段、x、y、图形、此、开始);
}
//将可能剩余的文本绘制为黑色
if(i
您可能需要使用Pattern.MULTILINE标志

e、 g


您可能需要使用Pattern.MULTILINE标志

e、 g


标记、注释和CDATA部分的正则表达式需要分为两部分:

Pattern TAG_START     = Pattern.compile("</?[\\w-]+");
Pattern TAG_END       = Pattern.compile("/?>");
Pattern COMMENT_START = Pattern.compile("<!--");
Pattern COMMENT_END   = Pattern.compile("-->");
Pattern CDATA_START   = Pattern.compile("<\\[CDATA\\[");
Pattern CDATA_END     = Pattern.compile("\\]\\]>");
…最初将消耗
中的所有内容
注意所有格量词(
*+
)和原子群(
(?>…)
)的使用。从正确性的角度来看,它们不是必需的,但它们使正则表达式更加高效,这在本项目中尤为重要


还有一件事:如果要使用
find()
,还应该像Friedl在中所做的那样,在每个正则表达式的开头添加
\G
(最后一个匹配锚点的结尾)。

标记、注释和CDATA部分的正则表达式需要分成两部分:

Pattern TAG_START     = Pattern.compile("</?[\\w-]+");
Pattern TAG_END       = Pattern.compile("/?>");
Pattern COMMENT_START = Pattern.compile("<!--");
Pattern COMMENT_END   = Pattern.compile("-->");
Pattern CDATA_START   = Pattern.compile("<\\[CDATA\\[");
Pattern CDATA_END     = Pattern.compile("\\]\\]>");
…最初将消耗
中的所有内容
注意所有格量词(
*+
)和原子群(
(?>…)
)的使用。从正确性的角度来看,它们不是必需的,但它们使正则表达式更加高效,这在本项目中尤为重要


还有一件事:如果要使用
find()
进行此操作,还应添加
\G
(上次比赛结束锚定)每个正则表达式的开头,就像Friedl在中所做的那样。

我不知道你在问什么。我只是建议你使用MULTILINE选项编译你的模式,这样你上面代码中的每一行看起来都像
patternColors.put(Pattern.compile(TAG_Pattern,Pattern.MULTILINE),new Color(163,21,21))
等等……我试过了,但它不起作用。我的问题是,当它在XMl文件中添加注释时,它会高亮显示为绿色,但当注释扩展到多行时,它不会显示为绿色。我希望即使注释有多行,它也应该显示为绿色,而现在它只对单行有效。我理解,但我不是苏re如何解决这个问题。drawUnselectedText是否同时为整个文本调用?如果逐行调用,则打开所有
多行
会改变锚定的行为,
^
$
,允许它们在行边界以及整个区域的边界上匹配。@suraj甚至没有使用e锚定,所以这没有帮助。另外,PlainView一次只绘制一行文本,所以没有,
drawUnselectedText()
从不跨越多行。我不知道你在问什么。我只是建议你使用多行选项编译你的模式,这样上面代码中的每一行看起来都像
patternColors.put(Pattern.compile(TAG\u Pattern,Pattern.MULTILINE),new Color(16
"(<\\!--[\\w * \\S]*-->)"
"<!--[^-]*+(?>-(?!->))*+-->"