1 21 22 package org.armedbear.j; 23 24 import java.io.BufferedReader ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.InputStreamReader ; 28 import java.util.ArrayList ; 29 import java.util.HashSet ; 30 31 public final class Keywords 32 { 33 private final Mode mode; 34 private final boolean ignoreCase; 35 36 private HashSet hashSet; 37 38 public Keywords(Mode mode) 39 { 40 this(mode, false); 41 } 42 43 public Keywords(Mode mode, boolean ignoreCase) 44 { 45 this.mode = mode; 46 this.ignoreCase = ignoreCase; 47 load(); 48 } 49 50 public void reload() 52 { 53 load(); 56 } 57 58 private void load() 59 { 60 ArrayList list = new ArrayList (256); 61 InputStream inputStream = null; 62 String className = mode.getClass().getName(); 63 int index = className.lastIndexOf('.'); 64 if (index >= 0) 65 className = className.substring(index+1); 66 FastStringBuffer sb = new FastStringBuffer(className); 67 sb.append('.'); 68 sb.append("keywords"); 69 final String key = sb.toString(); 70 final String fileName = Editor.preferences().getStringProperty(key); 71 if (fileName != null) { 72 File file = File.getInstance(fileName); 73 if (file != null) { 74 if (file.isFile()) { 75 try { 76 inputStream = file.getInputStream(); 77 Log.debug("loading " + className + " keywords from " + 78 file); 79 } 80 catch (IOException e) { 81 Log.error(e); 82 } 83 } else 84 Log.error("file not found " + file); 85 } else 86 Log.error("file is null, fileName = |" + fileName + "|"); 87 } 88 if (inputStream == null) 89 inputStream = mode.getClass().getResourceAsStream(key); 90 if (inputStream != null) { 91 try { 92 BufferedReader reader = 93 new BufferedReader (new InputStreamReader (inputStream)); 94 String s; 95 while ((s = reader.readLine()) != null) { 96 s = s.trim(); 97 if (s.length() > 0) { 98 if (ignoreCase) 99 list.add(s.toLowerCase()); 100 else 101 list.add(s); 102 } 103 } 104 } 105 catch (IOException e) { 106 Log.error(e); 107 } 108 } else 109 Log.error("no resource " + key); 110 hashSet = new HashSet (list); 111 } 112 113 public boolean isKeyword(String s) 114 { 115 if (ignoreCase) 116 return hashSet.contains(s.toLowerCase()); 117 else 118 return hashSet.contains(s); 119 } 120 } 121 | Popular Tags |