Don't die if we get passed no candidates

git-svn-id: http://google-refine.googlecode.com/svn/trunk@2210 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Tom Morris 2011-08-18 17:39:18 +00:00
parent 9025a64dd0
commit 9d7b8a5279
4 changed files with 83 additions and 77 deletions

View File

@ -79,34 +79,36 @@ public class ExcelImporter extends TabularImportingParserBase {
JSONArray sheetRecords = new JSONArray(); JSONArray sheetRecords = new JSONArray();
JSONUtilities.safePut(options, "sheetRecords", sheetRecords); JSONUtilities.safePut(options, "sheetRecords", sheetRecords);
try { try {
JSONObject firstFileRecord = fileRecords.get(0); if (fileRecords.size() > 0) {
File file = ImportingUtilities.getFile(job, firstFileRecord); JSONObject firstFileRecord = fileRecords.get(0);
InputStream is = new FileInputStream(file); File file = ImportingUtilities.getFile(job, firstFileRecord);
try { InputStream is = new FileInputStream(file);
Workbook wb = xmlBased ? try {
new XSSFWorkbook(is) : Workbook wb = xmlBased ?
new HSSFWorkbook(new POIFSFileSystem(is)); new XSSFWorkbook(is) :
new HSSFWorkbook(new POIFSFileSystem(is));
int sheetCount = wb.getNumberOfSheets();
boolean hasData = false; int sheetCount = wb.getNumberOfSheets();
for (int i = 0; i < sheetCount; i++) { boolean hasData = false;
Sheet sheet = wb.getSheetAt(i); for (int i = 0; i < sheetCount; i++) {
int rows = sheet.getLastRowNum() - sheet.getFirstRowNum() + 1; Sheet sheet = wb.getSheetAt(i);
int rows = sheet.getLastRowNum() - sheet.getFirstRowNum() + 1;
JSONObject sheetRecord = new JSONObject();
JSONUtilities.safePut(sheetRecord, "name", sheet.getSheetName()); JSONObject sheetRecord = new JSONObject();
JSONUtilities.safePut(sheetRecord, "rows", rows); JSONUtilities.safePut(sheetRecord, "name", sheet.getSheetName());
if (hasData) { JSONUtilities.safePut(sheetRecord, "rows", rows);
JSONUtilities.safePut(sheetRecord, "selected", false); if (hasData) {
} else if (rows > 1) { JSONUtilities.safePut(sheetRecord, "selected", false);
JSONUtilities.safePut(sheetRecord, "selected", true); } else if (rows > 1) {
hasData = true; JSONUtilities.safePut(sheetRecord, "selected", true);
} hasData = true;
JSONUtilities.append(sheetRecords, sheetRecord); }
JSONUtilities.append(sheetRecords, sheetRecord);
}
} finally {
is.close();
} }
} finally { }
is.close();
}
} catch (IOException e) { } catch (IOException e) {
// Ignore // Ignore
} }

View File

@ -30,25 +30,25 @@ public class FixedWidthImporter extends TabularImportingParserBase {
ImportingJob job, List<JSONObject> fileRecords, String format) { ImportingJob job, List<JSONObject> fileRecords, String format) {
JSONObject options = super.createParserUIInitializationData(job, fileRecords, format); JSONObject options = super.createParserUIInitializationData(job, fileRecords, format);
JSONArray columnWidths = new JSONArray(); JSONArray columnWidths = new JSONArray();
if (fileRecords.size() > 0) {
JSONObject firstFileRecord = fileRecords.get(0); JSONObject firstFileRecord = fileRecords.get(0);
String encoding = ImportingUtilities.getEncoding(firstFileRecord); String encoding = ImportingUtilities.getEncoding(firstFileRecord);
String location = JSONUtilities.getString(firstFileRecord, "location", null); String location = JSONUtilities.getString(firstFileRecord, "location", null);
if (location != null) { if (location != null) {
File file = new File(job.getRawDataDir(), location); File file = new File(job.getRawDataDir(), location);
int[] columnWidthsA = guessColumnWidths(file, encoding); int[] columnWidthsA = guessColumnWidths(file, encoding);
if (columnWidthsA != null) { if (columnWidthsA != null) {
for (int w : columnWidthsA) { for (int w : columnWidthsA) {
JSONUtilities.append(columnWidths, w); JSONUtilities.append(columnWidths, w);
}
} }
} }
JSONUtilities.safePut(options, "lineSeparator", "\n");
JSONUtilities.safePut(options, "headerLines", 0);
JSONUtilities.safePut(options, "columnWidths", columnWidths);
JSONUtilities.safePut(options, "guessCellValueTypes", true);
} }
JSONUtilities.safePut(options, "lineSeparator", "\n");
JSONUtilities.safePut(options, "headerLines", 0);
JSONUtilities.safePut(options, "columnWidths", columnWidths);
JSONUtilities.safePut(options, "guessCellValueTypes", true);
return options; return options;
} }

