
cucumber.table.TableConverter Maven / Gradle / Ivy
package cucumber.table;
import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.SingleValueConverter;
import com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import cucumber.runtime.CucumberException;
import cucumber.runtime.converters.LocalizedXStreams;
import cucumber.table.xstream.CellWriter;
import cucumber.table.xstream.ComplexTypeWriter;
import cucumber.table.xstream.ListOfComplexTypeReader;
import cucumber.table.xstream.ListOfSingleValueWriter;
import gherkin.formatter.model.Comment;
import gherkin.formatter.model.DataTableRow;
import gherkin.util.Mapper;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static cucumber.runtime.Utils.listItemType;
import static cucumber.runtime.Utils.mapKeyType;
import static cucumber.runtime.Utils.mapValueType;
import static gherkin.util.FixJava.map;
import static java.util.Arrays.asList;
/**
* This class converts a {@link DataTable to various other types}
*/
public class TableConverter {
private static final List NO_COMMENTS = Collections.emptyList();
private final LocalizedXStreams.LocalizedXStream xStream;
private final String dateFormat;
public TableConverter(LocalizedXStreams.LocalizedXStream xStream, String dateFormat) {
this.xStream = xStream;
this.dateFormat = dateFormat;
}
public T convert(Type type, DataTable dataTable) {
try {
xStream.setDateFormat(dateFormat);
if (type == null || (type instanceof Class && ((Class) type).isAssignableFrom(DataTable.class))) {
return (T) dataTable;
}
Type itemType = listItemType(type);
if (itemType == null) {
throw new CucumberException("Not a List type: " + type);
}
Type listItemType = listItemType(itemType);
if (listItemType == null) {
SingleValueConverter singleValueConverter = xStream.getSingleValueConverter(itemType);
if (singleValueConverter != null) {
return (T) toListOfSingleValue(dataTable, singleValueConverter);
} else {
if (itemType instanceof Class) {
if (Map.class.equals(itemType)) {
// Non-generic map
SingleValueConverter mapKeyConverter = xStream.getSingleValueConverter(String.class);
SingleValueConverter mapValueConverter = xStream.getSingleValueConverter(String.class);
return (T) toListOfSingleValueMap(dataTable, mapKeyConverter, mapValueConverter);
} else {
return (T) toListOfComplexType(dataTable, (Class) itemType);
}
} else {
SingleValueConverter mapKeyConverter = xStream.getSingleValueConverter(mapKeyType(itemType));
SingleValueConverter mapValueConverter = xStream.getSingleValueConverter(mapValueType(itemType));
if (mapKeyConverter != null && mapValueConverter != null) {
return (T) toListOfSingleValueMap(dataTable, mapKeyConverter, mapValueConverter);
} else {
throw new CucumberException("Can't convert a table to " + type + ". When using List, SomeComplexType must not be a generic type");
}
}
}
} else {
SingleValueConverter singleValueConverter = xStream.getSingleValueConverter(listItemType);
if (singleValueConverter != null) {
return (T) toListOfListOfSingleValue(dataTable, singleValueConverter);
} else {
// List>
throw new CucumberException("Can't convert to " + type.toString());
}
}
} finally {
xStream.unsetDateFormat();
}
}
private List toListOfComplexType(DataTable dataTable, Class itemType) {
HierarchicalStreamReader reader = new ListOfComplexTypeReader(itemType, convertTopCellsToFieldNames(dataTable), dataTable.cells(1));
try {
return Collections.unmodifiableList((List) xStream.unmarshal(reader));
} catch (AbstractReflectionConverter.UnknownFieldException e) {
throw new CucumberException(e.getShortMessage());
} catch (ConversionException e) {
throw new CucumberException(String.format("Can't assign null value to one of the primitive fields in %s. Please use boxed types.", e.get("class")));
}
}
private List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy