Gridarta Editor
KeywordValidator.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 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.var.crossfire.model.validation.checks;
21 
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Locale;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
33 import org.apache.log4j.Category;
34 import org.apache.log4j.Logger;
35 import org.jetbrains.annotations.NotNull;
36 
43 public class KeywordValidator<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
44 
48  @NotNull
49  private static final Category LOG = Logger.getLogger(KeywordValidator.class);
50 
54  @NotNull
55  private static final Pattern PATTERN_UPPER_CASE_LETTER = Pattern.compile("[A-Z]");
56 
60  @NotNull
61  private final Collection<String> keywords = new HashSet<>();
62 
67  @NotNull
68  private final Map<String, String> replyKeywords = new HashMap<>();
69 
73  private boolean matchAllKeyword;
74 
78  public KeywordValidator() {
79  if (LOG.isDebugEnabled()) {
80  LOG.debug("init");
81  }
82  }
83 
88  public void reset() {
89  if (LOG.isDebugEnabled()) {
90  LOG.debug("reset");
91  }
92 
93  keywords.clear();
94  replyKeywords.clear();
95  matchAllKeyword = false;
96  }
97 
103  public void match(@NotNull final ErrorGenerator<G, A, R> generator, @NotNull final Iterable<String> matchKeywords) {
104  if (LOG.isDebugEnabled()) {
105  LOG.debug("event: match");
106  }
107 
108  for (final String matchKeyword : matchKeywords) {
109  if (matchKeyword.isEmpty()) {
110  generator.errorKeywordError("empty keyword in '@match'");
111  } else if (matchKeyword.equals("*")) {
112  if (matchAllKeyword) {
113  generator.errorKeywordError("duplicate '@match *'. The second one will never match");
114  }
115  matchAllKeyword = true;
116  } else {
117  if (matchAllKeyword && !matchKeyword.equals("note")) { // allow "@match note" after "@match *": some maps use this for notes to developers
118  generator.errorKeywordError("keyword '" + matchKeyword + "' in '@match' after '@match *'. The keyword will never match");
119  }
120  if (!keywords.add(matchKeyword.toLowerCase(Locale.ROOT))) {
121  generator.errorKeywordError("duplicate keyword '" + matchKeyword + "' in '@match'. The second one will never match");
122  }
123  }
124  }
125  }
126 
133  public void reply(@NotNull final ErrorGenerator<G, A, R> generator, @NotNull final String command, @NotNull final String keyword) {
134  if (!command.equals("@reply") && !command.equals("@question")) {
135  throw new IllegalArgumentException("invalid command: " + command);
136  }
137  if (LOG.isDebugEnabled()) {
138  LOG.debug("event: reply " + command + " " + keyword);
139  }
140 
141  final Matcher matcher = PATTERN_UPPER_CASE_LETTER.matcher(keyword);
142  if (matcher.find()) {
143  generator.errorKeywordError("the word '" + keyword + "' uses upper-case letters. Matches are case-insensitive, therefore replies and questions should be specified in lower-case letters only");
144  }
145 
146  replyKeywords.put(keyword.toLowerCase(Locale.ROOT), command);
147  }
148 
153  public void eof(@NotNull final ErrorGenerator<G, A, R> generator) {
154  if (LOG.isDebugEnabled()) {
155  LOG.debug("event: eof");
156  }
157 
158  for (final Entry<String, String> e : replyKeywords.entrySet()) {
159  final String keyword = e.getKey();
160  if (!keywords.contains(keyword)) {
161  final String command = e.getValue();
162  generator.errorKeywordError("keyword '" + keyword + "' in '" + command + "' does not appear in any @match line");
163  }
164  }
165 
166  keywords.clear();
167  replyKeywords.clear();
168  matchAllKeyword = false;
169  }
170 
171 }
net.sf.gridarta.var.crossfire.model.validation.checks.KeywordValidator.KeywordValidator
KeywordValidator()
Creates a new instance.
Definition: KeywordValidator.java:78
net.sf.gridarta.var.crossfire.model.validation.checks.KeywordValidator.match
void match(@NotNull final ErrorGenerator< G, A, R > generator, @NotNull final Iterable< String > matchKeywords)
Processes a @match line.
Definition: KeywordValidator.java:103
net.sf.gridarta.var.crossfire.model.validation.checks.KeywordValidator.keywords
final Collection< String > keywords
All keywords that occurred in @match lines so far.
Definition: KeywordValidator.java:61
net.sf.gridarta.var.crossfire.model.validation.checks.ErrorGenerator
Generator for SuspiciousMsgChecker related error messages.
Definition: ErrorGenerator.java:38
net.sf.gridarta
Base package of all Gridarta classes.
net.sf
net.sf.gridarta.var.crossfire.model.validation.checks.KeywordValidator.reply
void reply(@NotNull final ErrorGenerator< G, A, R > generator, @NotNull final String command, @NotNull final String keyword)
Processes a @reply or @question line.
Definition: KeywordValidator.java:133
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.var.crossfire.model.validation.checks.KeywordValidator.LOG
static final Category LOG
The Logger for printing log messages.
Definition: KeywordValidator.java:49
net.sf.gridarta.var.crossfire.model.validation.checks.KeywordValidator
Checks that the matched keywords are correct.
Definition: KeywordValidator.java:43
net.sf.gridarta.var.crossfire.model.validation.checks.KeywordValidator.PATTERN_UPPER_CASE_LETTER
static final Pattern PATTERN_UPPER_CASE_LETTER
Matches any upper-case ASCI letter.
Definition: KeywordValidator.java:55
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.var.crossfire.model.archetype.Archetype
Implements Crossfire archetypes.
Definition: Archetype.java:30
net.sf.gridarta.var.crossfire.model.maparchobject.MapArchObject
MapArchObject contains the specific meta data about a map that is stored in the map-arch,...
Definition: MapArchObject.java:39
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.var.crossfire.model.validation.checks.KeywordValidator.matchAllKeyword
boolean matchAllKeyword
Whether the '*' keyword occurred so far.
Definition: KeywordValidator.java:73
net.sf.gridarta.var.crossfire.model.validation.checks.KeywordValidator.reset
void reset()
Resets the checker state.
Definition: KeywordValidator.java:88
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.var.crossfire.model.gameobject.GameObject
Handles the Crossfire GameObjects.
Definition: GameObject.java:41
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.var.crossfire.model.validation.checks.KeywordValidator.eof
void eof(@NotNull final ErrorGenerator< G, A, R > generator)
Will be called at the end of processing.
Definition: KeywordValidator.java:153
net.sf.gridarta.var.crossfire.model.validation.checks.KeywordValidator.replyKeywords
final Map< String, String > replyKeywords
All keywords that occurred in @reply or @question lines so far.
Definition: KeywordValidator.java:68