Catch/log exceptions in the importers a bit more carefully.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@2215 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2011-08-22 21:47:15 +00:00
parent f023b922e1
commit 4113a10b5b
4 changed files with 41 additions and 23 deletions

View File

@ -52,6 +52,8 @@ import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.refine.ProjectMetadata;
import com.google.refine.importing.ImportingJob;
@ -64,6 +66,8 @@ import com.google.refine.model.ReconCandidate;
import com.google.refine.util.JSONUtilities;
public class ExcelImporter extends TabularImportingParserBase {
static final Logger logger = LoggerFactory.getLogger(ExcelImporter.class);
public ExcelImporter() {
super(true);
}
@ -110,7 +114,7 @@ public class ExcelImporter extends TabularImportingParserBase {
}
}
} catch (IOException e) {
// Ignore
logger.error("Error generating parser UI initialization data for Excel file", e);
}
return options;

View File

@ -61,6 +61,8 @@ import com.google.refine.model.Project;
import com.google.refine.util.JSONUtilities;
public class JsonImporter extends TreeImportingParserBase {
static final Logger logger = LoggerFactory.getLogger(JsonImporter.class);
public JsonImporter() {
super(false);
}
@ -93,7 +95,7 @@ public class JsonImporter extends TreeImportingParserBase {
is.close();
}
} catch (IOException e) {
// Ignore
logger.error("Error generating parser UI initialization data for JSON file", e);
}
}

View File

@ -7,6 +7,8 @@ import java.util.ArrayList;
import java.util.List;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.refine.ProjectMetadata;
import com.google.refine.importing.ImportingJob;
@ -14,6 +16,8 @@ import com.google.refine.model.Project;
import com.google.refine.util.JSONUtilities;
public class LineBasedImporter extends TabularImportingParserBase {
static final Logger logger = LoggerFactory.getLogger(LineBasedImporter.class);
public LineBasedImporter() {
super(false);
}
@ -66,7 +70,7 @@ public class LineBasedImporter extends TabularImportingParserBase {
skip--;
}
} catch (IOException e) {
e.printStackTrace();
logger.error("Error reading line-based file", e);
}
JSONUtilities.safePut(options, "ignoreLines", -1);

View File

@ -47,6 +47,8 @@ import javax.xml.stream.XMLStreamReader;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.refine.ProjectMetadata;
import com.google.refine.importers.tree.ImportColumnGroup;
@ -58,6 +60,8 @@ import com.google.refine.model.Project;
import com.google.refine.util.JSONUtilities;
public class XmlImporter extends TreeImportingParserBase {
static final Logger logger = LoggerFactory.getLogger(XmlImporter.class);
public XmlImporter() {
super(true);
}
@ -94,20 +98,20 @@ public class XmlImporter extends TreeImportingParserBase {
// ignore everything else
}
}
} catch (XMLStreamException e) {
logger.warn("Error generating parser UI initialization data for XML file", e);
} finally {
is.close();
}
}
} catch (XMLStreamException e) {
// Ignore
} catch (IOException e) {
// Ignore
logger.error("Error generating parser UI initialization data for XML file", e);
}
return options;
}
final static private JSONObject descendElement(XMLStreamReader parser, PreviewParsingState state) throws XMLStreamException {
final static private JSONObject descendElement(XMLStreamReader parser, PreviewParsingState state) {
JSONObject result = new JSONObject();
{
String name = parser.getLocalName();
@ -154,25 +158,29 @@ public class XmlImporter extends TreeImportingParserBase {
}
JSONArray children = new JSONArray();
while (parser.hasNext() && state.tokenCount < PREVIEW_PARSING_LIMIT) {
int tokenType = parser.next();
state.tokenCount++;
if (tokenType == XMLStreamConstants.END_ELEMENT) {
break;
} else if (tokenType == XMLStreamConstants.START_ELEMENT) {
JSONObject childElement = descendElement(parser, state);
if (childElement != null) {
try {
while (parser.hasNext() && state.tokenCount < PREVIEW_PARSING_LIMIT) {
int tokenType = parser.next();
state.tokenCount++;
if (tokenType == XMLStreamConstants.END_ELEMENT) {
break;
} else if (tokenType == XMLStreamConstants.START_ELEMENT) {
JSONObject childElement = descendElement(parser, state);
if (childElement != null) {
JSONUtilities.append(children, childElement);
}
} else if (tokenType == XMLStreamConstants.CHARACTERS ||
tokenType == XMLStreamConstants.CDATA ||
tokenType == XMLStreamConstants.SPACE) {
JSONObject childElement = new JSONObject();
JSONUtilities.safePut(childElement, "t", parser.getText());
JSONUtilities.append(children, childElement);
} else {
// ignore everything else
}
} else if (tokenType == XMLStreamConstants.CHARACTERS ||
tokenType == XMLStreamConstants.CDATA ||
tokenType == XMLStreamConstants.SPACE) {
JSONObject childElement = new JSONObject();
JSONUtilities.safePut(childElement, "t", parser.getText());
JSONUtilities.append(children, childElement);
} else {
// ignore everything else
}
} catch (XMLStreamException e) {
logger.error("Error generating parser UI initialization data for XML file", e);
}
if (children.length() > 0) {