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-2006 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.api.languages;
043:
044: import java.util.List;
045:
046: /**
047: * Represents one token in AST.
048: */
049: public final class ASTToken extends ASTItem {
050:
051: private String identifier;
052: private int type;
053:
054: /**
055: * Creates new token with given parameters.
056: *
057: * @param mimeType a mime type of token
058: * @param type a type of token
059: * @param identifier token identifier
060: * @param offset token offset
061: * @param length token length
062: * @param children a list of token children
063: * @return new ASTToken
064: */
065: public static ASTToken create(Language language, int type,
066: String identifier, int offset, int length,
067: List<? extends ASTItem> children) {
068: return new ASTToken(language, type, identifier, offset, length,
069: children);
070: }
071:
072: /**
073: * Creates new token with given parameters.
074: *
075: * @param mimeType a mime type of token
076: * @param type a type of token
077: * @param identifier token identifier
078: * @param offset token offset
079: * @param length token length
080: * @param children a list of token children
081: * @return new ASTToken
082: */
083: public static ASTToken create(Language language, String typeName,
084: String identifier, int offset, int length,
085: List<? extends ASTItem> children) {
086: int typeID = ((org.netbeans.modules.languages.Language) language)
087: .getTokenID(typeName);
088: return new ASTToken(language, typeID, identifier, offset,
089: length, children);
090: }
091:
092: /**
093: * Creates new token with given parameters, no children and length
094: * derived from identifier.
095: *
096: * @param mimeType a mime type of token
097: * @param type a type of token
098: * @param identifier token identifier
099: * @param offset token offset
100: * @return new ASTToken
101: */
102: public static ASTToken create(Language language, int type,
103: String identifier, int offset) {
104: return new ASTToken(language, type, identifier, offset,
105: identifier == null ? 0 : identifier.length(), null);
106: }
107:
108: private ASTToken(Language language, int type, String identifier,
109: int offset, int length, List<? extends ASTItem> children) {
110: super (language, offset, length, children);
111: this .identifier = identifier;
112: this .type = type;
113: }
114:
115: /**
116: * Retruns type of token.
117: *
118: * @return type of token
119: */
120: public int getTypeID() {
121: return type;
122: }
123:
124: public String getTypeName() {
125: org.netbeans.modules.languages.Language l = (org.netbeans.modules.languages.Language) getLanguage();
126: if (l == null)
127: return null;
128: return l.getTokenType(type);
129: }
130:
131: /**
132: * Retruns token identifier.
133: *
134: * @return token identifier
135: */
136: public String getIdentifier() {
137: return identifier;
138: }
139:
140: private String toString;
141:
142: /**
143: * Retruns string representation of this token.
144: *
145: * @return string representation of this token
146: */
147: public String toString() {
148: if (toString == null) {
149: StringBuffer sb = new StringBuffer();
150: sb.append('<').append(getTypeName());
151: if (identifier != null)
152: sb.append(",'").append(e(identifier)).append("'");
153: sb.append('>');
154: toString = sb.toString();
155: }
156: return toString;
157: }
158:
159: private static String e(CharSequence t) {
160: StringBuilder sb = new StringBuilder();
161: int i, k = t.length();
162: for (i = 0; i < k; i++) {
163: if (t.charAt(i) == '\t')
164: sb.append("\\t");
165: else if (t.charAt(i) == '\r')
166: sb.append("\\r");
167: else if (t.charAt(i) == '\n')
168: sb.append("\\n");
169: else
170: sb.append(t.charAt(i));
171: }
172: return sb.toString();
173: }
174: }
|