Search in sources :

Example 1 with CSVParser

use of au.com.bytecode.opencsv.CSVParser in project OpenRefine by OpenRefine.

the class SeparatorBasedImporter method parseOneFile.

@Override
public void parseOneFile(Project project, ProjectMetadata metadata, ImportingJob job, String fileSource, Reader reader, int limit, JSONObject options, List<Exception> exceptions) {
    String sep = JSONUtilities.getString(options, "separator", "\\t");
    if (sep == null || "".equals(sep)) {
        sep = "\\t";
    }
    sep = StringEscapeUtils.unescapeJava(sep);
    boolean processQuotes = JSONUtilities.getBoolean(options, "processQuotes", true);
    boolean strictQuotes = JSONUtilities.getBoolean(options, "strictQuotes", false);
    final CSVParser parser = new CSVParser(sep, CSVParser.DEFAULT_QUOTE_CHARACTER, // we don't want escape processing
    (char) 0, strictQuotes, CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE, !processQuotes);
    final LineNumberReader lnReader = new LineNumberReader(reader);
    TableDataReader dataReader = new TableDataReader() {

        @Override
        public List<Object> getNextRowOfCells() throws IOException {
            String line = lnReader.readLine();
            if (line == null) {
                return null;
            } else {
                return getCells(line, parser, lnReader);
            }
        }
    };
    TabularImportingParserBase.readTable(project, metadata, job, dataReader, fileSource, limit, options, exceptions);
}
Also used : CSVParser(au.com.bytecode.opencsv.CSVParser) JSONObject(org.json.JSONObject) LineNumberReader(java.io.LineNumberReader)

Example 2 with CSVParser

use of au.com.bytecode.opencsv.CSVParser in project molgenis by molgenis.

the class TabixRepository method toEntity.

protected Entity toEntity(String line) throws IOException {
    Entity result = new DynamicEntity(entityType);
    CSVParser csvParser = getCsvParser();
    String[] columns = csvParser.parseLine(line);
    int i = 0;
    for (Attribute amd : entityType.getAtomicAttributes()) {
        if (i < columns.length) {
            result.set(amd.getName(), DataConverter.convert(columns[i++], amd));
        }
    }
    return result;
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) DynamicEntity(org.molgenis.data.support.DynamicEntity) Attribute(org.molgenis.data.meta.model.Attribute) CSVParser(au.com.bytecode.opencsv.CSVParser)

Example 3 with CSVParser

use of au.com.bytecode.opencsv.CSVParser in project OpenRefine by OpenRefine.

the class SmartSplit method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length >= 1 && args.length <= 2) {
        CSVParser parser = null;
        Object v = args[0];
        String s = v.toString();
        if (args.length > 1) {
            String sep = args[1].toString();
            parser = new CSVParser(sep, CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER, CSVParser.DEFAULT_STRICT_QUOTES, CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE, false);
        }
        if (parser == null) {
            int tab = s.indexOf('\t');
            if (tab >= 0) {
                parser = s_tabParser;
            } else {
                parser = s_commaParser;
            }
        }
        try {
            return parser.parseLine(s);
        } catch (IOException e) {
            return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " error: " + e.getMessage());
        }
    }
    return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 1 or 2 strings");
}
Also used : CSVParser(au.com.bytecode.opencsv.CSVParser) EvalError(com.google.refine.expr.EvalError) IOException(java.io.IOException)

Example 4 with CSVParser

use of au.com.bytecode.opencsv.CSVParser in project OpenRefine by OpenRefine.

the class SeparatorBasedImporter method parseOneFile.

@Override
public void parseOneFile(Project project, ProjectMetadata metadata, ImportingJob job, String fileSource, Reader reader, int limit, ObjectNode options, List<Exception> exceptions) {
    String sep = JSONUtilities.getString(options, "separator", "\\t");
    if (sep == null || "".equals(sep)) {
        sep = "\\t";
    }
    sep = StringEscapeUtils.unescapeJava(sep);
    boolean processQuotes = JSONUtilities.getBoolean(options, "processQuotes", true);
    boolean strictQuotes = JSONUtilities.getBoolean(options, "strictQuotes", false);
    List<Object> retrievedColumnNames = null;
    if (options.has("columnNames")) {
        String[] strings = JSONUtilities.getStringArray(options, "columnNames");
        if (strings.length > 0) {
            retrievedColumnNames = new ArrayList<Object>();
            for (String s : strings) {
                s = s.trim();
                if (!s.isEmpty()) {
                    retrievedColumnNames.add(s);
                }
            }
            if (!retrievedColumnNames.isEmpty()) {
                JSONUtilities.safePut(options, "headerLines", 1);
            } else {
                retrievedColumnNames = null;
            }
        }
    }
    final List<Object> columnNames = retrievedColumnNames;
    Character quote = CSVParser.DEFAULT_QUOTE_CHARACTER;
    String quoteCharacter = JSONUtilities.getString(options, "quoteCharacter", null);
    if (quoteCharacter != null && quoteCharacter.trim().length() == 1) {
        quote = quoteCharacter.trim().charAt(0);
    }
    final CSVParser parser = new CSVParser(sep, quote, // we don't want escape processing
    (char) 0, strictQuotes, CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE, !processQuotes);
    final LineNumberReader lnReader = new LineNumberReader(reader);
    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, parser, lnReader);
                }
            }
        }
    };
    TabularImportingParserBase.readTable(project, job, dataReader, limit, options, exceptions);
}
Also used : CSVParser(au.com.bytecode.opencsv.CSVParser) LineNumberReader(java.io.LineNumberReader)

Example 5 with CSVParser

use of au.com.bytecode.opencsv.CSVParser in project graylog2-server by Graylog2.

the class CsvConverter method convert.

@Override
public Object convert(String value) {
    if (value == null || value.isEmpty()) {
        return value;
    }
    final CSVParser parser = getCsvParser();
    final Map<String, String> fields = Maps.newHashMap();
    try {
        final String[] strings = parser.parseLine(value);
        if (strings.length != fieldNames.length) {
            LOG.error("Different number of columns in CSV data ({}) and configured field names ({}). Discarding input.", strings.length, fieldNames.length);
            return null;
        }
        for (int i = 0; i < strings.length; i++) {
            fields.put(fieldNames[i], strings[i]);
        }
    } catch (IOException e) {
        LOG.error("Invalid CSV input, discarding input", e);
        return null;
    }
    return fields;
}
Also used : CSVParser(au.com.bytecode.opencsv.CSVParser) IOException(java.io.IOException)

Aggregations

CSVParser (au.com.bytecode.opencsv.CSVParser)5 IOException (java.io.IOException)2 LineNumberReader (java.io.LineNumberReader)2 EvalError (com.google.refine.expr.EvalError)1 JSONObject (org.json.JSONObject)1 Entity (org.molgenis.data.Entity)1 Attribute (org.molgenis.data.meta.model.Attribute)1 DynamicEntity (org.molgenis.data.support.DynamicEntity)1