001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar 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     * Sonar 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
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.api.resources;
021    
022    import com.google.common.collect.Lists;
023    import org.apache.commons.lang.StringUtils;
024    
025    import java.io.File;
026    import java.util.Collection;
027    import java.util.List;
028    
029    /**
030     * @since 2.8
031     */
032    public final class InputFileUtils {
033    
034      private InputFileUtils() {
035        // only static methods
036      }
037    
038      /**
039       * @param inputFiles not nullable
040       * @return not null list
041       */
042      public static List<java.io.File> toFiles(Collection<InputFile> inputFiles) {
043        List<java.io.File> files = Lists.newArrayList();
044        for (InputFile inputFile : inputFiles) {
045          files.add(inputFile.getFile());
046        }
047        return files;
048      }
049    
050      /**
051       * Extract the directory from relative path. Examples :
052       * - returns "org/foo" when relative path is "org/foo/Bar.java"
053       * - returns "" when relative path is "Bar.java"
054       */
055      public static String getRelativeDirectory(InputFile inputFile) {
056        String relativePath = inputFile.getRelativePath();
057        if (StringUtils.contains(relativePath, "/")) {
058          return StringUtils.substringBeforeLast(relativePath, "/");
059        }
060        return "";
061      }
062    
063      /**
064       * For internal and for testing purposes. Please use the FileSystem component to access files.
065       */
066      public static InputFile create(java.io.File basedir, java.io.File file) {
067        String relativePath = getRelativePath(basedir, file);
068        if (relativePath != null) {
069          return create(basedir, relativePath);
070        }
071        return null;
072      }
073    
074      /**
075       * For internal and for testing purposes. Please use the FileSystem component to access files.
076       */
077      public static InputFile create(java.io.File basedir, String relativePath) {
078        return new DefaultInputFile(basedir, relativePath);
079      }
080    
081      /**
082       * For internal and for testing purposes. Please use the FileSystem component to access files.
083       */
084      public static List<InputFile> create(java.io.File basedir, Collection<java.io.File> files) {
085        List<InputFile> inputFiles = Lists.newArrayList();
086        for (File file : files) {
087          InputFile inputFile = create(basedir, file);
088          if (inputFile != null) {
089            inputFiles.add(inputFile);
090          }
091        }
092        return inputFiles;
093      }
094    
095      static String getRelativePath(java.io.File basedir, java.io.File file) {
096        List<String> stack = Lists.newArrayList(file.getName());
097        java.io.File cursor = file.getParentFile();
098        while (cursor != null) {
099          if (basedir.equals(cursor)) {
100            return StringUtils.join(stack, "/");
101          }
102          stack.add(0, cursor.getName());
103          cursor = cursor.getParentFile();
104        }
105        return null;
106      }
107    
108      static final class DefaultInputFile implements InputFile {
109        private java.io.File basedir;
110        private String relativePath;
111    
112        DefaultInputFile(java.io.File basedir, String relativePath) {
113          this.basedir = basedir;
114          this.relativePath = relativePath;
115        }
116    
117        public java.io.File getFileBaseDir() {
118          return basedir;
119        }
120    
121        public java.io.File getFile() {
122          return new java.io.File(basedir, relativePath);
123        }
124    
125        public String getRelativePath() {
126          return relativePath;
127        }
128    
129        @Override
130        public boolean equals(Object o) {
131          if (this == o) {
132            return true;
133          }
134          if (o == null || getClass() != o.getClass()) {
135            return false;
136          }
137          DefaultInputFile that = (DefaultInputFile) o;
138          return basedir.equals(that.basedir) && relativePath.equals(that.relativePath);
139    
140        }
141    
142        @Override
143        public int hashCode() {
144          int result = basedir.hashCode();
145          result = 31 * result + relativePath.hashCode();
146          return result;
147        }
148    
149        @Override
150        public String toString() {
151          return String.format("%s -> %s", basedir.getAbsolutePath(), relativePath);
152        }
153      }
154    }