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