• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2019 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.protolog.tool
18 
19 import com.android.json.stream.JsonReader
20 
21 open class ViewerConfigParser {
22     data class MessageEntry(
23         val messageString: String,
24         val level: String,
25         val groupName: String
26     )
27 
28     fun parseMessage(jsonReader: JsonReader): MessageEntry {
29         jsonReader.beginObject()
30         var message: String? = null
31         var level: String? = null
32         var groupName: String? = null
33         while (jsonReader.hasNext()) {
34             when (jsonReader.nextName()) {
35                 "message" -> message = jsonReader.nextString()
36                 "level" -> level = jsonReader.nextString()
37                 "group" -> groupName = jsonReader.nextString()
38                 else -> jsonReader.skipValue()
39             }
40         }
41         jsonReader.endObject()
42         if (message.isNullOrBlank() || level.isNullOrBlank() || groupName.isNullOrBlank()) {
43             throw InvalidViewerConfigException("Invalid message entry in viewer config")
44         }
45         return MessageEntry(message, level, groupName)
46     }
47 
48     data class GroupEntry(val tag: String)
49 
50     fun parseGroup(jsonReader: JsonReader): GroupEntry {
51         jsonReader.beginObject()
52         var tag: String? = null
53         while (jsonReader.hasNext()) {
54             when (jsonReader.nextName()) {
55                 "tag" -> tag = jsonReader.nextString()
56                 else -> jsonReader.skipValue()
57             }
58         }
59         jsonReader.endObject()
60         if (tag.isNullOrBlank()) {
61             throw InvalidViewerConfigException("Invalid group entry in viewer config")
62         }
63         return GroupEntry(tag)
64     }
65 
66     fun parseMessages(jsonReader: JsonReader): Map<Int, MessageEntry> {
67         val config: MutableMap<Int, MessageEntry> = mutableMapOf()
68         jsonReader.beginObject()
69         while (jsonReader.hasNext()) {
70             val key = jsonReader.nextName()
71             val hash = key.toIntOrNull()
72                     ?: throw InvalidViewerConfigException("Invalid key in messages viewer config")
73             config[hash] = parseMessage(jsonReader)
74         }
75         jsonReader.endObject()
76         return config
77     }
78 
79     fun parseGroups(jsonReader: JsonReader): Map<String, GroupEntry> {
80         val config: MutableMap<String, GroupEntry> = mutableMapOf()
81         jsonReader.beginObject()
82         while (jsonReader.hasNext()) {
83             val key = jsonReader.nextName()
84             config[key] = parseGroup(jsonReader)
85         }
86         jsonReader.endObject()
87         return config
88     }
89 
90     data class ConfigEntry(val messageString: String, val level: String, val tag: String)
91 
92     open fun parseConfig(jsonReader: JsonReader): Map<Int, ConfigEntry> {
93         var messages: Map<Int, MessageEntry>? = null
94         var groups: Map<String, GroupEntry>? = null
95         var version: String? = null
96 
97         jsonReader.beginObject()
98         while (jsonReader.hasNext()) {
99             when (jsonReader.nextName()) {
100                 "messages" -> messages = parseMessages(jsonReader)
101                 "groups" -> groups = parseGroups(jsonReader)
102                 "version" -> version = jsonReader.nextString()
103 
104                 else -> jsonReader.skipValue()
105             }
106         }
107         jsonReader.endObject()
108         if (messages == null || groups == null || version == null) {
109             throw InvalidViewerConfigException("Invalid config - definitions missing")
110         }
111         if (version != Constants.VERSION) {
112             throw InvalidViewerConfigException("Viewer config version not supported by this tool," +
113                     " config version $version, viewer version ${Constants.VERSION}")
114         }
115         return messages.map { msg ->
116             msg.key to ConfigEntry(
117                     msg.value.messageString, msg.value.level, groups[msg.value.groupName]?.tag
118                     ?: throw InvalidViewerConfigException(
119                             "Group definition missing for ${msg.value.groupName}"))
120         }.toMap()
121     }
122 }
123