KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > syntax > jedit > KeywordMap


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * KeywordMap.java
28  *
29  */

30
31 package org.syntax.jedit;
32
33 import org.syntax.jedit.tokenmarker.*;
34 import javax.swing.text.Segment JavaDoc;
35
36 /**
37  * A <code>KeywordMap</code> is similar to a hashtable in that it maps keys
38  * to values. However, the `keys' are Swing segments. This allows lookups of
39  * text substrings without the overhead of creating a new string object.
40  * <p>
41  * This class is used by <code>CTokenMarker</code> to map keywords to ids.
42  *
43  * @author Slava Pestov, Mike Dillon
44  * @version $Id: KeywordMap.java 932 2006-10-20 09:32:45Z gtoffoli $
45  */

46 public class KeywordMap
47 {
48     /**
49      * Creates a new <code>KeywordMap</code>.
50      * @param ignoreCase True if keys are case insensitive
51      */

52     public KeywordMap(boolean ignoreCase)
53     {
54         this(ignoreCase, 52);
55         this.ignoreCase = ignoreCase;
56     }
57
58     /**
59      * Creates a new <code>KeywordMap</code>.
60      * @param ignoreCase True if the keys are case insensitive
61      * @param mapLength The number of `buckets' to create.
62      * A value of 52 will give good performance for most maps.
63      */

64     public KeywordMap(boolean ignoreCase, int mapLength)
65     {
66         this.mapLength = mapLength;
67         this.ignoreCase = ignoreCase;
68         map = new Keyword[mapLength];
69     }
70
71     /**
72      * Looks up a key.
73      * @param text The text segment
74      * @param offset The offset of the substring within the text segment
75      * @param length The length of the substring
76      */

77     public byte lookup(Segment JavaDoc text, int offset, int length)
78     {
79         if(length == 0)
80             return Token.NULL;
81         Keyword k = map[getSegmentMapKey(text, offset, length)];
82         while(k != null)
83         {
84             if(length != k.keyword.length)
85             {
86                 k = k.next;
87                 continue;
88             }
89             if(SyntaxUtilities.regionMatches(ignoreCase,text,offset,
90                 k.keyword))
91                 return k.id;
92             k = k.next;
93         }
94         return Token.NULL;
95     }
96
97     /**
98      * Adds a key-value mapping.
99      * @param keyword The key
100      * @Param id The value
101      */

102     public void add(String JavaDoc keyword, byte id)
103     {
104         int key = getStringMapKey(keyword);
105         map[key] = new Keyword(keyword.toCharArray(),id,map[key]);
106     }
107
108     /**
109      * Returns true if the keyword map is set to be case insensitive,
110      * false otherwise.
111      */

112     public boolean getIgnoreCase()
113     {
114         return ignoreCase;
115     }
116
117     /**
118      * Sets if the keyword map should be case insensitive.
119      * @param ignoreCase True if the keyword map should be case
120      * insensitive, false otherwise
121      */

122     public void setIgnoreCase(boolean ignoreCase)
123     {
124         this.ignoreCase = ignoreCase;
125     }
126
127     // protected members
128
protected int mapLength;
129
130     protected int getStringMapKey(String JavaDoc s)
131     {
132         return (Character.toUpperCase(s.charAt(0)) +
133                 Character.toUpperCase(s.charAt(s.length()-1)))
134                 % mapLength;
135     }
136
137     protected int getSegmentMapKey(Segment JavaDoc s, int off, int len)
138     {
139         return (Character.toUpperCase(s.array[off]) +
140                 Character.toUpperCase(s.array[off + len - 1]))
141                 % mapLength;
142     }
143
144     // private members
145
class Keyword
146     {
147         public Keyword(char[] keyword, byte id, Keyword next)
148         {
149             this.keyword = keyword;
150             this.id = id;
151             this.next = next;
152         }
153
154         public char[] keyword;
155         public byte id;
156         public Keyword next;
157     }
158
159     private Keyword[] map;
160     private boolean ignoreCase;
161 }
162
Popular Tags