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.SensorContext;
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  Directory() {
038    // Used by factory
039  }
040
041  /**
042   * @deprecated since 4.2 use {@link #fromIOFile(java.io.File, Project)}
043   */
044  @Deprecated
045  public Directory(String relativePathFromSourceDir) {
046    this(relativePathFromSourceDir, null);
047  }
048
049  /**
050   * @deprecated since 4.2 use {@link #fromIOFile(java.io.File, Project)}
051   */
052  @Deprecated
053  public Directory(String relativePathFromSourceDir, Language language) {
054    setDeprecatedKey(parseKey(relativePathFromSourceDir));
055  }
056
057  @Override
058  public String getName() {
059    return getKey();
060  }
061
062  @Override
063  public String getLongName() {
064    return null;
065  }
066
067  @Override
068  public String getDescription() {
069    return null;
070  }
071
072  @Override
073  public Language getLanguage() {
074    return null;
075  }
076
077  @Override
078  public String getScope() {
079    return Scopes.DIRECTORY;
080  }
081
082  @Override
083  public String getQualifier() {
084    return Qualifiers.DIRECTORY;
085  }
086
087  @Override
088  public Resource getParent() {
089    return null;
090  }
091
092  @Override
093  public boolean matchFilePattern(String antPattern) {
094    WildcardPattern matcher = WildcardPattern.create(antPattern, "/");
095    return matcher.match(getKey());
096  }
097
098  public static String parseKey(String key) {
099    if (StringUtils.isBlank(key)) {
100      return ROOT;
101    }
102    String normalizedKey = key;
103    normalizedKey = normalizedKey.replace('\\', '/');
104    normalizedKey = StringUtils.trim(normalizedKey);
105    normalizedKey = StringUtils.removeStart(normalizedKey, Directory.SEPARATOR);
106    normalizedKey = StringUtils.removeEnd(normalizedKey, Directory.SEPARATOR);
107    return normalizedKey;
108  }
109
110  /**
111   * Creates a {@link Directory} from an absolute {@link java.io.File} and a module.
112   * The returned {@link Directory} can be then passed for example to
113   * {@link SensorContext#saveMeasure(Resource, org.sonar.api.measures.Measure)}.
114   * @param dir absolute path to a directory
115   * @param module
116   * @return null if the directory is not under module basedir.
117   * @since 4.2
118   */
119  @CheckForNull
120  public static Directory fromIOFile(java.io.File dir, Project module) {
121    String relativePathFromBasedir = new PathResolver().relativePath(module.getFileSystem().getBasedir(), dir);
122    if (relativePathFromBasedir != null) {
123      return Directory.create(relativePathFromBasedir);
124    }
125    return null;
126  }
127
128  /**
129   * Create a Directory that is partially initialized. But that's enough to call for example
130   * {@link SensorContext#saveMeasure(Resource, org.sonar.api.measures.Measure)} when resources are already indexed.
131   * Internal use only.
132   */
133  public static Directory create(String relativePathFromBaseDir) {
134    Directory d = new Directory();
135    String normalizedPath = normalize(relativePathFromBaseDir);
136    d.setKey(normalizedPath == null ? SEPARATOR : normalizedPath);
137    d.setPath(normalizedPath == null ? "" : normalizedPath);
138    return d;
139  }
140
141  /**
142   * Create a directory that is fully initialized. Use for indexing resources.
143   * Internal use only.
144   * @since 4.2
145   */
146  public static Directory create(String relativePathFromBaseDir, String relativePathFromSourceDir) {
147    Directory d = Directory.create(relativePathFromBaseDir);
148    d.setDeprecatedKey(parseKey(relativePathFromSourceDir));
149    return d;
150  }
151
152  @Override
153  public String toString() {
154    return new ToStringBuilder(this)
155      .append("key", getKey())
156      .append("deprecatedKey", getDeprecatedKey())
157      .append("path", getPath())
158      .toString();
159  }
160
161}