001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 SonarSource
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.sonar.api.utils.WildcardPattern;
024    
025    import java.io.File;
026    import java.util.List;
027    
028    /**
029     * A class that represents a Java class. This class can either be a Test class or source class
030     *
031     * @since 1.10
032     */
033    public class JavaFile extends Resource<JavaPackage> {
034    
035      private String filename;
036      private String longName;
037      private String packageKey;
038      private boolean unitTest;
039      private JavaPackage parent;
040    
041      /**
042       * Creates a JavaFile that is not a class of test based on package and file names
043       */
044      public JavaFile(String packageName, String className) {
045        this(packageName, className, false);
046      }
047    
048      /**
049       * Creates a JavaFile that can be of any type based on package and file names
050       *
051       * @param unitTest whether it is a unit test file or a source file
052       */
053      public JavaFile(String packageKey, String className, boolean unitTest) {
054        if (className == null) {
055          throw new IllegalArgumentException("Java filename can not be null");
056        }
057        this.filename = StringUtils.trim(className);
058        String key;
059        if (StringUtils.isBlank(packageKey)) {
060          this.packageKey = JavaPackage.DEFAULT_PACKAGE_NAME;
061          this.longName = this.filename;
062          key = new StringBuilder().append(this.packageKey).append(".").append(this.filename).toString();
063        } else {
064          this.packageKey = packageKey.trim();
065          key = new StringBuilder().append(this.packageKey).append(".").append(this.filename).toString();
066          this.longName = key;
067        }
068        setKey(key);
069        this.unitTest = unitTest;
070      }
071    
072      /**
073       * Creates a source file from its key
074       */
075      public JavaFile(String key) {
076        this(key, false);
077      }
078    
079      /**
080       * Creates any JavaFile from its key
081       *
082       * @param unitTest whether it is a unit test file or a source file
083       */
084      public JavaFile(String key, boolean unitTest) {
085        if (key == null) {
086          throw new IllegalArgumentException("Java filename can not be null");
087        }
088        String realKey = StringUtils.trim(key);
089        this.unitTest = unitTest;
090    
091        if (realKey.contains(".")) {
092          this.filename = StringUtils.substringAfterLast(realKey, ".");
093          this.packageKey = StringUtils.substringBeforeLast(realKey, ".");
094          this.longName = realKey;
095    
096        } else {
097          this.filename = realKey;
098          this.longName = realKey;
099          this.packageKey = JavaPackage.DEFAULT_PACKAGE_NAME;
100          realKey = new StringBuilder().append(JavaPackage.DEFAULT_PACKAGE_NAME).append(".").append(realKey).toString();
101        }
102        setKey(realKey);
103      }
104    
105      /**
106       * {@inheritDoc}
107       */
108      @Override
109      public JavaPackage getParent() {
110        if (parent == null) {
111          parent = new JavaPackage(packageKey);
112        }
113        return parent;
114    
115      }
116    
117      /**
118       * @return null
119       */
120      @Override
121      public String getDescription() {
122        return null;
123      }
124    
125      /**
126       * @return Java
127       */
128      @Override
129      public Language getLanguage() {
130        return Java.INSTANCE;
131      }
132    
133      /**
134       * {@inheritDoc}
135       */
136      @Override
137      public String getName() {
138        return filename;
139      }
140    
141      /**
142       * {@inheritDoc}
143       */
144      @Override
145      public String getLongName() {
146        return longName;
147      }
148    
149      /**
150       * @return SCOPE_ENTITY
151       */
152      @Override
153      public String getScope() {
154        return Scopes.FILE;
155      }
156    
157      /**
158       * @return QUALIFIER_UNIT_TEST_CLASS or QUALIFIER_CLASS depending whether it is a unit test class
159       */
160      @Override
161      public String getQualifier() {
162        return unitTest ? Qualifiers.UNIT_TEST_FILE : Qualifiers.CLASS;
163      }
164    
165      /**
166       * @return whether the JavaFile is a unit test class or not
167       */
168      public boolean isUnitTest() {
169        return unitTest;
170      }
171    
172      /**
173       * {@inheritDoc}
174       */
175      @Override
176      public boolean matchFilePattern(String antPattern) {
177        if (unitTest) {
178          return false;
179        }
180        String fileKey = getKey();
181        if (!fileKey.endsWith(".java")) {
182          fileKey += ".java";
183        }
184        if (StringUtils.substringAfterLast(antPattern, "/").indexOf(".") < 0) {
185          antPattern += ".*";
186        }
187        WildcardPattern matcher = WildcardPattern.create(antPattern, ".");
188        return matcher.match(fileKey);
189      }
190    
191      public static JavaFile fromRelativePath(String relativePath, boolean unitTest) {
192        if (relativePath != null) {
193          String pacname = null;
194          String classname = relativePath;
195    
196          if (relativePath.indexOf('/') >= 0) {
197            pacname = StringUtils.substringBeforeLast(relativePath, "/");
198            pacname = StringUtils.replace(pacname, "/", ".");
199            classname = StringUtils.substringAfterLast(relativePath, "/");
200          }
201          classname = StringUtils.substringBeforeLast(classname, ".");
202          return new JavaFile(pacname, classname, unitTest);
203        }
204        return null;
205      }
206    
207      /**
208       * Creates a JavaFile from a file in the source directories
209       *
210       * @return the JavaFile created if exists, null otherwise
211       */
212      public static JavaFile fromIOFile(File file, List<File> sourceDirs, boolean unitTest) {
213        if (file == null || !StringUtils.endsWithIgnoreCase(file.getName(), ".java")) {
214          return null;
215        }
216        String relativePath = DefaultProjectFileSystem.getRelativePath(file, sourceDirs);
217        return fromRelativePath(relativePath, unitTest);
218      }
219    
220      /**
221       * Shortcut to fromIOFile with an abolute path
222       */
223      public static JavaFile fromAbsolutePath(String path, List<File> sourceDirs, boolean unitTest) {
224        if (path == null) {
225          return null;
226        }
227        return fromIOFile(new File(path), sourceDirs, unitTest);
228      }
229    
230      @Override
231      public String toString() {
232        return getKey();
233      }
234    
235    }