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