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: 0a21f95409f846821239a4a8fd698add071578fc (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*******************************************************************************
 * Copyright (c) 2001, 2007 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
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.sse.core.internal.util;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.wst.common.uriresolver.internal.util.URIHelper;

import com.ibm.icu.util.StringTokenizer;

/**
 * @deprecated The URIResolver interface is deprecated. Use the resolver from
 *             org.eclipse.wst.common.uriresolver.
 */
public class ProjectResolver implements URIResolver {
	private String fFileBaseLocation = null;
	private IProject fProject = null;

	/**
	 * It is strongly recommended that clients use
	 * project.getAdapter(URIResolver.class) to obtain a URIResolver aware of
	 * the Project's special requirements. Note that a URIResolver may not be
	 * returned at all so manually creating this object may still be required.
	 */
	public ProjectResolver(IProject project) {
		super();
		fProject = project;
	}

	public String getFileBaseLocation() {
		return fFileBaseLocation;
	}

	public String getLocationByURI(String uri) {
		return getLocationByURI(uri, getFileBaseLocation());
	}

	// defect 244817 end
	/**
	 * Resolve the (possibly relative) URI acording to RFC1808 using the
	 * default file base location. Resolves resource references into absolute
	 * resource locations without ensuring that the resource actually exists.
	 * 
	 * Note: currently resolveCrossProjectLinks is ignored in this
	 * implementation.
	 */
	public String getLocationByURI(String uri, boolean resolveCrossProjectLinks) {
		return getLocationByURI(uri, getFileBaseLocation(), resolveCrossProjectLinks);
	}

	public String getLocationByURI(String uri, String baseReference) {
		if (uri == null)
			return null;
		/*
		 * defect 244817 try { URL aURL = new URL(uri);
		 */
		/**
		 * An actual URL was given, but only the "file:///" protocol is
		 * supported. Resolve the URI by finding the file to which it points.
		 */
		/*
		 * defect 244817 if (!aURL.getProtocol().equals("platform")) {
		 * //$NON-NLS-1$ if (aURL.getProtocol().equals("file") &&
		 * (aURL.getHost().equals("localhost") || aURL.getHost().length() ==
		 * 0)) { //$NON-NLS-2$//$NON-NLS-1$ return aURL.getFile(); } return
		 * uri; } } catch (MalformedURLException mfuExc) { }
		 */
		// defect 244817 start
		if (isFileURL(uri)) {
			try {
				URL url = new URL(uri);
				return getPath(url);
			}
			catch (MalformedURLException e) {
			}
		}
		// defect 244817 end

		// which of the serveral are we suppose to use here?
		//

		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=71223
		// Workaround for problem in URIHelper; uris starting with '/' are
		// returned as-is.
		String location = null;
		if (uri.startsWith("/")) { //$NON-NLS-1$
			IProject p = getProject();
			if (p != null && p.isAccessible()) {
				IFile file = p.getFile(uri);
				
				if (file.getLocation() != null) {
					location = file.getLocation().toString();
				}
				if (location == null && file.getLocationURI() != null) {
					location = file.getLocationURI().toString();
				}
				if (location == null) {
					location = file.getFullPath().toString();
				}
			}
		}
		if(location == null) {
			location = URIHelper.normalize(uri, baseReference, getRootLocationString());
		}
		return location;
	}

	/**
	 * Perform the getLocationByURI action using the baseReference as the
	 * point of reference instead of the default for this resolver
	 * 
	 * Note: currently resolveCrossProjectLinks is ignored in this
	 * implementation.
	 */
	public String getLocationByURI(String uri, String baseReference, boolean resolveCrossProjectLinks) {
		return getLocationByURI(uri, baseReference);
	}

	/**
	 * 
	 * @param path
	 * @param host
	 * @return String
	 */
	private String getPath(IPath path, String host) {
		IPath newPath = path;
		// They are potentially for only Windows operating system.
		// a.) if path has a device, and if it begins with IPath.SEPARATOR,
		// remove it
		final String device = path.getDevice();
		if ((device != null) && (device.length() > 0)) {
			if (device.charAt(0) == IPath.SEPARATOR) {
				final String newDevice = device.substring(1);
				newPath = path.setDevice(newDevice);
			}
		}
		// b.) if it has a hostname, it is UNC name... Any java or eclipse api
		// helps it ??
		if (path != null && host != null && host.length() != 0) {
			IPath uncPath = new Path(host);
			uncPath = uncPath.append(path);
			newPath = uncPath.makeUNC(true);
		}
		return newPath.toString();
	}

	/**
	 * 
	 * @param url
	 * @return String
	 */
	private String getPath(URL url) {
		String ref = url.getRef() == null ? "" : "#" + url.getRef(); //$NON-NLS-1$ //$NON-NLS-2$
		String strPath = url.getFile() + ref;
		IPath path;
		if (strPath.length() == 0) {
			path = Path.ROOT;
		}
		else {
			path = new Path(strPath);
			String query = null;
			StringTokenizer parser = new StringTokenizer(strPath, "?"); //$NON-NLS-1$
			int tokenCount = parser.countTokens();
			if (tokenCount == 2) {
				path = new Path((String) parser.nextElement());
				query = (String) parser.nextElement();
			}
			if (query == null) {
				parser = new StringTokenizer(path.toString(), "#"); //$NON-NLS-1$
				tokenCount = parser.countTokens();
				if (tokenCount == 2) {
					path = new Path((String) parser.nextElement());
				}
			}
		}
		return getPath(path, url.getHost());
	}

	public org.eclipse.core.resources.IProject getProject() {
		return fProject;
	}

	public org.eclipse.core.resources.IContainer getRootLocation() {
		return fProject;
	}

	protected String getRootLocationString() {
		String location = null;
		if (fProject == null)
			return null;
		if (fProject.getLocation() != null) {
			location = fProject.getLocation().toString();
		}
		if (location == null && fProject.getLocationURI() != null) {
			location = fProject.getLocationURI().toString();
		}
		if (location == null) {
			location = fProject.getFullPath().toString();
		}
		return location;
	}

	public InputStream getURIStream(String uri) {
		return null;
	}

	// defect 244817 start
	/**
	 * 
	 * @param passedSpec
	 * @return boolean
	 */
	private boolean isFileURL(String passedSpec) {
		if (passedSpec == null) {
			return false;
		}
		final String spec = passedSpec.trim();
		if (spec.length() == 0) {
			return false;
		}
		final int limit = spec.length();
		String newProtocol = null;
		for (int index = 0; index < limit; index++) {
			final char p = spec.charAt(index);
			if (p == '/') { //$NON-NLS-1$
				break;
			}
			if (p == ':') { //$NON-NLS-1$
				newProtocol = spec.substring(0, index);
				break;
			}
		}
		return (newProtocol != null && newProtocol.compareToIgnoreCase("file") == 0); //$NON-NLS-1$
	}

	public void setFileBaseLocation(String newFileBaseLocation) {
		fFileBaseLocation = newFileBaseLocation;
	}

	public void setProject(IProject newProject) {
		fProject = newProject;
	}
}

Back to the top