2
* Copyright (C) 2023 Square, Inc.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
16
@file:JvmName("GenerateIdnaMappingTableCode")
18
package okhttp3.internal.idn
20
import com.squareup.kotlinpoet.ClassName
21
import com.squareup.kotlinpoet.FileSpec
22
import com.squareup.kotlinpoet.KModifier
23
import com.squareup.kotlinpoet.PropertySpec
26
import okio.Path.Companion.toPath
28
fun main(vararg args: String) {
29
val data = loadIdnaMappingTableData()
30
val file = generateMappingTableFile(data)
31
file.writeTo(File(args[0]))
34
fun loadIdnaMappingTableData(): IdnaMappingTableData {
35
val path = "/okhttp3/internal/idna/IdnaMappingTable.txt".toPath()
37
FileSystem.RESOURCES.read(path) {
38
readPlainTextIdnaMappingTable()
40
return buildIdnaMappingTableData(table)
44
* Generate a file containing the mapping table's string literals, like this:
47
* internal val IDNA_MAPPING_TABLE: IdnaMappingTable = IdnaMappingTable(
54
fun generateMappingTableFile(data: IdnaMappingTableData): FileSpec {
55
val packageName = "okhttp3.internal.idn"
56
val idnaMappingTable = ClassName(packageName, "IdnaMappingTable")
58
return FileSpec.builder(packageName, "IdnaMappingTableInstance")
60
PropertySpec.builder("IDNA_MAPPING_TABLE", idnaMappingTable)
61
.addModifiers(KModifier.INTERNAL)
71
data.sections.escapeDataString(),
72
data.ranges.escapeDataString(),
73
data.mappings.escapeDataString(),
81
* KotlinPoet doesn't really know what to do with a string containing NUL, BEL, DEL, etc. We also
82
* don't want to perform `trimMargin()` at runtime.
84
fun String.escapeDataString(): String {
86
for (codePoint in this@escapeDataString.codePoints()) {
94
-> append(String.format("\\u%04x", codePoint))
96
else -> appendCodePoint(codePoint)