core/src/commonMain/kotlin/gms/OssLicenseParser.kt
author Da Risk <da_risk@geekorum.com>
Sun, 27 Apr 2025 17:37:41 -0400
changeset 65 f4443a561f38
parent 34 ce299aacc068
permissions -rw-r--r--
sample: update AndroidManifest.xml
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     1
/*
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     2
 * AboutOss is an utility library to retrieve and display
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     3
 * opensource licenses in Android applications.
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     4
 *
34
ce299aacc068 build: update license headers
Da Risk <da_risk@geekorum.com>
parents: 30
diff changeset
     5
 * Copyright (C) 2023-2025 by Frederic-Charles Barthelery.
24
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     6
 *
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     7
 * This file is part of AboutOss.
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     8
 *
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     9
 * AboutOss is free software: you can redistribute it and/or modify
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    10
 * it under the terms of the GNU General Public License as published by
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    11
 * the Free Software Foundation, either version 3 of the License, or
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    12
 * (at your option) any later version.
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    13
 *
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    14
 * AboutOss is distributed in the hope that it will be useful,
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    17
 * GNU General Public License for more details.
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    18
 *
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    19
 * You should have received a copy of the GNU General Public License
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    20
 * along with AboutOss.  If not, see <http://www.gnu.org/licenses/>.
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    21
 */
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    22
package com.geekorum.aboutoss.core.gms
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    23
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    24
import okio.ByteString
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    25
import okio.Source
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    26
import okio.buffer
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    27
import okio.use
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    28
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    29
/**
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    30
 * Parse licences data generated by the "com.google.android.gms.oss-licenses-plugin" gradle plugin.
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    31
 */
30
763a6573b900 core: refactor OssLicenseParser to use source as constructor params
Da Risk <da_risk@geekorum.com>
parents: 24
diff changeset
    32
class OssLicenseParser(
763a6573b900 core: refactor OssLicenseParser to use source as constructor params
Da Risk <da_risk@geekorum.com>
parents: 24
diff changeset
    33
    thirdPartyLicensesSource: Source,
763a6573b900 core: refactor OssLicenseParser to use source as constructor params
Da Risk <da_risk@geekorum.com>
parents: 24
diff changeset
    34
    thirdPartyLicensesMetadataSource: Source
763a6573b900 core: refactor OssLicenseParser to use source as constructor params
Da Risk <da_risk@geekorum.com>
parents: 24
diff changeset
    35
): AutoCloseable {
763a6573b900 core: refactor OssLicenseParser to use source as constructor params
Da Risk <da_risk@geekorum.com>
parents: 24
diff changeset
    36
763a6573b900 core: refactor OssLicenseParser to use source as constructor params
Da Risk <da_risk@geekorum.com>
parents: 24
diff changeset
    37
    private val thirdPartyLicensesInput = thirdPartyLicensesSource.buffer()
763a6573b900 core: refactor OssLicenseParser to use source as constructor params
Da Risk <da_risk@geekorum.com>
parents: 24
diff changeset
    38
    private val thirdPartyLicensesMetadataInput = thirdPartyLicensesMetadataSource.buffer()
24
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    39
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    40
    /**
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    41
     * Parse licenses
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    42
     * @param [thirdPartyLicensesInput] is usually res/raw/third_party_licenses file
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    43
     * @param [thirdPartyLicensesMetadataInput] is usually res/raw/third_party_license_metadata file
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    44
     */
30
763a6573b900 core: refactor OssLicenseParser to use source as constructor params
Da Risk <da_risk@geekorum.com>
parents: 24
diff changeset
    45
    fun parseLicenses(): Map<String, String> {
24
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    46
        val licenses = readLicensesFile(thirdPartyLicensesInput)
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    47
        return buildLicenseInfo(licenses, thirdPartyLicensesMetadataInput)
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    48
    }
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    49
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    50
    private fun readLicensesFile(thirdPartyLicensesInput: Source): ByteString {
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    51
        return thirdPartyLicensesInput.buffer().use {
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    52
            it.readByteString()
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    53
        }
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    54
    }
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    55
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    56
    private fun buildLicenseInfo(license: ByteString, thirdPartyLicensesMetadataInput: Source): Map<String, String> {
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    57
        return thirdPartyLicensesMetadataInput.buffer().use {
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    58
            buildMap {
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    59
                while (true) {
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    60
                    val line = it.readUtf8Line() ?: break
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    61
                    if (line.isNotBlank()) {
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    62
                        with(line.toLineParser()) {
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    63
                            val start = readStartIdx()
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    64
                            val length = readLength()
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    65
                            val dependency = readName()
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    66
                            val licenseTxt = license.substring(
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    67
                                beginIndex = start,
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    68
                                endIndex = start + length + 1
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    69
                            ).utf8()
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    70
                            put(dependency, licenseTxt)
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    71
                        }
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    72
                    }
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    73
                }
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    74
            }
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    75
        }
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    76
    }
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    77
30
763a6573b900 core: refactor OssLicenseParser to use source as constructor params
Da Risk <da_risk@geekorum.com>
parents: 24
diff changeset
    78
    override fun close() {
763a6573b900 core: refactor OssLicenseParser to use source as constructor params
Da Risk <da_risk@geekorum.com>
parents: 24
diff changeset
    79
        thirdPartyLicensesInput.close()
763a6573b900 core: refactor OssLicenseParser to use source as constructor params
Da Risk <da_risk@geekorum.com>
parents: 24
diff changeset
    80
        thirdPartyLicensesMetadataInput.close()
763a6573b900 core: refactor OssLicenseParser to use source as constructor params
Da Risk <da_risk@geekorum.com>
parents: 24
diff changeset
    81
    }
763a6573b900 core: refactor OssLicenseParser to use source as constructor params
Da Risk <da_risk@geekorum.com>
parents: 24
diff changeset
    82
24
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    83
    companion object
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    84
}
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    85
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    86
private class LicenseMetadataLineParser(
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    87
    private val line: String
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    88
) {
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    89
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    90
    private var idx = 0
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    91
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    92
    fun readStartIdx(): Int {
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    93
        val end = line.indexOf(':', startIndex = idx)
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    94
        val result = line.substring(idx, end).toInt()
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    95
        idx = end + 1
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    96
        return result
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    97
    }
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    98
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    99
    fun readLength(): Int {
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   100
        val end = line.indexOf(' ', startIndex = idx)
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   101
        val result = line.substring(idx, end).toInt()
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   102
        idx = end + 1
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   103
        return result
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   104
    }
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   105
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   106
    fun readName(): String {
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   107
        val result = line.substring(idx)
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   108
        idx = line.length + 1
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   109
        return result
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   110
    }
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   111
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   112
}
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   113
f07de07b90c4 core: port OssLicenseParser to multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   114
private fun String.toLineParser() = LicenseMetadataLineParser(this)