1 19 20 package org.netbeans.editor; 21 22 import java.lang.reflect.Field ; 23 import java.lang.reflect.Modifier ; 24 import java.util.HashMap ; 25 import java.util.Arrays ; 26 import java.util.ArrayList ; 27 28 45 46 public class TokenContext { 47 48 private static final TokenContext[] EMPTY_CHILDREN = new TokenContext[0]; 49 50 private final String namePrefix; 51 52 private final TokenContext[] children; 53 54 private final HashMap pathCache = new HashMap (37); 55 56 private final ArrayList tokenIDList = new ArrayList (); 57 58 private final ArrayList tokenCategoryList = new ArrayList (); 59 60 private TokenID[] tokenIDs; 61 62 private TokenCategory[] tokenCategories; 63 64 private TokenContextPath contextPath; 65 66 private TokenContextPath[] allContextPaths; 67 68 private TokenContextPath[] lastContextPathPair; 69 70 public TokenContext(String namePrefix) { 71 this(namePrefix, EMPTY_CHILDREN); 72 } 73 74 78 public TokenContext(String namePrefix, TokenContext[] children) { 79 if (namePrefix == null) { 80 throw new IllegalArgumentException ("Name prefix must be non-null."); } 82 83 this.namePrefix = namePrefix.intern(); 84 this.children = (children != null) ? children : EMPTY_CHILDREN; 85 86 contextPath = TokenContextPath.get(new TokenContext[] { this }); 87 } 88 89 90 public String getNamePrefix() { 91 return namePrefix; 92 } 93 94 97 public TokenContext[] getChildren() { 98 return children; 99 } 100 101 102 protected void addTokenID(TokenID tokenID) { 103 synchronized (tokenIDList) { 104 tokenIDList.add(tokenID); 105 tokenIDs = null; 106 107 TokenCategory tcat = tokenID.getCategory(); 109 if (tcat != null && tokenCategoryList.indexOf(tcat) < 0) { 110 tokenCategoryList.add(tcat); 111 tokenCategories = null; 112 } 113 } 114 } 115 116 119 protected void addDeclaredTokenIDs() throws IllegalAccessException , SecurityException { 120 Field [] fields = this.getClass().getDeclaredFields(); 121 for (int i = 0; i < fields.length; i++) { 122 int flags = Modifier.STATIC | Modifier.FINAL; 123 if ((fields[i].getModifiers() & flags) == flags 124 && TokenID.class.isAssignableFrom(fields[i].getType()) 125 ) { 126 addTokenID((TokenID)fields[i].get(null)); 127 } 128 } 129 } 130 131 134 public TokenID[] getTokenIDs() { 135 if (tokenIDs == null) { 136 synchronized (tokenIDList) { 137 tokenIDs = (TokenID[])tokenIDList.toArray(new TokenID[tokenIDList.size()]); 138 } 139 } 140 141 return tokenIDs; 142 } 143 144 147 public TokenCategory[] getTokenCategories() { 148 if (tokenCategories == null) { 149 synchronized (tokenCategoryList) { 150 tokenCategories = (TokenCategory[])tokenCategoryList.toArray( 151 new TokenCategory[tokenCategoryList.size()]); 152 } 153 } 154 155 return tokenCategories; 156 } 157 158 159 public TokenContextPath getContextPath() { 160 return contextPath; 161 } 162 163 166 public TokenContextPath getContextPath(TokenContextPath childPath) { 167 if (childPath == null) { 168 return contextPath; 169 } 170 171 TokenContextPath[] lastPair = lastContextPathPair; 172 if (lastPair == null || lastPair[0] != childPath) { 173 synchronized (pathCache) { 174 lastPair = (TokenContextPath[])pathCache.get(childPath); 175 if (lastPair == null) { 176 TokenContext[] origContexts = childPath.getContexts(); 178 TokenContext[] contexts = new TokenContext[origContexts.length + 1]; 179 System.arraycopy(origContexts, 0, contexts, 0, origContexts.length); 180 contexts[origContexts.length] = this; 181 182 TokenContextPath path = TokenContextPath.get(contexts); 183 184 lastPair = new TokenContextPath[] { childPath, path }; 185 pathCache.put(childPath, lastPair); 186 } 187 lastContextPathPair = lastPair; 188 } 189 } 190 191 return lastPair[1]; 192 } 193 194 197 public TokenContextPath[] getAllContextPaths() { 198 if (allContextPaths == null) { 199 ArrayList cpList = new ArrayList (); 200 cpList.add(getContextPath()); 201 202 for (int i = 0; i < children.length; i++) { 203 TokenContextPath[] childPaths = children[i].getAllContextPaths(); 204 for (int j = 0; j < childPaths.length; j++) { 205 cpList.add(getContextPath(childPaths[j])); 206 } 207 } 208 209 allContextPaths = new TokenContextPath[cpList.size()]; 210 cpList.toArray(allContextPaths); 211 } 212 213 return allContextPaths; 214 } 215 216 } 217 | Popular Tags |