001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube 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     * SonarQube 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     */
020    package org.sonar.api.scan.filesystem;
021    
022    import com.google.common.base.Joiner;
023    import com.google.common.base.Preconditions;
024    import com.google.common.collect.Lists;
025    import org.sonar.api.BatchComponent;
026    import org.sonar.api.utils.PathUtils;
027    
028    import javax.annotation.CheckForNull;
029    import java.io.File;
030    import java.util.Collection;
031    import java.util.List;
032    
033    /**
034     * @since 3.5
035     */
036    public class PathResolver implements BatchComponent {
037    
038      public File relativeFile(File dir, String path) {
039        Preconditions.checkArgument(dir.isDirectory(), "Not a directory: " + dir.getAbsolutePath());
040        File file = new File(path);
041        if (!file.isAbsolute()) {
042          try {
043            file = new File(dir, path).getCanonicalFile();
044          } catch (Exception e) {
045            throw new IllegalStateException("Fail to resolve path '" + path + "' relative to: " + dir.getAbsolutePath(), e);
046          }
047        }
048        return file;
049      }
050    
051      public List<File> relativeFiles(File dir, List<String> paths) {
052        List<File> result = Lists.newArrayList();
053        for (String path : paths) {
054          result.add(relativeFile(dir, path));
055        }
056        return result;
057      }
058    
059      @CheckForNull
060      public RelativePath relativePath(Collection<File> dirs, File file) {
061        List<String> stack = Lists.newArrayList();
062        File cursor = file;
063        while (cursor != null) {
064          File parentDir = parentDir(dirs, cursor);
065          if (parentDir != null) {
066            return new RelativePath(parentDir, Joiner.on("/").join(stack));
067          }
068          stack.add(0, cursor.getName());
069          cursor = cursor.getParentFile();
070        }
071        return null;
072      }
073    
074      @CheckForNull
075      public String relativePath(File dir, File file) {
076        List<String> stack = Lists.newArrayList();
077        String dirPath = PathUtils.canonicalPath(dir);
078        File cursor = file;
079        while (cursor != null) {
080          if (dirPath.equals(PathUtils.canonicalPath(cursor))) {
081            return Joiner.on("/").join(stack);
082          }
083          stack.add(0, cursor.getName());
084          cursor = cursor.getParentFile();
085        }
086        return null;
087      }
088    
089      @CheckForNull
090      private File parentDir(Collection<File> dirs, File cursor) {
091        for (File dir : dirs) {
092          if (PathUtils.canonicalPath(dir).equals(PathUtils.canonicalPath(cursor))) {
093            return dir;
094          }
095        }
096        return null;
097      }
098    
099      public static final class RelativePath {
100        private File dir;
101        private String path;
102    
103        public RelativePath(File dir, String path) {
104          this.dir = dir;
105          this.path = path;
106        }
107    
108        public File dir() {
109          return dir;
110        }
111    
112        public String path() {
113          return path;
114        }
115      }
116    }