]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9641 Introduce InputFile:uri()
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 31 Jul 2017 08:37:53 +0000 (10:37 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Fri, 4 Aug 2017 12:58:16 +0000 (14:58 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/IndexedFile.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputComponent.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputPath.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java
sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java

index 057c94efa72d22d2fbf208545a38fb93405e8a22..fe94fce9c3a7cf4b93d6f0810e1fd0296f74677b 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.api.batch.fs;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URI;
 import java.nio.file.Path;
 import javax.annotation.CheckForNull;
 
@@ -42,7 +43,9 @@ public interface IndexedFile extends InputPath {
    * <code>/path/to/module/src/main/java/com/Foo.java</code>.
    * <br>
    * Relative path is not null and is normalized ('foo/../foo' is replaced by 'foo').
+   * @deprecated since 6.6 use {@link #inputStream()}, {@link #filename()} or {@link #uri()}
    */
+  @Deprecated
   @Override
   String relativePath();
 
@@ -52,7 +55,9 @@ public interface IndexedFile extends InputPath {
    * This is not canonical path. Symbolic links are not resolved. For example if /project/src links
    * to /tmp/src and basedir is /project, then this method returns /project/src/index.php. Use
    * {@code file().getCanonicalPath()} to resolve symbolic link.
+   * @deprecated since 6.6 use {@link #inputStream()}, {@link #filename()} or {@link #uri()}
    */
+  @Deprecated
   @Override
   String absolutePath();
 
@@ -60,7 +65,9 @@ public interface IndexedFile extends InputPath {
    * The underlying absolute {@link java.io.File}. It should not be used to read the file in the filesystem.
    * @see #contents()
    * @see #inputStream()
+   * @deprecated since 6.6 use {@link #inputStream()}, {@link #filename()} or {@link #uri()}
    */
+  @Deprecated
   @Override
   File file();
 
@@ -70,10 +77,25 @@ public interface IndexedFile extends InputPath {
    * @see #contents()
    * @see #inputStream()
    * @since 5.1
+   * @deprecated since 6.6 use {@link #inputStream()}, {@link #filename()} or {@link #uri()}
    */
+  @Deprecated
   @Override
   Path path();
 
+  /**
+   * Identifier of the file. The only guarantee is that it is unique in the project.
+   * You should not assume it is a file:// URI.
+   * @since 6.6
+   */
+  URI uri();
+
+  /**
+   * Filename for this file (inclusing extension). For example: MyClass.java.
+   * @since 6.6
+   */
+  String filename();
+
   /**
    * Language, for example "java" or "php". Can be null if indexation of all files is enabled and no language claims to support the file.
    */
index 24ac24ad4faee3603115e9a661978f66140033e7..34b0d9edd12265e673e58bdebaf1b583b5de1bd6 100644 (file)
@@ -30,7 +30,7 @@ package org.sonar.api.batch.fs;
 public interface InputComponent {
 
   /**
-   * Component key shared by all part of SonarQube (batch, server, WS...). 
+   * Component key shared by all part of SonarQube (scanner, server, WS...). 
    * It doesn't include the branch.
    */
   String key();
index 00463b1ed090cebea0de967e760bf9edaef62aec..9225334786964734d125ba8601f44a85a34813d5 100644 (file)
@@ -63,7 +63,9 @@ public interface InputFile extends IndexedFile {
    * <code>/path/to/module/src/main/java/com/Foo.java</code>.
    * <br>
    * Relative path is not null and is normalized ('foo/../foo' is replaced by 'foo').
+   * @deprecated since 6.6 use {@link #inputStream()}, {@link #filename()} or {@link #uri()}
    */
+  @Deprecated
   @Override
   String relativePath();
 
@@ -73,7 +75,9 @@ public interface InputFile extends IndexedFile {
    * This is not canonical path. Symbolic links are not resolved. For example if /project/src links
    * to /tmp/src and basedir is /project, then this method returns /project/src/index.php. Use
    * {@code file().getCanonicalPath()} to resolve symbolic link.
+   * @deprecated since 6.6 use {@link #inputStream()}, {@link #filename()} or {@link #uri()}
    */
+  @Deprecated
   @Override
   String absolutePath();
 
@@ -81,7 +85,9 @@ public interface InputFile extends IndexedFile {
    * The underlying absolute {@link java.io.File}. It should not be used to read the file in the filesystem.
    * @see #contents()
    * @see #inputStream()
+   * @deprecated since 6.6 use {@link #inputStream()}, {@link #filename()} or {@link #uri()}
    */
+  @Deprecated
   @Override
   File file();
 
@@ -91,7 +97,9 @@ public interface InputFile extends IndexedFile {
    * @see #contents()
    * @see #inputStream()
    * @since 5.1
+   * @deprecated since 6.6 use {@link #inputStream()}, {@link #filename()} or {@link #uri()}
    */
+  @Deprecated
   @Override
   Path path();
 
index b85eb2895173f4222cb5bf715becf4a207988e27..274e05a3e448a2d843daa91224c581c0ae9e2c80 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.api.batch.fs;
 
 import java.io.File;
+import java.net.URI;
 import java.nio.file.Path;
 
 /**
@@ -34,26 +35,41 @@ public interface InputPath extends InputComponent {
   /**
    * @see InputFile#relativePath()
    * @see InputDir#relativePath()
+   * @deprecated since 6.5 use {@link #uri()}
    */
+  @Deprecated
   String relativePath();
 
   /**
    * @see InputFile#absolutePath()
    * @see InputDir#absolutePath()
+   * @deprecated since 6.5 use {@link #uri()}
    */
+  @Deprecated
   String absolutePath();
 
   /**
    * @see InputFile#file()
    * @see InputDir#file()
+   * @deprecated since 6.5 use {@link #uri()}
    */
+  @Deprecated
   File file();
 
   /**
    * @see InputFile#path()
    * @see InputDir#path()
    * @since 5.1
+   * @deprecated since 6.5 use {@link #uri()}
    */
+  @Deprecated
   Path path();
 
+  /**
+   * Identifier of the component. The only guarantee is that it is unique in the project.
+   * You should not assume it is a file:// URI.
+   * @since 6.5
+   */
+  URI uri();
+
 }
index b3a0dbbe9eec1d298c6817361fcb9a01b6b925a8..a91ab8ad2aaea10137b8e497851a819b380ab935 100644 (file)
@@ -22,13 +22,12 @@ package org.sonar.api.batch.fs.internal;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URI;
 import java.nio.file.Files;
 import java.nio.file.Path;
-
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import javax.annotation.concurrent.Immutable;
-
 import org.sonar.api.batch.fs.IndexedFile;
 import org.sonar.api.batch.fs.InputFile.Type;
 import org.sonar.api.utils.PathUtils;
@@ -142,4 +141,14 @@ public class DefaultIndexedFile extends DefaultInputComponent implements Indexed
   public boolean isFile() {
     return true;
   }
+
+  @Override
+  public String filename() {
+    return path().getFileName().toString();
+  }
+
+  @Override
+  public URI uri() {
+    return path().toUri();
+  }
 }
index e271a327daca704862ce1e76e9be84fa24280dc7..79031632b08e8c408a6ceefdb57a7a94b5230eeb 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.api.batch.fs.internal;
 
 import java.io.File;
+import java.net.URI;
 import java.nio.file.Path;
 import org.apache.commons.lang.StringUtils;
 import org.sonar.api.batch.fs.InputDir;
@@ -117,4 +118,9 @@ public class DefaultInputDir extends DefaultInputComponent implements InputDir {
   public String toString() {
     return "[moduleKey=" + moduleKey + ", relative=" + relativePath + ", basedir=" + moduleBaseDir + "]";
   }
+
+  @Override
+  public URI uri() {
+    return path().toUri();
+  }
 }
index b3ea8263c0d723fd95d3b161015e9587a205449c..4312c634c046acf10925f4a88d6ee72e94ba8b69 100644 (file)
@@ -25,6 +25,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URI;
 import java.nio.charset.Charset;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -77,8 +78,9 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile
 
   @Override
   public InputStream inputStream() throws IOException {
-    return contents != null ? new ByteArrayInputStream(contents.getBytes(charset())) : new BOMInputStream(Files.newInputStream(path()),
-      ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE);
+    return contents != null ? new ByteArrayInputStream(contents.getBytes(charset()))
+      : new BOMInputStream(Files.newInputStream(path()),
+        ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE);
   }
 
   @Override
@@ -337,4 +339,14 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile
     return true;
   }
 
+  @Override
+  public String filename() {
+    return indexedFile.filename();
+  }
+
+  @Override
+  public URI uri() {
+    return indexedFile.uri();
+  }
+
 }
index 82ccf11b3e80ab486f5209a8565e088b3966be6d..cf66115c6954f6f8049d7cf20ef78f2b780d51c8 100644 (file)
@@ -62,6 +62,8 @@ public class DefaultInputFileTest {
     assertThat(inputFile.relativePath()).isEqualTo("src/Foo.php");
     assertThat(new File(inputFile.relativePath())).isRelative();
     assertThat(inputFile.absolutePath()).endsWith("Foo.php");
+    assertThat(inputFile.filename()).isEqualTo("Foo.php");
+    assertThat(inputFile.uri()).hasPath(baseDir.resolve("src/Foo.php").toUri().getPath());
     assertThat(new File(inputFile.absolutePath())).isAbsolute();
     assertThat(inputFile.language()).isEqualTo("php");
     assertThat(inputFile.status()).isEqualTo(InputFile.Status.ADDED);