2010-09-22 19:04:10 +02:00
|
|
|
package com.google.refine.importers;
|
2010-05-05 01:24:48 +02:00
|
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
2010-09-22 19:04:10 +02:00
|
|
|
import com.google.refine.model.Column;
|
|
|
|
import com.google.refine.model.Project;
|
|
|
|
import com.google.refine.model.Row;
|
2010-05-05 01:24:48 +02:00
|
|
|
|
|
|
|
public class ImporterUtilities {
|
|
|
|
|
|
|
|
static public Serializable parseCellValue(String text) {
|
|
|
|
if (text.length() > 0) {
|
|
|
|
if (text.length() > 1 && text.startsWith("\"") && text.endsWith("\"")) {
|
2010-05-17 07:40:46 +02:00
|
|
|
return text.substring(1, text.length() - 1);
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|
2010-05-16 20:42:52 +02:00
|
|
|
|
2010-10-15 07:30:15 +02:00
|
|
|
String text2 = text.trim();
|
|
|
|
if (text2.length() > 0) {
|
|
|
|
try {
|
|
|
|
return Long.parseLong(text2);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
double d = Double.parseDouble(text2);
|
|
|
|
if (!Double.isInfinite(d) && !Double.isNaN(d)) {
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
} catch (NumberFormatException e) {
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
2010-05-16 20:42:52 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
static public int getIntegerOption(String name, Properties options, int def) {
|
|
|
|
int value = def;
|
|
|
|
if (options.containsKey(name)) {
|
|
|
|
String s = options.getProperty(name);
|
|
|
|
try {
|
|
|
|
value = Integer.parseInt(s);
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2010-05-16 20:42:52 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
static public boolean getBooleanOption(String name, Properties options, boolean def) {
|
|
|
|
boolean value = def;
|
|
|
|
if (options.containsKey(name)) {
|
|
|
|
String s = options.getProperty(name);
|
|
|
|
try {
|
2010-08-06 08:15:05 +02:00
|
|
|
value = s.equalsIgnoreCase("on") || s.equals("1") || Boolean.parseBoolean(s);
|
2010-05-05 01:24:48 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static public void appendColumnName(List<String> columnNames, int index, String name) {
|
|
|
|
name = name.trim();
|
2010-05-16 20:42:52 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
while (columnNames.size() <= index) {
|
|
|
|
columnNames.add("");
|
|
|
|
}
|
2010-05-16 20:42:52 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
if (!name.isEmpty()) {
|
|
|
|
String oldName = columnNames.get(index);
|
|
|
|
if (!oldName.isEmpty()) {
|
|
|
|
name = oldName + " " + name;
|
|
|
|
}
|
2010-05-16 20:42:52 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
columnNames.set(index, name);
|
|
|
|
}
|
|
|
|
}
|
2010-05-16 20:42:52 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
static public void ensureColumnsInRowExist(List<String> columnNames, Row row) {
|
|
|
|
int count = row.cells.size();
|
|
|
|
while (count > columnNames.size()) {
|
|
|
|
columnNames.add("");
|
|
|
|
}
|
|
|
|
}
|
2010-05-16 20:42:52 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
static public void setupColumns(Project project, List<String> columnNames) {
|
|
|
|
Map<String, Integer> nameToIndex = new HashMap<String, Integer>();
|
|
|
|
for (int c = 0; c < columnNames.size(); c++) {
|
|
|
|
String cell = columnNames.get(c).trim();
|
|
|
|
if (cell.isEmpty()) {
|
|
|
|
cell = "Column";
|
|
|
|
} else if (cell.startsWith("\"") && cell.endsWith("\"")) {
|
2010-05-17 05:22:22 +02:00
|
|
|
cell = cell.substring(1, cell.length() - 1).trim(); //FIXME is trimming quotation marks appropriate?
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|
2010-05-16 20:42:52 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
if (nameToIndex.containsKey(cell)) {
|
|
|
|
int index = nameToIndex.get(cell);
|
|
|
|
nameToIndex.put(cell, index + 1);
|
2010-05-16 20:42:52 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
cell = cell.contains(" ") ? (cell + " " + index) : (cell + index);
|
|
|
|
} else {
|
|
|
|
nameToIndex.put(cell, 2);
|
|
|
|
}
|
2010-05-16 20:42:52 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
Column column = new Column(c, cell);
|
2010-05-16 20:42:52 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
project.columnModel.columns.add(column);
|
|
|
|
}
|
|
|
|
}
|
2010-05-16 20:42:52 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|