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.resources;
021    
022    import com.google.common.base.Objects;
023    
024    import com.google.common.collect.Lists;
025    import org.apache.commons.lang.StringUtils;
026    
027    import java.io.BufferedInputStream;
028    import java.io.File;
029    import java.io.FileInputStream;
030    import java.io.FileNotFoundException;
031    import java.io.InputStream;
032    import java.util.Collection;
033    import java.util.List;
034    
035    /**
036     * @since 2.8
037     */
038    public final class InputFileUtils {
039    
040      private InputFileUtils() {
041        // only static methods
042      }
043    
044      /**
045       * @param inputFiles not nullable
046       * @return not null list
047       */
048      public static List<java.io.File> toFiles(Collection<InputFile> inputFiles) {
049        List<java.io.File> files = Lists.newArrayList();
050        for (InputFile inputFile : inputFiles) {
051          files.add(inputFile.getFile());
052        }
053        return files;
054      }
055    
056      /**
057       * Extract the directory from relative path. Examples :
058       * - returns "org/foo" when relative path is "org/foo/Bar.java"
059       * - returns "" when relative path is "Bar.java"
060       */
061      public static String getRelativeDirectory(InputFile inputFile) {
062        String relativePath = inputFile.getRelativePath();
063        if (StringUtils.contains(relativePath, "/")) {
064          return StringUtils.substringBeforeLast(relativePath, "/");
065        }
066        return "";
067      }
068    
069      /**
070       * For internal and for testing purposes. Please use the FileSystem component to access files.
071       */
072      public static InputFile create(java.io.File basedir, java.io.File file) {
073        String relativePath = getRelativePath(basedir, file);
074        if (relativePath != null) {
075          return create(basedir, relativePath);
076        }
077        return null;
078      }
079    
080      /**
081       * For internal and for testing purposes. Please use the FileSystem component to access files.
082       */
083      public static InputFile create(java.io.File basedir, String relativePath) {
084        return new DefaultInputFile(basedir, relativePath);
085      }
086    
087      /**
088       * For internal and for testing purposes. Please use the FileSystem component to access files.
089       */
090      public static List<InputFile> create(java.io.File basedir, Collection<java.io.File> files) {
091        List<InputFile> inputFiles = Lists.newArrayList();
092        for (File file : files) {
093          InputFile inputFile = create(basedir, file);
094          if (inputFile != null) {
095            inputFiles.add(inputFile);
096          }
097        }
098        return inputFiles;
099      }
100    
101      static String getRelativePath(java.io.File basedir, java.io.File file) {
102        List<String> stack = Lists.newArrayList(file.getName());
103        java.io.File cursor = file.getParentFile();
104        while (cursor != null) {
105          if (basedir.equals(cursor)) {
106            return StringUtils.join(stack, "/");
107          }
108          stack.add(0, cursor.getName());
109          cursor = cursor.getParentFile();
110        }
111        return null;
112      }
113    
114      static final class DefaultInputFile implements InputFile {
115        private java.io.File basedir;
116        private String relativePath;
117    
118        DefaultInputFile(java.io.File basedir, String relativePath) {
119          this.basedir = basedir;
120          this.relativePath = relativePath;
121        }
122    
123        public java.io.File getFileBaseDir() {
124          return basedir;
125        }
126    
127        public java.io.File getFile() {
128          return new java.io.File(basedir, relativePath);
129        }
130    
131        /**
132         * @since 3.1
133         */
134        public InputStream getInputStream() throws FileNotFoundException {
135          return new BufferedInputStream(new FileInputStream(getFile()));
136        }
137    
138        public String getRelativePath() {
139          return relativePath;
140        }
141    
142        @Override
143        public boolean equals(Object o) {
144          if (this == o) {
145            return true;
146          }
147          if (o instanceof DefaultInputFile) {
148            DefaultInputFile that = (DefaultInputFile) o;
149            return Objects.equal(basedir, that.basedir) && Objects.equal(relativePath, that.relativePath);
150          }
151          return false;
152        }
153    
154        @Override
155        public int hashCode() {
156          int result = basedir.hashCode();
157          result = 31 * result + relativePath.hashCode();
158          return result;
159        }
160    
161        @Override
162        public String toString() {
163          return String.format("%s -> %s", basedir.getAbsolutePath(), relativePath);
164        }
165      }
166    }