|
Lines 10-15
Link Here
|
| 10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.core.internal.resources; |
11 |
package org.eclipse.core.internal.resources; |
| 12 |
|
12 |
|
|
|
13 |
import org.eclipse.core.runtime.IPath; |
| 14 |
|
| 15 |
import org.eclipse.core.internal.resources.projectvariables.WorkspaceParentLocationVariableResolver; |
| 16 |
|
| 13 |
import java.net.URI; |
17 |
import java.net.URI; |
| 14 |
import java.util.ArrayList; |
18 |
import java.util.ArrayList; |
| 15 |
import java.util.LinkedList; |
19 |
import java.util.LinkedList; |
|
Lines 90-95
Link Here
|
| 90 |
if (variable.equals(WorkspaceLocationVariableResolver.NAME)) |
94 |
if (variable.equals(WorkspaceLocationVariableResolver.NAME)) |
| 91 |
continue; |
95 |
continue; |
| 92 |
} |
96 |
} |
|
|
97 |
if (variable.equals(WorkspaceParentLocationVariableResolver.NAME)) |
| 98 |
continue; |
| 93 |
if (variable.equals(ParentVariableResolver.NAME)) |
99 |
if (variable.equals(ParentVariableResolver.NAME)) |
| 94 |
continue; |
100 |
continue; |
| 95 |
// find closest path to the original path |
101 |
// find closest path to the original path |
|
Lines 119-124
Link Here
|
| 119 |
if (variable.equals(WorkspaceLocationVariableResolver.NAME)) |
125 |
if (variable.equals(WorkspaceLocationVariableResolver.NAME)) |
| 120 |
continue; |
126 |
continue; |
| 121 |
} |
127 |
} |
|
|
128 |
if (variable.equals(WorkspaceParentLocationVariableResolver.NAME)) |
| 129 |
continue; |
| 122 |
if (variable.equals(ParentVariableResolver.NAME)) |
130 |
if (variable.equals(ParentVariableResolver.NAME)) |
| 123 |
continue; |
131 |
continue; |
| 124 |
IPath value = URIUtil.toPath(pathVariableManager.getValue(variable, resource)); |
132 |
IPath value = URIUtil.toPath(pathVariableManager.getValue(variable, resource)); |
|
Lines 158-164
Link Here
|
| 158 |
return newPath; |
166 |
return newPath; |
| 159 |
} |
167 |
} |
| 160 |
|
168 |
|
| 161 |
private static IPath makeRelativeToVariable(IPathVariableManager pathVariableManager, IPath originalPath, IResource resource, boolean force, String variableHint, boolean generateMacro) throws CoreException { |
169 |
private static IPath makeRelativeToVariable(IPathVariableManager pathVariableManager, IPath originalPath, IResource resource, boolean force, String variableHint, boolean generateMacro) { |
| 162 |
IPath path = convertToProperCase(originalPath); |
170 |
IPath path = convertToProperCase(originalPath); |
| 163 |
IPath value = URIUtil.toPath(pathVariableManager.getValue(variableHint, resource)); |
171 |
IPath value = URIUtil.toPath(pathVariableManager.getValue(variableHint, resource)); |
| 164 |
value = convertToProperCase(URIUtil.toPath(pathVariableManager.resolveURI(URIUtil.toURI(value), resource))); |
172 |
value = convertToProperCase(URIUtil.toPath(pathVariableManager.resolveURI(URIUtil.toURI(value), resource))); |
|
Lines 173-229
Link Here
|
| 173 |
} |
181 |
} |
| 174 |
|
182 |
|
| 175 |
if (force) { |
183 |
if (force) { |
| 176 |
// transform "c:/foo/bar/other_child/file.txt" into "${PARENT-1-BAR_CHILD}/other_child/file.txt" |
184 |
if (devicesAreCompatible(path, value)) { |
| 177 |
int matchingFirstSegments = path.matchingFirstSegments(value); |
185 |
// transform "c:/foo/bar/other_child/file.txt" into "${PARENT-1-BAR_CHILD}/other_child/file.txt" |
| 178 |
if (matchingFirstSegments >= 0) { |
186 |
int matchingFirstSegments = path.matchingFirstSegments(value); |
| 179 |
String newValue = buildParentPathVariable(variableHint, valueSegmentCount - matchingFirstSegments, generateMacro); |
187 |
if (matchingFirstSegments >= 0) { |
| 180 |
String originalName; |
188 |
String originalName= buildParentPathVariable(variableHint, valueSegmentCount - matchingFirstSegments, true); |
| 181 |
if (generateMacro) |
189 |
IPath tmp = Path.fromOSString(originalName); |
| 182 |
originalName = newValue; |
190 |
for (int j = matchingFirstSegments ;j < originalPath.segmentCount(); j++) { |
| 183 |
else { |
191 |
tmp = tmp.append(originalPath.segment(j)); |
| 184 |
originalName = getExistingVariable(newValue, pathVariableManager, resource); |
|
|
| 185 |
if (originalName == null) { |
| 186 |
String name; |
| 187 |
if (matchingFirstSegments > 0) |
| 188 |
name = originalPath.segment(matchingFirstSegments - 1); |
| 189 |
else |
| 190 |
name = originalPath.getDevice(); |
| 191 |
if (name == null) |
| 192 |
name = "ROOT"; //$NON-NLS-1$ |
| 193 |
originalName = getUniqueVariableName(name, pathVariableManager, resource); |
| 194 |
pathVariableManager.setValue(originalName, resource, URIUtil.toURI(Path.fromOSString(newValue))); |
| 195 |
} |
192 |
} |
|
|
193 |
return tmp; |
| 196 |
} |
194 |
} |
| 197 |
IPath tmp = Path.fromOSString(originalName); |
|
|
| 198 |
for (int j = matchingFirstSegments ;j < originalPath.segmentCount(); j++) { |
| 199 |
tmp = tmp.append(originalPath.segment(j)); |
| 200 |
} |
| 201 |
return tmp; |
| 202 |
} |
195 |
} |
| 203 |
} |
196 |
} |
| 204 |
return originalPath; |
197 |
return originalPath; |
| 205 |
} |
198 |
} |
| 206 |
|
|
|
| 207 |
private static String getExistingVariable(String newValue, IPathVariableManager pathVariableManager, IResource resource) { |
| 208 |
IPath resolvedNewValue = convertToProperCase(URIUtil.toPath(pathVariableManager.resolveURI(URIUtil.toURI(Path.fromOSString(newValue)), resource))); |
| 209 |
String[] existingVariables = pathVariableManager.getPathVariableNames(resource); |
| 210 |
for (int i = 0; i < existingVariables.length; i++) { |
| 211 |
String variable = existingVariables[i]; |
| 212 |
URI uri = pathVariableManager.getValue(variable, resource); |
| 213 |
if (uri != null) { |
| 214 |
IPath value = URIUtil.toPath(uri); |
| 215 |
if (value != null) { |
| 216 |
if (value.toOSString().equals(newValue)) |
| 217 |
return variable; |
| 218 |
IPath resolvedValue = convertToProperCase(URIUtil.toPath(pathVariableManager.resolveURI(URIUtil.toURI(value), resource))); |
| 219 |
if (resolvedValue.equals(resolvedNewValue)) |
| 220 |
return variable; |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
return null; |
| 225 |
} |
| 226 |
|
199 |
|
|
|
200 |
private static boolean devicesAreCompatible(IPath path, IPath value) { |
| 201 |
return (path.getDevice() != null && value.getDevice() != null) ? |
| 202 |
(path.getDevice().equals(value.getDevice())) : |
| 203 |
(path.getDevice() == value.getDevice()); |
| 204 |
} |
| 205 |
|
| 227 |
static private IPath convertToProperCase(IPath path) { |
206 |
static private IPath convertToProperCase(IPath path) { |
| 228 |
if (Platform.getOS().equals(Platform.OS_WIN32)) |
207 |
if (Platform.getOS().equals(Platform.OS_WIN32)) |
| 229 |
return Path.fromPortableString(path.toPortableString().toLowerCase()); |
208 |
return Path.fromPortableString(path.toPortableString().toLowerCase()); |
|
Lines 270-277
Link Here
|
| 270 |
return Path.fromOSString(variable).append(relativeSrcValue.removeFirstSegments(1)); |
249 |
return Path.fromOSString(variable).append(relativeSrcValue.removeFirstSegments(1)); |
| 271 |
} |
250 |
} |
| 272 |
|
251 |
|
| 273 |
public static String convertFromUserEditableFormatInternal(IPathVariableManager manager, String userFormat, IResource resource) { |
252 |
public static String convertFromUserEditableFormatInternal(IPathVariableManager manager, String userFormat, boolean locationFormat, IResource resource) { |
| 274 |
boolean isAbsolute = (userFormat.length() > 0) && (userFormat.charAt(0) == '/' || userFormat.charAt(0) == '\\'); |
253 |
char pathPrefix = 0; |
|
|
254 |
if ((userFormat.length() > 0) && (userFormat.charAt(0) == '/' || userFormat.charAt(0) == '\\')) |
| 255 |
pathPrefix = userFormat.charAt(0); |
| 275 |
String components[] = splitPathComponents(userFormat); |
256 |
String components[] = splitPathComponents(userFormat); |
| 276 |
for (int i = 0; i < components.length; i++) { |
257 |
for (int i = 0; i < components.length; i++) { |
| 277 |
if (components[i] == null) |
258 |
if (components[i] == null) |
|
Lines 297-337
Link Here
|
| 297 |
if (components[j] == null) |
278 |
if (components[j] == null) |
| 298 |
continue; |
279 |
continue; |
| 299 |
String variable = extractVariable(components[j]); |
280 |
String variable = extractVariable(components[j]); |
|
|
281 |
|
| 282 |
boolean hasVariableWithMacroSyntax = true; |
| 283 |
if (variable.length() == 0 && (locationFormat && j == 0)) { |
| 284 |
variable = components[j]; |
| 285 |
hasVariableWithMacroSyntax = false; |
| 286 |
} |
| 287 |
|
| 300 |
try { |
288 |
try { |
| 301 |
if (variable.length() > 0) { |
289 |
if (variable.length() > 0) { |
| 302 |
int indexOfVariable = components[j].indexOf(variable) - "${".length(); //$NON-NLS-1$ |
290 |
String prefix = new String(); |
| 303 |
String prefix = components[j].substring(0, indexOfVariable); |
291 |
if (hasVariableWithMacroSyntax) { |
| 304 |
String suffix = components[j].substring(indexOfVariable + "${".length() + variable.length() + "}".length()); //$NON-NLS-1$ //$NON-NLS-2$ |
292 |
int indexOfVariable = components[j].indexOf(variable) - "${".length(); //$NON-NLS-1$ |
| 305 |
if (suffix.length() != 0) { |
293 |
prefix = components[j].substring(0, indexOfVariable); |
| 306 |
// Create an intermediate variable, since a syntax of "${VAR}foo/../" |
294 |
String suffix = components[j].substring(indexOfVariable + "${".length() + variable.length() + "}".length()); //$NON-NLS-1$ //$NON-NLS-2$ |
| 307 |
// can't be converted to a "${PARENT-1-VAR}foo" variable. |
295 |
if (suffix.length() != 0) { |
| 308 |
// So instead, an intermediate variable "VARFOO" will be created of value |
296 |
// Create an intermediate variable, since a syntax of "${VAR}foo/../" |
| 309 |
// "${VAR}foo", and the string "${PARENT-1-VARFOO}" will be inserted. |
297 |
// can't be converted to a "${PARENT-1-VAR}foo" variable. |
| 310 |
String intermediateVariable = PathVariableUtil.getValidVariableName(variable + suffix); |
298 |
// So instead, an intermediate variable "VARFOO" will be created of value |
| 311 |
IPath intermediateValue = Path.fromPortableString(components[j]); |
299 |
// "${VAR}foo", and the string "${PARENT-1-VARFOO}" will be inserted. |
| 312 |
int intermediateVariableIndex = 1; |
300 |
String intermediateVariable = PathVariableUtil.getValidVariableName(variable + suffix); |
| 313 |
String originalIntermediateVariableName = intermediateVariable; |
301 |
IPath intermediateValue = Path.fromPortableString(components[j]); |
| 314 |
while (manager.isDefined(intermediateVariable, resource)) { |
302 |
int intermediateVariableIndex = 1; |
| 315 |
IPath tmpValue = URIUtil.toPath(manager.getValue(intermediateVariable, resource)); |
303 |
String originalIntermediateVariableName = intermediateVariable; |
| 316 |
if (tmpValue.equals(intermediateValue)) |
304 |
while (manager.isDefined(intermediateVariable, resource)) { |
| 317 |
break; |
305 |
IPath tmpValue = URIUtil.toPath(manager.getValue(intermediateVariable, resource)); |
| 318 |
intermediateVariable = originalIntermediateVariableName + intermediateVariableIndex; |
306 |
if (tmpValue.equals(intermediateValue)) |
|
|
307 |
break; |
| 308 |
intermediateVariable = originalIntermediateVariableName + intermediateVariableIndex; |
| 309 |
} |
| 310 |
if (!manager.isDefined(intermediateVariable, resource)) |
| 311 |
manager.setValue(intermediateVariable, resource, URIUtil.toURI(intermediateValue)); |
| 312 |
variable = intermediateVariable; |
| 313 |
prefix = new String(); |
| 319 |
} |
314 |
} |
| 320 |
if (!manager.isDefined(intermediateVariable, resource)) |
|
|
| 321 |
manager.setValue(intermediateVariable, resource, URIUtil.toURI(intermediateValue)); |
| 322 |
variable = intermediateVariable; |
| 323 |
prefix = new String(); |
| 324 |
} |
315 |
} |
| 325 |
String newVariable = variable; |
316 |
String newVariable = variable; |
| 326 |
if (PathVariableUtil.isParentVariable(variable)) { |
317 |
if (PathVariableUtil.isParentVariable(variable)) { |
| 327 |
String argument = PathVariableUtil.getParentVariableArgument(variable); |
318 |
String argument = PathVariableUtil.getParentVariableArgument(variable); |
| 328 |
int count = PathVariableUtil.getParentVariableCount(variable); |
319 |
int count = PathVariableUtil.getParentVariableCount(variable); |
| 329 |
if (argument != null && count != -1) |
320 |
if (argument != null && count != -1) |
| 330 |
newVariable = PathVariableUtil.buildParentPathVariable(argument, count + parentCount, false); |
321 |
newVariable = PathVariableUtil.buildParentPathVariable(argument, count + parentCount, locationFormat); |
| 331 |
else |
322 |
else |
| 332 |
newVariable = PathVariableUtil.buildParentPathVariable(variable, parentCount, false); |
323 |
newVariable = PathVariableUtil.buildParentPathVariable(variable, parentCount, locationFormat); |
| 333 |
} else |
324 |
} else |
| 334 |
newVariable = PathVariableUtil.buildParentPathVariable(variable, parentCount, false); |
325 |
newVariable = PathVariableUtil.buildParentPathVariable(variable, parentCount, locationFormat); |
| 335 |
components[j] = prefix + newVariable; |
326 |
components[j] = prefix + newVariable; |
| 336 |
break; |
327 |
break; |
| 337 |
} |
328 |
} |
|
Lines 346-357
Link Here
|
| 346 |
} |
337 |
} |
| 347 |
} |
338 |
} |
| 348 |
StringBuffer buffer = new StringBuffer(); |
339 |
StringBuffer buffer = new StringBuffer(); |
| 349 |
if (isAbsolute) |
340 |
if (pathPrefix != 0) |
| 350 |
buffer.append('/'); |
341 |
buffer.append(pathPrefix); |
| 351 |
for (int i = 0; i < components.length; i++) { |
342 |
for (int i = 0; i < components.length; i++) { |
| 352 |
if (components[i] != null) { |
343 |
if (components[i] != null) { |
| 353 |
if (i > 0) |
344 |
if (i > 0) |
| 354 |
buffer.append('/'); |
345 |
buffer.append(java.io.File.separator); |
| 355 |
buffer.append(components[i]); |
346 |
buffer.append(components[i]); |
| 356 |
} |
347 |
} |
| 357 |
} |
348 |
} |
|
Lines 379-404
Link Here
|
| 379 |
return (String[]) list.toArray(new String[0]); |
370 |
return (String[]) list.toArray(new String[0]); |
| 380 |
} |
371 |
} |
| 381 |
|
372 |
|
| 382 |
public static String convertToUserEditableFormatInternal(String value) { |
373 |
public static String convertToUserEditableFormatInternal(String value, boolean locationFormat) { |
| 383 |
StringBuffer buffer = new StringBuffer(); |
374 |
StringBuffer buffer = new StringBuffer(); |
| 384 |
String components[] = splitVariablesAndContent(value); |
375 |
if (locationFormat) { |
| 385 |
for (int i = 0; i < components.length; i++) { |
376 |
IPath path = Path.fromOSString(value); |
| 386 |
String variable = extractVariable(components[i]); |
377 |
if (path.isAbsolute()) |
| 387 |
if (PathVariableUtil.isParentVariable(variable)) { |
378 |
return path.toOSString(); |
| 388 |
String argument = PathVariableUtil.getParentVariableArgument(variable); |
379 |
int index = value.indexOf(java.io.File.separator); |
| 389 |
int count = PathVariableUtil.getParentVariableCount(variable); |
380 |
String variable = index != -1 ? value.substring(0, index): value; |
| 390 |
if (argument != null && count != -1) { |
381 |
convertVariableToUserFormat(buffer, variable, variable, false); |
| 391 |
buffer.append(PathVariableUtil.buildVariableMacro(Path.fromOSString(argument))); |
382 |
if (index != -1) |
| 392 |
for (int j = 0; j < count; j++) { |
383 |
buffer.append(value.substring(index)); |
| 393 |
buffer.append("/.."); //$NON-NLS-1$ |
384 |
} else { |
| 394 |
} |
385 |
String components[] = splitVariablesAndContent(value); |
| 395 |
} else |
386 |
for (int i = 0; i < components.length; i++) { |
| 396 |
buffer.append(components[i]); |
387 |
String variable = extractVariable(components[i]); |
| 397 |
} else |
388 |
convertVariableToUserFormat(buffer, components[i], variable, true); |
| 398 |
buffer.append(components[i]); |
389 |
} |
| 399 |
} |
390 |
} |
| 400 |
return buffer.toString(); |
391 |
return buffer.toString(); |
| 401 |
} |
392 |
} |
|
|
393 |
|
| 394 |
private static void convertVariableToUserFormat(StringBuffer buffer, String component, String variable, boolean generateMacro) { |
| 395 |
if (PathVariableUtil.isParentVariable(variable)) { |
| 396 |
String argument = PathVariableUtil.getParentVariableArgument(variable); |
| 397 |
int count = PathVariableUtil.getParentVariableCount(variable); |
| 398 |
if (argument != null && count != -1) { |
| 399 |
buffer.append(generateMacro? PathVariableUtil.buildVariableMacro(Path.fromOSString(argument)):Path.fromOSString(argument)); |
| 400 |
for (int j = 0; j < count; j++) { |
| 401 |
buffer.append(java.io.File.separator + ".."); //$NON-NLS-1$ |
| 402 |
} |
| 403 |
} else |
| 404 |
buffer.append(component); |
| 405 |
} else |
| 406 |
buffer.append(component); |
| 407 |
} |
| 402 |
/* |
408 |
/* |
| 403 |
* Splits a value (returned by this.getValue(variable) in an array of |
409 |
* Splits a value (returned by this.getValue(variable) in an array of |
| 404 |
* string, where the array is divided between the value content and the |
410 |
* string, where the array is divided between the value content and the |