Gridarta Editor
AbstractMapArchObjectParser.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.model.io;
21 
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 
39 public abstract class AbstractMapArchObjectParser<A extends MapArchObject<A>> implements MapArchObjectParser<A> {
40 
44  @NotNull
45  private static final Pattern KEY_VALUE_PATTERN = Pattern.compile("([^ ]+) +(.+)");
46 
47  @Override
48  public void load(@NotNull final BufferedReader reader, @NotNull final A mapArchObject) throws IOException {
49  final String firstLine = reader.readLine();
50  if (firstLine == null) {
51  throw new IOException("unexpected end of file in map header");
52  }
53 
54  final String trimmedFirstLine = StringUtils.removeTrailingWhitespace(firstLine);
55  if (!trimmedFirstLine.equals("arch map")) {
56  throw new InvalidMapFormatException("unexpected first line of map file: '" + trimmedFirstLine + "', expected 'arch map'");
57  }
58 
59  while (true) {
60  final String line = reader.readLine();
61  if (line == null) {
62  throw new IOException("unexpected end of file in map header");
63  }
64 
65  final Matcher matcher = KEY_VALUE_PATTERN.matcher(line);
66  final String key;
67  @Nullable final String value;
68  if (matcher.matches()) {
69  key = matcher.group(1);
70  value = matcher.group(2);
71  } else {
72  key = line;
73  value = "";
74  }
75 
76  final String trimmedLine = StringUtils.removeTrailingWhitespace(line);
77  if (!parseLine(trimmedLine, key, value, mapArchObject, reader)) {
78  break;
79  }
80  }
81  }
82 
95  protected abstract boolean parseLine(@NotNull String line, @NotNull String key, @NotNull String value, @NotNull A mapArchObject, @NotNull BufferedReader reader) throws IOException;
96 
97 }
net.sf.gridarta
Base package of all Gridarta classes.
net.sf
net.sf.gridarta.utils.StringUtils.removeTrailingWhitespace
static String removeTrailingWhitespace(@NotNull final CharSequence str)
Removes trailing whitespace from a string.
Definition: StringUtils.java:123
net
net.sf.gridarta.model.io.MapArchObjectParser
Interface for classes that read or write MapArchObject instances.
Definition: MapArchObjectParser.java:33
net.sf.gridarta.model.io.AbstractMapArchObjectParser.KEY_VALUE_PATTERN
static final Pattern KEY_VALUE_PATTERN
Splits a line into key and value.
Definition: AbstractMapArchObjectParser.java:45
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.io.AbstractMapArchObjectParser
Abstract base class for classes implementing MapArchObjectParsers.
Definition: AbstractMapArchObjectParser.java:39
net.sf.gridarta.model.io.AbstractMapArchObjectParser.load
void load(@NotNull final BufferedReader reader, @NotNull final A mapArchObject)
Definition: AbstractMapArchObjectParser.java:48
net.sf.gridarta.model.io.InvalidMapFormatException
Exception that's thrown when a MapReader read a file and detected that it's in the wrong format.
Definition: InvalidMapFormatException.java:31
net.sf.gridarta.utils.StringUtils
Utility class for string manipulation.
Definition: StringUtils.java:31
net.sf.gridarta.model.io.AbstractMapArchObjectParser.parseLine
abstract boolean parseLine(@NotNull String line, @NotNull String key, @NotNull String value, @NotNull A mapArchObject, @NotNull BufferedReader reader)
Parse a line for this editor type.
net.sf.gridarta.model
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20