• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 android.util;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 
22 import org.xmlpull.v1.XmlPullParser;
23 import org.xmlpull.v1.XmlPullParserException;
24 
25 /**
26  * Specialization of {@link XmlPullParser} which adds explicit methods to
27  * support consistent and efficient conversion of primitive data types.
28  *
29  * @hide
30  */
31 public interface TypedXmlPullParser extends XmlPullParser {
32     /**
33      * @return index of requested attribute, otherwise {@code -1} if undefined
34      */
getAttributeIndex(@ullable String namespace, @NonNull String name)35     default int getAttributeIndex(@Nullable String namespace, @NonNull String name) {
36         final boolean namespaceNull = (namespace == null);
37         final int count = getAttributeCount();
38         for (int i = 0; i < count; i++) {
39             if ((namespaceNull || namespace.equals(getAttributeNamespace(i)))
40                     && name.equals(getAttributeName(i))) {
41                 return i;
42             }
43         }
44         return -1;
45     }
46 
47     /**
48      * @return index of requested attribute
49      * @throws XmlPullParserException if the value is undefined
50      */
getAttributeIndexOrThrow(@ullable String namespace, @NonNull String name)51     default int getAttributeIndexOrThrow(@Nullable String namespace, @NonNull String name)
52             throws XmlPullParserException {
53         final int index = getAttributeIndex(namespace, name);
54         if (index == -1) {
55             throw new XmlPullParserException("Missing attribute " + name);
56         } else {
57             return index;
58         }
59     }
60 
61     /**
62      * @return decoded strongly-typed {@link #getAttributeValue}
63      * @throws XmlPullParserException if the value is malformed
64      */
getAttributeBytesHex(int index)65     @NonNull byte[] getAttributeBytesHex(int index) throws XmlPullParserException;
66 
67     /**
68      * @return decoded strongly-typed {@link #getAttributeValue}
69      * @throws XmlPullParserException if the value is malformed
70      */
getAttributeBytesBase64(int index)71     @NonNull byte[] getAttributeBytesBase64(int index) throws XmlPullParserException;
72 
73     /**
74      * @return decoded strongly-typed {@link #getAttributeValue}
75      * @throws XmlPullParserException if the value is malformed
76      */
getAttributeInt(int index)77     int getAttributeInt(int index) throws XmlPullParserException;
78 
79     /**
80      * @return decoded strongly-typed {@link #getAttributeValue}
81      * @throws XmlPullParserException if the value is malformed
82      */
getAttributeIntHex(int index)83     int getAttributeIntHex(int index) throws XmlPullParserException;
84 
85     /**
86      * @return decoded strongly-typed {@link #getAttributeValue}
87      * @throws XmlPullParserException if the value is malformed
88      */
getAttributeLong(int index)89     long getAttributeLong(int index) throws XmlPullParserException;
90 
91     /**
92      * @return decoded strongly-typed {@link #getAttributeValue}
93      * @throws XmlPullParserException if the value is malformed
94      */
getAttributeLongHex(int index)95     long getAttributeLongHex(int index) throws XmlPullParserException;
96 
97     /**
98      * @return decoded strongly-typed {@link #getAttributeValue}
99      * @throws XmlPullParserException if the value is malformed
100      */
getAttributeFloat(int index)101     float getAttributeFloat(int index) throws XmlPullParserException;
102 
103     /**
104      * @return decoded strongly-typed {@link #getAttributeValue}
105      * @throws XmlPullParserException if the value is malformed
106      */
getAttributeDouble(int index)107     double getAttributeDouble(int index) throws XmlPullParserException;
108 
109     /**
110      * @return decoded strongly-typed {@link #getAttributeValue}
111      * @throws XmlPullParserException if the value is malformed
112      */
getAttributeBoolean(int index)113     boolean getAttributeBoolean(int index) throws XmlPullParserException;
114 
115     /**
116      * @return decoded strongly-typed {@link #getAttributeValue}
117      * @throws XmlPullParserException if the value is malformed or undefined
118      */
getAttributeBytesHex(@ullable String namespace, @NonNull String name)119     default @NonNull byte[] getAttributeBytesHex(@Nullable String namespace,
120             @NonNull String name) throws XmlPullParserException {
121         return getAttributeBytesHex(getAttributeIndexOrThrow(namespace, name));
122     }
123 
124     /**
125      * @return decoded strongly-typed {@link #getAttributeValue}
126      * @throws XmlPullParserException if the value is malformed or undefined
127      */
getAttributeBytesBase64(@ullable String namespace, @NonNull String name)128     default @NonNull byte[] getAttributeBytesBase64(@Nullable String namespace,
129             @NonNull String name) throws XmlPullParserException {
130         return getAttributeBytesBase64(getAttributeIndexOrThrow(namespace, name));
131     }
132 
133     /**
134      * @return decoded strongly-typed {@link #getAttributeValue}
135      * @throws XmlPullParserException if the value is malformed or undefined
136      */
getAttributeInt(@ullable String namespace, @NonNull String name)137     default int getAttributeInt(@Nullable String namespace, @NonNull String name)
138             throws XmlPullParserException {
139         return getAttributeInt(getAttributeIndexOrThrow(namespace, name));
140     }
141 
142     /**
143      * @return decoded strongly-typed {@link #getAttributeValue}
144      * @throws XmlPullParserException if the value is malformed or undefined
145      */
getAttributeIntHex(@ullable String namespace, @NonNull String name)146     default int getAttributeIntHex(@Nullable String namespace, @NonNull String name)
147             throws XmlPullParserException {
148         return getAttributeIntHex(getAttributeIndexOrThrow(namespace, name));
149     }
150 
151     /**
152      * @return decoded strongly-typed {@link #getAttributeValue}
153      * @throws XmlPullParserException if the value is malformed or undefined
154      */
getAttributeLong(@ullable String namespace, @NonNull String name)155     default long getAttributeLong(@Nullable String namespace, @NonNull String name)
156             throws XmlPullParserException {
157         return getAttributeLong(getAttributeIndexOrThrow(namespace, name));
158     }
159 
160     /**
161      * @return decoded strongly-typed {@link #getAttributeValue}
162      * @throws XmlPullParserException if the value is malformed or undefined
163      */
getAttributeLongHex(@ullable String namespace, @NonNull String name)164     default long getAttributeLongHex(@Nullable String namespace, @NonNull String name)
165             throws XmlPullParserException {
166         return getAttributeLongHex(getAttributeIndexOrThrow(namespace, name));
167     }
168 
169     /**
170      * @return decoded strongly-typed {@link #getAttributeValue}
171      * @throws XmlPullParserException if the value is malformed or undefined
172      */
getAttributeFloat(@ullable String namespace, @NonNull String name)173     default float getAttributeFloat(@Nullable String namespace, @NonNull String name)
174             throws XmlPullParserException {
175         return getAttributeFloat(getAttributeIndexOrThrow(namespace, name));
176     }
177 
178     /**
179      * @return decoded strongly-typed {@link #getAttributeValue}
180      * @throws XmlPullParserException if the value is malformed or undefined
181      */
getAttributeDouble(@ullable String namespace, @NonNull String name)182     default double getAttributeDouble(@Nullable String namespace, @NonNull String name)
183             throws XmlPullParserException {
184         return getAttributeDouble(getAttributeIndexOrThrow(namespace, name));
185     }
186 
187     /**
188      * @return decoded strongly-typed {@link #getAttributeValue}
189      * @throws XmlPullParserException if the value is malformed or undefined
190      */
getAttributeBoolean(@ullable String namespace, @NonNull String name)191     default boolean getAttributeBoolean(@Nullable String namespace, @NonNull String name)
192             throws XmlPullParserException {
193         return getAttributeBoolean(getAttributeIndexOrThrow(namespace, name));
194     }
195 
196     /**
197      * @return decoded strongly-typed {@link #getAttributeValue}, otherwise
198      *         default value if the value is malformed or undefined
199      */
getAttributeBytesHex(@ullable String namespace, @NonNull String name, @Nullable byte[] defaultValue)200     default @Nullable byte[] getAttributeBytesHex(@Nullable String namespace,
201             @NonNull String name, @Nullable byte[] defaultValue) {
202         final int index = getAttributeIndex(namespace, name);
203         if (index == -1) return defaultValue;
204         try {
205             return getAttributeBytesHex(index);
206         } catch (Exception ignored) {
207             return defaultValue;
208         }
209     }
210 
211     /**
212      * @return decoded strongly-typed {@link #getAttributeValue}, otherwise
213      *         default value if the value is malformed or undefined
214      */
getAttributeBytesBase64(@ullable String namespace, @NonNull String name, @Nullable byte[] defaultValue)215     default @Nullable byte[] getAttributeBytesBase64(@Nullable String namespace,
216             @NonNull String name, @Nullable byte[] defaultValue) {
217         final int index = getAttributeIndex(namespace, name);
218         if (index == -1) return defaultValue;
219         try {
220             return getAttributeBytesBase64(index);
221         } catch (Exception ignored) {
222             return defaultValue;
223         }
224     }
225 
226     /**
227      * @return decoded strongly-typed {@link #getAttributeValue}, otherwise
228      *         default value if the value is malformed or undefined
229      */
getAttributeInt(@ullable String namespace, @NonNull String name, int defaultValue)230     default int getAttributeInt(@Nullable String namespace, @NonNull String name,
231             int defaultValue) {
232         final int index = getAttributeIndex(namespace, name);
233         if (index == -1) return defaultValue;
234         try {
235             return getAttributeInt(index);
236         } catch (Exception ignored) {
237             return defaultValue;
238         }
239     }
240 
241     /**
242      * @return decoded strongly-typed {@link #getAttributeValue}, otherwise
243      *         default value if the value is malformed or undefined
244      */
getAttributeIntHex(@ullable String namespace, @NonNull String name, int defaultValue)245     default int getAttributeIntHex(@Nullable String namespace, @NonNull String name,
246             int defaultValue) {
247         final int index = getAttributeIndex(namespace, name);
248         if (index == -1) return defaultValue;
249         try {
250             return getAttributeIntHex(index);
251         } catch (Exception ignored) {
252             return defaultValue;
253         }
254     }
255 
256     /**
257      * @return decoded strongly-typed {@link #getAttributeValue}, otherwise
258      *         default value if the value is malformed or undefined
259      */
getAttributeLong(@ullable String namespace, @NonNull String name, long defaultValue)260     default long getAttributeLong(@Nullable String namespace, @NonNull String name,
261             long defaultValue) {
262         final int index = getAttributeIndex(namespace, name);
263         if (index == -1) return defaultValue;
264         try {
265             return getAttributeLong(index);
266         } catch (Exception ignored) {
267             return defaultValue;
268         }
269     }
270 
271     /**
272      * @return decoded strongly-typed {@link #getAttributeValue}, otherwise
273      *         default value if the value is malformed or undefined
274      */
getAttributeLongHex(@ullable String namespace, @NonNull String name, long defaultValue)275     default long getAttributeLongHex(@Nullable String namespace, @NonNull String name,
276             long defaultValue) {
277         final int index = getAttributeIndex(namespace, name);
278         if (index == -1) return defaultValue;
279         try {
280             return getAttributeLongHex(index);
281         } catch (Exception ignored) {
282             return defaultValue;
283         }
284     }
285 
286     /**
287      * @return decoded strongly-typed {@link #getAttributeValue}, otherwise
288      *         default value if the value is malformed or undefined
289      */
getAttributeFloat(@ullable String namespace, @NonNull String name, float defaultValue)290     default float getAttributeFloat(@Nullable String namespace, @NonNull String name,
291             float defaultValue) {
292         final int index = getAttributeIndex(namespace, name);
293         if (index == -1) return defaultValue;
294         try {
295             return getAttributeFloat(index);
296         } catch (Exception ignored) {
297             return defaultValue;
298         }
299     }
300 
301     /**
302      * @return decoded strongly-typed {@link #getAttributeValue}, otherwise
303      *         default value if the value is malformed or undefined
304      */
getAttributeDouble(@ullable String namespace, @NonNull String name, double defaultValue)305     default double getAttributeDouble(@Nullable String namespace, @NonNull String name,
306             double defaultValue) {
307         final int index = getAttributeIndex(namespace, name);
308         if (index == -1) return defaultValue;
309         try {
310             return getAttributeDouble(index);
311         } catch (Exception ignored) {
312             return defaultValue;
313         }
314     }
315 
316     /**
317      * @return decoded strongly-typed {@link #getAttributeValue}, otherwise
318      *         default value if the value is malformed or undefined
319      */
getAttributeBoolean(@ullable String namespace, @NonNull String name, boolean defaultValue)320     default boolean getAttributeBoolean(@Nullable String namespace, @NonNull String name,
321             boolean defaultValue) {
322         final int index = getAttributeIndex(namespace, name);
323         if (index == -1) return defaultValue;
324         try {
325             return getAttributeBoolean(index);
326         } catch (Exception ignored) {
327             return defaultValue;
328         }
329     }
330 }
331