Search in sources :

Example 1 with CSVLookupReader

use of eu.esdihumboldt.hale.io.csv.reader.internal.CSVLookupReader in project hale by halestudio.

the class LookupTablePage method readLookupTable.

// read lookup table from file (specified by provider in corresponding
// wizard)
private Map<Value, Value> readLookupTable() {
    Map<Value, Value> lookupTable = new HashMap<Value, Value>();
    try {
        LookupTableImport provider = getWizard().getProvider();
        if (provider instanceof CSVLookupReader) {
            DefaultCSVLookupReader reader = new DefaultCSVLookupReader();
            lookupTable = reader.read(provider.getSource().getInput(), provider.getCharset(), provider.getParameter(CSVConstants.PARAM_SEPARATOR).as(String.class).charAt(0), provider.getParameter(CSVConstants.PARAM_QUOTE).as(String.class).charAt(0), provider.getParameter(CSVConstants.PARAM_ESCAPE).as(String.class).charAt(0), skip, keyColumn.getSelectionIndex(), valueColumn.getSelectionIndex());
        } else {
            Workbook workbook;
            // write xls file
            String file = provider.getSource().getLocation().getPath();
            String fileExtension = file.substring(file.lastIndexOf("."), file.length());
            if (fileExtension.equals(".xls")) {
                workbook = new HSSFWorkbook(provider.getSource().getInput());
            } else // write xlsx file
            if (fileExtension.equals(".xlsx")) {
                workbook = new XSSFWorkbook(provider.getSource().getInput());
            } else
                return new HashMap<Value, Value>();
            DefaultXLSLookupTableReader reader = new DefaultXLSLookupTableReader();
            lookupTable = reader.read(workbook, skip, keyColumn.getSelectionIndex(), valueColumn.getSelectionIndex(), ignoreEmptyString.getSelection());
        }
    } catch (IOException e) {
        return lookupTable;
    }
    return lookupTable;
}
Also used : LookupTableImport(eu.esdihumboldt.hale.common.lookup.LookupTableImport) DefaultCSVLookupReader(eu.esdihumboldt.hale.io.csv.reader.DefaultCSVLookupReader) CSVLookupReader(eu.esdihumboldt.hale.io.csv.reader.internal.CSVLookupReader) HashMap(java.util.HashMap) DefaultCSVLookupReader(eu.esdihumboldt.hale.io.csv.reader.DefaultCSVLookupReader) Value(eu.esdihumboldt.hale.common.core.io.Value) DefaultXLSLookupTableReader(eu.esdihumboldt.hale.io.xls.reader.DefaultXLSLookupTableReader) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) StyledString(org.eclipse.jface.viewers.StyledString) IOException(java.io.IOException) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 2 with CSVLookupReader

use of eu.esdihumboldt.hale.io.csv.reader.internal.CSVLookupReader in project hale by halestudio.

the class LookupTablePage method readHeader.

// read line from a file specified by provider of corresponding wizard
private String[] readHeader() {
    LookupTableImport provider = getWizard().getProvider();
    List<String> items = new ArrayList<String>();
    try {
        if (provider instanceof CSVLookupReader) {
            CSVReader reader = CSVUtil.readFirst(getWizard().getProvider());
            return reader.readNext();
        } else {
            Workbook workbook;
            // write xls file
            String file = provider.getSource().getLocation().getPath();
            String fileExtension = file.substring(file.lastIndexOf("."), file.length());
            if (fileExtension.equals(".xls")) {
                workbook = new HSSFWorkbook(provider.getSource().getInput());
            } else // write xlsx file
            if (fileExtension.equals(".xlsx")) {
                workbook = new XSSFWorkbook(provider.getSource().getInput());
            } else
                return new String[0];
            Sheet sheet = workbook.getSheetAt(0);
            Row currentRow = sheet.getRow(0);
            for (int cell = 0; cell < currentRow.getPhysicalNumberOfCells(); cell++) {
                items.add(currentRow.getCell(cell).getStringCellValue());
            }
            return items.toArray(new String[0]);
        }
    } catch (IOException e) {
        return new String[0];
    }
}
Also used : DefaultCSVLookupReader(eu.esdihumboldt.hale.io.csv.reader.DefaultCSVLookupReader) CSVLookupReader(eu.esdihumboldt.hale.io.csv.reader.internal.CSVLookupReader) CSVReader(au.com.bytecode.opencsv.CSVReader) ArrayList(java.util.ArrayList) StyledString(org.eclipse.jface.viewers.StyledString) IOException(java.io.IOException) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) LookupTableImport(eu.esdihumboldt.hale.common.lookup.LookupTableImport) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet)

Aggregations

LookupTableImport (eu.esdihumboldt.hale.common.lookup.LookupTableImport)2 DefaultCSVLookupReader (eu.esdihumboldt.hale.io.csv.reader.DefaultCSVLookupReader)2 CSVLookupReader (eu.esdihumboldt.hale.io.csv.reader.internal.CSVLookupReader)2 IOException (java.io.IOException)2 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)2 Workbook (org.apache.poi.ss.usermodel.Workbook)2 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)2 StyledString (org.eclipse.jface.viewers.StyledString)2 CSVReader (au.com.bytecode.opencsv.CSVReader)1 Value (eu.esdihumboldt.hale.common.core.io.Value)1 DefaultXLSLookupTableReader (eu.esdihumboldt.hale.io.xls.reader.DefaultXLSLookupTableReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Row (org.apache.poi.ss.usermodel.Row)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1