Gridarta Editor
MapPathUtils.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.mapmodel;
21 
22 import org.jetbrains.annotations.NotNull;
23 
28 public class MapPathUtils {
29 
33  private MapPathUtils() {
34  }
35 
42  @NotNull
43  public static MapPath newMapPath(@NotNull final String string) {
44  if (string.startsWith("/")) {
45  return append(new AbsoluteMapPath(), string);
46  }
47  return append(new RelativeMapPath(), string);
48  }
49 
56  @NotNull
57  public static AbsoluteMapPath newAbsoluteMapPath(@NotNull final String string) {
58  if (!string.startsWith("/")) {
59  throw new IllegalArgumentException("map path '" + string + "' is not absolute");
60  }
61  return append(new AbsoluteMapPath(), string);
62  }
63 
70  @NotNull
71  public static RelativeMapPath newRelativeMapPath(@NotNull final String string) {
72  if (string.startsWith("/")) {
73  throw new IllegalArgumentException("map path '" + string + "' is not relative");
74  }
75  return append(new RelativeMapPath(), string);
76  }
77 
84  @NotNull
85  public static AbsoluteMapPath append(@NotNull final AbsoluteMapPath baseMapPath, @NotNull final MapPath mapPath) {
86  return mapPath instanceof AbsoluteMapPath ? (AbsoluteMapPath) mapPath : append(baseMapPath, mapPath.getPath());
87  }
88 
95  @NotNull
96  public static RelativeMapPath append(@NotNull final RelativeMapPath baseMapPath, @NotNull final RelativeMapPath mapPath) {
97  return append(baseMapPath, mapPath.getPath());
98  }
99 
106  @NotNull
107  private static AbsoluteMapPath append(@NotNull final AbsoluteMapPath mapPath, @NotNull final CharSequence string) {
108  AbsoluteMapPath result = mapPath;
109  final String tmpPath = result.getPath();
110  if (!tmpPath.isEmpty() && !tmpPath.equals("..") && !tmpPath.endsWith("/..")) {
111  result = new AbsoluteMapPath(result, "..");
112  }
113  for (final String path : MapPath.SEPARATOR.split(string, -1)) {
114  if (!path.isEmpty()) {
115  result = new AbsoluteMapPath(result, path);
116  }
117  }
118  return result;
119  }
120 
127  @NotNull
128  private static RelativeMapPath append(@NotNull final RelativeMapPath mapPath, @NotNull final CharSequence string) {
129  RelativeMapPath result = mapPath;
130  final String tmpPath = result.getPath();
131  if (!tmpPath.isEmpty() && !tmpPath.equals("..") && !tmpPath.endsWith("/..")) {
132  result = new RelativeMapPath(result, "..");
133  }
134  for (final String path : MapPath.SEPARATOR.split(string, -1)) {
135  if (!path.isEmpty()) {
136  result = new RelativeMapPath(result, path);
137  }
138  }
139  return result;
140  }
141 
142 }
net.sf.gridarta.model.mapmodel.MapPathUtils.MapPathUtils
MapPathUtils()
Private constructor to prevent instantiation.
Definition: MapPathUtils.java:33
net.sf.gridarta.model.mapmodel.AbsoluteMapPath.getPath
String getPath()
Returns the map path information.
Definition: AbsoluteMapPath.java:80
net.sf.gridarta.model.mapmodel.AbsoluteMapPath
A MapPath that is absolute, that is, it starts with a "/".
Definition: AbsoluteMapPath.java:29
net.sf.gridarta.model.mapmodel.MapPathUtils.newRelativeMapPath
static RelativeMapPath newRelativeMapPath(@NotNull final String string)
Creates a RelativeMapPath instance from string representation.
Definition: MapPathUtils.java:71
net.sf.gridarta.model.mapmodel.MapPathUtils.newMapPath
static MapPath newMapPath(@NotNull final String string)
Creates a MapPath instance from string representation.
Definition: MapPathUtils.java:43
net.sf.gridarta.model.mapmodel.MapPathUtils
Utility class for MapPath related functions.
Definition: MapPathUtils.java:28
net.sf.gridarta.model.mapmodel.MapPathUtils.append
static AbsoluteMapPath append(@NotNull final AbsoluteMapPath mapPath, @NotNull final CharSequence string)
Appends a relative path to a MapPath.
Definition: MapPathUtils.java:107
net.sf.gridarta.model.mapmodel.MapPathUtils.append
static AbsoluteMapPath append(@NotNull final AbsoluteMapPath baseMapPath, @NotNull final MapPath mapPath)
Appends a map path to another map path.
Definition: MapPathUtils.java:85
net.sf.gridarta.model.mapmodel.MapPathUtils.newAbsoluteMapPath
static AbsoluteMapPath newAbsoluteMapPath(@NotNull final String string)
Creates an AbsoluteMapPath instance from string representation.
Definition: MapPathUtils.java:57
net.sf.gridarta.model.mapmodel.MapPath
Represents a maps directory local map path.
Definition: MapPath.java:31
net.sf.gridarta.model.mapmodel.RelativeMapPath
Represents a maps directory local map path.
Definition: RelativeMapPath.java:29
net.sf.gridarta.model.mapmodel.MapPath.SEPARATOR
Pattern SEPARATOR
A Pattern to split a map path's string representation into components.
Definition: MapPath.java:38
net.sf.gridarta.model.mapmodel.MapPathUtils.append
static RelativeMapPath append(@NotNull final RelativeMapPath mapPath, @NotNull final CharSequence string)
Appends a relative path to a MapPath.
Definition: MapPathUtils.java:128
net.sf.gridarta.model.mapmodel.RelativeMapPath.getPath
String getPath()
Returns the map path information.
Definition: RelativeMapPath.java:110
net.sf.gridarta.model.mapmodel.MapPathUtils.append
static RelativeMapPath append(@NotNull final RelativeMapPath baseMapPath, @NotNull final RelativeMapPath mapPath)
Appends a map path to another map path.
Definition: MapPathUtils.java:96