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