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 org.apache.commons.lang.StringUtils;
023import org.apache.commons.lang.builder.ToStringBuilder;
024import org.sonar.api.batch.fs.FileSystem;
025import org.sonar.api.batch.fs.InputFile;
026import org.sonar.api.utils.WildcardPattern;
027
028/**
029 * @since 1.10
030 * @deprecated since 5.6 replaced by {@link InputFile}.
031 */
032@Deprecated
033public class File extends Resource {
034
035  public static final String SCOPE = Scopes.FILE;
036
037  private String filename;
038  private Language language;
039  private Directory parent;
040  private String qualifier = Qualifiers.FILE;
041
042  protected File() {
043    // Used by factory method
044  }
045
046  /**
047   * {@inheritDoc}
048   *
049   * @see Resource#getParent()
050   */
051  @Override
052  public Directory getParent() {
053    return parent;
054  }
055
056  /**
057   * {@inheritDoc}
058   *
059   * @see Resource#matchFilePattern(String)
060   */
061  @Override
062  public boolean matchFilePattern(String antPattern) {
063    WildcardPattern matcher = WildcardPattern.create(antPattern, Directory.SEPARATOR);
064    return matcher.match(getKey());
065  }
066
067  /**
068   * {@inheritDoc}
069   *
070   * @see Resource#getName()
071   */
072  @Override
073  public String getName() {
074    return filename;
075  }
076
077  /**
078   * {@inheritDoc}
079   *
080   * @see Resource#getLongName()
081   */
082  @Override
083  public String getLongName() {
084    return StringUtils.defaultIfBlank(getPath(), getKey());
085  }
086
087  /**
088   * {@inheritDoc}
089   *
090   * @see Resource#getDescription()
091   */
092  @Override
093  public String getDescription() {
094    return null;
095  }
096
097  /**
098   * {@inheritDoc}
099   *
100   * @see Resource#getLanguage()
101   */
102  @Override
103  public Language getLanguage() {
104    return language;
105  }
106
107  /**
108   * Sets the language of the file
109   */
110  public void setLanguage(Language language) {
111    this.language = language;
112  }
113
114  /**
115   * @return SCOPE_ENTITY
116   */
117  @Override
118  public final String getScope() {
119    return SCOPE;
120  }
121
122  /**
123   * Returns the qualifier associated to this File. Should be QUALIFIER_FILE or QUALIFIER_UNIT_TEST_CLASS
124   */
125  @Override
126  public String getQualifier() {
127    return qualifier;
128  }
129
130  public void setQualifier(String qualifier) {
131    this.qualifier = qualifier;
132  }
133
134  /**
135   * Internal use only.
136   * @deprecated since 5.1 use {@link FileSystem#inputFile(org.sonar.api.batch.fs.FilePredicate)}
137   */
138  @Deprecated
139  public static File create(String relativePathFromBasedir) {
140    File file = new File();
141    String normalizedPath = normalize(relativePathFromBasedir);
142    file.setKey(normalizedPath);
143    file.setPath(normalizedPath);
144    String directoryPath;
145    if (normalizedPath != null && normalizedPath.contains(Directory.SEPARATOR)) {
146      directoryPath = StringUtils.substringBeforeLast(normalizedPath, Directory.SEPARATOR);
147      file.filename = StringUtils.substringAfterLast(normalizedPath, Directory.SEPARATOR);
148    } else {
149      directoryPath = Directory.SEPARATOR;
150      file.filename = normalizedPath;
151    }
152    file.parent = Directory.create(directoryPath);
153    return file;
154  }
155
156  /**
157   * Internal use only.
158   * @deprecated since 5.1 use {@link FileSystem#inputFile(org.sonar.api.batch.fs.FilePredicate)}
159   */
160  @Deprecated
161  public static File create(String relativePathFromBasedir, Language language, boolean unitTest) {
162    File file = create(relativePathFromBasedir);
163    file.setLanguage(language);
164    if (unitTest) {
165      file.setQualifier(Qualifiers.UNIT_TEST_FILE);
166    }
167    return file;
168  }
169
170  @Override
171  public String toString() {
172    return new ToStringBuilder(this)
173      .append("key", getKey())
174      .append("path", getPath())
175      .append("filename", filename)
176      .append("language", language)
177      .toString();
178  }
179}