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.batch.fs;
021
022import java.io.File;
023import java.io.IOException;
024import java.io.InputStream;
025import java.nio.file.Files;
026import java.nio.file.Path;
027
028import javax.annotation.CheckForNull;
029
030import org.sonar.api.batch.fs.FileSystem;
031import org.sonar.api.batch.fs.InputFile;
032import org.sonar.api.batch.fs.InputPath;
033
034/**
035 * Represents the indexed view of an {@link InputFile}. Accessing any of data exposed here won't trigger the expensive generation of 
036 * metadata for the {@link InputFile}.
037 * 
038 * @since 6.3 
039 */
040public interface IndexedFile extends InputPath {
041  /**
042   * Path relative to module base directory. Path is unique and identifies file
043   * within given <code>{@link FileSystem}</code>. File separator is the forward
044   * slash ('/'), even on Microsoft Windows.
045   * <br>
046   * Returns <code>src/main/java/com/Foo.java</code> if module base dir is
047   * <code>/path/to/module</code> and if file is
048   * <code>/path/to/module/src/main/java/com/Foo.java</code>.
049   * <br>
050   * Relative path is not null and is normalized ('foo/../foo' is replaced by 'foo').
051   */
052  @Override
053  String relativePath();
054
055  /**
056   * Normalized absolute path. File separator is forward slash ('/'), even on Microsoft Windows.
057   * <br>
058   * This is not canonical path. Symbolic links are not resolved. For example if /project/src links
059   * to /tmp/src and basedir is /project, then this method returns /project/src/index.php. Use
060   * {@code file().getCanonicalPath()} to resolve symbolic link.
061   */
062  @Override
063  String absolutePath();
064
065  /**
066   * The underlying absolute {@link java.io.File}. It should not be used to read the file in the filesystem.
067   * @see #contents()
068   * @see #inputStream()
069   */
070  @Override
071  File file();
072
073  /**
074   * The underlying absolute {@link Path}.
075   * It should not be used to read the file in the filesystem.
076   * @see #contents()
077   * @see #inputStream()
078   * @since 5.1
079   */
080  @Override
081  Path path();
082
083  /**
084   * Language, for example "java" or "php". Can be null if indexation of all files is enabled and no language claims to support the file.
085   */
086  @CheckForNull
087  String language();
088
089  /**
090   * Does it contain main or test code ?
091   */
092  InputFile.Type type();
093
094  /**
095   * Creates a stream of the file's contents. Depending on the runtime context, the source might be a file in a physical or virtual filesystem.
096   * Typically, it won't be buffered. <b>The stream must be closed by the caller</b>.
097   * Note that there is a default implementation.
098   * @since 6.2
099   */
100  default InputStream inputStream() throws IOException {
101    return Files.newInputStream(path());
102  }
103}