Make duplicated column names unique during import by appending indices to them.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@392 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-04-06 17:55:36 +00:00
parent 9d9329ca96
commit 5320cc6587
2 changed files with 26 additions and 1 deletions

View File

@ -5,7 +5,9 @@ import java.io.InputStream;
import java.io.Reader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang.NotImplementedException;
@ -106,8 +108,19 @@ public class ExcelImporter implements Importer {
/*
* Create columns
*/
Map<String, Integer> nameToIndex = new HashMap<String, Integer>();
for (int c = 0; c < nonBlankIndices.size(); c++) {
Column column = new Column(c, nonBlankHeaderStrings.get(c));
String cell = nonBlankHeaderStrings.get(c);
if (nameToIndex.containsKey(cell)) {
int index = nameToIndex.get(cell);
nameToIndex.put(cell, index + 1);
cell = cell.contains(" ") ? (cell + " " + index) : (cell + index);
} else {
nameToIndex.put(cell, 2);
}
Column column = new Column(c, cell);
project.columnModel.columns.add(column);
}

View File

@ -3,6 +3,8 @@ package com.metaweb.gridworks.importers;
import java.io.InputStream;
import java.io.LineNumberReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang.NotImplementedException;
@ -46,6 +48,7 @@ public class TsvCsvImporter implements Importer {
if (first) {
String[] cells = StringUtils.splitPreserveAllTokens(line, sep);
Map<String, Integer> nameToIndex = new HashMap<String, Integer>();
first = false;
for (int c = 0; c < cells.length; c++) {
@ -54,6 +57,15 @@ public class TsvCsvImporter implements Importer {
cell = cell.substring(1, cell.length() - 1);
}
if (nameToIndex.containsKey(cell)) {
int index = nameToIndex.get(cell);
nameToIndex.put(cell, index + 1);
cell = cell.contains(" ") ? (cell + " " + index) : (cell + index);
} else {
nameToIndex.put(cell, 2);
}
Column column = new Column(c, cell);
project.columnModel.columns.add(column);