1 11 package org.eclipse.jface.text.rules; 12 13 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 import org.eclipse.core.runtime.Assert; 19 20 21 22 31 public class WordRule implements IRule { 32 33 34 protected static final int UNDEFINED= -1; 35 36 37 protected IWordDetector fDetector; 38 39 protected IToken fDefaultToken; 40 41 protected int fColumn= UNDEFINED; 42 43 protected Map fWords= new HashMap (); 44 45 private StringBuffer fBuffer= new StringBuffer (); 46 50 private boolean fIgnoreCase= false; 51 52 61 public WordRule(IWordDetector detector) { 62 this(detector, Token.UNDEFINED, false); 63 } 64 65 75 public WordRule(IWordDetector detector, IToken defaultToken) { 76 this(detector, defaultToken, false); 77 } 78 79 91 public WordRule(IWordDetector detector, IToken defaultToken, boolean ignoreCase) { 92 Assert.isNotNull(detector); 93 Assert.isNotNull(defaultToken); 94 95 fDetector= detector; 96 fDefaultToken= defaultToken; 97 fIgnoreCase= ignoreCase; 98 } 99 100 106 public void addWord(String word, IToken token) { 107 Assert.isNotNull(word); 108 Assert.isNotNull(token); 109 110 fWords.put(word, token); 111 } 112 113 121 public void setColumnConstraint(int column) { 122 if (column < 0) 123 column= UNDEFINED; 124 fColumn= column; 125 } 126 127 130 public IToken evaluate(ICharacterScanner scanner) { 131 int c= scanner.read(); 132 if (c != ICharacterScanner.EOF && fDetector.isWordStart((char) c)) { 133 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) { 134 135 fBuffer.setLength(0); 136 do { 137 fBuffer.append((char) c); 138 c= scanner.read(); 139 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c)); 140 scanner.unread(); 141 142 String buffer= fBuffer.toString(); 143 IToken token= (IToken)fWords.get(buffer); 144 145 if(fIgnoreCase) { 146 Iterator iter= fWords.keySet().iterator(); 147 while (iter.hasNext()) { 148 String key= (String )iter.next(); 149 if(buffer.equalsIgnoreCase(key)) { 150 token= (IToken)fWords.get(key); 151 break; 152 } 153 } 154 } else 155 token= (IToken)fWords.get(buffer); 156 157 if (token != null) 158 return token; 159 160 if (fDefaultToken.isUndefined()) 161 unreadBuffer(scanner); 162 163 return fDefaultToken; 164 } 165 } 166 167 scanner.unread(); 168 return Token.UNDEFINED; 169 } 170 171 176 protected void unreadBuffer(ICharacterScanner scanner) { 177 for (int i= fBuffer.length() - 1; i >= 0; i--) 178 scanner.unread(); 179 } 180 181 } 182 | Popular Tags |