I have an Eclipse plugin where the user can select files or folders. Currently I’m storing the absolute paths in a configuration file. Since the configuration file is shared between users (e.g. with SVN) the absolute paths are a problem.
My configuration file currently looks something like this:
<?xml version="1.0" encoding="UTF-8"?>
<paths>
<path value="C:\Users\UserA\someFolder\foo.jar"/>
<path value="C:\Users\UserA\someFolder\bar.jar"/>
</paths>
In order to fix the problem I’d like to use the Linked Resources feature of Eclipse. So, when the user defines a path variable (Eclipse preferences -> General -> Workspace -> Linked Resources) with the name MY_JARS and a value of “C:\Users\UserA\someFolder” the configuration file my plugin writes should look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<paths>
<path value="${MY_JARS}\foo.jar"/>
<path value="${MY_JARS}\bar.jar"/>
</paths>
How can I replace certain parts of a path by variable names that are defined in Eclipse’s Linked Resources? And how can I revert this when I’m reading the path from the configuration file in order to get a correct absolute path?
I’ve looked at IPathVariableManager which looks quite promising. However, I couldn’t figure out how to use it properly.
Assuming that user specifies an absolute path and you want to replace it with relative path using any matching defined variable, you’d use
IPathVariableManager.convertToRelative(...). To get absolute path from relative, useIPathVariableManager.resolveURI(...).Naturally, this assumes that the variable is defined in all environments where the configuration file is used, which might not always be the case. Therefore, it is better to let users specify paths with known variables rather than convert automatically behind the scenes.