KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > token > AbstractToken


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.lexer.token;
21
22 import org.netbeans.api.lexer.PartType;
23 import org.netbeans.api.lexer.Token;
24 import org.netbeans.api.lexer.TokenHierarchy;
25 import org.netbeans.api.lexer.TokenId;
26 import org.netbeans.lib.editor.util.CharSequenceUtilities;
27 import org.netbeans.lib.lexer.EmbeddedTokenList;
28 import org.netbeans.lib.lexer.LexerApiPackageAccessor;
29 import org.netbeans.lib.lexer.TokenList;
30
31 /**
32  * Abstract token is base class of all token implementations used in the lexer module.
33  *
34  * @author Miloslav Metelka
35  * @version 1.00
36  */

37
38 public abstract class AbstractToken<T extends TokenId> extends Token<T> implements CharSequence JavaDoc {
39     
40     private final T id; // 12 bytes (8-super + 4)
41

42     private TokenList<T> tokenList; // 16 bytes
43

44     private int rawOffset; // 20 bytes
45

46     /**
47      * @id non-null token id.
48      */

49     public AbstractToken(T id) {
50         assert (id != null);
51         this.id = id;
52     }
53     
54     AbstractToken(T id, TokenList<T> tokenList, int rawOffset) {
55         this.id = id;
56         this.tokenList = tokenList;
57         this.rawOffset = rawOffset;
58     }
59     
60     public abstract int length();
61     
62     /**
63      * Get identification of this token.
64      *
65      * @return non-null identification of this token.
66      */

67     @Override JavaDoc
68     public final T id() {
69         return id;
70     }
71
72     /**
73      * Get text represented by this token.
74      */

75     @Override JavaDoc
76     public CharSequence JavaDoc text() {
77         if (tokenList != null) {
78             if (tokenList.getClass() == EmbeddedTokenList.class) {
79                 ((EmbeddedTokenList)tokenList).updateStartOffset();
80             }
81             return this;
82         } else {
83             return null;
84         }
85     }
86
87     /**
88      * Get token list to which this token delegates its operation.
89      */

90     public final TokenList<T> tokenList() {
91         return tokenList;
92     }
93
94     /**
95      * Release this token from being attached to its parent token list.
96      */

97     public final void setTokenList(TokenList<T> tokenList) {
98         this.tokenList = tokenList;
99     }
100
101     /**
102      * Get raw offset of this token.
103      * <br/>
104      * Raw offset must be preprocessed before obtaining the real offset.
105      */

106     public final int rawOffset() {
107         return rawOffset;
108     }
109
110     /**
111      * Set raw offset of this token.
112      *
113      * @param rawOffset new raw offset.
114      */

115     public final void setRawOffset(int rawOffset) {
116         this.rawOffset = rawOffset;
117     }
118
119     @Override JavaDoc
120     public final boolean isFlyweight() {
121         return (rawOffset == -1);
122     }
123
124     public final void makeFlyweight() {
125         setRawOffset(-1);
126     }
127     
128     @Override JavaDoc
129     public PartType partType() {
130         return PartType.COMPLETE;
131     }
132
133     @Override JavaDoc
134     public boolean isCustomText() {
135         return false;
136     }
137
138     @Override JavaDoc
139     public final int offset(TokenHierarchy<?> tokenHierarchy) {
140         if (rawOffset == -1) { // flyweight token
141
return -1;
142         }
143
144         if (tokenHierarchy != null) {
145             return LexerApiPackageAccessor.get().tokenHierarchyOperation(
146                     tokenHierarchy).tokenOffset(this, tokenList, rawOffset);
147         } else {
148             return (tokenList != null)
149                 ? tokenList.childTokenOffset(rawOffset)
150                 : rawOffset;
151         }
152     }
153     
154     @Override JavaDoc
155     public boolean isPreprocessedText() {
156         return false;
157     }
158     
159     @Override JavaDoc
160     public CharSequence JavaDoc preprocessedText() {
161         return null;
162     }
163     
164     @Override JavaDoc
165     public String JavaDoc preprocessError() {
166         return null;
167     }
168
169     @Override JavaDoc
170     public int preprocessErrorIndex() {
171         return -1;
172     }
173     
174     @Override JavaDoc
175     public boolean hasProperties() {
176         return false;
177     }
178     
179     @Override JavaDoc
180     public Object JavaDoc getProperty(Object JavaDoc key) {
181         return null;
182     }
183
184     // CharSequence methods
185
/**
186      * Implementation of <code>CharSequence.charAt()</code>
187      */

188     public final char charAt(int index) {
189         if (index < 0 || index >= length()) {
190             throw new IndexOutOfBoundsException JavaDoc(
191                 "index=" + index + ", length=" + length() // NOI18N
192
);
193         }
194         
195         return tokenList.childTokenCharAt(rawOffset, index);
196     }
197
198     public final CharSequence JavaDoc subSequence(int start, int end) {
199         return CharSequenceUtilities.toString(this, start, end);
200     }
201     
202     /**
203      * This method is in fact <code>CharSequence.toString()</code> implementation.
204      */

205     public String JavaDoc toString() {
206         return CharSequenceUtilities.toString(this, 0, length());
207     }
208
209     /**
210      * Dump various information about this token
211      * into a string for debugging purporses.
212      * <br>
213      * A regular <code>toString()</code> usually just returns
214      * a text of the token to satisfy acting of the token instance
215      * as <code>CharSequence</code>.
216      *
217      * @param tokenHierarchy token hierarchy to which the dumped
218      * token offsets are related. It may be null which means
219      * that the live token hierarchy will be used.
220      * @return dump of the thorough token information.
221      */

222     public String JavaDoc dumpInfo(TokenHierarchy tokenHierarchy) {
223         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
224         sb.append(dumpInfoTokenType());
225         sb.append('(').append(id()).append(", "); // NOI18N
226
CharSequence JavaDoc text = text();
227         if (text != null) {
228             sb.append('"');
229             for (int i = 0; i < text.length(); i++) {
230                 try {
231                     CharSequenceUtilities.debugChar(sb, text.charAt(i));
232                 } catch (IndexOutOfBoundsException JavaDoc e) {
233                     // For debugging purposes it's better than to completely fail
234
sb.append("IOOBE at index=").append(i).append("!!!"); // NOI18N
235
break;
236                 }
237             }
238             sb.append('"');
239         } else {
240             sb.append("<null>"); // NOI18N
241
}
242         sb.append(", ").append(offset(tokenHierarchy)).append(')'); // NOI18N
243
return sb.toString();
244     }
245     
246     protected String JavaDoc dumpInfoTokenType() {
247         return "AbsT"; // NOI18N "AbstractToken"
248
}
249     
250 }
251
Popular Tags