001/*
002 * SonarQube
003 * Copyright (C) 2009-2016 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.scan.filesystem;
021
022import com.google.common.base.Joiner;
023import com.google.common.base.Preconditions;
024import org.sonar.api.batch.BatchSide;
025import org.sonar.api.utils.PathUtils;
026
027import javax.annotation.CheckForNull;
028
029import java.io.File;
030import java.util.ArrayList;
031import java.util.Collection;
032import java.util.List;
033
034/**
035 * @since 3.5
036 */
037@BatchSide
038public class PathResolver {
039
040  public File relativeFile(File dir, String path) {
041    Preconditions.checkArgument(dir.isDirectory(), "Not a directory: " + dir.getAbsolutePath());
042    File file = new File(path);
043    if (!file.isAbsolute()) {
044      try {
045        file = new File(dir, path).getAbsoluteFile();
046      } catch (Exception e) {
047        throw new IllegalStateException("Fail to resolve path '" + path + "' relative to: " + dir.getAbsolutePath(), e);
048      }
049    }
050    return file;
051  }
052
053  public List<File> relativeFiles(File dir, List<String> paths) {
054    List<File> result = new ArrayList<>();
055    for (String path : paths) {
056      result.add(relativeFile(dir, path));
057    }
058    return result;
059  }
060
061  @CheckForNull
062  public RelativePath relativePath(Collection<File> dirs, File file) {
063    List<String> stack = new ArrayList<>();
064    File cursor = file;
065    while (cursor != null) {
066      File parentDir = parentDir(dirs, cursor);
067      if (parentDir != null) {
068        return new RelativePath(parentDir, Joiner.on("/").join(stack));
069      }
070      stack.add(0, cursor.getName());
071      cursor = cursor.getParentFile();
072    }
073    return null;
074  }
075
076  @CheckForNull
077  public String relativePath(File dir, File file) {
078    List<String> stack = new ArrayList<>();
079    String dirPath = PathUtils.canonicalPath(dir);
080    File cursor = file;
081    while (cursor != null) {
082      if (dirPath.equals(PathUtils.canonicalPath(cursor))) {
083        return Joiner.on("/").join(stack);
084      }
085      stack.add(0, cursor.getName());
086      cursor = cursor.getParentFile();
087    }
088    return null;
089  }
090
091  @CheckForNull
092  private static File parentDir(Collection<File> dirs, File cursor) {
093    for (File dir : dirs) {
094      if (PathUtils.canonicalPath(dir).equals(PathUtils.canonicalPath(cursor))) {
095        return dir;
096      }
097    }
098    return null;
099  }
100
101  public static final class RelativePath {
102    private File dir;
103    private String path;
104
105    public RelativePath(File dir, String path) {
106      this.dir = dir;
107      this.path = path;
108    }
109
110    public File dir() {
111      return dir;
112    }
113
114    public String path() {
115      return path;
116    }
117  }
118}