KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.*;
23
24 import javax.swing.text.*;
25
26 import org.netbeans.editor.ext.*;
27
28 /**
29  *
30  * @author Petr Jiricka, Petr Nejedly
31  * @version
32  */

33 public abstract class SyntaxElement extends Object JavaDoc {
34
35     private JspSyntaxSupport support;
36     private SyntaxElement previous;
37     private SyntaxElement next;
38
39     int offset;
40     int length;
41
42     /** Creates new SyntaxElement */
43     public SyntaxElement( JspSyntaxSupport support, int from, int to ) {
44         this.support = support;
45         this.offset = from;
46         this.length = to-from;
47     }
48     
49     public abstract int getCompletionContext();
50
51     public int getElementOffset() {
52         return offset;
53     }
54
55     public int getElementLength() {
56         return length;
57     }
58     
59     public SyntaxElement getPrevious() throws BadLocationException {
60         if( previous == null ) {
61             previous = support.getPreviousElement( offset );
62             if( previous != null ) previous.next = this;
63         }
64         return previous;
65     }
66
67     public SyntaxElement getNext() throws BadLocationException {
68         if ( next == null ) {
69             next = support.getNextElement( offset+length );
70             if ( next != null ) next.previous = this;
71         }
72         return next;
73     }
74
75     public String JavaDoc getImage() throws BadLocationException {
76         return support.getDocument().getText(offset, length);
77     }
78
79     public String JavaDoc toString() {
80         String JavaDoc content = "???";
81         try {
82             content = support.getDocument().getText(getElementOffset(), getElementLength());
83         }catch(BadLocationException e) {
84             //do not handle
85
}
86         return "Element [" + offset + "," + (offset+length-1) + "] (" + content + ")"; // NOI18N
87
}
88
89     public static class Comment extends SyntaxElement {
90         public Comment( JspSyntaxSupport support, int from, int to ) {
91             super( support, from, to );
92         }
93
94         public int getCompletionContext() {
95             return JspSyntaxSupport.COMMENT_COMPLETION_CONTEXT;
96         }
97         
98         public String JavaDoc toString() {
99             return "JSP Comment " + super.toString(); // NOI18N
100
}
101     }
102     
103     public static class ExpressionLanguage extends SyntaxElement {
104         public ExpressionLanguage( JspSyntaxSupport support, int from, int to ) {
105             super( support, from, to );
106         }
107
108         public int getCompletionContext() {
109             return JspSyntaxSupport.EL_COMPLETION_CONTEXT;
110         }
111         
112         public String JavaDoc toString() {
113             return "Expression Language " + super.toString(); // NOI18N
114
}
115     }
116
117     public static class Text extends SyntaxElement {
118         public Text( JspSyntaxSupport support, int from, int to ) {
119             super( support, from, to );
120         }
121
122         public int getCompletionContext() {
123             return JspSyntaxSupport.TEXT_COMPLETION_CONTEXT;
124         }
125         
126         public String JavaDoc toString() {
127             return "JSP Text " + super.toString(); // NOI18N
128
}
129     }
130
131     public static class ContentL extends SyntaxElement {
132         public ContentL( JspSyntaxSupport support, int from, int to ) {
133             super( support, from, to );
134         }
135
136         public int getCompletionContext() {
137             return JspSyntaxSupport.CONTENTL_COMPLETION_CONTEXT;
138         }
139         
140         public String JavaDoc toString() {
141             return "JSP Content Language " + super.toString(); // NOI18N
142
}
143     }
144
145     public static class ScriptingL extends SyntaxElement {
146         public ScriptingL( JspSyntaxSupport support, int from, int to ) {
147             super( support, from, to );
148         }
149
150         public int getCompletionContext() {
151             return JspSyntaxSupport.SCRIPTINGL_COMPLETION_CONTEXT;
152         }
153         
154         public String JavaDoc toString() {
155             return "JSP Scripting Language " + super.toString(); // NOI18N
156
}
157     }
158
159     public static class Error extends SyntaxElement {
160         public Error( JspSyntaxSupport support, int from, int to ) {
161             super( support, from, to );
162         }
163
164         public int getCompletionContext() {
165             return JspSyntaxSupport.ERROR_COMPLETION_CONTEXT;
166         }
167         
168         public String JavaDoc toString() {
169             return "JSP Error " + super.toString(); // NOI18N
170
}
171     }
172     
173     public static abstract class TagLikeElement extends SyntaxElement {
174         String JavaDoc name;
175
176         public TagLikeElement(JspSyntaxSupport support, int from, int to, String JavaDoc name) {
177             super( support, from,to );
178             this.name = name;
179         }
180
181         public String JavaDoc getName() {
182             return name;
183         }
184         
185         public String JavaDoc toString() {
186             return super.toString() + " - '" + name + "'"; // NOI18N
187
}
188     }
189
190     public static class EndTag extends TagLikeElement {
191         public EndTag(JspSyntaxSupport support, int from, int to, String JavaDoc name) {
192             super(support, from, to, name);
193         }
194
195         public int getCompletionContext() {
196             return JspSyntaxSupport.ENDTAG_COMPLETION_CONTEXT;
197         }
198         
199         public String JavaDoc toString() {
200             return "JSP EndTag " + super.toString(); // NOI18N
201
}
202     }
203     
204
205     public static abstract class TagDirective extends TagLikeElement {
206         Map attribs;
207
208         public TagDirective( JspSyntaxSupport support, int from, int to, String JavaDoc name, Map attribs ) {
209             super(support, from, to, name);
210             this.attribs = attribs;
211         }
212
213         public Map getAttributes() {
214             return attribs;
215         }
216
217         public String JavaDoc toString() {
218             StringBuffer JavaDoc ret = new StringBuffer JavaDoc(super.toString() + " - {" ); // NOI18N
219

220             for( Iterator i = attribs.keySet().iterator(); i.hasNext(); ) {
221                 Object JavaDoc next = i.next();
222                 ret.append( next ).
223                 append( "='" ). // NOI18N
224
append( attribs.get(next) ).
225                 append( "', " ); // NOI18N
226
}
227
228             ret.append( "}" ); // NOI18N
229
return ret.toString();
230         }
231     }
232     
233     public static class Tag extends TagDirective {
234         /** is tag closed immediately (it has not a body and closing tag) */
235         private boolean isClosed;
236
237         public Tag( JspSyntaxSupport support, int from, int to, String JavaDoc name, Map attribs, boolean isClosed) {
238             super(support, from, to, name, attribs);
239             this.isClosed = isClosed;
240         }
241
242         public Tag( JspSyntaxSupport support, int from, int to, String JavaDoc name, Map attribs ) {
243             this(support, from, to, name, attribs, false);
244         }
245
246         public int getCompletionContext() {
247             return JspSyntaxSupport.TAG_COMPLETION_CONTEXT;
248         }
249         
250         public boolean isClosed () {
251             return isClosed;
252         }
253         
254         public String JavaDoc toString() {
255             return "JSP Tag " + super.toString(); // NOI18N
256
}
257     }
258
259     public static class Directive extends TagDirective {
260
261         public Directive( JspSyntaxSupport support, int from, int to, String JavaDoc name, Map attribs ) {
262             super(support, from, to, name, attribs);
263         }
264
265         public int getCompletionContext() {
266             return JspSyntaxSupport.DIRECTIVE_COMPLETION_CONTEXT;
267         }
268         
269         public String JavaDoc toString() {
270             return "JSP Directive " + super.toString(); // NOI18N
271
}
272     }
273
274
275 }
Popular Tags