1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.tools.metalava.reporter
18 
19 /** Formats a [Report] for output. */
20 open class DefaultReportFormatter protected constructor() : ReportFormatter {
21 
beginImportantSectionnull22     protected open fun beginImportantSection(builder: StringBuilder) {}
23 
beginSeveritynull24     protected open fun beginSeverity(builder: StringBuilder, severity: Severity) {}
25 
endSeveritynull26     protected open fun endSeverity(builder: StringBuilder) {}
27 
endImportantSectionnull28     protected open fun endImportantSection(builder: StringBuilder) {}
29 
<lambda>null30     override fun format(report: Report) = buildString {
31         val (severity, relativePath, line, message, id) = report
32         beginImportantSection(this)
33         relativePath?.let {
34             append(it)
35             if (line > 0) append(":").append(line)
36             append(": ")
37         }
38 
39         beginSeverity(this, severity)
40         when (severity) {
41             Severity.INFO -> {
42                 append("info: ")
43             }
44             Severity.WARNING,
45             Severity.WARNING_ERROR_WHEN_NEW -> {
46                 append("warning: ")
47             }
48             Severity.ERROR -> {
49                 append("error: ")
50             }
51             Severity.INHERIT,
52             Severity.HIDDEN -> {}
53         }
54         endSeverity(this)
55         endImportantSection(this)
56 
57         append(message)
58         append(severity.messageSuffix)
59         id?.let<Issues.Issue, Unit> { append(" [").append(it.name).append("]") }
60     }
61 
62     companion object {
63         val DEFAULT = DefaultReportFormatter()
64     }
65 }
66