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.ondevicepersonalization.services.manifest;
18 
19 
20 
21 import com.android.ondevicepersonalization.internal.util.LoggerFactory;
22 
23 import org.xmlpull.v1.XmlPullParser;
24 import org.xmlpull.v1.XmlPullParserException;
25 
26 import java.io.IOException;
27 
28 /** Parser and validator for OnDevicePersonalization app manifest configs. */
29 public class AppManifestConfigParser {
30     private static final LoggerFactory.Logger sLogger = LoggerFactory.getLogger();
31     private static final String TAG = "AppManifestConfigParser";
32     private static final String TAG_ON_DEVICE_PERSONALIZATION_CONFIG = "on-device-personalization";
33     private static final String TAG_DOWNLOAD_SETTINGS = "download-settings";
34     private static final String TAG_FEDERATED_COMPUTE_SETTINGS = "federated-compute-settings";
35     private static final String TAG_SERVICE = "service";
36     private static final String ATTR_DOWNLOAD_URL = "url";
37     private static final String ATTR_FC_URL = "url";
38     private static final String ATTR_NAME = "name";
39 
AppManifestConfigParser()40     private AppManifestConfigParser() {
41     }
42 
43     /**
44      * Parses and validates given XML resource
45      *
46      * @param parser the XmlParser representing the OnDevicePersonalization app manifest config
47      */
getConfig(XmlPullParser parser)48     public static AppManifestConfig getConfig(XmlPullParser parser) throws IOException,
49             XmlPullParserException {
50         String downloadUrl = null;
51         String serviceName = null;
52         String fcServerUrl = null;
53 
54         while (parser.getEventType() != XmlPullParser.START_TAG) {
55             parser.next();
56         }
57         parser.require(XmlPullParser.START_TAG, null, TAG_ON_DEVICE_PERSONALIZATION_CONFIG);
58         parser.next();
59         while (parser.getEventType() != XmlPullParser.START_TAG) {
60             parser.next();
61         }
62         parser.require(XmlPullParser.START_TAG, null, TAG_SERVICE);
63         serviceName = parser.getAttributeValue(null, ATTR_NAME);
64         parser.next();
65 
66         // Walk through the config to parse required values.
67         while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
68             if (parser.getEventType() != XmlPullParser.START_TAG) {
69                 parser.next();
70                 continue;
71             }
72             switch (parser.getName()) {
73                 case TAG_DOWNLOAD_SETTINGS:
74                     downloadUrl = parser.getAttributeValue(null, ATTR_DOWNLOAD_URL);
75                     break;
76                 case TAG_FEDERATED_COMPUTE_SETTINGS:
77                     fcServerUrl = parser.getAttributeValue(null, ATTR_FC_URL);
78                     break;
79                 default:
80                     sLogger.i(TAG + ": Unknown tag: " + parser.getName());
81             }
82             parser.next();
83         }
84 
85         return new AppManifestConfig(downloadUrl, serviceName, fcServerUrl);
86     }
87 }
88