• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
24  * Serialize content into [OutputStream] with [BinaryXmlSerializer].
25  */
26 @Throws(IOException::class)
serializeBinaryXmlnull27 inline fun OutputStream.serializeBinaryXml(block: BinaryXmlSerializer.() -> Unit) {
28     BinaryXmlSerializer().apply {
29         setOutput(this@serializeBinaryXml, null)
30         document(block)
31     }
32 }
33 
34 /**
35  * Write a document with [BinaryXmlSerializer].
36  *
37  * @see BinaryXmlSerializer.startDocument
38  * @see BinaryXmlSerializer.endDocument
39  */
40 @Throws(IOException::class)
documentnull41 inline fun BinaryXmlSerializer.document(block: BinaryXmlSerializer.() -> Unit) {
42     startDocument(null, true)
43     block()
44     endDocument()
45 }
46 
47 /**
48  * Write a tag with [BinaryXmlSerializer].
49  *
50  * @see BinaryXmlSerializer.startTag
51  * @see BinaryXmlSerializer.endTag
52  */
53 @Throws(IOException::class)
tagnull54 inline fun BinaryXmlSerializer.tag(name: String, block: BinaryXmlSerializer.() -> Unit) {
55     startTag(null, name)
56     block()
57     endTag(null, name)
58 }
59 
60 /**
61  * @see BinaryXmlSerializer.attribute
62  */
63 @Suppress("NOTHING_TO_INLINE")
64 @Throws(IOException::class)
attributenull65 inline fun BinaryXmlSerializer.attribute(name: String, value: String) {
66     attribute(null, name, value)
67 }
68 
69 /**
70  * @see BinaryXmlSerializer.attributeInterned
71  */
72 @Suppress("NOTHING_TO_INLINE")
73 @Throws(IOException::class)
attributeInternednull74 inline fun BinaryXmlSerializer.attributeInterned(name: String, value: String) {
75     attributeInterned(null, name, value)
76 }
77 
78 /**
79  * @see BinaryXmlSerializer.attributeBytesHex
80  */
81 @Suppress("NOTHING_TO_INLINE")
82 @Throws(IOException::class)
attributeBytesHexnull83 inline fun BinaryXmlSerializer.attributeBytesHex(name: String, value: ByteArray) {
84     attributeBytesHex(null, name, value)
85 }
86 
87 /**
88  * @see BinaryXmlSerializer.attributeBytesBase64
89  */
90 @Suppress("NOTHING_TO_INLINE")
91 @Throws(IOException::class)
attributeBytesBase64null92 inline fun BinaryXmlSerializer.attributeBytesBase64(name: String, value: ByteArray) {
93     attributeBytesBase64(null, name, value)
94 }
95 
96 /**
97  * @see BinaryXmlSerializer.attributeInt
98  */
99 @Suppress("NOTHING_TO_INLINE")
100 @Throws(IOException::class)
attributeIntnull101 inline fun BinaryXmlSerializer.attributeInt(name: String, value: Int) {
102     attributeInt(null, name, value)
103 }
104 
105 /**
106  * @see BinaryXmlSerializer.attributeInt
107  */
108 @Suppress("NOTHING_TO_INLINE")
109 @Throws(IOException::class)
attributeIntWithDefaultnull110 inline fun BinaryXmlSerializer.attributeIntWithDefault(
111     name: String,
112     value: Int,
113     defaultValue: Int
114 ) {
115     if (value != defaultValue) {
116         attributeInt(null, name, value)
117     }
118 }
119 
120 /**
121  * @see BinaryXmlSerializer.attributeIntHex
122  */
123 @Suppress("NOTHING_TO_INLINE")
124 @Throws(IOException::class)
attributeIntHexnull125 inline fun BinaryXmlSerializer.attributeIntHex(name: String, value: Int) {
126     attributeIntHex(null, name, value)
127 }
128 
129 /**
130  * @see BinaryXmlSerializer.attributeIntHex
131  */
132 @Suppress("NOTHING_TO_INLINE")
133 @Throws(IOException::class)
attributeIntHexWithDefaultnull134 inline fun BinaryXmlSerializer.attributeIntHexWithDefault(
135     name: String,
136     value: Int,
137     defaultValue: Int
138 ) {
139     if (value != defaultValue) {
140         attributeIntHex(null, name, value)
141     }
142 }
143 
144 /**
145  * @see BinaryXmlSerializer.attributeLong
146  */
147 @Suppress("NOTHING_TO_INLINE")
148 @Throws(IOException::class)
attributeLongnull149 inline fun BinaryXmlSerializer.attributeLong(name: String, value: Long) {
150     attributeLong(null, name, value)
151 }
152 
153 /**
154  * @see BinaryXmlSerializer.attributeLong
155  */
156 @Suppress("NOTHING_TO_INLINE")
157 @Throws(IOException::class)
attributeLongWithDefaultnull158 inline fun BinaryXmlSerializer.attributeLongWithDefault(
159     name: String,
160     value: Long,
161     defaultValue: Long
162 ) {
163     if (value != defaultValue) {
164         attributeLong(null, name, value)
165     }
166 }
167 
168 /**
169  * @see BinaryXmlSerializer.attributeLongHex
170  */
171 @Suppress("NOTHING_TO_INLINE")
172 @Throws(IOException::class)
attributeLongHexnull173 inline fun BinaryXmlSerializer.attributeLongHex(name: String, value: Long) {
174     attributeLongHex(null, name, value)
175 }
176 
177 /**
178  * @see BinaryXmlSerializer.attributeLongHex
179  */
180 @Suppress("NOTHING_TO_INLINE")
181 @Throws(IOException::class)
attributeLongHexWithDefaultnull182 inline fun BinaryXmlSerializer.attributeLongHexWithDefault(
183     name: String,
184     value: Long,
185     defaultValue: Long
186 ) {
187     if (value != defaultValue) {
188         attributeLongHex(null, name, value)
189     }
190 }
191 
192 /**
193  * @see BinaryXmlSerializer.attributeFloat
194  */
195 @Suppress("NOTHING_TO_INLINE")
196 @Throws(IOException::class)
attributeFloatnull197 inline fun BinaryXmlSerializer.attributeFloat(name: String, value: Float) {
198     attributeFloat(null, name, value)
199 }
200 
201 /**
202  * @see BinaryXmlSerializer.attributeFloat
203  */
204 @Suppress("NOTHING_TO_INLINE")
205 @Throws(IOException::class)
attributeFloatWithDefaultnull206 inline fun BinaryXmlSerializer.attributeFloatWithDefault(
207     name: String,
208     value: Float,
209     defaultValue: Float
210 ) {
211     if (value != defaultValue) {
212         attributeFloat(null, name, value)
213     }
214 }
215 
216 /**
217  * @see BinaryXmlSerializer.attributeDouble
218  */
219 @Suppress("NOTHING_TO_INLINE")
220 @Throws(IOException::class)
attributeDoublenull221 inline fun BinaryXmlSerializer.attributeDouble(name: String, value: Double) {
222     attributeDouble(null, name, value)
223 }
224 
225 /**
226  * @see BinaryXmlSerializer.attributeDouble
227  */
228 @Suppress("NOTHING_TO_INLINE")
229 @Throws(IOException::class)
attributeDoubleWithDefaultnull230 inline fun BinaryXmlSerializer.attributeDoubleWithDefault(
231     name: String,
232     value: Double,
233     defaultValue: Double
234 ) {
235     if (value != defaultValue) {
236         attributeDouble(null, name, value)
237     }
238 }
239 
240 /**
241  * @see BinaryXmlSerializer.attributeBoolean
242  */
243 @Suppress("NOTHING_TO_INLINE")
244 @Throws(IOException::class)
attributeBooleannull245 inline fun BinaryXmlSerializer.attributeBoolean(name: String, value: Boolean) {
246     attributeBoolean(null, name, value)
247 }
248 
249 /**
250  * @see BinaryXmlSerializer.attributeBoolean
251  */
252 @Suppress("NOTHING_TO_INLINE")
253 @Throws(IOException::class)
attributeBooleanWithDefaultnull254 inline fun BinaryXmlSerializer.attributeBooleanWithDefault(
255     name: String,
256     value: Boolean,
257     defaultValue: Boolean
258 ) {
259     if (value != defaultValue) {
260         attributeBoolean(null, name, value)
261     }
262 }
263