1 /*
2  * Copyright (C) 2022 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.server.permission.access.util
18 
19 import com.android.modules.utils.BinaryXmlSerializer
20 import java.io.IOException
21 import java.io.OutputStream
22 
23 /** Serialize content into [OutputStream] with [BinaryXmlSerializer]. */
24 @Throws(IOException::class)
serializeBinaryXmlnull25 inline fun OutputStream.serializeBinaryXml(block: BinaryXmlSerializer.() -> Unit) {
26     BinaryXmlSerializer().apply {
27         setOutput(this@serializeBinaryXml, null)
28         document(block)
29     }
30 }
31 
32 /**
33  * Write a document with [BinaryXmlSerializer].
34  *
35  * @see BinaryXmlSerializer.startDocument
36  * @see BinaryXmlSerializer.endDocument
37  */
38 @Throws(IOException::class)
documentnull39 inline fun BinaryXmlSerializer.document(block: BinaryXmlSerializer.() -> Unit) {
40     startDocument(null, true)
41     block()
42     endDocument()
43 }
44 
45 /**
46  * Write a tag with [BinaryXmlSerializer].
47  *
48  * @see BinaryXmlSerializer.startTag
49  * @see BinaryXmlSerializer.endTag
50  */
51 @Throws(IOException::class)
tagnull52 inline fun BinaryXmlSerializer.tag(name: String, block: BinaryXmlSerializer.() -> Unit) {
53     startTag(null, name)
54     block()
55     endTag(null, name)
56 }
57 
58 /** @see BinaryXmlSerializer.attribute */
59 @Suppress("NOTHING_TO_INLINE")
60 @Throws(IOException::class)
attributenull61 inline fun BinaryXmlSerializer.attribute(name: String, value: String) {
62     attribute(null, name, value)
63 }
64 
65 /** @see BinaryXmlSerializer.attributeInterned */
66 @Suppress("NOTHING_TO_INLINE")
67 @Throws(IOException::class)
attributeInternednull68 inline fun BinaryXmlSerializer.attributeInterned(name: String, value: String) {
69     attributeInterned(null, name, value)
70 }
71 
72 /** @see BinaryXmlSerializer.attributeBytesHex */
73 @Suppress("NOTHING_TO_INLINE")
74 @Throws(IOException::class)
attributeBytesHexnull75 inline fun BinaryXmlSerializer.attributeBytesHex(name: String, value: ByteArray) {
76     attributeBytesHex(null, name, value)
77 }
78 
79 /** @see BinaryXmlSerializer.attributeBytesBase64 */
80 @Suppress("NOTHING_TO_INLINE")
81 @Throws(IOException::class)
attributeBytesBase64null82 inline fun BinaryXmlSerializer.attributeBytesBase64(name: String, value: ByteArray) {
83     attributeBytesBase64(null, name, value)
84 }
85 
86 /** @see BinaryXmlSerializer.attributeInt */
87 @Suppress("NOTHING_TO_INLINE")
88 @Throws(IOException::class)
attributeIntnull89 inline fun BinaryXmlSerializer.attributeInt(name: String, value: Int) {
90     attributeInt(null, name, value)
91 }
92 
93 /** @see BinaryXmlSerializer.attributeInt */
94 @Suppress("NOTHING_TO_INLINE")
95 @Throws(IOException::class)
attributeIntWithDefaultnull96 inline fun BinaryXmlSerializer.attributeIntWithDefault(
97     name: String,
98     value: Int,
99     defaultValue: Int
100 ) {
101     if (value != defaultValue) {
102         attributeInt(null, name, value)
103     }
104 }
105 
106 /** @see BinaryXmlSerializer.attributeIntHex */
107 @Suppress("NOTHING_TO_INLINE")
108 @Throws(IOException::class)
attributeIntHexnull109 inline fun BinaryXmlSerializer.attributeIntHex(name: String, value: Int) {
110     attributeIntHex(null, name, value)
111 }
112 
113 /** @see BinaryXmlSerializer.attributeIntHex */
114 @Suppress("NOTHING_TO_INLINE")
115 @Throws(IOException::class)
attributeIntHexWithDefaultnull116 inline fun BinaryXmlSerializer.attributeIntHexWithDefault(
117     name: String,
118     value: Int,
119     defaultValue: Int
120 ) {
121     if (value != defaultValue) {
122         attributeIntHex(null, name, value)
123     }
124 }
125 
126 /** @see BinaryXmlSerializer.attributeLong */
127 @Suppress("NOTHING_TO_INLINE")
128 @Throws(IOException::class)
attributeLongnull129 inline fun BinaryXmlSerializer.attributeLong(name: String, value: Long) {
130     attributeLong(null, name, value)
131 }
132 
133 /** @see BinaryXmlSerializer.attributeLong */
134 @Suppress("NOTHING_TO_INLINE")
135 @Throws(IOException::class)
attributeLongWithDefaultnull136 inline fun BinaryXmlSerializer.attributeLongWithDefault(
137     name: String,
138     value: Long,
139     defaultValue: Long
140 ) {
141     if (value != defaultValue) {
142         attributeLong(null, name, value)
143     }
144 }
145 
146 /** @see BinaryXmlSerializer.attributeLongHex */
147 @Suppress("NOTHING_TO_INLINE")
148 @Throws(IOException::class)
attributeLongHexnull149 inline fun BinaryXmlSerializer.attributeLongHex(name: String, value: Long) {
150     attributeLongHex(null, name, value)
151 }
152 
153 /** @see BinaryXmlSerializer.attributeLongHex */
154 @Suppress("NOTHING_TO_INLINE")
155 @Throws(IOException::class)
attributeLongHexWithDefaultnull156 inline fun BinaryXmlSerializer.attributeLongHexWithDefault(
157     name: String,
158     value: Long,
159     defaultValue: Long
160 ) {
161     if (value != defaultValue) {
162         attributeLongHex(null, name, value)
163     }
164 }
165 
166 /** @see BinaryXmlSerializer.attributeFloat */
167 @Suppress("NOTHING_TO_INLINE")
168 @Throws(IOException::class)
attributeFloatnull169 inline fun BinaryXmlSerializer.attributeFloat(name: String, value: Float) {
170     attributeFloat(null, name, value)
171 }
172 
173 /** @see BinaryXmlSerializer.attributeFloat */
174 @Suppress("NOTHING_TO_INLINE")
175 @Throws(IOException::class)
attributeFloatWithDefaultnull176 inline fun BinaryXmlSerializer.attributeFloatWithDefault(
177     name: String,
178     value: Float,
179     defaultValue: Float
180 ) {
181     if (value != defaultValue) {
182         attributeFloat(null, name, value)
183     }
184 }
185 
186 /** @see BinaryXmlSerializer.attributeDouble */
187 @Suppress("NOTHING_TO_INLINE")
188 @Throws(IOException::class)
attributeDoublenull189 inline fun BinaryXmlSerializer.attributeDouble(name: String, value: Double) {
190     attributeDouble(null, name, value)
191 }
192 
193 /** @see BinaryXmlSerializer.attributeDouble */
194 @Suppress("NOTHING_TO_INLINE")
195 @Throws(IOException::class)
attributeDoubleWithDefaultnull196 inline fun BinaryXmlSerializer.attributeDoubleWithDefault(
197     name: String,
198     value: Double,
199     defaultValue: Double
200 ) {
201     if (value != defaultValue) {
202         attributeDouble(null, name, value)
203     }
204 }
205 
206 /** @see BinaryXmlSerializer.attributeBoolean */
207 @Suppress("NOTHING_TO_INLINE")
208 @Throws(IOException::class)
attributeBooleannull209 inline fun BinaryXmlSerializer.attributeBoolean(name: String, value: Boolean) {
210     attributeBoolean(null, name, value)
211 }
212 
213 /** @see BinaryXmlSerializer.attributeBoolean */
214 @Suppress("NOTHING_TO_INLINE")
215 @Throws(IOException::class)
attributeBooleanWithDefaultnull216 inline fun BinaryXmlSerializer.attributeBooleanWithDefault(
217     name: String,
218     value: Boolean,
219     defaultValue: Boolean
220 ) {
221     if (value != defaultValue) {
222         attributeBoolean(null, name, value)
223     }
224 }
225