001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info 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 java.io.File;
024import java.nio.file.Path;
025import java.util.ArrayList;
026import java.util.Collection;
027import java.util.List;
028import javax.annotation.CheckForNull;
029import org.apache.commons.io.FilenameUtils;
030import org.sonar.api.batch.ScannerSide;
031import org.sonar.api.utils.PathUtils;
032
033/**
034 * @since 3.5
035 */
036@ScannerSide
037public class PathResolver {
038
039  public File relativeFile(File dir, String path) {
040    return dir.toPath().resolve(path).normalize().toFile();
041  }
042
043  public List<File> relativeFiles(File dir, List<String> paths) {
044    List<File> result = new ArrayList<>();
045    for (String path : paths) {
046      result.add(relativeFile(dir, path));
047    }
048    return result;
049  }
050
051  /**
052   * @deprecated since 6.0 was used when component keys were relative to source dirs
053   */
054  @Deprecated
055  @CheckForNull
056  public RelativePath relativePath(Collection<File> dirs, File file) {
057    List<String> stack = new ArrayList<>();
058    File cursor = file;
059    while (cursor != null) {
060      File parentDir = parentDir(dirs, cursor);
061      if (parentDir != null) {
062        return new RelativePath(parentDir, Joiner.on("/").join(stack));
063      }
064      stack.add(0, cursor.getName());
065      cursor = cursor.getParentFile();
066    }
067    return null;
068  }
069
070  /**
071   * Similar to {@link Path#relativize(Path)} except that:
072   *   <ul>
073   *   <li>null is returned if file is not a child of dir
074   *   <li>the resulting path is converted to use Unix separators
075   *   </ul> 
076   * @since 6.0
077   */
078  @CheckForNull
079  public String relativePath(Path dir, Path file) {
080    Path baseDir = dir.normalize();
081    Path path = file.normalize();
082    if (!path.startsWith(baseDir)) {
083      return null;
084    }
085    try {
086      Path relativized = baseDir.relativize(path);
087      return FilenameUtils.separatorsToUnix(relativized.toString());
088    } catch (IllegalArgumentException e) {
089      return null;
090    }
091  }
092
093  @CheckForNull
094  public String relativePath(File dir, File file) {
095    return relativePath(dir.toPath(), file.toPath());
096  }
097
098  @CheckForNull
099  private static File parentDir(Collection<File> dirs, File cursor) {
100    for (File dir : dirs) {
101      if (PathUtils.canonicalPath(dir).equals(PathUtils.canonicalPath(cursor))) {
102        return dir;
103      }
104    }
105    return null;
106  }
107
108  /**
109   * @deprecated since 6.0 was used when component keys were relative to source dirs
110   */
111  @Deprecated
112  public static final class RelativePath {
113    private File dir;
114    private String path;
115
116    public RelativePath(File dir, String path) {
117      this.dir = dir;
118      this.path = path;
119    }
120
121    public File dir() {
122      return dir;
123    }
124
125    public String path() {
126      return path;
127    }
128  }
129}