Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ca0ad681d3a177e66f0bf6572352fda4e22b4560 (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
/*
* 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 v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
* 
* Contributors:
*   IBM - Initial API and implementation
*   Jens Lukowski/Innoopract - initial renaming/restructuring
* 
*/
package org.eclipse.wst.sse.core.util;

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

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.wst.xml.uriresolver.util.URIHelper;

public class ProjectResolver implements URIResolver {
	private IProject fProject = null;
	private String fFileBaseLocation = 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 java.lang.String getFileBaseLocation() {
		return fFileBaseLocation;
	}

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

	public java.lang.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
		return URIHelper.normalize(uri, baseReference, getRootLocationString());
	}

	//	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$
	}

	/**
	 * 
	 * @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());
	}

	/**
	 * 
	 * @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();
	}

	//	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 java.lang.String getLocationByURI(String uri, boolean resolveCrossProjectLinks) {
		return getLocationByURI(uri, getFileBaseLocation(), resolveCrossProjectLinks);
	}

	/**
	 * 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 java.lang.String getLocationByURI(String uri, String baseReference, boolean resolveCrossProjectLinks) {
		return getLocationByURI(uri, baseReference);
	}

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

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

	protected String getRootLocationString() {
		return null;
	}

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

	public void setProject(org.eclipse.core.resources.IProject newProject) {
		fProject = newProject;
	}

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

Back to the top