1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.api.resources;
import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
/**
* @since 2.8
* @deprecated since 5.0 as {@link InputFile} is deprecated
*/
@Deprecated
public final class InputFileUtils {
private InputFileUtils() {
// only static methods
}
/**
* @param inputFiles not nullable
* @return not null list
*/
public static List<java.io.File> toFiles(Collection<InputFile> inputFiles) {
List<java.io.File> files = Lists.newArrayList();
for (InputFile inputFile : inputFiles) {
files.add(inputFile.getFile());
}
return files;
}
/**
* Extract the directory from relative path. Examples :
* - returns "org/foo" when relative path is "org/foo/Bar.java"
* - returns "" when relative path is "Bar.java"
*/
public static String getRelativeDirectory(InputFile inputFile) {
String relativePath = inputFile.getRelativePath();
if (StringUtils.contains(relativePath, "/")) {
return StringUtils.substringBeforeLast(relativePath, "/");
}
return "";
}
/**
* For internal and for testing purposes. Please use the FileSystem component to access files.
*/
public static InputFile create(java.io.File basedir, java.io.File file) {
String relativePath = getRelativePath(basedir, file);
if (relativePath != null) {
return create(basedir, relativePath);
}
return null;
}
/**
* For internal and for testing purposes. Please use the FileSystem component to access files.
*/
public static InputFile create(java.io.File basedir, String relativePath) {
return new DefaultInputFile(basedir, relativePath);
}
/**
* For internal and for testing purposes. Please use the FileSystem component to access files.
*/
public static List<InputFile> create(java.io.File basedir, Collection<java.io.File> files) {
List<InputFile> inputFiles = Lists.newArrayList();
for (File file : files) {
InputFile inputFile = create(basedir, file);
if (inputFile != null) {
inputFiles.add(inputFile);
}
}
return inputFiles;
}
static String getRelativePath(java.io.File basedir, java.io.File file) {
List<String> stack = Lists.newArrayList(file.getName());
java.io.File cursor = file.getParentFile();
while (cursor != null) {
if (basedir.equals(cursor)) {
return StringUtils.join(stack, "/");
}
stack.add(0, cursor.getName());
cursor = cursor.getParentFile();
}
return null;
}
static final class DefaultInputFile implements InputFile {
private java.io.File basedir;
private String relativePath;
DefaultInputFile(java.io.File basedir, String relativePath) {
this.basedir = basedir;
this.relativePath = relativePath;
}
@Override
public java.io.File getFileBaseDir() {
return basedir;
}
@Override
public java.io.File getFile() {
return new java.io.File(basedir, relativePath);
}
/**
* @since 3.1
*/
@Override
public InputStream getInputStream() throws FileNotFoundException {
return new BufferedInputStream(new FileInputStream(getFile()));
}
@Override
public String getRelativePath() {
return relativePath;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o instanceof DefaultInputFile) {
DefaultInputFile that = (DefaultInputFile) o;
return Objects.equal(basedir, that.basedir) && Objects.equal(relativePath, that.relativePath);
}
return false;
}
@Override
public int hashCode() {
int result = basedir.hashCode();
result = 31 * result + relativePath.hashCode();
return result;
}
@Override
public String toString() {
return String.format("%s -> %s", basedir.getAbsolutePath(), relativePath);
}
}
}
|