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