Gridarta Editor
TextValidator.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 TextValidator<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(TextValidator.class);
43 
47  @NotNull
48  private State state = State.N;
49 
53  public TextValidator() {
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  generator.errorSyntaxError("@match without following text");
160  return;
161 
162  case T:
163  setState(State.M);
164  return;
165 
166  case TE:
167  generator.errorSyntaxError("the text ends with an empty line");
168  setState(State.M);
169  return;
170 
171  case TEE:
172  setState(State.M);
173  return;
174  }
175 
176  throw new AssertionError("state=" + state);
177  }
178 
183  public void reply(@NotNull final ErrorGenerator<G, A, R> generator) {
184  if (LOG.isDebugEnabled()) {
185  LOG.debug("event: reply");
186  }
187 
188  switch (state) {
189  case N:
190  case N2:
191  generator.errorSyntaxError("@reply or @question without @match");
192  return;
193 
194  case M:
195  case T:
196  case TE:
197  case TEE:
198  return;
199  }
200 
201  throw new AssertionError("state=" + state);
202  }
203 
208  public void eof(@NotNull final ErrorGenerator<G, A, R> generator) {
209  if (LOG.isDebugEnabled()) {
210  LOG.debug("event: eof");
211  }
212 
213  switch (state) {
214  case N:
215  case N2:
216  return;
217 
218  case M:
219  generator.errorSyntaxError("@match without following text");
220  return;
221 
222  case T:
223  return;
224 
225  case TE:
226  generator.errorSyntaxError("the text ends with an empty line");
227  return;
228 
229  case TEE:
230  return;
231  }
232 
233  throw new AssertionError("state=" + state);
234  }
235 
240  private void setState(@NotNull final State state) {
241  if (this.state == state) {
242  return;
243  }
244 
245  if (LOG.isDebugEnabled()) {
246  LOG.debug("state=" + state);
247  }
248  this.state = state;
249  }
250 
254  private enum State {
255 
259  N,
260 
264  N2,
265 
269  M,
270 
274  T,
275 
279  TE,
280 
285  TEE
286 
287  }
288 
289 }
net.sf.gridarta.var.crossfire.model.validation.checks.TextValidator.TextValidator
TextValidator()
Creates a new instance.
Definition: TextValidator.java:53
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.TextValidator.State.N
N
Nothing processed yet.
Definition: TextValidator.java:259
net.sf
net.sf.gridarta.var.crossfire.model.validation.checks.TextValidator.eof
void eof(@NotNull final ErrorGenerator< G, A, R > generator)
Will be called at the end of processing.
Definition: TextValidator.java:208
net.sf.gridarta.var.crossfire.model.validation.checks.TextValidator
Checks for suspicious text lines.
Definition: TextValidator.java:36
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.TextValidator.text
void text(@NotNull final ErrorGenerator< G, A, R > generator)
Processes a non-empty line of text.
Definition: TextValidator.java:74
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.TextValidator.empty
void empty(@NotNull final ErrorGenerator< G, A, R > generator)
Processes an empty line of text.
Definition: TextValidator.java:108
net.sf.gridarta.var.crossfire.model.validation.checks.TextValidator.match
void match(@NotNull final ErrorGenerator< G, A, R > generator)
Processes a @match line.
Definition: TextValidator.java:147
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.var.crossfire.model.validation.checks.TextValidator.State
The FSM states.
Definition: TextValidator.java:254
net.sf.gridarta.var.crossfire.model.validation.checks.TextValidator.reply
void reply(@NotNull final ErrorGenerator< G, A, R > generator)
Processes a qreply or @question line.
Definition: TextValidator.java:183
net.sf.gridarta.var.crossfire.model.validation.checks.TextValidator.State.T
T
The last processed line was a non-empty text line.
Definition: TextValidator.java:274
net.sf.gridarta.var.crossfire.model.validation.checks.TextValidator.State.TE
TE
The last processed line was an empty text line.
Definition: TextValidator.java:279
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.var.crossfire.model.validation.checks.TextValidator.State.M
M
A @match line has been processed.
Definition: TextValidator.java:269
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.var.crossfire.model.validation.checks.TextValidator.State.N2
N2
Nothing processed yet but an error message has been printed.
Definition: TextValidator.java:264
net.sf.gridarta.var.crossfire.model.validation.checks.TextValidator.setState
void setState(@NotNull final State state)
Updates state and prints a debug message if it changes.
Definition: TextValidator.java:240
net.sf.gridarta.var.crossfire.model.validation.checks.TextValidator.reset
void reset()
Resets the checker state.
Definition: TextValidator.java:63