|
1 /* |
|
2 * AboutOss is an utility library to retrieve and display |
|
3 * opensource licenses in Android applications. |
|
4 * |
|
5 * Copyright (C) 2023 by Frederic-Charles Barthelery. |
|
6 * |
|
7 * This file is part of AboutOss. |
|
8 * |
|
9 * AboutOss is free software: you can redistribute it and/or modify |
|
10 * it under the terms of the GNU General Public License as published by |
|
11 * the Free Software Foundation, either version 3 of the License, or |
|
12 * (at your option) any later version. |
|
13 * |
|
14 * AboutOss is distributed in the hope that it will be useful, |
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 * GNU General Public License for more details. |
|
18 * |
|
19 * You should have received a copy of the GNU General Public License |
|
20 * along with AboutOss. If not, see <http://www.gnu.org/licenses/>. |
|
21 */ |
|
22 package com.geekorum.aboutoss.core.licensee |
|
23 |
|
24 import kotlinx.serialization.Serializable |
|
25 import kotlinx.serialization.json.Json |
|
26 import okio.Source |
|
27 import okio.buffer |
|
28 |
|
29 class LicenseeParser( |
|
30 input: Source |
|
31 ): AutoCloseable { |
|
32 private val buffered = input.buffer() |
|
33 |
|
34 fun readLicensee(): Map<String, String> { |
|
35 val json = Json { |
|
36 ignoreUnknownKeys = true |
|
37 } |
|
38 val items: List<LicenseItem> = json.decodeFromString(buffered.readUtf8()) |
|
39 |
|
40 return items.associate { |
|
41 val name = it.name ?: "${it.groupId}:${it.artifactId}" |
|
42 val license = it.spdxLicenses.firstNotNullOfOrNull { |
|
43 "${it.name}\n\n${it.url}" |
|
44 } ?: it.unknownLicenses.firstNotNullOf { |
|
45 "${it.name}\n\n${it.url}" |
|
46 } |
|
47 name to license |
|
48 } |
|
49 } |
|
50 |
|
51 override fun close() { |
|
52 buffered.close() |
|
53 } |
|
54 } |
|
55 |
|
56 |
|
57 @Serializable |
|
58 private data class LicenseItem( |
|
59 val groupId: String, |
|
60 val artifactId: String, |
|
61 val version: String, |
|
62 val spdxLicenses: List<SpdxLicense> = emptyList(), |
|
63 val unknownLicenses: List<UnknownLicense> = emptyList(), |
|
64 val name: String? = null, |
|
65 val scm: Scm? = null, |
|
66 ) |
|
67 |
|
68 @Serializable |
|
69 private data class SpdxLicense( |
|
70 val identifier: String, |
|
71 val name: String, |
|
72 val url: String, |
|
73 ) |
|
74 |
|
75 @Serializable |
|
76 private data class UnknownLicense( |
|
77 val name: String, |
|
78 val url: String |
|
79 ) |
|
80 |
|
81 @Serializable |
|
82 private data class Scm( |
|
83 val url: String, |
|
84 ) |