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 import java.util.Set; 33 34 /** 35 * Load dynamic config for test cases 36 */ 37 public class DynamicConfig { 38 39 //XML constant 40 public static final String NS = null; 41 public static final String CONFIG_TAG = "dynamicConfig"; 42 public static final String ENTRY_TAG = "entry"; 43 public static final String VALUE_TAG = "value"; 44 public static final String KEY_ATTR = "key"; 45 public final static String CONFIG_FOLDER_ON_DEVICE = "/sdcard/dynamic-config-files/"; 46 47 protected Map<String, List<String>> mDynamicConfigMap = new HashMap<String, List<String>>(); 48 initializeConfig(File file)49 protected void initializeConfig(File file) throws XmlPullParserException, IOException { 50 mDynamicConfigMap = createConfigMap(file); 51 } 52 getValue(String key)53 public String getValue(String key) { 54 List<String> singleValue = mDynamicConfigMap.get(key); 55 if (singleValue == null || singleValue.size() == 0 || singleValue.size() > 1) { 56 // key must exist in the map, and map to a list containing exactly one string 57 return null; 58 } 59 return singleValue.get(0); 60 } 61 getValues(String key)62 public List<String> getValues(String key) { 63 return mDynamicConfigMap.get(key); 64 } 65 keySet()66 public Set<String> keySet() { 67 return mDynamicConfigMap.keySet(); 68 } 69 getConfigFile(File configFolder, String moduleName)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 createConfigMap(File file)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