Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2af0e3b166e1ac603f1599495c9f9c6edca051ba (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*******************************************************************************
 * Copyright (c) 2003, 2005 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.jst.ws.internal.axis.consumption.ui.util;


import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;

public class PlatformUtils {

	public static final String PLATFORM_ROOT = "platform:/resource";
	public static final String WIN_FILE_PROTOCOL = "file:/";
	public static final String LNX_FILE_PROTOCOL = "file://";
	public static final String PLATFORM = "platform";
	public static final String RESOURCE = "/resource";

	private PlatformUtils() {
	}

	/**
	* Returns the string representation of platform URL given an IPath
	* @param IPath path
	* @return String platformURL
	*/
	public static String getPlatformURL(IPath path) {
		String platformURL;
		platformURL = URI.createPlatformResourceURI(path.toString()).toString();
		// old method: file system representation
		// 	platformURL = ResourcesPlugin.getWorkspace().getRoot().getFile(path).getLocation().toString();
		return platformURL;
	}

	/**
	* Returns the URL representation of the Platform URL given an IPath
	* The PlatformResourceManager underdstands this format and therefore
	* this is suited for command URL's
	* @param IPath path
	* @return URL Platform URL
	*/
	public static URL getCommandPlatformURL(IPath path) {
		URL url = null;
		try {
			url = new URL(PLATFORM, null, RESOURCE + path.toString());
		} catch (MalformedURLException e) {
			return url;
		}
		return url;
	}

	/**
	* Returns the string representation of path given platform URL string
	* @param String platform URL string
	* @return String path string
	*/
	public static String getPathFromPlatform(String platformStr) {
		String pathStr = platformStr;

		String rootLocation;
		rootLocation = PLATFORM_ROOT;
		// old method: file system representation
		//	rootLocation = ResourcesPlugin.getWorkspace().getRoot().getLocation().toString();
		if (platformStr.startsWith(rootLocation))
			pathStr = platformStr.substring(rootLocation.length());
		return pathStr;
	}

	/**
	* Returns the string representation of platform URL given the path string
	* @param String path string
	* @return String platform URL string
	*/
	public static String getPlatformFromPath(String pathStr) {
		String platformStr = pathStr;

		String rootLocation;
		rootLocation = PLATFORM_ROOT;
		// old method: file system representation
		//	rootLocation = ResourcesPlugin.getWorkspace().getRoot().getLocation().toString();
		platformStr = rootLocation + pathStr;
		return platformStr;
	}

	/**
	* Returns the string representation of the local file system file given platform URL string
	* @param String platform URL string
	* @return String file string
	*/
	public static String getFileFromPlatform(String platformStr) {
		String fileStr = platformStr;
		String pathStr = getPathFromPlatform(platformStr);

		fileStr =
			ResourcesPlugin
				.getWorkspace()
				.getRoot()
				.getFile(new Path(pathStr))
				.getLocation()
				.toString();

		// old method: file system representation
		//	Do nothing

		return fileStr;
	}

	/**
	* Returns the file protocol representation of the local file system file given platform URL string
	* @param String platform URL string
	* @return String the file protocol uri
	*/
	public static String getFileURLFromPlatform(String platformStr) {
		String fileStr = platformStr;
		String pathStr = getPathFromPlatform(platformStr);
		try {
			fileStr =
				ResourcesPlugin
					.getWorkspace()
					.getRoot()
					.getFile(new Path(pathStr))
					.getLocation()
					.toFile()
					.toURL()
					.toString();
		} catch (MalformedURLException murle) {
			fileStr = getFileURLFromPath(new Path(pathStr));
		}
		return fileStr;
	}

	/**
	* Returns the file protocol representation of a local file
	* @param IPath the Path object representing the file
	* @return String the file protocol uri
	*/
	public static String getFileURLFromPath(IPath path) {

		String file = null;
		if (path != null) {
			if (!path.isAbsolute()) {
				file =
					ResourcesPlugin
						.getWorkspace()
						.getRoot()
						.getFile(path)
						.getLocation()
						.toString();
				if (file.charAt(0) == IPath.SEPARATOR) {
					file = LNX_FILE_PROTOCOL + file;
				} else {
					file = WIN_FILE_PROTOCOL + file;
				}
			} else {
				file = path.toString();
			}
			if (file != null && file.charAt(0) == IPath.SEPARATOR) {
				file = LNX_FILE_PROTOCOL + file;
			} else {
				file = WIN_FILE_PROTOCOL + file;
			}
		}
		return file == null ? null : file;

	}
}

Back to the top