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