KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > lexer > demo > DemoToken


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.modules.lexer.demo;
21
22 import org.netbeans.api.lexer.Token;
23 import org.netbeans.api.lexer.TokenId;
24 import org.netbeans.spi.lexer.inc.RawOffsetToken;
25 import org.netbeans.spi.lexer.util.IntegerCache;
26
27 /**
28  * Simple token implementation for demo purposes.
29  *
30  * @author Miloslav Metelka
31  * @version 1.00
32  */

33
34 public class DemoToken implements RawOffsetToken, CharSequence JavaDoc {
35
36     private final DemoTokenUpdater tokenUpdater;
37
38     private final TokenId id;
39
40     private int rawOffset;
41
42     private final int length;
43
44     private int lookahead;
45     
46     private int lookback;
47     
48     private Object JavaDoc state;
49
50     
51     DemoToken(DemoTokenUpdater tokenUpdater, TokenId id, int rawOffset, int length) {
52         if (tokenUpdater == null) {
53             throw new NullPointerException JavaDoc();
54         }
55         
56         if (id == null) {
57             throw new NullPointerException JavaDoc();
58         }
59         
60         if (rawOffset < 0) {
61             throw new IllegalArgumentException JavaDoc("rawOffset=" + rawOffset);
62         }
63         
64         if (length < 0) {
65             throw new IllegalArgumentException JavaDoc("length=" + length);
66         }
67         
68         this.tokenUpdater = tokenUpdater;
69         this.id = id;
70         this.rawOffset = rawOffset;
71         this.length = length;
72     }
73     
74     protected final DemoTokenUpdater getTokenUpdater() {
75         return tokenUpdater;
76     }
77     
78     public TokenId getId() {
79         return id;
80     }
81
82     public CharSequence JavaDoc getText() {
83         return this;
84     }
85     
86     public int getOffset() {
87         return tokenUpdater.getOffset(rawOffset);
88     }
89     
90     public int getRawOffset() {
91         return rawOffset;
92     }
93     
94     public void setRawOffset(int rawOffset) {
95         this.rawOffset = rawOffset;
96     }
97     
98     public void updateRawOffset(int diff) {
99         rawOffset += diff;
100     }
101     
102     public int length() {
103         return length;
104     }
105     
106     public char charAt(int index) {
107         if (index < 0 || index >= length) {
108             throw new IllegalStateException JavaDoc("index=" + index + ", length=" + length);
109         }
110
111         return tokenUpdater.charAt(rawOffset, index);
112     }
113     
114     public CharSequence JavaDoc subSequence(int start, int end) {
115         if (start < 0 || end < 0 || start > end || end > length()) {
116             throw new IndexOutOfBoundsException JavaDoc(
117                 "start=" + start + ", end=" + end + ", length()=" + length());
118         }
119
120         return (CharSequence JavaDoc)(Object JavaDoc)toString().substring(start, end); // 1.3 compilability
121
}
122     
123     public int getLookahead() {
124         return lookahead;
125     }
126
127     void setLookahead(int lookahead) {
128         this.lookahead = lookahead;
129     }
130
131     public int getLookback() {
132         return lookback;
133     }
134     
135     void setLookback(int lookback) {
136         this.lookback = lookback;
137     }
138     
139     public Object JavaDoc getState() {
140         return state;
141     }
142     
143     void setState(Object JavaDoc state) {
144         this.state = state;
145     }
146     
147     public String JavaDoc toString() {
148         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
149         for (int i = 0; i < length(); i++) {
150             sb.append(charAt(i));
151         }
152         return sb.toString();
153     }
154
155 }
156
157
Popular Tags