KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > core > syntax > JspParserErrorAnnotation


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.core.syntax;
21
22 /**
23  *
24  * @author Petr Pisl, Marek Fukala
25  */

26
27
28 import org.netbeans.api.jsp.lexer.JspTokenId;
29 import org.netbeans.api.lexer.Token;
30 import org.netbeans.api.lexer.TokenHierarchy;
31 import org.netbeans.api.lexer.TokenSequence;
32 import org.netbeans.editor.ext.ExtSyntaxSupport;
33 import org.netbeans.modules.editor.NbEditorDocument;
34 import org.netbeans.modules.web.core.syntax.spi.ErrorAnnotation;
35 import org.openide.text.Line;
36 import org.openide.text.Line.Set;
37 import org.openide.text.NbDocument;
38
39 public class JspParserErrorAnnotation extends ErrorAnnotation.LineSetAnnotation {
40     
41     /** Document line where the bug is reported
42      */

43     private Line docline;
44     /** Line and column, where the bug is reported
45      *
46      */

47     private final int line,column;
48     /** The description of the error.
49      */

50     private final String JavaDoc error;
51     /** The document, where the error is.
52      */

53     private NbEditorDocument document;
54     /** Creates a new instance of JspParserErrorAnnotation */
55     public JspParserErrorAnnotation(int line, int column, String JavaDoc error, NbEditorDocument document) {
56         this.line = line;
57         this.column = column;
58         this.error = error;
59         this.document = document;
60     }
61     
62     public String JavaDoc getShortDescription() {
63         // Localize this with NbBundle:
64
return error;
65     }
66     
67     public int getLine(){
68         return line;
69     }
70     
71     public int getColumn(){
72         return column;
73     }
74     
75     public String JavaDoc getError(){
76         return error;
77     }
78     
79     public String JavaDoc getAnnotationType() {
80         return "org-netbeans-modules-web-core-syntax-JspParserErrorAnnotation"; //NOI18N
81
}
82     
83     public void attachToLineSet(Set lines) {
84         char string[];
85         int start,end;
86         Line.Part part;
87         
88         try {
89             docline=lines.getCurrent(line-1);
90         } catch (IndexOutOfBoundsException JavaDoc ex) {
91             // the document has been changed and the line is deleted
92
return;
93         }
94         
95         String JavaDoc annTxt = docline.getText(); // text on the line
96
if (annTxt == null) return; // document is already closed
97

98         ExtSyntaxSupport support = (ExtSyntaxSupport)document.getSyntaxSupport();
99         int offset = NbDocument.findLineOffset(document, docline.getLineNumber()) + column+1; // offset, where the bug is reported
100
start = 0; // column, where the underlining starts on the line, where the bug should be attached. default first column
101
string = annTxt.toCharArray();
102         end = string.length - 1; // length of the underlining
103

104         // when the error is reported outside the page, underline the first line
105
if (offset < 1){
106             textOnLine(docline);
107             return;
108         }
109         TokenHierarchy tokenHierarchy = TokenHierarchy.get(document);
110         TokenSequence tokenSequence = tokenHierarchy.tokenSequence();
111         tokenSequence.move(offset - 1);
112         if (!tokenSequence.moveNext() && !tokenSequence.movePrevious()) {
113             //no token
114
textOnLine(docline);
115             return ;
116         }
117         start = NbDocument.findLineColumn(document, tokenSequence.token().offset(tokenHierarchy));
118         offset = tokenSequence.token().offset(tokenHierarchy);
119         
120         // Find the start and the end of the appropriate tag or EL
121
if (tokenSequence.token().id() != JspTokenId.EL){
122             // the error is in the tag or directive
123
// find the start of the tag, directive
124
while (!(tokenSequence.token().id() == JspTokenId.SYMBOL
125                     && tokenSequence.token().text().toString().charAt(0) == '<' //directive
126
|| tokenSequence.token().id() == JspTokenId.TAG) //or jsp tag
127
&& tokenSequence.token().id() != JspTokenId.EOL
128                     && tokenSequence.movePrevious()) {
129                 start = NbDocument.findLineColumn(document, tokenSequence.token().offset(tokenHierarchy));
130                 offset = tokenSequence.token().offset(tokenHierarchy);
131             }
132             
133             // find the end of the tag or directive
134
while ((tokenSequence.token().id() != JspTokenId.SYMBOL
135                     || tokenSequence.token().text().toString().charAt(tokenSequence.token().text().toString().trim().length()-1) != '>')
136                     && tokenSequence.token().id() != JspTokenId.EOL && tokenSequence.moveNext());
137         } else {
138             // The error is in EL - start and offset are set properly - we have one big EL token now in JspLexer
139
}
140         
141         end = tokenSequence.token().offset(tokenHierarchy) + tokenSequence.token().length() - offset;
142         
143 // if (token != null)
144
// end = token.getOffset() + token.getImage().trim().length() - offset;
145
// else {
146
// while (end >= 0 && end > start && string[end] != ' ') {
147
// end--;
148
// }
149
// }
150

151         part=docline.createPart(start, end);//token.getImage().length());
152
attach(part);
153     }
154     
155     private void textOnLine(Line docline){
156         int start = 0; // column, where the underlining starts on the line, where the bug should be attached. default first column
157
char string[] = docline.getText().toCharArray();
158         int end = string.length - 1; // length of the underlining
159
Line.Part part;
160         
161         while (start<=end && string[start]<=' ') {
162             start++;
163         }
164         while (start<=end && string[end]<=' ') {
165             end--;
166         }
167         if (start<=end)
168             part=docline.createPart(start,end-start+1);
169         else
170             part=docline.createPart(0,string.length);
171         attach(part);
172         return;
173     }
174     
175     public boolean equals(Object JavaDoc obj) {
176         if (obj instanceof JspParserErrorAnnotation) {
177             JspParserErrorAnnotation ann=(JspParserErrorAnnotation)obj;
178             
179             if (this==obj)
180                 return true;
181             if (line!=ann.getLine())
182                 return false;
183             if (column!=ann.getColumn())
184                 return false;
185             if (!error.equals(ann.getError()))
186                 return false;
187             /*if (getState()==STATE_DETACHED || ann.getState()==STATE_DETACHED)
188                 return false;*/

189             return true;
190         }
191         return false;
192     }
193 }
194
Popular Tags