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 */ 020package org.sonar.api.scan.filesystem.internal; 021 022import org.apache.commons.io.FilenameUtils; 023import org.apache.commons.lang.StringUtils; 024import org.sonar.api.utils.PathUtils; 025 026import javax.annotation.CheckForNull; 027 028import java.io.File; 029import java.util.Map; 030 031/** 032 * PLUGINS MUST NOT USE THIS CLASS, EVEN FOR UNIT TESTING. 033 * 034 * @since 4.0 035 */ 036public class DefaultInputFile implements InputFile { 037 038 /** 039 * We're not sure that this is the correct way, so not in API yet. 040 */ 041 public static final String ATTRIBUTE_COMPONENT_KEY = "CMP_KEY"; 042 043 private final String absolutePath; 044 private final String path; 045 private final Map<String, String> attributes; 046 047 private DefaultInputFile(File file, String path, Map<String, String> attributes) { 048 this.absolutePath = PathUtils.canonicalPath(file); 049 this.path = FilenameUtils.separatorsToUnix(path); 050 this.attributes = attributes; 051 } 052 053 /** 054 * Plugins must not build their own instances of {@link InputFile}. 055 * {@link org.sonar.api.scan.filesystem.ModuleFileSystem} must be used to search for files to scan. 056 * <p/> 057 * Usage: <code>InputFile.create(file, "src/main/java/com/Foo.java", attributes)</code> 058 */ 059 public static DefaultInputFile create(File file, String path, Map<String, String> attributes) { 060 return new DefaultInputFile(file, path, attributes); 061 } 062 063 @Override 064 public String path() { 065 return path; 066 } 067 068 @Override 069 public String absolutePath() { 070 return absolutePath; 071 } 072 073 @Override 074 public File file() { 075 return new File(absolutePath); 076 } 077 078 @Override 079 public String name() { 080 return file().getName(); 081 } 082 083 @Override 084 public String type() { 085 return attribute(ATTRIBUTE_TYPE); 086 } 087 088 @Override 089 public boolean has(String attribute, String value) { 090 return StringUtils.equals(attributes.get(attribute), value); 091 } 092 093 @Override 094 @CheckForNull 095 public String attribute(String key) { 096 return attributes.get(key); 097 } 098 099 @Override 100 public Map<String, String> attributes() { 101 return attributes; 102 } 103 104 @Override 105 public boolean equals(Object o) { 106 if (this == o) { 107 return true; 108 } 109 if (o == null || getClass() != o.getClass()) { 110 return false; 111 } 112 DefaultInputFile other = (DefaultInputFile) o; 113 return absolutePath.equals(other.absolutePath); 114 } 115 116 @Override 117 public int hashCode() { 118 return absolutePath.hashCode(); 119 } 120 121 @Override 122 public String toString() { 123 return String.format("[%s,%s]", path, type()); 124 } 125}