2010-11-11 14:15:41 +01:00
|
|
|
package com.google.refine.importers;
|
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
2010-11-11 14:15:41 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.LineNumberReader;
|
|
|
|
import java.io.Reader;
|
2011-08-02 05:34:47 +02:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2010-11-11 14:15:41 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
2010-11-11 14:15:41 +01:00
|
|
|
|
|
|
|
import com.google.refine.ProjectMetadata;
|
2011-08-02 05:34:47 +02:00
|
|
|
import com.google.refine.importing.ImportingJob;
|
|
|
|
import com.google.refine.importing.ImportingUtilities;
|
2010-11-11 14:15:41 +01:00
|
|
|
import com.google.refine.model.Project;
|
2011-08-02 05:34:47 +02:00
|
|
|
import com.google.refine.util.JSONUtilities;
|
2010-11-11 14:15:41 +01:00
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
public class FixedWidthImporter extends TabularImportingParserBase {
|
|
|
|
public FixedWidthImporter() {
|
|
|
|
super(false);
|
|
|
|
}
|
2010-11-11 14:15:41 +01:00
|
|
|
|
|
|
|
@Override
|
2011-08-02 05:34:47 +02:00
|
|
|
public JSONObject createParserUIInitializationData(
|
|
|
|
ImportingJob job, List<JSONObject> fileRecords, String format) {
|
|
|
|
JSONObject options = super.createParserUIInitializationData(job, fileRecords, format);
|
|
|
|
JSONArray columnWidths = new JSONArray();
|
|
|
|
|
|
|
|
JSONObject firstFileRecord = fileRecords.get(0);
|
|
|
|
String encoding = ImportingUtilities.getEncoding(firstFileRecord);
|
|
|
|
String location = JSONUtilities.getString(firstFileRecord, "location", null);
|
|
|
|
if (location != null) {
|
|
|
|
File file = new File(job.getRawDataDir(), location);
|
|
|
|
int[] columnWidthsA = guessColumnWidths(file, encoding);
|
|
|
|
if (columnWidthsA != null) {
|
|
|
|
for (int w : columnWidthsA) {
|
|
|
|
JSONUtilities.append(columnWidths, w);
|
|
|
|
}
|
|
|
|
}
|
2010-11-11 14:15:41 +01:00
|
|
|
}
|
2011-08-02 05:34:47 +02:00
|
|
|
|
|
|
|
JSONUtilities.safePut(options, "lineSeparator", "\n");
|
|
|
|
JSONUtilities.safePut(options, "headerLines", 0);
|
|
|
|
JSONUtilities.safePut(options, "columnWidths", columnWidths);
|
|
|
|
JSONUtilities.safePut(options, "guessCellValueTypes", true);
|
|
|
|
|
|
|
|
return options;
|
2010-11-11 14:15:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-08-02 05:34:47 +02:00
|
|
|
public void parseOneFile(
|
|
|
|
Project project,
|
|
|
|
ProjectMetadata metadata,
|
|
|
|
ImportingJob job,
|
|
|
|
String fileSource,
|
|
|
|
Reader reader,
|
|
|
|
int limit,
|
|
|
|
JSONObject options,
|
|
|
|
List<Exception> exceptions
|
|
|
|
) {
|
|
|
|
// String lineSeparator = JSONUtilities.getString(options, "lineSeparator", "\n");
|
|
|
|
final int[] columnWidths = JSONUtilities.getIntArray(options, "columnWidths");
|
2010-11-11 14:15:41 +01:00
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
final List<Object> columnNames;
|
|
|
|
if (options.has("columnNames")) {
|
|
|
|
columnNames = new ArrayList<Object>();
|
|
|
|
String[] strings = JSONUtilities.getStringArray(options, "columnNames");
|
|
|
|
for (String s : strings) {
|
|
|
|
columnNames.add(s);
|
|
|
|
}
|
|
|
|
JSONUtilities.safePut(options, "headerLines", 1);
|
|
|
|
} else {
|
|
|
|
columnNames = null;
|
|
|
|
}
|
2010-11-11 14:15:41 +01:00
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
final LineNumberReader lnReader = new LineNumberReader(reader);
|
2010-11-11 14:15:41 +01:00
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
TableDataReader dataReader = new TableDataReader() {
|
|
|
|
boolean usedColumnNames = false;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<Object> getNextRowOfCells() throws IOException {
|
|
|
|
if (columnNames != null && !usedColumnNames) {
|
|
|
|
usedColumnNames = true;
|
|
|
|
return columnNames;
|
|
|
|
} else {
|
|
|
|
String line = lnReader.readLine();
|
|
|
|
if (line == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return getCells(line, columnWidths);
|
2010-11-11 14:15:41 +01:00
|
|
|
}
|
|
|
|
}
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
|
|
|
};
|
2010-11-11 14:15:41 +01:00
|
|
|
|
2011-08-11 02:35:01 +02:00
|
|
|
TabularImportingParserBase.readTable(project, metadata, job, dataReader, fileSource, limit, options, exceptions);
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
|
|
|
|
2010-11-11 14:15:41 +01:00
|
|
|
/**
|
|
|
|
* Splits the line into columns
|
|
|
|
* @param line
|
|
|
|
* @param lnReader
|
|
|
|
* @param splitIntoColumns
|
|
|
|
* @return
|
|
|
|
*/
|
2011-08-02 05:34:47 +02:00
|
|
|
static private ArrayList<Object> getCells(String line, int[] widths) {
|
|
|
|
ArrayList<Object> cells = new ArrayList<Object>();
|
|
|
|
|
|
|
|
int columnStartCursor = 0;
|
|
|
|
int columnEndCursor = 0;
|
|
|
|
for (int width : widths) {
|
|
|
|
if (columnStartCursor >= line.length()) {
|
|
|
|
cells.add(null); //FIXME is adding a null cell (to represent no data) OK?
|
|
|
|
continue;
|
2010-11-11 14:15:41 +01:00
|
|
|
}
|
2011-08-02 05:34:47 +02:00
|
|
|
|
|
|
|
columnEndCursor = columnStartCursor + width;
|
|
|
|
|
|
|
|
if (columnEndCursor > line.length()) {
|
|
|
|
columnEndCursor = line.length();
|
|
|
|
}
|
|
|
|
if (columnEndCursor <= columnStartCursor) {
|
|
|
|
cells.add(null); //FIXME is adding a null cell (to represent no data, or a zero width column) OK?
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
cells.add(line.substring(columnStartCursor, columnEndCursor));
|
|
|
|
|
|
|
|
columnStartCursor = columnEndCursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Residual text
|
|
|
|
if (columnStartCursor < line.length()) {
|
|
|
|
cells.add(line.substring(columnStartCursor));
|
2010-11-11 14:15:41 +01:00
|
|
|
}
|
|
|
|
return cells;
|
|
|
|
}
|
2011-08-02 05:34:47 +02:00
|
|
|
|
|
|
|
static public int[] guessColumnWidths(File file, String encoding) {
|
|
|
|
try {
|
|
|
|
InputStream is = new FileInputStream(file);
|
|
|
|
try {
|
|
|
|
Reader reader = encoding != null ? new InputStreamReader(is, encoding) : new InputStreamReader(is);
|
|
|
|
LineNumberReader lineNumberReader = new LineNumberReader(reader);
|
|
|
|
|
|
|
|
int[] counts = null;
|
|
|
|
int totalBytes = 0;
|
|
|
|
int lineCount = 0;
|
|
|
|
String s;
|
|
|
|
while (totalBytes < 64 * 1024 &&
|
|
|
|
lineCount < 100 &&
|
|
|
|
(s = lineNumberReader.readLine()) != null) {
|
|
|
|
|
|
|
|
totalBytes += s.length() + 1; // count the new line character
|
|
|
|
if (s.length() == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
lineCount++;
|
|
|
|
|
|
|
|
if (counts == null) {
|
|
|
|
counts = new int[s.length()];
|
|
|
|
for (int c = 0; c < counts.length; c++) {
|
|
|
|
counts[c] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int c = 0; c < counts.length && c < s.length(); c++) {
|
|
|
|
char ch = s.charAt(c);
|
|
|
|
if (ch == ' ') {
|
|
|
|
counts[c]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (counts != null) {
|
|
|
|
List<Integer> widths = new ArrayList<Integer>();
|
|
|
|
|
|
|
|
int startIndex = 0;
|
|
|
|
for (int c = 0; c < counts.length; c++) {
|
|
|
|
int count = counts[c];
|
|
|
|
if (count == lineCount && c > startIndex) {
|
|
|
|
widths.add(c - startIndex + 1);
|
|
|
|
startIndex = c + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = widths.size() - 1; i > 0; i--) {
|
|
|
|
if (widths.get(i) == 1) {
|
|
|
|
widths.remove(i);
|
|
|
|
widths.set(i - 1, widths.get(i - 1) + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int[] widthA = new int[widths.size()];
|
|
|
|
for (int i = 0; i < widthA.length; i++) {
|
|
|
|
widthA[i] = widths.get(i);
|
|
|
|
}
|
|
|
|
return widthA;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
is.close();
|
2010-11-11 14:15:41 +01:00
|
|
|
}
|
2011-08-02 05:34:47 +02:00
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2010-11-11 14:15:41 +01:00
|
|
|
}
|
2011-08-02 05:34:47 +02:00
|
|
|
return null;
|
2010-11-11 14:15:41 +01:00
|
|
|
}
|
|
|
|
}
|