core/src/commonMain/kotlin/gms/OssLicenseParser.kt
changeset 24 f07de07b90c4
child 30 763a6573b900
equal deleted inserted replaced
23:5bba9369df0f 24:f07de07b90c4
       
     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.gms
       
    23 
       
    24 import okio.ByteString
       
    25 import okio.Source
       
    26 import okio.buffer
       
    27 import okio.use
       
    28 
       
    29 /**
       
    30  * Parse licences data generated by the "com.google.android.gms.oss-licenses-plugin" gradle plugin.
       
    31  */
       
    32 class OssLicenseParser {
       
    33 
       
    34     /**
       
    35      * Parse licenses
       
    36      * @param [thirdPartyLicensesInput] is usually res/raw/third_party_licenses file
       
    37      * @param [thirdPartyLicensesMetadataInput] is usually res/raw/third_party_license_metadata file
       
    38      */
       
    39     fun parseLicenses(
       
    40         thirdPartyLicensesInput: Source, thirdPartyLicensesMetadataInput: Source
       
    41     ): Map<String, String> {
       
    42         val licenses = readLicensesFile(thirdPartyLicensesInput)
       
    43         return buildLicenseInfo(licenses, thirdPartyLicensesMetadataInput)
       
    44     }
       
    45 
       
    46     private fun readLicensesFile(thirdPartyLicensesInput: Source): ByteString {
       
    47         return thirdPartyLicensesInput.buffer().use {
       
    48             it.readByteString()
       
    49         }
       
    50     }
       
    51 
       
    52     private fun buildLicenseInfo(license: ByteString, thirdPartyLicensesMetadataInput: Source): Map<String, String> {
       
    53         return thirdPartyLicensesMetadataInput.buffer().use {
       
    54             buildMap {
       
    55                 while (true) {
       
    56                     val line = it.readUtf8Line() ?: break
       
    57                     if (line.isNotBlank()) {
       
    58                         with(line.toLineParser()) {
       
    59                             val start = readStartIdx()
       
    60                             val length = readLength()
       
    61                             val dependency = readName()
       
    62                             val licenseTxt = license.substring(
       
    63                                 beginIndex = start,
       
    64                                 endIndex = start + length + 1
       
    65                             ).utf8()
       
    66                             put(dependency, licenseTxt)
       
    67                         }
       
    68                     }
       
    69                 }
       
    70             }
       
    71         }
       
    72     }
       
    73 
       
    74     companion object
       
    75 }
       
    76 
       
    77 private class LicenseMetadataLineParser(
       
    78     private val line: String
       
    79 ) {
       
    80 
       
    81     private var idx = 0
       
    82 
       
    83     fun readStartIdx(): Int {
       
    84         val end = line.indexOf(':', startIndex = idx)
       
    85         val result = line.substring(idx, end).toInt()
       
    86         idx = end + 1
       
    87         return result
       
    88     }
       
    89 
       
    90     fun readLength(): Int {
       
    91         val end = line.indexOf(' ', startIndex = idx)
       
    92         val result = line.substring(idx, end).toInt()
       
    93         idx = end + 1
       
    94         return result
       
    95     }
       
    96 
       
    97     fun readName(): String {
       
    98         val result = line.substring(idx)
       
    99         idx = line.length + 1
       
   100         return result
       
   101     }
       
   102 
       
   103 }
       
   104 
       
   105 private fun String.toLineParser() = LicenseMetadataLineParser(this)