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