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.nio.file.Files; 026import java.nio.file.Path; 027 028import javax.annotation.CheckForNull; 029import javax.annotation.Nullable; 030import javax.annotation.concurrent.Immutable; 031 032import org.sonar.api.batch.fs.IndexedFile; 033import org.sonar.api.batch.fs.InputFile.Type; 034import org.sonar.api.utils.PathUtils; 035 036/** 037 * @since 6.3 038 */ 039@Immutable 040public class DefaultIndexedFile extends DefaultInputComponent implements IndexedFile { 041 private final String relativePath; 042 private final String moduleKey; 043 private final Path moduleBaseDir; 044 private final String language; 045 private final Type type; 046 private final Path path; 047 048 /** 049 * Testing purposes only! 050 */ 051 public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath, @Nullable String language) { 052 this(moduleKey, moduleBaseDir, relativePath, language, TestInputFileBuilder.nextBatchId()); 053 } 054 055 public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath, @Nullable String language, int batchId) { 056 this(moduleKey, moduleBaseDir, relativePath, Type.MAIN, language, batchId); 057 } 058 059 public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath, Type type, @Nullable String language, int batchId) { 060 super(batchId); 061 this.moduleKey = moduleKey; 062 this.relativePath = PathUtils.sanitize(relativePath); 063 this.moduleBaseDir = moduleBaseDir.normalize(); 064 this.type = type; 065 this.language = language; 066 this.path = this.moduleBaseDir.resolve(this.relativePath); 067 } 068 069 @Override 070 public String relativePath() { 071 return relativePath; 072 } 073 074 @Override 075 public String absolutePath() { 076 return PathUtils.sanitize(path().toString()); 077 } 078 079 @Override 080 public File file() { 081 return path().toFile(); 082 } 083 084 @Override 085 public Path path() { 086 return path; 087 } 088 089 @Override 090 public InputStream inputStream() throws IOException { 091 return Files.newInputStream(path()); 092 } 093 094 @CheckForNull 095 @Override 096 public String language() { 097 return language; 098 } 099 100 @Override 101 public Type type() { 102 return type; 103 } 104 105 /** 106 * Component key (without branch). 107 */ 108 @Override 109 public String key() { 110 return new StringBuilder().append(moduleKey).append(":").append(relativePath).toString(); 111 } 112 113 public String moduleKey() { 114 return moduleKey; 115 } 116 117 @Override 118 public boolean equals(Object o) { 119 if (this == o) { 120 return true; 121 } 122 123 if (!(o instanceof DefaultIndexedFile)) { 124 return false; 125 } 126 127 DefaultIndexedFile that = (DefaultIndexedFile) o; 128 return moduleKey.equals(that.moduleKey) && relativePath.equals(that.relativePath); 129 } 130 131 @Override 132 public int hashCode() { 133 return moduleKey.hashCode() + relativePath.hashCode() * 13; 134 } 135 136 @Override 137 public String toString() { 138 return "[moduleKey=" + moduleKey + ", relative=" + relativePath + ", basedir=" + moduleBaseDir + "]"; 139 } 140 141 @Override 142 public boolean isFile() { 143 return true; 144 } 145}