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 */ 020package org.sonar.api.batch.fs.internal; 021 022import org.sonar.api.batch.fs.InputFile; 023import org.sonar.api.utils.PathUtils; 024 025import javax.annotation.CheckForNull; 026import javax.annotation.Nullable; 027 028import java.io.File; 029import java.nio.charset.Charset; 030import java.nio.file.Path; 031 032/** 033 * @since 4.2 034 */ 035public class DefaultInputFile implements InputFile { 036 037 private final String relativePath; 038 private final String moduleKey; 039 protected Path moduleBaseDir; 040 private String language; 041 private Type type = Type.MAIN; 042 private Status status; 043 private int lines; 044 private Charset charset; 045 private int lastValidOffset; 046 047 public DefaultInputFile(String moduleKey, String relativePath) { 048 this.moduleKey = moduleKey; 049 this.relativePath = PathUtils.sanitize(relativePath); 050 } 051 052 @Override 053 public String relativePath() { 054 return relativePath; 055 } 056 057 @Override 058 public String absolutePath() { 059 return PathUtils.sanitize(path().toString()); 060 } 061 062 @Override 063 public File file() { 064 return path().toFile(); 065 } 066 067 @Override 068 public Path path() { 069 if (moduleBaseDir == null) { 070 throw new IllegalStateException("Can not return the java.nio.file.Path because module baseDir is not set (see method setModuleBaseDir(java.io.File))"); 071 } 072 return moduleBaseDir.resolve(relativePath); 073 } 074 075 @CheckForNull 076 @Override 077 public String language() { 078 return language; 079 } 080 081 @Override 082 public Type type() { 083 return type; 084 } 085 086 /** 087 * {@link #setStatus(org.sonar.api.batch.fs.InputFile.Status)} 088 */ 089 @Override 090 public Status status() { 091 return status; 092 } 093 094 @Override 095 public int lines() { 096 return lines; 097 } 098 099 /** 100 * Component key. 101 */ 102 public String key() { 103 return new StringBuilder().append(moduleKey).append(":").append(relativePath).toString(); 104 } 105 106 public String moduleKey() { 107 return moduleKey; 108 } 109 110 public Charset charset() { 111 return charset; 112 } 113 114 /** 115 * For testing purpose. Will be automaticall set when file is added to {@link DefaultFileSystem} 116 */ 117 public DefaultInputFile setModuleBaseDir(Path moduleBaseDir) { 118 this.moduleBaseDir = moduleBaseDir.normalize(); 119 return this; 120 } 121 122 public DefaultInputFile setLanguage(@Nullable String language) { 123 this.language = language; 124 return this; 125 } 126 127 public DefaultInputFile setType(Type type) { 128 this.type = type; 129 return this; 130 } 131 132 public DefaultInputFile setStatus(Status status) { 133 this.status = status; 134 return this; 135 } 136 137 public DefaultInputFile setLines(int lines) { 138 this.lines = lines; 139 return this; 140 } 141 142 public DefaultInputFile setCharset(Charset charset) { 143 this.charset = charset; 144 return this; 145 } 146 147 public int lastValidOffset() { 148 return lastValidOffset; 149 } 150 151 public DefaultInputFile setLastValidOffset(int lastValidOffset) { 152 this.lastValidOffset = lastValidOffset; 153 return this; 154 } 155 156 @Override 157 public boolean equals(Object o) { 158 if (this == o) { 159 return true; 160 } 161 162 // Use instanceof to support DeprecatedDefaultInputFile 163 if (!(o instanceof DefaultInputFile)) { 164 return false; 165 } 166 167 DefaultInputFile that = (DefaultInputFile) o; 168 return moduleKey.equals(that.moduleKey) && relativePath.equals(that.relativePath); 169 } 170 171 @Override 172 public int hashCode() { 173 return moduleKey.hashCode() + relativePath.hashCode() * 13; 174 } 175 176 @Override 177 public String toString() { 178 return "[moduleKey=" + moduleKey + ", relative=" + relativePath + ", basedir=" + moduleBaseDir + "]"; 179 } 180 181}