001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar 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     * Sonar 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
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
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     * A class that represents a Java package in Sonar
028     *
029     * @since 1.10
030     */
031    public class JavaPackage extends Resource {
032    
033      /**
034       * Default package name for classes without package definition
035       */
036      public static final String DEFAULT_PACKAGE_NAME = "[default]";
037    
038      /**
039       * Defaul constructor
040       */
041      public JavaPackage() {
042        this(null);
043      }
044    
045      /**
046       * Creates a JavaPackage from its key. Will use DEFAULT_PACKAGE_NAME if key is null
047       */
048      public JavaPackage(String key) {
049        setKey(StringUtils.defaultIfEmpty(StringUtils.trim(key), DEFAULT_PACKAGE_NAME));
050      }
051    
052      /**
053       * @return whether the JavaPackage key is the defult key
054       */
055      public boolean isDefault() {
056        return StringUtils.equals(getKey(), DEFAULT_PACKAGE_NAME);
057      }
058    
059      /**
060       * {@inheritDoc}
061       */
062      public boolean matchFilePattern(String antPattern) {
063        String patternWithoutFileSuffix = StringUtils.substringBeforeLast(antPattern, ".");
064        WildcardPattern matcher = WildcardPattern.create(patternWithoutFileSuffix, ".");
065        return matcher.match(getKey());
066      }
067    
068      /**
069       * {@inheritDoc}
070       */
071      public String getDescription() {
072        return null;
073      }
074    
075      /**
076       * @return SCOPE_SPACE
077       */
078      public String getScope() {
079        return Resource.SCOPE_SPACE;
080      }
081    
082      /**
083       * @return QUALIFIER_PACKAGE
084       */
085      public String getQualifier() {
086        return Resource.QUALIFIER_PACKAGE;
087      }
088    
089      /**
090       * {@inheritDoc}
091       */
092      public String getName() {
093        return getKey();
094      }
095    
096      /**
097       * {@inheritDoc}
098       */
099      public Resource<?> getParent() {
100        return null;
101      }
102    
103      /**
104       * {@inheritDoc}
105       */
106      public String getLongName() {
107        return null;
108      }
109    
110      /**
111       * @return Java
112       */
113      public Language getLanguage() {
114        return Java.INSTANCE;
115      }
116    
117      @Override
118      public String toString() {
119        return new ToStringBuilder(this)
120          .append("id", getId())  
121          .append("key", getKey())
122          .toString();
123      }
124    }