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.batch.fs.internal;
021
022 import org.apache.commons.io.FilenameUtils;
023 import org.sonar.api.batch.fs.InputFile;
024 import org.sonar.api.utils.PathUtils;
025
026 import javax.annotation.CheckForNull;
027 import java.io.*;
028
029 /**
030 * @since 4.2
031 */
032 public class DefaultInputFile implements InputFile, org.sonar.api.resources.InputFile, Serializable {
033
034 private final String relativePath;
035 private String absolutePath;
036 private String language;
037 private Type type = Type.MAIN;
038 private Status status;
039 private String hash;
040 private int lines;
041 private String key;
042 private String deprecatedKey;
043 private String sourceDirAbsolutePath;
044 private String pathRelativeToSourceDir;
045 private String basedir;
046
047 public DefaultInputFile(String relativePath) {
048 this.relativePath = FilenameUtils.normalize(relativePath, true);
049 }
050
051 @Override
052 public String relativePath() {
053 return relativePath;
054 }
055
056 /**
057 * Marked as nullable just for the unit tests that do not call {@link #setFile(java.io.File)}
058 * previously.
059 */
060 @Override
061 @CheckForNull
062 public String absolutePath() {
063 return absolutePath;
064 }
065
066 @Override
067 public File file() {
068 if (absolutePath == null) {
069 throw new IllegalStateException("Can not return the java.io.File because absolute path is not set (see method setFile(java.io.File))");
070 }
071 return new File(absolutePath);
072 }
073
074 /**
075 * Marked as nullable just for the unit tests that do not call {@link #setLanguage(String)}
076 * previously.
077 */
078 @CheckForNull
079 @Override
080 public String language() {
081 return language;
082 }
083
084 @Override
085 public Type type() {
086 return type;
087 }
088
089 /**
090 * Marked as nullable just for the unit tests that do not previously call
091 * {@link #setStatus(org.sonar.api.batch.fs.InputFile.Status)}
092 */
093 @CheckForNull
094 @Override
095 public Status status() {
096 return status;
097 }
098
099 /**
100 * Digest hash of the file. Marked as nullable just for the unit tests
101 * that do not previously call {@link #setHash(String)}
102 */
103 @CheckForNull
104 public String hash() {
105 return hash;
106 }
107
108 @Override
109 public int lines() {
110 return lines;
111 }
112
113 /**
114 * Component key. It's marked as nullable just for the unit tests that
115 * do not previously call {@link #setKey(String)}.
116 */
117 @CheckForNull
118 public String key() {
119 return key;
120 }
121
122 public DefaultInputFile setAbsolutePath(String s) {
123 this.absolutePath = FilenameUtils.normalize(s, true);
124 return this;
125 }
126
127 public DefaultInputFile setLanguage(String language) {
128 this.language = language;
129 return this;
130 }
131
132 public DefaultInputFile setFile(File file) {
133 setAbsolutePath(file.getAbsolutePath());
134 return this;
135 }
136
137 public DefaultInputFile setType(Type type) {
138 this.type = type;
139 return this;
140 }
141
142 public DefaultInputFile setStatus(Status status) {
143 this.status = status;
144 return this;
145 }
146
147 public DefaultInputFile setHash(String hash) {
148 this.hash = hash;
149 return this;
150 }
151
152 public DefaultInputFile setLines(int lines) {
153 this.lines = lines;
154 return this;
155 }
156
157 public DefaultInputFile setKey(String s) {
158 this.key = s;
159 return this;
160 }
161
162 /**
163 * Key used before version 4.2. It can be different than {@link #key} on Java files.
164 */
165 public String deprecatedKey() {
166 return deprecatedKey;
167 }
168
169 public DefaultInputFile setDeprecatedKey(String s) {
170 this.deprecatedKey = s;
171 return this;
172 }
173
174 /**
175 * Used only for backward-compatibility. Meaningless since version 4.2.
176 */
177 public String sourceDirAbsolutePath() {
178 return sourceDirAbsolutePath;
179 }
180
181 public DefaultInputFile setSourceDirAbsolutePath(String s) {
182 this.sourceDirAbsolutePath = FilenameUtils.normalize(s, true);
183 return this;
184 }
185
186 /**
187 * Used only for backward-compatibility. Meaningless since version 4.2.
188 */
189
190 public String pathRelativeToSourceDir() {
191 return pathRelativeToSourceDir;
192 }
193
194 public DefaultInputFile setPathRelativeToSourceDir(String s) {
195 this.pathRelativeToSourceDir = FilenameUtils.normalize(s, true);
196 return this;
197 }
198
199 /**
200 * @deprecated in 4.2. Replaced by {@link org.sonar.api.batch.fs.FileSystem#baseDir()}
201 */
202 @Deprecated
203 @Override
204 public File getFileBaseDir() {
205 return new File(basedir);
206 }
207
208 public void setBasedir(File basedir) {
209 this.basedir = PathUtils.sanitize(basedir.getAbsolutePath());
210 }
211
212 /**
213 * @deprecated in 4.2. Use {@link #file()}
214 */
215 @Deprecated
216 @Override
217 public File getFile() {
218 return file();
219 }
220
221 /**
222 * @deprecated in 4.2. Use {@link #relativePath()}
223 */
224 @Deprecated
225 @Override
226 public String getRelativePath() {
227 return pathRelativeToSourceDir;
228 }
229
230 @Override
231 public InputStream getInputStream() throws FileNotFoundException {
232 return new BufferedInputStream(new FileInputStream(file()));
233 }
234
235 @Override
236 public boolean equals(Object o) {
237 if (this == o) {
238 return true;
239 }
240 if (o == null || getClass() != o.getClass()) {
241 return false;
242 }
243
244 DefaultInputFile that = (DefaultInputFile) o;
245 return relativePath.equals(that.relativePath);
246 }
247
248 @Override
249 public int hashCode() {
250 return relativePath.hashCode();
251 }
252
253 @Override
254 public String toString() {
255 return "[relative=" + relativePath + ", abs=" + absolutePath + "]";
256 }
257 }
258
259
260