001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * IReportKeywordLookup.java
028: *
029: */
030:
031: package org.syntax.jedit;
032:
033: import java.util.*;
034:
035: import org.syntax.jedit.tokenmarker.*;
036: import javax.swing.text.Segment;
037:
038: public class IReportKeywordLookup implements KeywordLookupIF {
039: private ArrayList keys = new ArrayList();
040:
041: public IReportKeywordLookup() {
042: }
043:
044: public void addKeyword(String keyword) {
045: addKeyword(keyword, Token.PARAMETER_OK);
046: }
047:
048: public void addKeyword(String keyword, byte token) {
049: //System.out.println("add : " + keyword);
050: keys.add(new Key(keyword, token));
051: }
052:
053: public void removeKeyword(String keyword) {
054: Key key;
055:
056: for (int i = 0; i < keys.size(); i++) {
057: key = (Key) keys.get(i);
058: if (key.keyword.equals(keyword)) {
059: keys.remove(key);
060: }
061: }
062: }
063:
064: /**
065: * Looks up a key.
066: * @param text The text segment
067: * @param offset The offset of the substring within the text segment
068: * @param length The length of the substring
069: */
070: public byte lookup(Segment text, int offset, int length) {
071: Key key;
072: String keyword;
073: boolean found;
074:
075: for (int i = 0; i < keys.size(); i++) {
076: key = (Key) keys.get(i);
077: keyword = key.keyword;
078:
079: if (keyword.length() != length) {
080: continue;
081: }
082:
083: found = true;
084: for (int j = 0; j < keyword.length(); j++) {
085: if (keyword.charAt(j) != text.array[offset + j]) {
086: found = false;
087: break;
088: }
089: }
090:
091: if (found) {
092: return key.token;
093: }
094: }
095:
096: return Token.NULL;
097: }
098:
099: private class Key {
100: String keyword;
101: byte token;
102:
103: Key(String keyword, byte token) {
104: this.keyword = keyword;
105: this.token = token;
106: }
107: }
108:
109: }
|