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