KickJava   Java API By Example, From Geeks To Geeks.

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


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.TokenId;
23 import org.netbeans.lib.lexer.TokenList;
24
25 /**
26  * Token with an explicit text - either serving as a custom text token
27  * or a flyweight token.
28  * <br/>
29  * The represented text can differ from the original content
30  * of the recognized text input portion.
31  * <br/>
32  * Token with the custom text cannot be branched by a language embedding.
33  *
34  * <p>
35  * The text token can act as a flyweight token by calling
36  * {@link AbstractToken.makeFlyweight()}. In such case a single token
37  * instance is shared for all the occurrences of the token.
38  * <br/>
39  * The rawOffset is -1 and tokenList reference is null.
40  * </p>
41  *
42  * @author Miloslav Metelka
43  * @version 1.00
44  */

45
46 public class TextToken<T extends TokenId> extends AbstractToken<T> {
47     
48     private final CharSequence JavaDoc text; // 24 bytes (20-super + 4)
49

50     /**
51      * Create text token. The token's text
52      * is expected to correspond to the recognized input portion
53      * (i.e. the text is not custom).
54      * <br/>
55      * The token can be made flyweight by using <code>setRawOffset(-1)</code>.
56      *
57      * @param id non-null identification of the token.
58      * @param text non-null text of the token.
59      */

60     public TextToken(T id, CharSequence JavaDoc text) {
61         super(id);
62         assert (text != null);
63         this.text = text;
64     }
65     
66     private TextToken(T id, TokenList<T> tokenList, int rawOffset, CharSequence JavaDoc text) {
67         super(id, tokenList, rawOffset);
68         assert (text != null);
69         this.text = text;
70     }
71
72     @Override JavaDoc
73     public final int length() {
74         return text.length();
75     }
76
77     @Override JavaDoc
78     public final CharSequence JavaDoc text() {
79         return text;
80     }
81     
82     public final TextToken<T> createCopy(TokenList<T> tokenList, int rawOffset) {
83         return new TextToken<T>(id(), tokenList, rawOffset, text());
84     }
85     
86     @Override JavaDoc
87     protected String JavaDoc dumpInfoTokenType() {
88         return isFlyweight() ? "FlyT" : "TexT"; // NOI18N "TextToken" or "FlyToken"
89
}
90
91     public String JavaDoc toString() {
92         return text.toString();
93     }
94
95 }
96
Popular Tags