use of org.opengrok.indexer.index.IndexedSymlink in project OpenGrok by OpenGrok.
the class SearchHelper method getPrimeRelativePath.
/**
* Determines if there is a prime equivalent to {@code relativePath}
* according to indexed symlinks and translate (or not) accordingly.
* @param project the project name or empty string if projects are not used
* @param relativePath an OpenGrok-style (i.e. starting with a file
* separator) relative path
* @return a prime relative path or just {@code relativePath} if no prime
* is matched
*/
public String getPrimeRelativePath(String project, String relativePath) throws IOException, ForbiddenSymlinkException {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
String sourceRoot = env.getSourceRootPath();
if (sourceRoot == null) {
throw new IllegalStateException("sourceRoot is not defined");
}
File absolute = new File(sourceRoot + relativePath);
ensureSettingsHelper();
settingsHelper.getSettings(project);
Map<String, IndexedSymlink> indexedSymlinks = settingsHelper.getSymlinks(project);
if (indexedSymlinks != null) {
String canonical = absolute.getCanonicalFile().getPath();
for (IndexedSymlink entry : indexedSymlinks.values()) {
if (canonical.equals(entry.getCanonical())) {
if (absolute.getPath().equals(entry.getAbsolute())) {
return relativePath;
}
Path newAbsolute = Paths.get(entry.getAbsolute());
return env.getPathRelativeToSourceRoot(newAbsolute.toFile());
} else if (canonical.startsWith(entry.getCanonicalSeparated())) {
Path newAbsolute = Paths.get(entry.getAbsolute(), canonical.substring(entry.getCanonicalSeparated().length()));
return env.getPathRelativeToSourceRoot(newAbsolute.toFile());
}
}
}
return relativePath;
}
Aggregations