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-2007 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.lexer.demo;
043:
044: import org.netbeans.api.lexer.Token;
045: import org.netbeans.api.lexer.TokenId;
046: import org.netbeans.spi.lexer.inc.RawOffsetToken;
047: import org.netbeans.spi.lexer.util.IntegerCache;
048:
049: /**
050: * Simple token implementation for demo purposes.
051: *
052: * @author Miloslav Metelka
053: * @version 1.00
054: */
055:
056: public class DemoToken implements RawOffsetToken, CharSequence {
057:
058: private final DemoTokenUpdater tokenUpdater;
059:
060: private final TokenId id;
061:
062: private int rawOffset;
063:
064: private final int length;
065:
066: private int lookahead;
067:
068: private int lookback;
069:
070: private Object state;
071:
072: DemoToken(DemoTokenUpdater tokenUpdater, TokenId id, int rawOffset,
073: int length) {
074: if (tokenUpdater == null) {
075: throw new NullPointerException();
076: }
077:
078: if (id == null) {
079: throw new NullPointerException();
080: }
081:
082: if (rawOffset < 0) {
083: throw new IllegalArgumentException("rawOffset=" + rawOffset);
084: }
085:
086: if (length < 0) {
087: throw new IllegalArgumentException("length=" + length);
088: }
089:
090: this .tokenUpdater = tokenUpdater;
091: this .id = id;
092: this .rawOffset = rawOffset;
093: this .length = length;
094: }
095:
096: protected final DemoTokenUpdater getTokenUpdater() {
097: return tokenUpdater;
098: }
099:
100: public TokenId getId() {
101: return id;
102: }
103:
104: public CharSequence getText() {
105: return this ;
106: }
107:
108: public int getOffset() {
109: return tokenUpdater.getOffset(rawOffset);
110: }
111:
112: public int getRawOffset() {
113: return rawOffset;
114: }
115:
116: public void setRawOffset(int rawOffset) {
117: this .rawOffset = rawOffset;
118: }
119:
120: public void updateRawOffset(int diff) {
121: rawOffset += diff;
122: }
123:
124: public int length() {
125: return length;
126: }
127:
128: public char charAt(int index) {
129: if (index < 0 || index >= length) {
130: throw new IllegalStateException("index=" + index
131: + ", length=" + length);
132: }
133:
134: return tokenUpdater.charAt(rawOffset, index);
135: }
136:
137: public CharSequence subSequence(int start, int end) {
138: if (start < 0 || end < 0 || start > end || end > length()) {
139: throw new IndexOutOfBoundsException("start=" + start
140: + ", end=" + end + ", length()=" + length());
141: }
142:
143: return (CharSequence) (Object) toString().substring(start, end); // 1.3 compilability
144: }
145:
146: public int getLookahead() {
147: return lookahead;
148: }
149:
150: void setLookahead(int lookahead) {
151: this .lookahead = lookahead;
152: }
153:
154: public int getLookback() {
155: return lookback;
156: }
157:
158: void setLookback(int lookback) {
159: this .lookback = lookback;
160: }
161:
162: public Object getState() {
163: return state;
164: }
165:
166: void setState(Object state) {
167: this .state = state;
168: }
169:
170: public String toString() {
171: StringBuffer sb = new StringBuffer();
172: for (int i = 0; i < length(); i++) {
173: sb.append(charAt(i));
174: }
175: return sb.toString();
176: }
177:
178: }
|