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.gen;
043:
044: import java.util.HashMap;
045: import java.util.List;
046: import java.util.ArrayList;
047: import java.util.Iterator;
048: import org.netbeans.modules.lexer.gen.util.LexerGenUtilities;
049:
050: /**
051: * Mutable alternative of the {@link org.netbeans.api.lexer.TokenId}
052: * used when the generated language source is being composed.
053: *
054: * @author Miloslav Metelka
055: * @version 1.00
056: */
057:
058: public class MutableTokenId {
059:
060: public static final String SAMPLE_TEXT_CHECK_NONE = "none";
061: public static final String SAMPLE_TEXT_CHECK_LENGTH = "length";
062: public static final String SAMPLE_TEXT_CHECK_TEXT = "text";
063:
064: private final LanguageData languageData;
065:
066: private final String name;
067:
068: private int intId;
069:
070: private String tokenTypeName;
071:
072: private List categoryNames;
073:
074: private List sampleTexts;
075:
076: private String sampleTextCheck;
077:
078: private boolean caseInsensitive;
079:
080: private String comment;
081:
082: /**
083: * Construct new MutableTokenId. All the properties
084: * are set by corresponding setters.
085: */
086: public MutableTokenId(LanguageData languageData, String name) {
087: this .languageData = languageData;
088: this .name = name;
089: this .intId = -1; // assign invalid int id initially
090:
091: categoryNames = new ArrayList();
092: sampleTexts = new ArrayList();
093: }
094:
095: /**
096: * @return the language data to which this mutable tokenId belongs.
097: */
098: public final LanguageData getLanguageData() {
099: return languageData;
100: }
101:
102: /**
103: * @return name of this tokenId.
104: */
105: public String getName() {
106: return name;
107: }
108:
109: /**
110: * @return numeric identification of this tokenId.
111: * The initially assigned intId is set to -1.
112: */
113: public int getIntId() {
114: return intId;
115: }
116:
117: /**
118: * Override the currently set intId (if any).
119: * <BR>The value being set
120: * will be skipped in the LanguageData automatically.
121: * @param intId integer identification of this tokenId.
122: */
123: public void setIntId(int intId) {
124: this .intId = intId;
125: // Skip this numeric intId
126: languageData.skipIntId(intId);
127: }
128:
129: /**
130: * @return name of the static field for this tokenId
131: * in the generated language class.
132: */
133: public String getFieldName() {
134: return LexerGenUtilities.idToUpperCase(getName());
135: }
136:
137: /**
138: * @return name of the static field for the integer constant
139: * for this tokenId in the generated language class.
140: */
141: public String getIntIdFieldName() {
142: return getFieldName() + "_INT";
143: }
144:
145: /**
146: * Assign unique int identification by using
147: * {@link LanguageData#uniqueIntId()}.
148: */
149: public void assignUniqueIntId() {
150: setIntId(languageData.uniqueIntId());
151: }
152:
153: /**
154: * @return name of the field in the token-types class
155: * (e.g. xxxConstants for javacc or xxxTokenTypes for antlr)
156: * that corresponds to this token id or null
157: * if this token-id does not have any associated field
158: * in the token-types class.
159: */
160: public String getTokenTypeName() {
161: return tokenTypeName;
162: }
163:
164: public void updateByTokenType(String tokenTypeName) {
165: this .tokenTypeName = tokenTypeName;
166: languageData.getTokenTypes().updateId(this );
167: }
168:
169: /**
170: * @return list of category names to which this token belongs.
171: * The list can be modified by adding/removing category names.
172: */
173: public List getCategoryNames() {
174: return categoryNames;
175: }
176:
177: /**
178: * @return whether this tokenId is case insensitive
179: * which means that when adding a new sampleText
180: * by {@link addSampleText(String)} then
181: * the corresponding upper-case and lower-case
182: * representations are added too.
183: * <BR>For "html" sample text the "HTML"
184: * will be added too.
185: * <BR>For "Begin" sample text the "begin" and "BEGIN"
186: * will be added too.
187: *
188: * <P>The value of this flag is not transferred
189: * into resulting {@link org.netbeans.api.lexer.TokenId}
190: * in any way.
191: */
192: public boolean isCaseInsensitive() {
193: return caseInsensitive;
194: }
195:
196: /**
197: * Set whether this tokenId is case insensitive or not.
198: * @param caseInsensitive whether this tokenId is case insensitive.
199: * If the value is true then all the existing sample texts
200: * will be re-added so the number of the sample
201: * texts can grow as explained in {@link #isCaseInsensitive()}.
202: * @see #isCaseInsensitive()
203: */
204: public void setCaseInsensitive(boolean caseInsensitive) {
205: this .caseInsensitive = caseInsensitive;
206:
207: if (caseInsensitive) { // re-add the existing samples
208: Iterator currentSamplesIterator = new ArrayList(sampleTexts)
209: .iterator();
210: sampleTexts.clear();
211: while (currentSamplesIterator.hasNext()) {
212: addSampleText((String) currentSamplesIterator.next());
213: }
214: }
215: }
216:
217: /** Add a sample text to the tokenId.
218: * @param sampleText sample text to add to the current list of sample texts.
219: */
220: public void addSampleText(String sampleText) {
221: addUniqueSampleText(sampleText);
222:
223: if (caseInsensitive) {
224: addUniqueSampleText(sampleText.toLowerCase());
225: addUniqueSampleText(sampleText.toUpperCase());
226: }
227: }
228:
229: private void addUniqueSampleText(String sampleText) {
230: if (sampleTexts.indexOf(sampleText) == -1) {
231: sampleTexts.add(sampleText);
232: }
233: }
234:
235: /** Get the sample texts list.
236: */
237: public List getSampleTexts() {
238: return sampleTexts;
239: }
240:
241: /**
242: * Clear all existing sample texts.
243: */
244: public void resetSamples() {
245: sampleTexts.clear();
246: }
247:
248: public String getSampleTextCheck() {
249: return sampleTextCheck;
250: }
251:
252: public void setSampleTextCheck(String sampleTextCheck) {
253: this .sampleTextCheck = sampleTextCheck;
254: }
255:
256: public String getComment() {
257: return comment;
258: }
259:
260: public void setComment(String comment) {
261: this .comment = comment;
262: }
263:
264: public String toString() {
265: StringBuffer sb = new StringBuffer();
266: sb.append(getName());
267: sb.append(", intId=");
268: sb.append(getIntId());
269: sb.append(", tokenType=");
270: sb.append(getTokenTypeName());
271: List samples = getSampleTexts();
272: int samplesCount = samples.size();
273: for (int i = 0; i < samplesCount; i++) {
274: if (i == 0) {
275: sb.append(", samples={");
276: }
277: sb.append('"');
278: sb.append(samples.get(i));
279: sb.append('"');
280: if (i == samplesCount - 1) {
281: sb.append('}');
282: }
283: }
284: sb.append(", sampleTextCheck=");
285: sb.append(getSampleTextCheck());
286:
287: return sb.toString();
288: }
289:
290: }
|