Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a5b90da289bf04ab58c1c2950a7f3b48f40a1754 (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
/*******************************************************************************
 * Copyright (c) 2002 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 * IBM - Initial implementation
 ******************************************************************************/
package org.eclipse.team.internal.core.target;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

public class UrlUtil {
	
	public static IPath getTrailingPath(URL fullURL, URL startingURL) {
		IPath fullPath = new Path(fullURL.getPath());
		IPath startingPath = new Path(startingURL.getPath());
		int matchingCount = fullPath.matchingFirstSegments(startingPath);
		return fullPath.removeFirstSegments(matchingCount);
	}
	
	public static URL concat(String root, IPath end) throws MalformedURLException {
		return new URL(concatString(root, end));
	}

	private static String concatString(String root, IPath end) {
		if(end.isEmpty())
			return root;
		if(root.length() == 0)
			return end.toString();
		boolean rootHasTrailing = root.charAt(root.length() - 1) == Path.SEPARATOR;  //has trailing '/'
		boolean endHasLeading = end.isAbsolute();	// has leading '/'
			
		if(rootHasTrailing && endHasLeading) //http://mysite/ + /myFolder
			return root + end.toString().substring(1);	 // we have two seperators, drop one
			
		if(!rootHasTrailing && !endHasLeading) //http://mysite + myFolder
			return root + Path.SEPARATOR + end.toString();
				
		return root + end.toString();	//have one separator between the two, just concat end
	}
}

Back to the top