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    import java.util.List;
027    
028    /**
029     * This class is an implementation of a resource of type FILE
030     * @since 1.10
031     */
032    public class File implements Resource<Directory> {
033    
034      private String directoryKey;
035      private String key;
036      private String filename;
037      private Language language;
038      private Directory parent;
039    
040      /**
041       * File in project. Key is the path relative to project source directories. It is not the absolute path
042       * and it does not include the path to source directories. Example : <code>new File("org/sonar/foo.sql")</code>. The
043       * absolute path may be c:/myproject/src/main/sql/org/sonar/foo.sql. Project root is c:/myproject and source dir
044       * is src/main/sql.
045       */
046      public File(String key) {
047        if (key == null) {
048          throw new IllegalArgumentException("File key is null");
049        }
050        this.key = parseKey(key);
051        if (this.key.indexOf(Directory.SEPARATOR) >= 0) {
052          this.directoryKey = Directory.parseKey(StringUtils.substringBeforeLast(key, Directory.SEPARATOR));
053          this.filename = StringUtils.substringAfterLast(this.key, Directory.SEPARATOR);
054          this.key = new StringBuilder().append(this.directoryKey).append(Directory.SEPARATOR).append(filename).toString();
055    
056        } else {
057          this.filename = key;
058        }
059      }
060    
061      /**
062       * Creates a file from its containing directory and name
063       */
064      public File(String directory, String filename) {
065        this.filename = StringUtils.trim(filename);
066        if (StringUtils.isBlank(directory)) {
067          this.key = filename;
068    
069        } else {
070          this.directoryKey = Directory.parseKey(directory);
071          this.key = new StringBuilder().append(directoryKey).append(Directory.SEPARATOR).append(this.filename).toString();
072        }
073      }
074    
075      /**
076       * Creates a File from its language and its key
077       */
078      public File(Language language, String key) {
079        this(key);
080        this.language = language;
081      }
082    
083      /**
084       * Creates a File from language, directory and filename
085       */
086      public File(Language language, String directory, String filename) {
087        this(directory, filename);
088        this.language = language;
089      }
090    
091        /**
092         * {@inheritDoc}
093         *
094         * @see Resource#getParent()
095         */
096      public Directory getParent() {
097        if (parent == null) {
098          parent = new Directory(directoryKey);
099        }
100        return parent;
101      }
102    
103      private static String parseKey(String key) {
104        if (StringUtils.isBlank(key)) {
105          return null;
106        }
107    
108        key = key.replace('\\', '/');
109        key = StringUtils.trim(key);
110        return key;
111      }
112    
113      /**
114       * {@inheritDoc}
115       *
116       * @see Resource#matchFilePattern(String)
117       */
118      public boolean matchFilePattern(String antPattern) {
119        WildcardPattern matcher = WildcardPattern.create(antPattern, "/");
120        return matcher.match(getKey());
121      }
122    
123      /**
124       * Creates a File from an io.file and a list of sources directories
125       */
126      public static File fromIOFile(java.io.File file, List<java.io.File> sourceDirs) {
127        String relativePath = DefaultProjectFileSystem.getRelativePath(file, sourceDirs);
128        if (relativePath != null) {
129          return new File(relativePath);
130        }
131        return null;
132      }
133    
134      /**
135       * Creates a File from its name and a project
136       */
137      public static File fromIOFile(java.io.File file, Project project) {
138        return fromIOFile(file, project.getFileSystem().getSourceDirs());
139      }
140    
141      /**
142       * {@inheritDoc}
143       *
144       * @see Resource#getKey()
145       */
146      public String getKey() {
147        return key;
148      }
149    
150      /**
151       * {@inheritDoc}
152       *
153       * @see Resource#getName()
154       */
155      public String getName() {
156        return filename;
157      }
158    
159      /**
160       * {@inheritDoc}
161       *
162       * @see Resource#getLongName()
163       */
164      public String getLongName() {
165        return key;
166      }
167    
168      /**
169       * {@inheritDoc}
170       *
171       * @see Resource#getDescription()
172       */
173      public String getDescription() {
174        return null;
175      }
176    
177      /**
178       * {@inheritDoc}
179       *
180       * @see Resource#getLanguage()
181       */
182      public Language getLanguage() {
183        return language;
184      }
185    
186      /**
187       * Sets the language of the file
188       */
189      public void setLanguage(Language language) {
190        this.language = language;
191      }
192    
193      /**
194       * @return SCOPE_ENTITY
195       */
196      public String getScope() {
197        return Resource.SCOPE_ENTITY;
198      }
199    
200      /**
201       * @return QUALIFIER_FILE
202       */
203      public String getQualifier() {
204        return Resource.QUALIFIER_FILE;
205      }
206    
207      @Override
208      public boolean equals(Object obj) {
209        if (!(obj instanceof File)) {
210          return false;
211        }
212        if (this == obj) {
213          return true;
214        }
215        File other = (File) obj;
216        return StringUtils.equals(key, other.getKey());
217      }
218    
219      @Override
220      public int hashCode() {
221        return key.hashCode();
222      }
223    
224      @Override
225      public String toString() {
226        return new ToStringBuilder(this)
227            .append("key", key)
228            .append("dir", directoryKey)
229            .append("filename", filename)
230            .append("language", language)
231            .toString();
232      }
233    }