1 /*
2 * Copyright (C) 2021 INRAE AgroClim
3 *
4 * This file is part of Indicators.
5 *
6 * Indicators is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Indicators is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Indicators. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package fr.inrae.agroclim.indicators.util;
20
21 import java.nio.file.Path;
22 import java.nio.file.Paths;
23
24 /**
25 * Helper methods for file paths.
26 *
27 * Last changed : $Date$
28 *
29 * @author $Author$
30 * @version $Revision$
31 */
32 public abstract class PathUtils {
33 /**
34 * The UNIX separator.
35 */
36 private static final String UNIX_SEP = "/";
37
38 /**
39 * Constructs a relative path between a base path and another path.
40 *
41 * <p> Relativization is the inverse of {@link #resolve(java.lang.String, java.lang.String) resolution}.
42 * This method attempts to construct a {@link Path#isAbsolute relative} path that when
43 * {@link #resolve(java.lang.String, java.lang.String) resolved} against this path, yields a path that locates
44 * the same file as the given path.</p>
45 *
46 * @param basePath absolute path of base directory
47 * @param absolutePath absolute path to relativize
48 * @return relative path
49 */
50 public static String relativize(final String basePath, final String absolutePath) {
51 if (absolutePath == null) {
52 return null;
53 }
54 final Path base = Paths.get(basePath);
55 final Path absolute = Paths.get(absolutePath);
56 // example "."
57 if (base.getRoot() == null) {
58 return absolutePath;
59 }
60 if (UNIX_SEP.equals(base.getRoot().toString())) {
61 if (!base.getName(0).equals(absolute.getName(0))) {
62 return absolutePath;
63 }
64 } else if (!base.getRoot().equals(absolute.getRoot())) {
65 return absolutePath;
66 }
67 final Path path = base.relativize(absolute);
68 return path.toString();
69 }
70
71 /**
72 * Resolve the given path against this path.
73 *
74 * <p> Resolution is the inverse of {@link #relativize(java.lang.String, java.lang.String) relativization}.
75 * If the {@code relativePath} parameter is an {@link Path#isAbsolute() absolute} path then this method trivially
76 * returns {@code relativePath}.</p>
77 *
78 * @param basePath absolute path of base directory
79 * @param relativePath relative path to resolve
80 * @return absolute path
81 */
82 public static String resolve(final String basePath, final String relativePath) {
83 if (relativePath == null) {
84 return null;
85 }
86 if (Paths.get(relativePath).isAbsolute()) {
87 return relativePath;
88 }
89 final Path path = Paths.get(basePath).resolve(relativePath).normalize();
90 return path.toString();
91 }
92
93 /**
94 * No constructor for helper class.
95 */
96 private PathUtils() {
97 //
98 }
99
100 }