1 23 package org.gjt.sp.jedit.syntax; 24 25 import javax.swing.text.Segment ; 26 import java.util.Vector ; 27 28 36 public class KeywordMap 37 { 38 43 public KeywordMap(boolean ignoreCase) 44 { 45 this(ignoreCase, 52); 46 this.ignoreCase = ignoreCase; 47 noWordSep = new StringBuffer (); 48 } 50 57 public KeywordMap(boolean ignoreCase, int mapLength) 58 { 59 this.mapLength = mapLength; 60 this.ignoreCase = ignoreCase; 61 map = new Keyword[mapLength]; 62 } 64 71 public byte lookup(Segment text, int offset, int length) 72 { 73 if(length == 0) 74 return Token.NULL; 75 Keyword k = map[getSegmentMapKey(text, offset, length)]; 76 while(k != null) 77 { 78 if(length != k.keyword.length) 79 { 80 k = k.next; 81 continue; 82 } 83 if(SyntaxUtilities.regionMatches(ignoreCase,text,offset, 84 k.keyword)) 85 return k.id; 86 k = k.next; 87 } 88 return Token.NULL; 89 } 91 97 public void add(String keyword, byte id) 98 { 99 add(keyword.toCharArray(),id); 100 } 102 109 public void add(char[] keyword, byte id) 110 { 111 int key = getStringMapKey(keyword); 112 113 loop: for(int i = 0; i < keyword.length; i++) 116 { 117 char ch = keyword[i]; 118 if(!Character.isLetterOrDigit(ch)) 119 { 120 for(int j = 0; j < noWordSep.length(); j++) 121 { 122 if(noWordSep.charAt(j) == ch) 123 continue loop; 124 } 125 126 noWordSep.append(ch); 127 } 128 } 129 130 map[key] = new Keyword(keyword,id,map[key]); 131 } 133 139 public String getNonAlphaNumericChars() 140 { 141 return noWordSep.toString(); 142 } 144 149 public String [] getKeywords() 150 { 151 Vector vector = new Vector (100); 152 for(int i = 0; i < map.length; i++) 153 { 154 Keyword keyword = map[i]; 155 while(keyword != null) 156 { 157 vector.addElement(new String (keyword.keyword)); 158 keyword = keyword.next; 159 } 160 } 161 String [] retVal = new String [vector.size()]; 162 vector.copyInto(retVal); 163 return retVal; 164 } 166 171 public boolean getIgnoreCase() 172 { 173 return ignoreCase; 174 } 176 182 public void setIgnoreCase(boolean ignoreCase) 183 { 184 this.ignoreCase = ignoreCase; 185 } 187 192 public void add(KeywordMap map) 193 { 194 for(int i = 0; i < map.map.length; i++) 195 { 196 Keyword k = map.map[i]; 197 while(k != null) 198 { 199 add(k.keyword,k.id); 200 k = k.next; 201 } 202 } 203 } 205 207 private int mapLength; 209 private Keyword[] map; 210 private boolean ignoreCase; 211 private StringBuffer noWordSep; 212 214 private int getStringMapKey(char[] s) 216 { 217 return (Character.toUpperCase(s[0]) + 218 Character.toUpperCase(s[s.length-1])) 219 % mapLength; 220 } 222 protected int getSegmentMapKey(Segment s, int off, int len) 224 { 225 return (Character.toUpperCase(s.array[off]) + 226 Character.toUpperCase(s.array[off + len - 1])) 227 % mapLength; 228 } 230 232 class Keyword 234 { 235 public Keyword(char[] keyword, byte id, Keyword next) 236 { 237 this.keyword = keyword; 238 this.id = id; 239 this.next = next; 240 } 241 242 public char[] keyword; 243 public byte id; 244 public Keyword next; 245 } } 247 | Popular Tags |