View File

@ -75,24 +75,26 @@ public class JsonImporter extends TreeImportingParserBase {
public JSONObject createParserUIInitializationData( public JSONObject createParserUIInitializationData(
ImportingJob job, List<JSONObject> fileRecords, String format) { ImportingJob job, List<JSONObject> fileRecords, String format) {
JSONObject options = super.createParserUIInitializationData(job, fileRecords, format); JSONObject options = super.createParserUIInitializationData(job, fileRecords, format);
try { if (fileRecords.size() > 0) {
JSONObject firstFileRecord = fileRecords.get(0);
File file = ImportingUtilities.getFile(job, firstFileRecord);
InputStream is = new FileInputStream(file);
try { try {
JsonFactory factory = new JsonFactory(); JSONObject firstFileRecord = fileRecords.get(0);
JsonParser parser = factory.createJsonParser(is); File file = ImportingUtilities.getFile(job, firstFileRecord);
InputStream is = new FileInputStream(file);
PreviewParsingState state = new PreviewParsingState(); try {
Object rootValue = parseForPreview(parser, state); JsonFactory factory = new JsonFactory();
if (rootValue != null) { JsonParser parser = factory.createJsonParser(is);
JSONUtilities.safePut(options, "dom", rootValue);
PreviewParsingState state = new PreviewParsingState();
Object rootValue = parseForPreview(parser, state);
if (rootValue != null) {
JSONUtilities.safePut(options, "dom", rootValue);
}
} finally {
is.close();
} }
} finally { } catch (IOException e) {
is.close(); // Ignore
} }
} catch (IOException e) {
// Ignore
} }
return options; return options;

View File

@ -73,28 +73,30 @@ public class XmlImporter extends TreeImportingParserBase {
ImportingJob job, List<JSONObject> fileRecords, String format) { ImportingJob job, List<JSONObject> fileRecords, String format) {
JSONObject options = super.createParserUIInitializationData(job, fileRecords, format); JSONObject options = super.createParserUIInitializationData(job, fileRecords, format);
try { try {
JSONObject firstFileRecord = fileRecords.get(0); if (fileRecords.size() > 0) {
File file = ImportingUtilities.getFile(job, firstFileRecord); JSONObject firstFileRecord = fileRecords.get(0);
InputStream is = new FileInputStream(file); File file = ImportingUtilities.getFile(job, firstFileRecord);
try { InputStream is = new FileInputStream(file);
XMLStreamReader parser = createXMLStreamReader(is); try {
PreviewParsingState state = new PreviewParsingState(); XMLStreamReader parser = createXMLStreamReader(is);
PreviewParsingState state = new PreviewParsingState();
while (parser.hasNext() && state.tokenCount < PREVIEW_PARSING_LIMIT) {
int tokenType = parser.next(); while (parser.hasNext() && state.tokenCount < PREVIEW_PARSING_LIMIT) {
state.tokenCount++; int tokenType = parser.next();
if (tokenType == XMLStreamConstants.START_ELEMENT) { state.tokenCount++;
JSONObject rootElement = descendElement(parser, state); if (tokenType == XMLStreamConstants.START_ELEMENT) {
if (rootElement != null) { JSONObject rootElement = descendElement(parser, state);
JSONUtilities.safePut(options, "dom", rootElement); if (rootElement != null) {
break; JSONUtilities.safePut(options, "dom", rootElement);
break;
}
} else {
// ignore everything else
} }
} else {
// ignore everything else
} }
} finally {
is.close();
} }
} finally {
is.close();
} }
} catch (XMLStreamException e) { } catch (XMLStreamException e) {
// Ignore // Ignore