KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > Keywords


1 /*
2  * Keywords.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: Keywords.java,v 1.1.1.1 2002/09/24 16:08:25 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.io.BufferedReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashSet JavaDoc;
30
31 public final class Keywords
32 {
33     private final Mode mode;
34     private final boolean ignoreCase;
35
36     private HashSet JavaDoc 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     // Called by AbstractMode.reset().
51
public void reload()
52     {
53         // We could be smarter about this and only call load() if something
54
// has actually changed... ;)
55
load();
56     }
57
58     private void load()
59     {
60         ArrayList JavaDoc list = new ArrayList JavaDoc(256);
61         InputStream JavaDoc inputStream = null;
62         String JavaDoc 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 JavaDoc key = sb.toString();
70         final String JavaDoc 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 JavaDoc 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 JavaDoc reader =
93                     new BufferedReader JavaDoc(new InputStreamReader JavaDoc(inputStream));
94                 String JavaDoc 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 JavaDoc e) {
106                 Log.error(e);
107             }
108         } else
109             Log.error("no resource " + key);
110         hashSet = new HashSet JavaDoc(list);
111     }
112
113     public boolean isKeyword(String JavaDoc s)
114     {
115         if (ignoreCase)
116             return hashSet.contains(s.toLowerCase());
117         else
118             return hashSet.contains(s);
119     }
120 }
121
Popular Tags