1 /* 2 * Copyright (C) 2015 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.compatibility.common.util; 18 19 import org.xmlpull.v1.XmlPullParser; 20 import org.xmlpull.v1.XmlPullParserException; 21 import org.xmlpull.v1.XmlPullParserFactory; 22 23 import java.io.File; 24 import java.io.FileInputStream; 25 import java.io.FileNotFoundException; 26 import java.io.IOException; 27 import java.io.InputStreamReader; 28 import java.util.ArrayList; 29 import java.util.HashMap; 30 import java.util.List; 31 import java.util.Map; 32 33 /** 34 * Load dynamic config for test cases 35 */ 36 public class DynamicConfig { 37 38 //XML constant 39 public static final String NS = null; 40 public static final String CONFIG_TAG = "dynamicConfig"; 41 public static final String ENTRY_TAG = "entry"; 42 public static final String VALUE_TAG = "value"; 43 public static final String KEY_ATTR = "key"; 44 45 public final static String CONFIG_FOLDER_ON_DEVICE = "/sdcard/dynamic-config-files/"; 46 public final static String CONFIG_FOLDER_ON_HOST = 47 System.getProperty("java.io.tmpdir") + "/dynamic-config-files/"; 48 public final static String MERGED_CONFIG_FILE_FOLDER = 49 System.getProperty("java.io.tmpdir") + "/dynamic-config-files/merged"; 50 51 protected Map<String, List<String>> mDynamicConfigMap = new HashMap<String, List<String>>(); 52 53 protected void initializeConfig(File file) throws XmlPullParserException, IOException { 54 mDynamicConfigMap = createConfigMap(file); 55 } 56 57 public String getValue(String key) { 58 List<String> singleValue = mDynamicConfigMap.get(key); 59 if (singleValue == null || singleValue.size() == 0 || singleValue.size() > 1) { 60 // key must exist in the map, and map to a list containing exactly one string 61 return null; 62 } 63 return singleValue.get(0); 64 } 65 66 public List<String> getValues(String key) { 67 return mDynamicConfigMap.get(key); 68 } 69 70 public static File getConfigFile(File configFolder, String moduleName) 71 throws FileNotFoundException { 72 File config = new File(configFolder, String.format("%s.dynamic", moduleName)); 73 if (!config.exists()) { 74 throw new FileNotFoundException(String.format("Cannot find %s.dynamic", moduleName)); 75 } 76 return config; 77 } 78 79 public static Map<String, List<String>> createConfigMap(File file) 80 throws XmlPullParserException, IOException { 81 82 Map<String, List<String>> dynamicConfigMap = new HashMap<String, List<String>>(); 83 XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); 84 parser.setInput(new InputStreamReader(new FileInputStream(file))); 85 parser.nextTag(); 86 parser.require(XmlPullParser.START_TAG, NS, CONFIG_TAG); 87 88 while (parser.nextTag() == XmlPullParser.START_TAG) { 89 parser.require(XmlPullParser.START_TAG, NS, ENTRY_TAG); 90 String key = parser.getAttributeValue(NS, KEY_ATTR); 91 List<String> valueList = new ArrayList<String>(); 92 while (parser.nextTag() == XmlPullParser.START_TAG) { 93 parser.require(XmlPullParser.START_TAG, NS, VALUE_TAG); 94 valueList.add(parser.nextText()); 95 parser.require(XmlPullParser.END_TAG, NS, VALUE_TAG); 96 } 97 parser.require(XmlPullParser.END_TAG, NS, ENTRY_TAG); 98 if (key != null && !key.isEmpty()) { 99 dynamicConfigMap.put(key, valueList); 100 } 101 } 102 103 parser.require(XmlPullParser.END_TAG, NS, CONFIG_TAG); 104 return dynamicConfigMap; 105 } 106 } 107