1 /* 2 * Copyright (C) 2017 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 package com.android.compatibility.common.tradefed.util; 17 18 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper; 19 import com.android.compatibility.common.util.DynamicConfig; 20 import com.android.tradefed.build.IBuildInfo; 21 import com.android.tradefed.log.LogUtil.CLog; 22 23 import org.xmlpull.v1.XmlPullParserException; 24 25 import java.io.File; 26 import java.io.IOException; 27 import java.util.List; 28 import java.util.Map; 29 30 /** 31 * Utility to read the data from a dynamic config file. 32 */ 33 public class DynamicConfigFileReader { 34 35 /** 36 * Returns the value of a key from a downloaded file. 37 * 38 * @param file The file downloaded, can be retrieve via 39 * {@link CompatibilityBuildHelper#getDynamicConfigFiles()} 40 * @param key the key inside the file which value we want to return 41 * @return the value associated to the key in the config file provided. 42 */ 43 public static String getValueFromConfig(File file, String key) 44 throws XmlPullParserException, IOException { 45 DynamicConfig config = new DynamicConfig(); 46 config.initializeConfig(file); 47 return config.getValue(key); 48 } 49 50 /** 51 * Returns the multiple values of a key from a downloaded file. 52 * 53 * @param file The file downloaded, can be retrieve via 54 * {@link CompatibilityBuildHelper#getDynamicConfigFiles()} 55 * @param key the key inside the file which values we want to return 56 * @return the values associated to the key in the config file provided. 57 */ 58 public static List<String> getValuesFromConfig(File file, String key) 59 throws XmlPullParserException, IOException { 60 DynamicConfig config = new DynamicConfig(); 61 config.initializeConfig(file); 62 return config.getValues(key); 63 } 64 65 /** 66 * Returns the value of a key from the build info and module targeted. 67 * 68 * @param info the {@link IBuildInfo} of the run. 69 * @param moduleName the name of the module we need the dynamic file from. 70 * @param key the key inside the file which value we want to return 71 * @return the value associated to the key in the dynamic config associated with the module. 72 */ 73 public static String getValueFromConfig(IBuildInfo info, String moduleName, String key) 74 throws XmlPullParserException, IOException { 75 CompatibilityBuildHelper helper = new CompatibilityBuildHelper(info); 76 File dynamicConfig = helper.getDynamicConfigFiles().get(moduleName); 77 if (dynamicConfig == null) { 78 CLog.w("Config file %s, not found in the map of dynamic configs.", moduleName); 79 return null; 80 } 81 return getValueFromConfig(dynamicConfig, key); 82 } 83 84 /** 85 * Returns the multiple values of a key from the build info and module targeted. 86 * 87 * @param info the {@link IBuildInfo} of the run. 88 * @param moduleName the name of the module we need the dynamic file from. 89 * @param key the key inside the file which values we want to return 90 * @return the values associated to the key in the dynamic config associated with the module. 91 */ 92 public static List<String> getValuesFromConfig(IBuildInfo info, String moduleName, String key) 93 throws XmlPullParserException, IOException { 94 CompatibilityBuildHelper helper = new CompatibilityBuildHelper(info); 95 File dynamicConfig = helper.getDynamicConfigFiles().get(moduleName); 96 if (dynamicConfig == null) { 97 CLog.w("Config file %s, not found in the map of dynamic configs.", moduleName); 98 return null; 99 } 100 return getValuesFromConfig(dynamicConfig, key); 101 } 102 } 103