001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.core.syntax;
043:
044: import java.util.*;
045:
046: import javax.swing.text.*;
047:
048: import org.netbeans.editor.ext.*;
049:
050: /**
051: *
052: * @author Petr Jiricka, Petr Nejedly
053: * @version
054: */
055: public abstract class SyntaxElement extends Object {
056:
057: private JspSyntaxSupport support;
058: private SyntaxElement previous;
059: private SyntaxElement next;
060:
061: int offset;
062: int length;
063:
064: /** Creates new SyntaxElement */
065: public SyntaxElement(JspSyntaxSupport support, int from, int to) {
066: this .support = support;
067: this .offset = from;
068: this .length = to - from;
069: }
070:
071: public abstract int getCompletionContext();
072:
073: public int getElementOffset() {
074: return offset;
075: }
076:
077: public int getElementLength() {
078: return length;
079: }
080:
081: public SyntaxElement getPrevious() throws BadLocationException {
082: if (previous == null) {
083: previous = support.getPreviousElement(offset);
084: if (previous != null)
085: previous.next = this ;
086: }
087: return previous;
088: }
089:
090: public SyntaxElement getNext() throws BadLocationException {
091: if (next == null) {
092: next = support.getNextElement(offset + length);
093: if (next != null)
094: next.previous = this ;
095: }
096: return next;
097: }
098:
099: public String getImage() throws BadLocationException {
100: return support.getDocument().getText(offset, length);
101: }
102:
103: public String toString() {
104: String content = "???";
105: try {
106: content = support.getDocument().getText(getElementOffset(),
107: getElementLength());
108: } catch (BadLocationException e) {
109: //do not handle
110: }
111: return "Element [" + offset + "," + (offset + length - 1)
112: + "] (" + content + ")"; // NOI18N
113: }
114:
115: public static class Comment extends SyntaxElement {
116: public Comment(JspSyntaxSupport support, int from, int to) {
117: super (support, from, to);
118: }
119:
120: public int getCompletionContext() {
121: return JspSyntaxSupport.COMMENT_COMPLETION_CONTEXT;
122: }
123:
124: public String toString() {
125: return "JSP Comment " + super .toString(); // NOI18N
126: }
127: }
128:
129: public static class ExpressionLanguage extends SyntaxElement {
130: public ExpressionLanguage(JspSyntaxSupport support, int from,
131: int to) {
132: super (support, from, to);
133: }
134:
135: public int getCompletionContext() {
136: return JspSyntaxSupport.EL_COMPLETION_CONTEXT;
137: }
138:
139: public String toString() {
140: return "Expression Language " + super .toString(); // NOI18N
141: }
142: }
143:
144: public static class Text extends SyntaxElement {
145: public Text(JspSyntaxSupport support, int from, int to) {
146: super (support, from, to);
147: }
148:
149: public int getCompletionContext() {
150: return JspSyntaxSupport.TEXT_COMPLETION_CONTEXT;
151: }
152:
153: public String toString() {
154: return "JSP Text " + super .toString(); // NOI18N
155: }
156: }
157:
158: public static class ContentL extends SyntaxElement {
159: public ContentL(JspSyntaxSupport support, int from, int to) {
160: super (support, from, to);
161: }
162:
163: public int getCompletionContext() {
164: return JspSyntaxSupport.CONTENTL_COMPLETION_CONTEXT;
165: }
166:
167: public String toString() {
168: return "JSP Content Language " + super .toString(); // NOI18N
169: }
170: }
171:
172: public static class ScriptingL extends SyntaxElement {
173: public ScriptingL(JspSyntaxSupport support, int from, int to) {
174: super (support, from, to);
175: }
176:
177: public int getCompletionContext() {
178: return JspSyntaxSupport.SCRIPTINGL_COMPLETION_CONTEXT;
179: }
180:
181: public String toString() {
182: return "JSP Scripting Language " + super .toString(); // NOI18N
183: }
184: }
185:
186: public static class Error extends SyntaxElement {
187: public Error(JspSyntaxSupport support, int from, int to) {
188: super (support, from, to);
189: }
190:
191: public int getCompletionContext() {
192: return JspSyntaxSupport.ERROR_COMPLETION_CONTEXT;
193: }
194:
195: public String toString() {
196: return "JSP Error " + super .toString(); // NOI18N
197: }
198: }
199:
200: public static abstract class TagLikeElement extends SyntaxElement {
201: String name;
202:
203: public TagLikeElement(JspSyntaxSupport support, int from,
204: int to, String name) {
205: super (support, from, to);
206: this .name = name;
207: }
208:
209: public String getName() {
210: return name;
211: }
212:
213: public String toString() {
214: return super .toString() + " - '" + name + "'"; // NOI18N
215: }
216: }
217:
218: public static class EndTag extends TagLikeElement {
219: public EndTag(JspSyntaxSupport support, int from, int to,
220: String name) {
221: super (support, from, to, name);
222: }
223:
224: public int getCompletionContext() {
225: return JspSyntaxSupport.ENDTAG_COMPLETION_CONTEXT;
226: }
227:
228: public String toString() {
229: return "JSP EndTag " + super .toString(); // NOI18N
230: }
231: }
232:
233: public static abstract class TagDirective extends TagLikeElement {
234: Map attribs;
235:
236: public TagDirective(JspSyntaxSupport support, int from, int to,
237: String name, Map attribs) {
238: super (support, from, to, name);
239: this .attribs = attribs;
240: }
241:
242: public Map getAttributes() {
243: return attribs;
244: }
245:
246: public String toString() {
247: StringBuffer ret = new StringBuffer(super .toString()
248: + " - {"); // NOI18N
249:
250: for (Iterator i = attribs.keySet().iterator(); i.hasNext();) {
251: Object next = i.next();
252: ret.append(next).append("='"). // NOI18N
253: append(attribs.get(next)).append("', "); // NOI18N
254: }
255:
256: ret.append("}"); // NOI18N
257: return ret.toString();
258: }
259: }
260:
261: public static class Tag extends TagDirective {
262: /** is tag closed immediately (it has not a body and closing tag) */
263: private boolean isClosed;
264:
265: public Tag(JspSyntaxSupport support, int from, int to,
266: String name, Map attribs, boolean isClosed) {
267: super (support, from, to, name, attribs);
268: this .isClosed = isClosed;
269: }
270:
271: public Tag(JspSyntaxSupport support, int from, int to,
272: String name, Map attribs) {
273: this (support, from, to, name, attribs, false);
274: }
275:
276: public int getCompletionContext() {
277: return JspSyntaxSupport.TAG_COMPLETION_CONTEXT;
278: }
279:
280: public boolean isClosed() {
281: return isClosed;
282: }
283:
284: public String toString() {
285: return "JSP Tag " + super .toString(); // NOI18N
286: }
287: }
288:
289: public static class Directive extends TagDirective {
290:
291: public Directive(JspSyntaxSupport support, int from, int to,
292: String name, Map attribs) {
293: super (support, from, to, name, attribs);
294: }
295:
296: public int getCompletionContext() {
297: return JspSyntaxSupport.DIRECTIVE_COMPLETION_CONTEXT;
298: }
299:
300: public String toString() {
301: return "JSP Directive " + super .toString(); // NOI18N
302: }
303: }
304:
305: }
|