Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 9fa8433609315b4d445776e5c32348e4bb4cf47f (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
/*******************************************************************************
 * Copyright (c) 2004 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.css.core.internal.util;



import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;

/**
 * 
 */
public class CSSPathService {

	// Constants
	private static final String FILEURLSCHEME = "file";//$NON-NLS-1$
	private static final String URLCOLON = ":"; //$NON-NLS-1$
	private static final String FILEURLPREFIX = FILEURLSCHEME + URLCOLON + "//";//$NON-NLS-1$
	private static final String URLSEPARATOR = "/"; //$NON-NLS-1$

	/**
	 * @return java.lang.String
	 */
	public static String getAbsoluteURL(IStructuredModel baseModel, String ref) {
		String absLink = URLModelProviderCSS.resolveURI(baseModel, ref, true);
		String url = absLink;
		if (absLink != null) { // if it has not scheme, we must add "file"
								// scheme
			try {
				new java.net.URL(absLink);
			}
			catch (java.net.MalformedURLException e) {
				IPath path = new Path(absLink);
				if (path != null)
					url = toURL(path);
			}
		}
		return url;
	}

	/**
	 * 
	 * @return org.eclipse.core.resources.IFile
	 * @param location
	 *            java.lang.String
	 */
	public static IFile location2File(String location) {
		Path path = new Path(location);
		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
		if (file == null && path.segmentCount() > 1)
			file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
		return file;
	}

	/**
	 * Gets a file URL for the path.
	 */
	private static String toURL(IPath path) {
		path = new Path(path.toFile().getAbsolutePath());
		if (path.isUNC() == true) {
			// it's UNC. return file://host/foo/bar/baz.html
			return FILEURLSCHEME + URLCOLON + path.toString();
		}
		return FILEURLPREFIX + URLSEPARATOR + path.toString();
	}
}

Back to the top