KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > lexer > gen > MutableTokenId


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.gen;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import org.netbeans.modules.lexer.gen.util.LexerGenUtilities;
27
28 /**
29  * Mutable alternative of the {@link org.netbeans.api.lexer.TokenId}
30  * used when the generated language source is being composed.
31  *
32  * @author Miloslav Metelka
33  * @version 1.00
34  */

35
36 public class MutableTokenId {
37
38     public static final String JavaDoc SAMPLE_TEXT_CHECK_NONE = "none";
39     public static final String JavaDoc SAMPLE_TEXT_CHECK_LENGTH = "length";
40     public static final String JavaDoc SAMPLE_TEXT_CHECK_TEXT = "text";
41     
42     private final LanguageData languageData;
43     
44     private final String JavaDoc name;
45     
46     private int intId;
47     
48     private String JavaDoc tokenTypeName;
49     
50     private List JavaDoc categoryNames;
51     
52     private List JavaDoc sampleTexts;
53     
54     private String JavaDoc sampleTextCheck;
55     
56     private boolean caseInsensitive;
57     
58     private String JavaDoc comment;
59     
60
61     /**
62      * Construct new MutableTokenId. All the properties
63      * are set by corresponding setters.
64      */

65     public MutableTokenId(LanguageData languageData, String JavaDoc name) {
66         this.languageData = languageData;
67         this.name = name;
68         this.intId = -1; // assign invalid int id initially
69

70         categoryNames = new ArrayList JavaDoc();
71         sampleTexts = new ArrayList JavaDoc();
72     }
73     
74     /**
75      * @return the language data to which this mutable tokenId belongs.
76      */

77     public final LanguageData getLanguageData() {
78         return languageData;
79     }
80     
81     /**
82      * @return name of this tokenId.
83      */

84     public String JavaDoc getName() {
85         return name;
86     }
87     
88     /**
89      * @return numeric identification of this tokenId.
90      * The initially assigned intId is set to -1.
91      */

92     public int getIntId() {
93         return intId;
94     }
95
96     /**
97      * Override the currently set intId (if any).
98      * <BR>The value being set
99      * will be skipped in the LanguageData automatically.
100      * @param intId integer identification of this tokenId.
101      */

102     public void setIntId(int intId) {
103         this.intId = intId;
104         // Skip this numeric intId
105
languageData.skipIntId(intId);
106     }
107     
108     /**
109      * @return name of the static field for this tokenId
110      * in the generated language class.
111      */

112     public String JavaDoc getFieldName() {
113         return LexerGenUtilities.idToUpperCase(getName());
114     }
115
116     /**
117      * @return name of the static field for the integer constant
118      * for this tokenId in the generated language class.
119      */

120     public String JavaDoc getIntIdFieldName() {
121         return getFieldName() + "_INT";
122     }
123     
124     /**
125      * Assign unique int identification by using
126      * {@link LanguageData#uniqueIntId()}.
127      */

128     public void assignUniqueIntId() {
129         setIntId(languageData.uniqueIntId());
130     }
131
132     /**
133      * @return name of the field in the token-types class
134      * (e.g. xxxConstants for javacc or xxxTokenTypes for antlr)
135      * that corresponds to this token id or null
136      * if this token-id does not have any associated field
137      * in the token-types class.
138      */

139     public String JavaDoc getTokenTypeName() {
140         return tokenTypeName;
141     }
142     
143     public void updateByTokenType(String JavaDoc tokenTypeName) {
144         this.tokenTypeName = tokenTypeName;
145         languageData.getTokenTypes().updateId(this);
146     }
147     
148     /**
149      * @return list of category names to which this token belongs.
150      * The list can be modified by adding/removing category names.
151      */

152     public List JavaDoc getCategoryNames() {
153         return categoryNames;
154     }
155     
156     /**
157      * @return whether this tokenId is case insensitive
158      * which means that when adding a new sampleText
159      * by {@link addSampleText(String)} then
160      * the corresponding upper-case and lower-case
161      * representations are added too.
162      * <BR>For "html" sample text the "HTML"
163      * will be added too.
164      * <BR>For "Begin" sample text the "begin" and "BEGIN"
165      * will be added too.
166      *
167      * <P>The value of this flag is not transferred
168      * into resulting {@link org.netbeans.api.lexer.TokenId}
169      * in any way.
170      */

171     public boolean isCaseInsensitive() {
172         return caseInsensitive;
173     }
174
175     /**
176      * Set whether this tokenId is case insensitive or not.
177      * @param caseInsensitive whether this tokenId is case insensitive.
178      * If the value is true then all the existing sample texts
179      * will be re-added so the number of the sample
180      * texts can grow as explained in {@link #isCaseInsensitive()}.
181      * @see #isCaseInsensitive()
182      */

183     public void setCaseInsensitive(boolean caseInsensitive) {
184         this.caseInsensitive = caseInsensitive;
185         
186         if (caseInsensitive) { // re-add the existing samples
187
Iterator JavaDoc currentSamplesIterator = new ArrayList JavaDoc(sampleTexts).iterator();
188             sampleTexts.clear();
189             while (currentSamplesIterator.hasNext()) {
190                 addSampleText((String JavaDoc)currentSamplesIterator.next());
191             }
192         }
193     }
194
195     /** Add a sample text to the tokenId.
196      * @param sampleText sample text to add to the current list of sample texts.
197      */

198     public void addSampleText(String JavaDoc sampleText) {
199         addUniqueSampleText(sampleText);
200         
201         if (caseInsensitive) {
202             addUniqueSampleText(sampleText.toLowerCase());
203             addUniqueSampleText(sampleText.toUpperCase());
204         }
205     }
206
207     private void addUniqueSampleText(String JavaDoc sampleText) {
208         if (sampleTexts.indexOf(sampleText) == -1) {
209             sampleTexts.add(sampleText);
210         }
211     }
212     
213     /** Get the sample texts list.
214      */

215     public List JavaDoc getSampleTexts() {
216         return sampleTexts;
217     }
218     
219     /**
220      * Clear all existing sample texts.
221      */

222     public void resetSamples() {
223         sampleTexts.clear();
224     }
225     
226     public String JavaDoc getSampleTextCheck() {
227         return sampleTextCheck;
228     }
229     
230     public void setSampleTextCheck(String JavaDoc sampleTextCheck) {
231         this.sampleTextCheck = sampleTextCheck;
232     }
233     
234     public String JavaDoc getComment() {
235         return comment;
236     }
237     
238     public void setComment(String JavaDoc comment) {
239         this.comment = comment;
240     }
241
242     public String JavaDoc toString() {
243         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
244         sb.append(getName());
245         sb.append(", intId=");
246         sb.append(getIntId());
247         sb.append(", tokenType=");
248         sb.append(getTokenTypeName());
249         List JavaDoc samples = getSampleTexts();
250         int samplesCount = samples.size();
251         for (int i = 0; i < samplesCount; i++) {
252             if (i == 0) {
253                 sb.append(", samples={");
254             }
255             sb.append('"');
256             sb.append(samples.get(i));
257             sb.append('"');
258             if (i == samplesCount - 1) {
259                 sb.append('}');
260             }
261         }
262         sb.append(", sampleTextCheck=");
263         sb.append(getSampleTextCheck());
264         
265         return sb.toString();
266     }
267
268 }
269
Popular Tags