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-2015 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  } else {
47  return append(new RelativeMapPath(), string);
48  }
49  }
50 
57  @NotNull
58  public static AbsoluteMapPath newAbsoluteMapPath(@NotNull final String string) {
59  if (!string.startsWith("/")) {
60  throw new IllegalArgumentException("map path '" + string + "' is not absolute");
61  }
62  return append(new AbsoluteMapPath(), string);
63  }
64 
71  @NotNull
72  public static RelativeMapPath newRelativeMapPath(@NotNull final String string) {
73  if (string.startsWith("/")) {
74  throw new IllegalArgumentException("map path '" + string + "' is not relative");
75  }
76  return append(new RelativeMapPath(), string);
77  }
78 
85  @NotNull
86  public static AbsoluteMapPath append(@NotNull final AbsoluteMapPath baseMapPath, @NotNull final MapPath mapPath) {
87  return mapPath instanceof AbsoluteMapPath ? (AbsoluteMapPath) mapPath : append(baseMapPath, mapPath.getPath());
88  }
89 
97  @NotNull
98  public static RelativeMapPath append(@NotNull final RelativeMapPath baseMapPath, @NotNull final RelativeMapPath mapPath) {
99  return append(baseMapPath, mapPath.getPath());
100  }
101 
108  @NotNull
109  private static AbsoluteMapPath append(@NotNull final AbsoluteMapPath mapPath, @NotNull final CharSequence string) {
110  AbsoluteMapPath result = mapPath;
111  final String tmpPath = result.getPath();
112  if (!tmpPath.isEmpty() && !tmpPath.equals("..") && !tmpPath.endsWith("/..")) {
113  result = new AbsoluteMapPath(result, "..");
114  }
115  for (final String path : MapPath.SEPARATOR.split(string, -1)) {
116  if (!path.isEmpty()) {
117  result = new AbsoluteMapPath(result, path);
118  }
119  }
120  return result;
121  }
122 
129  @NotNull
130  private static RelativeMapPath append(@NotNull final RelativeMapPath mapPath, @NotNull final CharSequence string) {
131  RelativeMapPath result = mapPath;
132  final String tmpPath = result.getPath();
133  if (!tmpPath.isEmpty() && !tmpPath.equals("..") && !tmpPath.endsWith("/..")) {
134  result = new RelativeMapPath(result, "..");
135  }
136  for (final String path : MapPath.SEPARATOR.split(string, -1)) {
137  if (!path.isEmpty()) {
138  result = new RelativeMapPath(result, path);
139  }
140  }
141  return result;
142  }
143 
144 }
String getPath()
Returns the map path information.
static MapPath newMapPath(@NotNull final String string)
Creates a MapPath instance from string representation.
A MapPath that is absolute, that is, it starts with a "/".
Pattern SEPARATOR
A Pattern to split a map path's string representation into components.
Definition: MapPath.java:38
Represents a maps directory local map path.
Definition: MapPath.java:31
MapPathUtils()
Private constructor to prevent instantiation.
static AbsoluteMapPath newAbsoluteMapPath(@NotNull final String string)
Creates an AbsoluteMapPath instance from string representation.
static RelativeMapPath append(@NotNull final RelativeMapPath mapPath, @NotNull final CharSequence string)
Appends a relative path to a MapPath.
String getPath()
Returns the map path information.
static RelativeMapPath newRelativeMapPath(@NotNull final String string)
Creates a RelativeMapPath instance from string representation.
Utility class for MapPath related functions.
static RelativeMapPath append(@NotNull final RelativeMapPath baseMapPath, @NotNull final RelativeMapPath mapPath)
Appends a map path to another map path.
static AbsoluteMapPath append(@NotNull final AbsoluteMapPath mapPath, @NotNull final CharSequence string)
Appends a relative path to a MapPath.
static AbsoluteMapPath append(@NotNull final AbsoluteMapPath baseMapPath, @NotNull final MapPath mapPath)
Appends a map path to another map path.
Represents a maps directory local map path.