001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2013 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     */
020    package org.sonar.api.resources;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.apache.commons.lang.builder.ToStringBuilder;
024    import org.sonar.api.utils.WildcardPattern;
025    
026    /**
027     * @since 1.10
028     */
029    public class Directory extends Resource {
030    
031      public static final String SEPARATOR = "/";
032      public static final String ROOT = "[root]";
033    
034      private Language language;
035    
036      public Directory(String key) {
037        this(key, null);
038      }
039    
040      public Directory(String key, Language language) {
041        setKey(parseKey(key));
042        this.language = language;
043      }
044    
045      @Override
046      public String getName() {
047        return getKey();
048      }
049    
050      @Override
051      public String getLongName() {
052        return null;
053      }
054    
055      @Override
056      public String getDescription() {
057        return null;
058      }
059    
060      @Override
061      public Language getLanguage() {
062        return language;
063      }
064    
065      @Override
066      public String getScope() {
067        return Scopes.DIRECTORY;
068      }
069    
070      @Override
071      public String getQualifier() {
072        return Qualifiers.DIRECTORY;
073      }
074    
075      @Override
076      public Resource getParent() {
077        return null;
078      }
079    
080      @Override
081      public boolean matchFilePattern(String antPattern) {
082        WildcardPattern matcher = WildcardPattern.create(antPattern, "/");
083        return matcher.match(getKey());
084      }
085    
086      public static String parseKey(String key) {
087        if (StringUtils.isBlank(key)) {
088          return ROOT;
089        }
090    
091        key = key.replace('\\', '/');
092        key = StringUtils.trim(key);
093        key = StringUtils.removeStart(key, Directory.SEPARATOR);
094        key = StringUtils.removeEnd(key, Directory.SEPARATOR);
095        return key;
096      }
097    
098      @Override
099      public String toString() {
100        return new ToStringBuilder(this)
101            .append("key", getKey())
102            .append("language", language)
103            .toString();
104      }
105    }