blob: 18c5d2613f82f469e358ed3acfcdfbd87a1e3039 [file] [log] [blame]
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.compatibility.common.util;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Load dynamic config for test cases
*/
public class DynamicConfig {
public final static String MODULE_NAME = "module-name";
//XML constant
public static final String NS = null;
public static final String CONFIG_TAG = "dynamicConfig";
public static final String ENTRY_TAG = "entry";
public static final String VALUE_TAG = "value";
public static final String KEY_ATTR = "key";
public final static String CONFIG_FOLDER_ON_DEVICE = "/sdcard/dynamic-config-files/";
public final static String CONFIG_FOLDER_ON_HOST =
System.getProperty("java.io.tmpdir") + "/dynamic-config-files/";
public final static String MERGED_CONFIG_FILE_FOLDER =
System.getProperty("java.io.tmpdir") + "/dynamic-config-files/merged";
protected Map<String, List<String>> mDynamicConfigMap = new HashMap<String, List<String>>();
protected void initializeConfig(File file) throws XmlPullParserException, IOException {
mDynamicConfigMap = createConfigMap(file);
}
public String getValue(String key) {
return mDynamicConfigMap.get(key).get(0);
}
public List<String> getValues(String key) {
return mDynamicConfigMap.get(key);
}
public static File getConfigFile(File configFolder, String moduleName)
throws FileNotFoundException {
File config = new File(configFolder, String.format("%s.dynamic", moduleName));
if (!config.exists()) {
throw new FileNotFoundException(String.format("Cannot find %s.dynamic", moduleName));
}
return config;
}
public static Map<String, List<String>> createConfigMap(File file)
throws XmlPullParserException, IOException {
Map<String, List<String>> dynamicConfigMap = new HashMap<String, List<String>>();
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setInput(new InputStreamReader(new FileInputStream(file)));
parser.nextTag();
parser.require(XmlPullParser.START_TAG, NS, CONFIG_TAG);
while (parser.nextTag() == XmlPullParser.START_TAG) {
parser.require(XmlPullParser.START_TAG, NS, ENTRY_TAG);
String key = parser.getAttributeValue(NS, KEY_ATTR);
List<String> valueList = new ArrayList<String>();
while (parser.nextTag() == XmlPullParser.START_TAG) {
parser.require(XmlPullParser.START_TAG, NS, VALUE_TAG);
valueList.add(parser.nextText());
parser.require(XmlPullParser.END_TAG, NS, VALUE_TAG);
}
parser.require(XmlPullParser.END_TAG, NS, ENTRY_TAG);
if (key != null && !key.isEmpty()) {
dynamicConfigMap.put(key, valueList);
}
}
parser.require(XmlPullParser.END_TAG, NS, CONFIG_TAG);
return dynamicConfigMap;
}
}