001/*
002 * SonarQube
003 * Copyright (C) 2009-2016 SonarSource SA
004 * mailto:contact 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   * @deprecated since 4.2 use {@link #fromIOFile(java.io.File, Project)}
049   */
050  @Deprecated
051  public Directory(String relativePathFromSourceDir) {
052    this(relativePathFromSourceDir, null);
053  }
054
055  /**
056   * @deprecated since 4.2 use {@link #fromIOFile(java.io.File, Project)}
057   */
058  @Deprecated
059  public Directory(String relativePathFromSourceDir, Language language) {
060    this.relativePathFromSourceDir = parseKey(relativePathFromSourceDir);
061  }
062
063  /**
064   * Internal.
065   */
066  public String relativePathFromSourceDir() {
067    return relativePathFromSourceDir;
068  }
069
070  @Override
071  public String getName() {
072    return getKey();
073  }
074
075  @Override
076  public String getLongName() {
077    return null;
078  }
079
080  @Override
081  public String getDescription() {
082    return null;
083  }
084
085  @Override
086  public Language getLanguage() {
087    return null;
088  }
089
090  @Override
091  public String getScope() {
092    return Scopes.DIRECTORY;
093  }
094
095  @Override
096  public String getQualifier() {
097    return Qualifiers.DIRECTORY;
098  }
099
100  @Override
101  public Resource getParent() {
102    return null;
103  }
104
105  @Override
106  public boolean matchFilePattern(String antPattern) {
107    WildcardPattern matcher = WildcardPattern.create(antPattern, "/");
108    return matcher.match(getKey());
109  }
110
111  public static String parseKey(String key) {
112    if (StringUtils.isBlank(key)) {
113      return ROOT;
114    }
115    String normalizedKey = key;
116    normalizedKey = normalizedKey.replace('\\', '/');
117    normalizedKey = StringUtils.trim(normalizedKey);
118    normalizedKey = StringUtils.removeStart(normalizedKey, Directory.SEPARATOR);
119    normalizedKey = StringUtils.removeEnd(normalizedKey, Directory.SEPARATOR);
120    return normalizedKey;
121  }
122
123  /**
124   * @since 4.2
125   * @deprecated since 5.1 use {@link FileSystem#inputDir(java.io.File)}
126   */
127  @Deprecated
128  @CheckForNull
129  public static Directory fromIOFile(java.io.File dir, Project module) {
130    String relativePathFromBasedir = new PathResolver().relativePath(module.getBaseDir(), dir);
131    if (relativePathFromBasedir != null) {
132      return Directory.create(relativePathFromBasedir);
133    }
134    return null;
135  }
136
137  /**
138   * Internal use only.
139   * @deprecated since 5.1 use {@link FileSystem#inputDir(java.io.File)}
140   */
141  @Deprecated
142  public static Directory create(String relativePathFromBaseDir) {
143    Directory d = new Directory();
144    String normalizedPath = normalize(relativePathFromBaseDir);
145    d.setKey(normalizedPath == null ? SEPARATOR : normalizedPath);
146    d.setPath(normalizedPath == null ? "" : normalizedPath);
147    return d;
148  }
149
150  @Override
151  public String toString() {
152    return new ToStringBuilder(this)
153      .append("key", getKey())
154      .append("path", getPath())
155      .toString();
156  }
157
158}