001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info 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.batch.fs.internal;
021
022import java.io.File;
023import java.io.IOException;
024import java.io.InputStream;
025import java.net.URI;
026import java.nio.file.Files;
027import java.nio.file.Path;
028import javax.annotation.CheckForNull;
029import javax.annotation.Nullable;
030import javax.annotation.concurrent.Immutable;
031import org.sonar.api.batch.fs.IndexedFile;
032import org.sonar.api.batch.fs.InputFile.Type;
033import org.sonar.api.utils.PathUtils;
034
035/**
036 * @since 6.3
037 */
038@Immutable
039public class DefaultIndexedFile extends DefaultInputComponent implements IndexedFile {
040  private final String projectRelativePath;
041  private final String moduleRelativePath;
042  private final String moduleKey;
043  private final String language;
044  private final Type type;
045  private final Path absolutePath;
046  private final SensorStrategy sensorStrategy;
047
048  /**
049   * Testing purposes only!
050   */
051  public DefaultIndexedFile(String moduleKey, Path baseDir, String relativePath, @Nullable String language) {
052    this(baseDir.resolve(relativePath), moduleKey, PathUtils.sanitize(relativePath), PathUtils.sanitize(relativePath), Type.MAIN, language, TestInputFileBuilder.nextBatchId(),
053      new SensorStrategy());
054  }
055
056  public DefaultIndexedFile(Path absolutePath, String moduleKey, String projectRelativePath, String moduleRelativePath, Type type, @Nullable String language, int batchId,
057    SensorStrategy sensorStrategy) {
058    super(batchId);
059    this.moduleKey = moduleKey;
060    this.projectRelativePath = projectRelativePath;
061    this.moduleRelativePath = moduleRelativePath;
062    this.type = type;
063    this.language = language;
064    this.sensorStrategy = sensorStrategy;
065    this.absolutePath = absolutePath;
066  }
067
068  @Override
069  public String relativePath() {
070    return sensorStrategy.isGlobal() ? projectRelativePath : moduleRelativePath;
071  }
072
073  public String getModuleRelativePath() {
074    return moduleRelativePath;
075  }
076
077  public String getProjectRelativePath() {
078    return projectRelativePath;
079  }
080
081  @Override
082  public String absolutePath() {
083    return PathUtils.sanitize(path().toString());
084  }
085
086  @Override
087  public File file() {
088    return path().toFile();
089  }
090
091  @Override
092  public Path path() {
093    return absolutePath;
094  }
095
096  @Override
097  public InputStream inputStream() throws IOException {
098    return Files.newInputStream(path());
099  }
100
101  @CheckForNull
102  @Override
103  public String language() {
104    return language;
105  }
106
107  @Override
108  public Type type() {
109    return type;
110  }
111
112  /**
113   * Component key (without branch).
114   */
115  @Override
116  public String key() {
117    return new StringBuilder().append(moduleKey).append(":").append(moduleRelativePath).toString();
118  }
119
120  public String moduleKey() {
121    return moduleKey;
122  }
123
124  @Override
125  public boolean equals(Object o) {
126    if (this == o) {
127      return true;
128    }
129
130    if (!(o instanceof DefaultIndexedFile)) {
131      return false;
132    }
133
134    DefaultIndexedFile that = (DefaultIndexedFile) o;
135    return projectRelativePath.equals(that.projectRelativePath);
136  }
137
138  @Override
139  public int hashCode() {
140    return projectRelativePath.hashCode();
141  }
142
143  @Override
144  public String toString() {
145    return projectRelativePath;
146  }
147
148  @Override
149  public boolean isFile() {
150    return true;
151  }
152
153  @Override
154  public String filename() {
155    return path().getFileName().toString();
156  }
157
158  @Override
159  public URI uri() {
160    return path().toUri();
161  }
162}