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.lib.lexer.token;
043:
044: import org.netbeans.api.lexer.TokenId;
045: import org.netbeans.lib.lexer.TokenList;
046:
047: /**
048: * Token with an explicit text - either serving as a custom text token
049: * or a flyweight token.
050: * <br/>
051: * The represented text can differ from the original content
052: * of the recognized text input portion.
053: * <br/>
054: * Token with the custom text cannot be branched by a language embedding.
055: *
056: * <p>
057: * The text token can act as a flyweight token by calling
058: * {@link AbstractToken.makeFlyweight()}. In such case a single token
059: * instance is shared for all the occurrences of the token.
060: * <br/>
061: * The rawOffset is -1 and tokenList reference is null.
062: * </p>
063: *
064: * @author Miloslav Metelka
065: * @version 1.00
066: */
067:
068: public class TextToken<T extends TokenId> extends AbstractToken<T> {
069:
070: private final CharSequence text; // 24 bytes (20-super + 4)
071:
072: /**
073: * Create text token. The token's text
074: * is expected to correspond to the recognized input portion
075: * (i.e. the text is not custom).
076: * <br/>
077: * The token can be made flyweight by using <code>setRawOffset(-1)</code>.
078: *
079: * @param id non-null identification of the token.
080: * @param text non-null text of the token.
081: */
082: public TextToken(T id, CharSequence text) {
083: super (id);
084: assert (text != null);
085: this .text = text;
086: }
087:
088: private TextToken(T id, TokenList<T> tokenList, int rawOffset,
089: CharSequence text) {
090: super (id, tokenList, rawOffset);
091: assert (text != null);
092: this .text = text;
093: }
094:
095: @Override
096: public final int length() {
097: return text.length();
098: }
099:
100: @Override
101: public final CharSequence text() {
102: return text;
103: }
104:
105: public final TextToken<T> createCopy(TokenList<T> tokenList,
106: int rawOffset) {
107: return new TextToken<T>(id(), tokenList, rawOffset, text());
108: }
109:
110: @Override
111: protected String dumpInfoTokenType() {
112: return isFlyweight() ? "FlyT" : "TexT"; // NOI18N "TextToken" or "FlyToken"
113: }
114:
115: public String toString() {
116: return text.toString();
117: }
118:
119: }
|