001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2009 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar 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 * Sonar 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 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.api.resources; 021 022 import org.apache.commons.lang.StringUtils; 023 import org.sonar.api.utils.WildcardPattern; 024 025 import java.util.List; 026 027 /** 028 * @since 1.10 029 */ 030 public class File extends AbstractResource<Directory> { 031 032 private String directoryKey; 033 034 /** 035 * File in project. Key is the path relative to project source directories. It is not the absolute path 036 * and it does not include the path to source directories. Example : <code>new File("org/sonar/foo.sql")</code>. The 037 * absolute path may be c:/myproject/src/main/sql/org/sonar/foo.sql. Project root is c:/myproject and source dir 038 * is src/main/sql. 039 */ 040 public File(String key) { 041 super(Resource.SCOPE_ENTITY, Resource.QUALIFIER_FILE); 042 key = parseKey(key); 043 if (key == null) { 044 throw new IllegalArgumentException("File key is null"); 045 } 046 if (key.indexOf(Directory.SEPARATOR) >= 0) { 047 this.directoryKey = Directory.parseKey(StringUtils.substringBeforeLast(key, Directory.SEPARATOR)); 048 String filename = StringUtils.substringAfterLast(key, Directory.SEPARATOR); 049 setKey(this.directoryKey + Directory.SEPARATOR + filename); 050 setName(filename); 051 052 } else { 053 setKey(key); 054 setName(key); 055 } 056 } 057 058 public File(String directory, String filename) { 059 super(Resource.SCOPE_ENTITY, Resource.QUALIFIER_FILE); 060 filename = StringUtils.trim(filename); 061 if (StringUtils.isBlank(directory)) { 062 setKey(filename); 063 064 } else { 065 this.directoryKey = Directory.parseKey(directory); 066 setKey(directoryKey + Directory.SEPARATOR + filename); 067 } 068 setName(filename); 069 } 070 071 public File(Language language, String key) { 072 this(key); 073 setLanguage(language); 074 } 075 076 public File(Language language, String directory, String filename) { 077 this(directory, filename); 078 setLanguage(language); 079 } 080 081 @Override 082 public Directory getParent() { 083 return new Directory(directoryKey); 084 } 085 086 private static String parseKey(String key) { 087 if (StringUtils.isBlank(key)) { 088 return null; 089 } 090 091 key = key.replace('\\', '/'); 092 key = StringUtils.trim(key); 093 return key; 094 } 095 096 public boolean matchFilePattern(String antPattern) { 097 WildcardPattern matcher = WildcardPattern.create(antPattern, "/"); 098 return matcher.match(getKey()); 099 } 100 101 public static File fromIOFile(java.io.File file, List<java.io.File> sourceDirs) { 102 String relativePath = DefaultProjectFileSystem.getRelativePath(file, sourceDirs); 103 if (relativePath != null) { 104 return new File(relativePath); 105 } 106 return null; 107 } 108 109 public static File fromIOFile(java.io.File file, Project project) { 110 return fromIOFile(file, project.getFileSystem().getSourceDirs()); 111 } 112 }