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