KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > lexer > gen > javacc > JavaCCTokenTypes


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.javacc;
21
22 import java.lang.reflect.Field JavaDoc;
23 import java.lang.SecurityException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.List JavaDoc;
31 import org.netbeans.modules.lexer.gen.MutableTokenId;
32 import org.netbeans.modules.lexer.gen.TokenTypes;
33
34 /**
35  * Encapsulation of xxxConstants class generated by JavaCC.
36  *
37  * @author Miloslav Metelka
38  * @version 1.00
39  */

40
41 public class JavaCCTokenTypes extends TokenTypes {
42     
43     private boolean defaultStateFieldFound;
44     
45     private final Map JavaDoc name2sample = new HashMap JavaDoc();
46     
47     /**
48      * Maximum value of state determined as maximum constant that occurs
49      * in the xxxConstants class after DEFAULT field.
50      */

51     private int maxState;
52     
53     public JavaCCTokenTypes(Class JavaDoc tokenTypesClass) {
54         super(tokenTypesClass);
55     }
56     
57     protected void updateId(MutableTokenId id) {
58         super.updateId(id);
59         
60         String JavaDoc sampleText = getSampleText(id.getTokenTypeName());
61         if (sampleText != null) {
62             id.addSampleText(sampleText);
63         }
64     }
65
66     public String JavaDoc getSampleText(String JavaDoc tokenTypeName) {
67         inspect();
68
69         return (String JavaDoc)name2sample.get(tokenTypeName);
70     }
71     
72     public int getMaxState() {
73         return maxState;
74     }
75
76     protected boolean inspect() {
77         if (!super.inspect()) {
78             return false;
79         }
80
81         try {
82             Field JavaDoc tokenImageField = getTokenTypesClass().getDeclaredField("tokenImage");
83             String JavaDoc[] tokenImage = (String JavaDoc[])tokenImageField.get(null);
84
85             for (int i = 0; i < tokenImage.length; i++) {
86                 String JavaDoc tokenTypeName = getTokenTypeName(i);
87                 if (tokenTypeName != null) {
88                     String JavaDoc img = tokenImage[i];
89                     if (img != null && img.startsWith("\"")) {
90                         img = img.substring(1, img.length() - 1);
91                         name2sample.put(tokenTypeName, img);
92                     }
93                 }
94             }
95
96         } catch (NoSuchFieldException JavaDoc e) {
97             e.printStackTrace();
98         } catch (SecurityException JavaDoc e) {
99             e.printStackTrace();
100         } catch (IllegalAccessException JavaDoc e) {
101             e.printStackTrace();
102         }
103         
104         return true; // inspection really done
105
}
106
107     protected boolean isAccepted(String JavaDoc tokenTypeName, int tokenTypeValue) {
108         if (!defaultStateFieldFound) {
109             if ("DEFAULT".equals(tokenTypeName)) {
110                 defaultStateFieldFound = true;
111             }
112
113         } else { // default state already found
114
maxState = Math.max(maxState, tokenTypeValue);
115         }
116
117         return !defaultStateFieldFound;
118     }
119
120 }
121
122
Popular Tags