Gridarta Editor
SmoothFacesLoader.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.smoothface;
21 
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.Reader;
27 import java.net.URL;
28 import java.util.regex.Pattern;
32 import net.sf.gridarta.utils.IOUtils;
33 import org.apache.log4j.Category;
34 import org.apache.log4j.Logger;
35 import org.jetbrains.annotations.NotNull;
36 
42 public class SmoothFacesLoader {
43 
47  private static final Category LOG = Logger.getLogger(SmoothFacesLoader.class);
48 
52  private SmoothFacesLoader() {
53  }
54 
61  public static void load(@NotNull final URL url, @NotNull final SmoothFaces smoothFaces, @NotNull final ErrorView errorView) {
62  final ErrorViewCollector errorViewCollector = new ErrorViewCollector(errorView, url);
63  try {
64  try (InputStream inputStream = url.openStream()) {
65  try (Reader reader = new InputStreamReader(inputStream, IOUtils.MAP_ENCODING)) {
66  try (Reader bufferedReader = new BufferedReader(reader)) {
67  load(url.toString(), bufferedReader, smoothFaces, errorViewCollector);
68  }
69  }
70  }
71  } catch (final IOException ex) {
72  errorViewCollector.addWarning(ErrorViewCategory.SMOOTH_FILE_INVALID, ex.getMessage());
73  }
74  }
75 
84  private static void load(@NotNull final String readerName, @NotNull final Reader reader, @NotNull final SmoothFaces smoothFaces, @NotNull final ErrorViewCollector errorViewCollector) throws IOException {
85  int smoothEntries = 0;
86  try (BufferedReader bufferedReader = new BufferedReader(reader)) {
87  final Pattern pattern = Pattern.compile(" ");
88  for (String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) {
89  if (line.trim().isEmpty() || line.startsWith("#")) {
90  continue;
91  }
92  final String[] elements = pattern.split(line);
93  if (elements.length != 2) {
94  errorViewCollector.addWarning(ErrorViewCategory.SMOOTH_FILE_INVALID, "syntax error in line " + line);
95  return;
96  }
97  final SmoothFace smoothFace = new SmoothFace(elements[0], elements[1]);
98  try {
99  smoothFaces.add(smoothFace);
100  smoothEntries++;
101  } catch (final DuplicateSmoothFaceException ex) {
102  errorViewCollector.addWarning(ErrorViewCategory.SMOOTH_FILE_INVALID, "inconsistent smooth face '" + ex.getMessage() + "' maps to '" + ex.getNewValue() + "' and '" + ex.getOldValue() + "'");
103  }
104  }
105  }
106  if (LOG.isInfoEnabled()) {
107  LOG.info("Loaded " + smoothEntries + " smooth rules from '" + readerName + "'.");
108  }
109  }
110 
111 }
net.sf.gridarta.model.errorview.ErrorViewCollector.addWarning
void addWarning(@NotNull final ErrorViewCategory category)
Adds a warning message.
Definition: ErrorViewCollector.java:68
net.sf.gridarta.model.smoothface.SmoothFace
Smoothing information for one face name.
Definition: SmoothFace.java:28
net.sf.gridarta.model.smoothface.SmoothFacesLoader.SmoothFacesLoader
SmoothFacesLoader()
Private constructor to prevent instantiation.
Definition: SmoothFacesLoader.java:52
net.sf.gridarta
Base package of all Gridarta classes.
net.sf
net.sf.gridarta.model.errorview.ErrorView
Interface for classes displaying error messages.
Definition: ErrorView.java:28
net.sf.gridarta.model.errorview.ErrorViewCategory.SMOOTH_FILE_INVALID
SMOOTH_FILE_INVALID
Definition: ErrorViewCategory.java:82
net
net.sf.gridarta.model.errorview
Definition: ErrorView.java:20
net.sf.gridarta.model.errorview.ErrorViewCollector
Convenience class for adding messages to a ErrorView instance using a fixed category name.
Definition: ErrorViewCollector.java:31
net.sf.gridarta.model.errorview.ErrorViewCategory
Defines possible error categories for ErrorView instances.
Definition: ErrorViewCategory.java:28
net.sf.gridarta.model.smoothface.DuplicateSmoothFaceException
Thrown to indicate that a SmoothFace instance is not unique.
Definition: DuplicateSmoothFaceException.java:28
net.sf.gridarta.model
net.sf.gridarta.model.smoothface.SmoothFacesLoader
Loader for smooth files.
Definition: SmoothFacesLoader.java:42
net.sf.gridarta.model.smoothface.SmoothFaces
Collection of all smoothing information.
Definition: SmoothFaces.java:37
net.sf.gridarta.model.smoothface.DuplicateSmoothFaceException.getNewValue
String getNewValue()
Returns the new value.
Definition: DuplicateSmoothFaceException.java:75
net.sf.gridarta.utils.IOUtils
Utility-class for Gridarta's I/O.
Definition: IOUtils.java:40
net.sf.gridarta.model.smoothface.SmoothFacesLoader.LOG
static final Category LOG
The Logger for printing log messages.
Definition: SmoothFacesLoader.java:47
net.sf.gridarta.model.smoothface.SmoothFacesLoader.load
static void load(@NotNull final String readerName, @NotNull final Reader reader, @NotNull final SmoothFaces smoothFaces, @NotNull final ErrorViewCollector errorViewCollector)
Loads a smooth file.
Definition: SmoothFacesLoader.java:84
net.sf.gridarta.model.smoothface.DuplicateSmoothFaceException.getOldValue
String getOldValue()
Returns the existing value.
Definition: DuplicateSmoothFaceException.java:66
net.sf.gridarta.model.smoothface.SmoothFacesLoader.load
static void load(@NotNull final URL url, @NotNull final SmoothFaces smoothFaces, @NotNull final ErrorView errorView)
Loads a smooth file.
Definition: SmoothFacesLoader.java:61
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.utils.IOUtils.MAP_ENCODING
static final String MAP_ENCODING
Encoding to use for maps and other data.
Definition: IOUtils.java:52