public ResourceSet createResources(StringTable table) throws ParseException {
assert table != null;
String uriType = uriHeaderProvider.nextLabel();
ResourceSet resources = new DefaultResourceSet();
resources.setLabel("import"); // TODO changeable, inc number
if (table.getRowCount() == 0) {
return resources;
}
// use first row to determine types
DataType[] columnTypes = new DataType[table.getColumnCount()];
for (int column = 0; column < table.getColumnCount(); column++) {
String stringValue = table.getValue(0, column);
if (NUMBER_DETECTION_REGEX.test(stringValue)) {
columnTypes[column] = DataType.NUMBER;
} else if (DATE_FORMAT_1_REGEX.test(stringValue)) {
columnTypes[column] = DataType.DATE;
} else if (DATE_FORMAT_2_REGEX.test(stringValue)) {
columnTypes[column] = DataType.DATE;
} else if (LOCATION_DETECTION_REGEX.test(stringValue)) {
columnTypes[column] = DataType.LOCATION;
} else {
columnTypes[column] = DataType.TEXT;
}
}
for (int row = 0; row < table.getRowCount(); row++) {
String uri = uriType + ":" + row;
Resource resource = new Resource(uri);
for (int column = 0; column < table.getColumnCount(); column++) {
String stringValue = table.getValue(row, column);
/*
* TODO should not be parsed at this point - change once setting
* property types possible
*/
Serializable value;
switch (columnTypes[column]) {
case NUMBER:
if (!NUMBER_DETECTION_REGEX.test(stringValue)) {
throw new ParseException("Invalid number", stringValue,
row + 1);
}
value = new Double(stringValue);
break;
case DATE:
if (DATE_FORMAT_1_REGEX.test(stringValue)) {
value = parseDate1(stringValue);
} else if (DATE_FORMAT_2_REGEX.test(stringValue)) {
value = parseDate2(stringValue);
} else {
throw new ParseException("Invalid date", stringValue,
row + 1);
}
break;
case LOCATION:
if (!LOCATION_DETECTION_REGEX.test(stringValue)) {
throw new ParseException("Invalid location",
stringValue, row + 1);
}
Resource locationResource = new Resource();
String[] split = stringValue.split("\\/");
locationResource.putValue(ResourceSetUtils.LATITUDE,
Double.parseDouble(split[0]));
locationResource.putValue(ResourceSetUtils.LONGITUDE,
Double.parseDouble(split[1]));
value = locationResource;
break;
default:
value = stringValue;
break;
}
resource.putValue(table.getColumnName(column), value);
}
resources.add(resource);
}
return resources;
}