Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 74845efe9eb68556f2a5caf4fa65a1fd9e6cc840 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * CONTRIBUTORS:
 * 		Thomas Schuetz and Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.core.common.base.util;

import org.eclipse.emf.common.util.URI;

/**
 * @author Henrik Rentz-Reichert
 *
 */
public class RelativePathHelpers {
	
	private static boolean bothNullOrEqual(String s1, String s2) {
		if (s1==null && s2==null)
			return true;
		if (s1==null || s2==null)
			return false;
		return s1.equals(s2);
	}

	/**
	 * the given paths are converted to file URIs (using {@link URI#createFileURI(String)}
	 * and then {@link #getRelativePath(URI, URI, boolean)} is called with <code>goUpIfNeeded=false</code>.
	 * 
	 * @param base the base path
	 * @param path the path for which the relative path is computed
	 * @return relative path (<code>null</code>if there is none)
	 */
	public static String getRelativePath(String base, String path) {
		return getRelativePath(URI.createFileURI(base), URI.createFileURI(path), false);
	}

	/**
	 * the given paths are converted to file URIs (using {@link URI#createFileURI(String)}
	 * and then {@link #getRelativePath(URI, URI, boolean)} is called.
	 * 
	 * @param base the base path
	 * @param path the path for which the relative path is computed
	 * @param goUpIfNeeded allow also ascending to parent directories
	 * @return relative path (<code>null</code>if there is none)
	 */
	public static String getRelativePath(String base, String path, boolean goUpIfNeeded) {
		return getRelativePath(URI.createFileURI(base), URI.createFileURI(path), goUpIfNeeded);
	}
	
	/**
	 * {@link #getRelativePath(URI, URI, boolean)} is called with
	 * <code>goUpIfNeeded=false</code>
	 * 
	 * @param base the base path
	 * @param path the path for which the relative path is computed
	 * @return relative path (<code>null</code>if there is none)
	 */
	public static String getRelativePath(URI base, URI path) {
		return getRelativePath(base, path, false);
	}
	
	/**
	 * compute a relative path to a given base path.
	 * Both paths must be of the same scheme and absolute and the given
	 * path has to have the first segments identical with the base path.
	 * Returned is a relative path separated by / characters.
	 * If there is no such relative path <code>null</code> is returned.
	 * 
	 * @param base the base path
	 * @param path the path for which the relative path is computed
	 * @param goUpIfNeeded allow also ascending to parent directories
	 * @return relative path (<code>null</code>if there is none)
	 */
	public static String getRelativePath(URI base, URI path, boolean goUpIfNeeded) {
		if (base==null || path==null)
			return null;
		
		if (!bothNullOrEqual(base.scheme(),path.scheme()))
			return null;
		
		if (!base.hasAbsolutePath())
			return null;
		
		if (!path.hasAbsolutePath())
			return null;
		
		if (!bothNullOrEqual(path.device(), base.device()))
			return null;
		
		StringBuffer result = new StringBuffer();
		if (goUpIfNeeded) {
			int max = base.segmentCount()<path.segmentCount()? base.segmentCount():path.segmentCount();
			int common;
			for (common=0; common<max; ++common) {
				if (!base.segment(common).equals(path.segment(common)))
					break;
			}
			for (int i=common; i<base.segmentCount(); ++i) {
				result.append("../");
			}
			for (int i=common; i<path.segmentCount(); ++i) {
				result.append(path.segment(i)+"/");
			}
			
			if (result.length()==0)
				return "";
			
			return result.substring(0, result.length()-1);
		}
		else {
			if (path.segmentCount()<base.segmentCount())
				return null;
			
			for (int i=0; i<base.segmentCount(); ++i) {
				if (!base.segment(i).equals(path.segment(i)))
					return null;
			}
			
			for (int i=base.segmentCount(); i<path.segmentCount(); ++i) {
				result.append(path.segment(i)+"/");
			}

			if(result.length()==0)
				return "";
			
			return result.substring(0, result.length()-1);
		}
	}
}

Back to the top