Gridarta Editor
KeywordMap.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.textedit.textarea.tokenmarker;
21 
22 import javax.swing.text.Segment;
24 
35 public class KeywordMap {
36 
40  private final Node rootNode = new Node();
41 
45  private final boolean ignoreCase;
46 
51  public KeywordMap(final boolean ignoreCase) {
52  this.ignoreCase = ignoreCase;
53  }
54 
63  public byte lookup(final Segment text, final int offset, final int length) {
64  Node node = rootNode;
65  for (int i = offset, end = offset + length; i < end; i++) {
66  node = node.lookup(ignoreCase ? Character.toUpperCase(text.array[i]) : text.array[i]);
67  if (node == null) {
68  return Token.NULL;
69  }
70  }
71  return node.getId();
72  }
73 
79  public void add(final CharSequence keyword, final byte id) {
80  Node node = rootNode;
81  for (int i = 0, len = keyword.length(); i < len; i++) {
82  node = node.define(ignoreCase ? Character.toUpperCase(keyword.charAt(i)) : keyword.charAt(i));
83  }
84  node.setId(id);
85  }
86 
87 }
byte getId()
Returns the id for this node.
Definition: Node.java:91
void setId(final byte id)
Sets the id to return for this node.
Definition: Node.java:83
final boolean ignoreCase
Whether case should be ignored when matching keys.
Definition: KeywordMap.java:45
Node define(final char ch)
Looks up or defines the next node for a given character.
Definition: Node.java:61
byte lookup(final Segment text, final int offset, final int length)
Looks up a key.
Definition: KeywordMap.java:63
void add(final CharSequence keyword, final byte id)
Adds a key-value mapping.
Definition: KeywordMap.java:79
Base package of all Gridarta classes.
Node lookup(final char ch)
Looks up the next node for a given character.
Definition: Node.java:75
A linked list of tokens.
Definition: Token.java:21
KeywordMap(final boolean ignoreCase)
Creates a new instance.
Definition: KeywordMap.java:51
This package contains the other part of the script editor.
static final byte NULL
Normal text token id.
Definition: Token.java:26
A node in the tree that is used to store all key-value pairs.
Definition: Node.java:29