Search in sources :

Example 26 with Metadata

use of org.sonar.api.batch.fs.internal.Metadata in project sonarqube by SonarSource.

the class MetadataGenerator method setMetadata.

/**
 * Sets all metadata in the file, including charset and status.
 * It is an expensive computation, reading the entire file.
 */
public void setMetadata(String moduleKeyWithBranch, final DefaultInputFile inputFile, Charset defaultEncoding) {
    CharsetDetector charsetDetector = new CharsetDetector(inputFile.path(), defaultEncoding);
    try {
        Charset charset;
        if (charsetDetector.run()) {
            charset = charsetDetector.charset();
        } else {
            LOG.debug("Failed to detect a valid charset for file '{}'. Using default charset.", inputFile);
            charset = defaultEncoding;
        }
        InputStream is = charsetDetector.inputStream();
        inputFile.setCharset(charset);
        Metadata metadata = fileMetadata.readMetadata(is, charset, inputFile.absolutePath(), exclusionsScanner.createCharHandlerFor(inputFile));
        inputFile.setMetadata(metadata);
        inputFile.setStatus(statusDetection.status(moduleKeyWithBranch, inputFile, metadata.hash()));
        LOG.debug("'{}' generated metadata{} with charset '{}'", inputFile, inputFile.type() == Type.TEST ? " as test " : "", charset);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}
Also used : InputStream(java.io.InputStream) Metadata(org.sonar.api.batch.fs.internal.Metadata) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) Charset(java.nio.charset.Charset)

Aggregations

Metadata (org.sonar.api.batch.fs.internal.Metadata)26 FileMetadata (org.sonar.api.batch.fs.internal.FileMetadata)25 Test (org.junit.Test)24 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)23 File (java.io.File)15 FileInputStream (java.io.FileInputStream)14 DefaultIndexedFile (org.sonar.api.batch.fs.internal.DefaultIndexedFile)7 IOException (java.io.IOException)5 InputStream (java.io.InputStream)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 StringReader (java.io.StringReader)2 Charset (java.nio.charset.Charset)2 Path (java.nio.file.Path)2 BufferedWriter (java.io.BufferedWriter)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 InputFile (org.sonar.api.batch.fs.InputFile)1 TextRange (org.sonar.api.batch.fs.TextRange)1