Gridarta Editor
MagicEarValidator.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 
25 import org.apache.log4j.Category;
26 import org.apache.log4j.Logger;
27 import org.jetbrains.annotations.NotNull;
28 
35 @SuppressWarnings("DuplicateBranchesInSwitch")
36 public class MagicEarValidator<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
37 
41  @NotNull
42  private static final Category LOG = Logger.getLogger(MagicEarValidator.class);
43 
47  @NotNull
48  private State state = State.N;
49 
53  public MagicEarValidator() {
54  if (LOG.isDebugEnabled()) {
55  LOG.debug("init: state=" + state);
56  }
57  }
58 
63  public void reset() {
64  state = State.N;
65  if (LOG.isDebugEnabled()) {
66  LOG.debug("reset: state=" + state);
67  }
68  }
69 
74  public void text(@NotNull final ErrorGenerator<G, A, R> generator) {
75  if (LOG.isDebugEnabled()) {
76  LOG.debug("event: text");
77  }
78 
79  switch (state) {
80  case N:
81  generator.errorSyntaxError("text before @match");
82  setState(State.N2);
83  return;
84 
85  case N2:
86  return;
87 
88  case M:
89  setState(State.T);
90  return;
91 
92  case T:
93  return;
94 
95  case TE:
96  case TEE:
97  setState(State.T);
98  return;
99  }
100 
101  throw new AssertionError("state=" + state);
102  }
103 
108  public void empty(@NotNull final ErrorGenerator<G, A, R> generator) {
109  if (LOG.isDebugEnabled()) {
110  LOG.debug("event: empty");
111  }
112 
113  switch (state) {
114  case N:
115  generator.errorSyntaxError("text before @match");
116  setState(State.N2);
117  return;
118 
119  case N2:
120  return;
121 
122  case M:
123  generator.errorSyntaxError("the text starts with an empty line");
124  setState(State.T);
125  return;
126 
127  case T:
128  setState(State.TE);
129  return;
130 
131  case TE:
132  generator.errorSyntaxError("the text contains more than one consecutive empty line");
133  setState(State.TEE);
134  return;
135 
136  case TEE:
137  return;
138  }
139 
140  throw new AssertionError("state=" + state);
141  }
142 
147  public void match(@NotNull final ErrorGenerator<G, A, R> generator) {
148  if (LOG.isDebugEnabled()) {
149  LOG.debug("event: match");
150  }
151 
152  switch (state) {
153  case N:
154  case N2:
155  setState(State.M);
156  return;
157 
158  case M:
159  return;
160 
161  case T:
162  setState(State.M);
163  return;
164 
165  case TE:
166  generator.errorSyntaxError("the text ends with an empty line");
167  setState(State.M);
168  return;
169 
170  case TEE:
171  setState(State.M);
172  return;
173  }
174 
175  throw new AssertionError("state=" + state);
176  }
177 
182  public void reply(@NotNull final ErrorGenerator<G, A, R> generator) {
183  if (LOG.isDebugEnabled()) {
184  LOG.debug("event: reply");
185  }
186 
187  generator.errorSyntaxError("@reply or @question should not be used for a magic ear");
188  }
189 
194  public void eof(@NotNull final ErrorGenerator<G, A, R> generator) {
195  if (LOG.isDebugEnabled()) {
196  LOG.debug("event: eof");
197  }
198 
199  switch (state) {
200  case N:
201  case N2:
202  case M:
203  case T:
204  return;
205 
206  case TE:
207  generator.errorSyntaxError("the text ends with an empty line");
208  return;
209 
210  case TEE:
211  return;
212  }
213 
214  throw new AssertionError("state=" + state);
215  }
216 
221  private void setState(@NotNull final State state) {
222  if (this.state == state) {
223  return;
224  }
225 
226  if (LOG.isDebugEnabled()) {
227  LOG.debug("state=" + state);
228  }
229  this.state = state;
230  }
231 
235  private enum State {
236 
240  N,
241 
245  N2,
246 
250  M,
251 
255  T,
256 
260  TE,
261 
266  TEE
267 
268  }
269 
270 }
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.gridarta.var.crossfire.model.validation.checks.MagicEarValidator.match
void match(@NotNull final ErrorGenerator< G, A, R > generator)
Processes a @match line.
Definition: MagicEarValidator.java:147
net.sf.gridarta.var.crossfire.model.validation.checks.MagicEarValidator
Checks for suspicious text lines in magic ears.
Definition: MagicEarValidator.java:36
net.sf
net.sf.gridarta.var.crossfire.model.validation.checks.MagicEarValidator.eof
void eof(@NotNull final ErrorGenerator< G, A, R > generator)
Will be called at the end of processing.
Definition: MagicEarValidator.java:194
net.sf.gridarta.var.crossfire.model.validation.checks.MagicEarValidator.State.N
N
Nothing processed yet.
Definition: MagicEarValidator.java:240
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.MagicEarValidator.State.T
T
The last processed line was a non-empty text line.
Definition: MagicEarValidator.java:255
net.sf.gridarta.var.crossfire.model.validation.checks.MagicEarValidator.setState
void setState(@NotNull final State state)
Updates state and prints a debug message if it changes.
Definition: MagicEarValidator.java:221
net.sf.gridarta.var.crossfire.model.validation.checks.MagicEarValidator.State
The FSM states.
Definition: MagicEarValidator.java:235
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.var.crossfire.model.validation.checks.MagicEarValidator.reply
void reply(@NotNull final ErrorGenerator< G, A, R > generator)
Processes a qreply or @question line.
Definition: MagicEarValidator.java:182
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.var.crossfire.model.validation.checks.MagicEarValidator.text
void text(@NotNull final ErrorGenerator< G, A, R > generator)
Processes a non-empty line of text.
Definition: MagicEarValidator.java:74
net.sf.gridarta.var.crossfire.model.validation.checks.MagicEarValidator.empty
void empty(@NotNull final ErrorGenerator< G, A, R > generator)
Processes an empty line of text.
Definition: MagicEarValidator.java:108
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.var.crossfire.model.validation.checks.MagicEarValidator.State.N2
N2
Nothing processed yet but an error message has been printed.
Definition: MagicEarValidator.java:245
net.sf.gridarta.var.crossfire.model.gameobject.GameObject
Handles the Crossfire GameObjects.
Definition: GameObject.java:41
net.sf.gridarta.var.crossfire.model.validation.checks.MagicEarValidator.State.M
M
A @match line has been processed.
Definition: MagicEarValidator.java:250
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.var.crossfire.model.validation.checks.MagicEarValidator.State.TE
TE
The last processed line was an empty text line.
Definition: MagicEarValidator.java:260
net.sf.gridarta.var.crossfire.model.validation.checks.MagicEarValidator.MagicEarValidator
MagicEarValidator()
Creates a new instance.
Definition: MagicEarValidator.java:53
net.sf.gridarta.var.crossfire.model.validation.checks.MagicEarValidator.reset
void reset()
Resets the checker state.
Definition: MagicEarValidator.java:63