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.resources; 021 022import javax.annotation.CheckForNull; 023import org.apache.commons.lang.StringUtils; 024import org.apache.commons.lang.builder.ToStringBuilder; 025import org.sonar.api.batch.fs.FileSystem; 026import org.sonar.api.scan.filesystem.PathResolver; 027import org.sonar.api.utils.WildcardPattern; 028 029/** 030 * @since 1.10 031 */ 032public class Directory extends Resource { 033 034 public static final String SEPARATOR = "/"; 035 public static final String ROOT = "[root]"; 036 037 private final String relativePathFromSourceDir; 038 039 Directory() { 040 // Used by factory 041 this.relativePathFromSourceDir = null; 042 } 043 044 /** 045 * @deprecated since 4.2 use {@link #fromIOFile(java.io.File, Project)} 046 */ 047 @Deprecated 048 public Directory(String relativePathFromSourceDir) { 049 this(relativePathFromSourceDir, null); 050 } 051 052 /** 053 * @deprecated since 4.2 use {@link #fromIOFile(java.io.File, Project)} 054 */ 055 @Deprecated 056 public Directory(String relativePathFromSourceDir, Language language) { 057 this.relativePathFromSourceDir = parseKey(relativePathFromSourceDir); 058 } 059 060 /** 061 * Internal. 062 */ 063 public String relativePathFromSourceDir() { 064 return relativePathFromSourceDir; 065 } 066 067 @Override 068 public String getName() { 069 return getKey(); 070 } 071 072 @Override 073 public String getLongName() { 074 return null; 075 } 076 077 @Override 078 public String getDescription() { 079 return null; 080 } 081 082 @Override 083 public Language getLanguage() { 084 return null; 085 } 086 087 @Override 088 public String getScope() { 089 return Scopes.DIRECTORY; 090 } 091 092 @Override 093 public String getQualifier() { 094 return Qualifiers.DIRECTORY; 095 } 096 097 @Override 098 public Resource getParent() { 099 return null; 100 } 101 102 @Override 103 public boolean matchFilePattern(String antPattern) { 104 WildcardPattern matcher = WildcardPattern.create(antPattern, "/"); 105 return matcher.match(getKey()); 106 } 107 108 public static String parseKey(String key) { 109 if (StringUtils.isBlank(key)) { 110 return ROOT; 111 } 112 String normalizedKey = key; 113 normalizedKey = normalizedKey.replace('\\', '/'); 114 normalizedKey = StringUtils.trim(normalizedKey); 115 normalizedKey = StringUtils.removeStart(normalizedKey, Directory.SEPARATOR); 116 normalizedKey = StringUtils.removeEnd(normalizedKey, Directory.SEPARATOR); 117 return normalizedKey; 118 } 119 120 /** 121 * @since 4.2 122 * @deprecated since 5.1 use {@link FileSystem#inputDir(java.io.File)} 123 */ 124 @Deprecated 125 @CheckForNull 126 public static Directory fromIOFile(java.io.File dir, Project module) { 127 String relativePathFromBasedir = new PathResolver().relativePath(module.getBaseDir(), dir); 128 if (relativePathFromBasedir != null) { 129 return Directory.create(relativePathFromBasedir); 130 } 131 return null; 132 } 133 134 /** 135 * Internal use only. 136 * @deprecated since 5.1 use {@link FileSystem#inputDir(java.io.File)} 137 */ 138 @Deprecated 139 public static Directory create(String relativePathFromBaseDir) { 140 Directory d = new Directory(); 141 String normalizedPath = normalize(relativePathFromBaseDir); 142 d.setKey(normalizedPath == null ? SEPARATOR : normalizedPath); 143 d.setPath(normalizedPath == null ? "" : normalizedPath); 144 return d; 145 } 146 147 @Override 148 public String toString() { 149 return new ToStringBuilder(this) 150 .append("key", getKey()) 151 .append("path", getPath()) 152 .toString(); 153 } 154 155}