001: /*
002: * Keywords.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: Keywords.java,v 1.1.1.1 2002/09/24 16:08:25 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.io.BufferedReader;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.InputStreamReader;
028: import java.util.ArrayList;
029: import java.util.HashSet;
030:
031: public final class Keywords {
032: private final Mode mode;
033: private final boolean ignoreCase;
034:
035: private HashSet hashSet;
036:
037: public Keywords(Mode mode) {
038: this (mode, false);
039: }
040:
041: public Keywords(Mode mode, boolean ignoreCase) {
042: this .mode = mode;
043: this .ignoreCase = ignoreCase;
044: load();
045: }
046:
047: // Called by AbstractMode.reset().
048: public void reload() {
049: // We could be smarter about this and only call load() if something
050: // has actually changed... ;)
051: load();
052: }
053:
054: private void load() {
055: ArrayList list = new ArrayList(256);
056: InputStream inputStream = null;
057: String className = mode.getClass().getName();
058: int index = className.lastIndexOf('.');
059: if (index >= 0)
060: className = className.substring(index + 1);
061: FastStringBuffer sb = new FastStringBuffer(className);
062: sb.append('.');
063: sb.append("keywords");
064: final String key = sb.toString();
065: final String fileName = Editor.preferences().getStringProperty(
066: key);
067: if (fileName != null) {
068: File file = File.getInstance(fileName);
069: if (file != null) {
070: if (file.isFile()) {
071: try {
072: inputStream = file.getInputStream();
073: Log.debug("loading " + className
074: + " keywords from " + file);
075: } catch (IOException e) {
076: Log.error(e);
077: }
078: } else
079: Log.error("file not found " + file);
080: } else
081: Log
082: .error("file is null, fileName = |" + fileName
083: + "|");
084: }
085: if (inputStream == null)
086: inputStream = mode.getClass().getResourceAsStream(key);
087: if (inputStream != null) {
088: try {
089: BufferedReader reader = new BufferedReader(
090: new InputStreamReader(inputStream));
091: String s;
092: while ((s = reader.readLine()) != null) {
093: s = s.trim();
094: if (s.length() > 0) {
095: if (ignoreCase)
096: list.add(s.toLowerCase());
097: else
098: list.add(s);
099: }
100: }
101: } catch (IOException e) {
102: Log.error(e);
103: }
104: } else
105: Log.error("no resource " + key);
106: hashSet = new HashSet(list);
107: }
108:
109: public boolean isKeyword(String s) {
110: if (ignoreCase)
111: return hashSet.contains(s.toLowerCase());
112: else
113: return hashSet.contains(s);
114: }
115: }
|