nl.hsac.fitnesse.fixture.util.MapHelper Maven / Gradle / Ivy
package nl.hsac.fitnesse.fixture.util;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import nl.hsac.fitnesse.fixture.slim.SlimFixtureException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MapHelper {
private static final Pattern LIST_INDEX_PATTERN = Pattern.compile("(\\S+)\\[(\\d+)\\]");
protected HtmlCleaner htmlCleaner = new HtmlCleaner();
/**
* Gets value from map.
* @param map map to get value from.
* @param name name of (possibly nested) property to get value from.
* @return value found, if it could be found, null otherwise.
*/
public Object getValue(Map map, String name) {
String cleanName = htmlCleaner.cleanupValue(name);
return getValueImpl(map, cleanName, true);
}
protected Object getValueImpl(Map map, String name, boolean throwIfNoList) {
Object value = null;
if (map.containsKey(name)) {
value = map.get(name);
} else {
String[] parts = name.split("\\.", 2);
if (parts.length > 1) {
Object nested = getValueImpl(map, parts[0], throwIfNoList);
if (nested instanceof Map) {
Map nestedMap = (Map) nested;
value = getValueImpl(nestedMap, parts[1], throwIfNoList);
}
} else if (isListName(name)) {
value = getListValue(map, name);
} else if (isListIndexExpr(name)) {
value = getIndexedListValue(map, name, throwIfNoList);
}
}
return value;
}
/**
* Stores value in map.
* @param value value to be passed.
* @param name name to use this value for.
* @param map map to store value in.
*/
public void setValueForIn(Object value, String name, Map map) {
if (isListName(name)) {
String valueStr = null;
if (value != null) {
valueStr = value.toString();
}
setValuesForIn(valueStr, stripListIndicator(name), map);
} else {
if (name.endsWith("\\[]")) {
name = name.replace("\\[]", "[]");
}
String cleanName = htmlCleaner.cleanupValue(name);
Object cleanValue = getCleanValue(value);
if (map.containsKey(cleanName)) {
// overwrite current value
map.put(cleanName, cleanValue);
} else {
int firstDot = cleanName.indexOf(".");
if (firstDot > -1) {
String key = cleanName.substring(0, firstDot);
Object nested = getValueImpl(map, key, false);
if (nested == null) {
nested = new LinkedHashMap();
if (isListIndexExpr(key)) {
setIndexedListValue(map, key, nested);
} else {
map.put(key, nested);
}
}
if (nested instanceof Map) {
Map nestedMap = (Map) nested;
String lastPart = cleanName.substring(firstDot + 1);
setValueForIn(cleanValue, lastPart, nestedMap);
} else {
throw new SlimFixtureException(false, key + " is not a map, but " + nested.getClass());
}
} else if (isListIndexExpr(name)) {
setIndexedListValue(map, cleanName, cleanValue);
} else {
map.put(cleanName, cleanValue);
}
}
}
}
/**
* Adds a value to the end of a list.
* @param value value to be passed.
* @param name name to use this value for.
* @param map map to store value in.
*/
public void addValueToIn(Object value, String name, Map map) {
Object val = getValue(map, name);
if (val instanceof Collection) {
Object cleanValue = getCleanValue(value);
((Collection) val).add(cleanValue);
} else if (val == null) {
setValueForIn(value, name + "[0]", map);
} else {
throw new SlimFixtureException(false, "name is not a list but: " + val.getClass().getSimpleName());
}
}
/**
* Adds all values in the otherMap to map.
* @param otherMap to obtain values from.
* @param map map to store value in.
*/
public void copyValuesFromTo(Map otherMap, Map map) {
map.putAll(otherMap);
}
/**
* Stores list of values in map.
* @param values comma separated list of values.
* @param name name to use this list for.
* @param map map to store values in.
*/
public void setValuesForIn(String values, String name, Map map) {
String cleanName = htmlCleaner.cleanupValue(name);
String[] valueArrays = values.split("\\s*,\\s*");
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy