use of cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter in project cucumber-jvm by cucumber.
the class ParameterInfo method convert.
public Object convert(String value, LocalizedXStreams.LocalizedXStream xStream) {
try {
xStream.setParameterInfo(this);
SingleValueConverter converter;
xStream.processAnnotations(getRawType());
// Needed to unlock annotation processing
xStream.autodetectAnnotations(true);
if (transformer != null) {
transformer.setParameterInfoAndLocale(this, xStream.getLocale());
converter = transformer;
} else {
if (List.class.isAssignableFrom(getRawType())) {
converter = getListConverter(type, xStream);
} else {
converter = xStream.getSingleValueConverter(getRawType());
}
if (converter == null) {
throw new CucumberException(String.format("Don't know how to convert \"%s\" into %s.\n" + "Try writing your own converter:\n" + "\n" + "@%s(%sConverter.class)\n" + "public class %s {}\n", value, getRawType().getName(), XStreamConverter.class.getName(), getRawType().getSimpleName(), getRawType().getSimpleName()));
}
}
return converter.fromString(value);
} finally {
xStream.unsetParameterInfo();
}
}
use of cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter in project cucumber-jvm by cucumber.
the class ParameterInfo method getListConverter.
private SingleValueConverter getListConverter(Type type, LocalizedXStreams.LocalizedXStream xStream) {
Class elementType = type instanceof ParameterizedType ? getRawType(((ParameterizedType) type).getActualTypeArguments()[0]) : Object.class;
SingleValueConverter elementConverter = xStream.getSingleValueConverter(elementType);
if (elementConverter == null) {
return null;
} else {
return xStream.createListConverter(delimiter, elementConverter);
}
}
use of cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter in project cucumber-jvm by cucumber.
the class TableConverter method convert.
/**
* This method converts a {@link cucumber.api.DataTable} to abother type.
* When a Step Definition is passed a Gherkin Data Table, the runtime will use this method to convert the
* {@link cucumber.api.DataTable} to the declared type before invoking the Step Definition.
* <p/>
* This method uses reflection to inspect the type and delegates to the appropriate {@code toXxx} method.
*
* @param dataTable the table to convert
* @param type the type to convert to
* @param transposed whether the table should be transposed first.
* @return the transformed object.
*/
public <T> T convert(DataTable dataTable, Type type, boolean transposed) {
if (transposed) {
dataTable = dataTable.transpose();
}
if (type == null || (type instanceof Class && ((Class) type).isAssignableFrom(DataTable.class))) {
return (T) dataTable;
}
Type mapKeyType = mapKeyType(type);
if (mapKeyType != null) {
Type mapValueType = mapValueType(type);
return (T) toMap(dataTable, mapKeyType, mapValueType);
}
Type itemType = listItemType(type);
if (itemType == null) {
throw new CucumberException("Not a Map or List type: " + type);
}
Type listItemType = listItemType(itemType);
if (listItemType != null) {
return (T) toLists(dataTable, listItemType);
} else {
SingleValueConverter singleValueConverter = xStream.getSingleValueConverter(itemType);
if (singleValueConverter != null) {
return (T) toList(dataTable, singleValueConverter);
} else {
if (itemType instanceof Class) {
if (Map.class.equals(itemType)) {
// Non-generic map
return (T) toMaps(dataTable, String.class, String.class);
} else {
return (T) toListOfComplexType(dataTable, (Class) itemType);
}
} else {
return (T) toMaps(dataTable, mapKeyType(itemType), mapValueType(itemType));
}
}
}
}
use of cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter in project cucumber-jvm by cucumber.
the class TableConverter method toLists.
public <T> List<List<T>> toLists(DataTable dataTable, Type itemType) {
try {
xStream.setParameterInfo(parameterInfo);
SingleValueConverter itemConverter = xStream.getSingleValueConverter(itemType);
if (itemConverter == null) {
throw new CucumberException(String.format("Can't convert DataTable to List<List<%s>>", itemType));
}
List<List<T>> result = new ArrayList<List<T>>();
for (List<String> row : dataTable.raw()) {
List<T> convertedRow = new ArrayList<T>();
for (String cell : row) {
convertedRow.add((T) itemConverter.fromString(cell));
}
result.add(Collections.unmodifiableList(convertedRow));
}
return Collections.unmodifiableList(result);
} finally {
xStream.unsetParameterInfo();
}
}
use of cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter in project cucumber-jvm by cucumber.
the class TableConverter method toMap.
public <K, V> Map<K, V> toMap(DataTable dataTable, Type keyType, Type valueType) {
try {
xStream.setParameterInfo(parameterInfo);
SingleValueConverter keyConverter = xStream.getSingleValueConverter(keyType);
SingleValueConverter valueConverter = xStream.getSingleValueConverter(valueType);
if (keyConverter == null || valueConverter == null) {
throw new CucumberException(String.format("Can't convert DataTable to Map<%s,%s>", keyType, valueType));
}
Map<K, V> result = new LinkedHashMap<K, V>();
for (List<String> row : dataTable.raw()) {
if (row.size() != 2) {
throw new CucumberException("A DataTable can only be converted to a Map when there are 2 columns");
}
K key = (K) keyConverter.fromString(row.get(0));
V value = (V) valueConverter.fromString(row.get(1));
result.put(key, value);
}
return Collections.unmodifiableMap(result);
} finally {
xStream.unsetParameterInfo();
}
}
Aggregations