Creating Error Annotations in NetBeans IDE (Part 4)

I'm visiting Andreas and we've been working on adding error annotations to the Tapestry Support Module for NetBeans. Here's the result so far, a prototype for an annotation on the object attribute in Tapestry inject elements:

I provided the annotation, its registration in the XML layer, the DataEditorSupport implementation, and its addition to the DataObject's Lookup. All this is very standard code, nothing special at all. The contribution by Andreas was more complex—parsing the XML document to the point where the error occurs. Here, a dummy value is filled in. Eventually all the allowed values will be compared against whatever is typed in as the object attribute's value. The method returns the offset of the value that is wrong, as well as the length of the value:

public int[] findError(Document doc) {
    int[] errors = null;
    BaseDocument bdoc = (BaseDocument) doc;
    ExtSyntaxSupport sup = (ExtSyntaxSupport)bdoc.getSyntaxSupport();
    boolean prev = false;
    try {
        TokenItem token = sup.getTokenChain(0, 1);
        while (token!=null) {
            if ("attribute".equals(token.getTokenID().getName())) {
                String attr = token.getImage();
                if ("object".equals(attr)) {
                    TokenItem next = token.getNext().getNext();
                    if (next!=null && "value".equals(next.getTokenID().getName())) {
                        String val = next.getImage();
                        if (val.startsWith("\\"service:tapestry.")) {
                            errors = new int[2];
                            errors[0] = next.getOffset() + 1;
                            errors[1] = next.getNext().getOffset() - next.getOffset();       
            token = token.getNext();
    } catch (BadLocationException ex) {
    return errors;

However, note that here BaseDocument.getSyntaxSupport() is used to get the tokens. There's no other way to do this, as far as I'm aware, although using this approach is not ideal since the Lexer module provides a different way of working with tokens. So, this will have to be rewritten once the Lexer module becomes the official way of working with tokens.


Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« August 2015