Gridarta Editor
PathManager.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.io;
21 
22 import java.io.File;
27 import net.sf.gridarta.utils.IOUtils;
28 import org.jetbrains.annotations.NotNull;
29 
38 public class PathManager {
39 
43  @NotNull
45 
51  public PathManager(@NotNull final ProjectSettings projectSettings) {
52  this.projectSettings = projectSettings;
53  }
54 
60  @NotNull
61  public String getMapPath(@NotNull final File file) {
62  return PathManagerUtils.getMapPath(file, projectSettings.getMapsDirectory());
63  }
64 
70  @NotNull
71  public MapFile getMapFile(@NotNull final AbsoluteMapPath mapPath) {
72  return new MapFile(new MapFile(projectSettings.getMapsDirectory()), mapPath);
73  }
74 
82  @NotNull
83  public MapFile getMapFile(@NotNull final File file) {
84  final String canonicalFile = IOUtils.getCanonicalPath(file);
85  final File mapsDirectory = projectSettings.getMapsDirectory();
86  final String canonicalMapsDir = IOUtils.getCanonicalPath(mapsDirectory) + "/";
87  final String mapPath;
88  final File baseDir;
89  if (canonicalFile.startsWith(canonicalMapsDir)) {
90  // within maps directory => base on maps directory
91  mapPath = canonicalFile.substring(canonicalMapsDir.length());
92  baseDir = mapsDirectory;
93  } else {
94  // outside maps directory => base on filesystem root
95  mapPath = canonicalFile;
96  baseDir = new File("/");
97  }
98  return new MapFile(new MapFile(baseDir), MapPathUtils.newMapPath(mapPath));
99  }
100 
101 }
final ProjectSettings projectSettings
The ProjectSettings instance for obtaining folder information.
MapFile getMapFile(@NotNull final File file)
Returns a MapFile instance for a File.
static MapPath newMapPath(@NotNull final String string)
Creates a MapPath instance from string representation.
This class contains methods for converting relative map paths to absolute map paths and vice versa...
Settings that apply to a project.
Utility class for converting relative map paths to absolute map paths and vice versa.
A MapPath that is absolute, that is, it starts with a "/".
MapFile getMapFile(@NotNull final AbsoluteMapPath mapPath)
Returns a MapFile instance from an AbsoluteMapPath.
Base package of all Gridarta classes.
static String getCanonicalPath(@NotNull final File file)
Calls File#getCanonicalPath().
Definition: IOUtils.java:191
PathManager(@NotNull final ProjectSettings projectSettings)
Creates a new instance.
Utility-class for Gridarta's I/O.
Definition: IOUtils.java:40
Utility class for MapPath related functions.
static String getMapPath(@NotNull final File file, @NotNull final File baseDir)
Returns a relative path path for a File.
File getMapsDirectory()
Returns the default maps directory.
String getMapPath(@NotNull final File file)
Returns a map path for a File.
The location of a map file with a map directory.
Definition: MapFile.java:31