Search in sources :

Example 1 with BomInputStreamReader

use of org.eclipse.scout.rt.platform.util.BomInputStreamReader in project scout.rt by eclipse.

the class CsvHelperTest method doTestImportData.

protected void doTestImportData(boolean withBom, Charset charset) throws IOException {
    StringBuilder sb = new StringBuilder();
    if (withBom) {
        sb.append(BomInputStreamReader.BOM_CHAR);
    }
    sb.append('"').append("value1").append('"').append(',');
    sb.append('"').append("value2").append('"').append(',');
    sb.append('"').append("value3").append('"').append('\n');
    try (Reader reader = new BomInputStreamReader(new ByteArrayInputStream(sb.toString().getBytes(charset)), charset)) {
        Object[][] importedData = m_csvHelper.importData(reader, 0, null, 1);
        Object[][] expectedData = new Object[][] { { "value1", "value2", "value3" } };
        m_collector.checkThat(String.format("charset=%s, withBom=%b", charset.name(), withBom), importedData, CoreMatchers.equalTo(expectedData));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BomInputStreamReader(org.eclipse.scout.rt.platform.util.BomInputStreamReader) Reader(java.io.Reader) FileReader(java.io.FileReader) BomInputStreamReader(org.eclipse.scout.rt.platform.util.BomInputStreamReader)

Example 2 with BomInputStreamReader

use of org.eclipse.scout.rt.platform.util.BomInputStreamReader in project scout.rt by eclipse.

the class CsvSqlAdapter method importData.

public void importData(CsvSettings params) {
    CsvHelper h = new CsvHelper(params.getContentLocale(), params.getColSeparator(), params.getTextDelimiter(), "\n");
    if (params.getCsvColumnTypes() != null) {
        h.setColumnTypes(params.getCsvColumnTypes());
    }
    if (params.getCsvColumnNames() != null) {
        h.setColumnNames(params.getCsvColumnNames());
    }
    Collection<String> cols = new ArrayList<String>();
    if (params.getGroupKeyValue() != null) {
        cols.add(params.getGroupKeyColumnName());
    }
    if (params.getLineNumberColumnName() != null) {
        cols.add(params.getLineNumberColumnName());
    }
    // 
    cols.addAll(params.getCsvColumnNames());
    StringBuilder buf = new StringBuilder();
    buf.append("INSERT INTO ");
    buf.append(params.getTableName());
    buf.append("(");
    for (Iterator<String> it = cols.iterator(); it.hasNext(); ) {
        String colName = it.next();
        if (!CsvHelper.IGNORED_COLUMN_NAME.equals(colName)) {
            buf.append(colName);
            buf.append(",");
        }
    }
    buf.deleteCharAt(buf.length() - 1);
    buf.append(") VALUES (");
    int i = 0;
    for (Iterator<String> it = cols.iterator(); it.hasNext(); ) {
        String colName = it.next();
        if (!CsvHelper.IGNORED_COLUMN_NAME.equals(colName)) {
            buf.append(":v" + i);
            buf.append(",");
            i++;
        }
    }
    buf.deleteCharAt(buf.length() - 1);
    buf.append(")");
    String stm = buf.toString();
    try (FileInputStream in = new FileInputStream(params.getFile());
        Reader reader = new BomInputStreamReader(in, params.getEncoding())) {
        SqlInsertDataConsumer cons = new SqlInsertDataConsumer(stm, params.getGroupKeyValue(), params.getLineNumberColumnName() != null);
        h.importData(cons, reader, false, false, params.getHeaderRowCount(), -1, params.getAllowVariableColumnCount());
    } catch (IOException e) {
        throw new ProcessingException(e.getMessage(), e);
    }
}
Also used : ArrayList(java.util.ArrayList) BomInputStreamReader(org.eclipse.scout.rt.platform.util.BomInputStreamReader) Reader(java.io.Reader) IOException(java.io.IOException) BomInputStreamReader(org.eclipse.scout.rt.platform.util.BomInputStreamReader) FileInputStream(java.io.FileInputStream) CsvHelper(org.eclipse.scout.rt.shared.csv.CsvHelper) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Aggregations

Reader (java.io.Reader)2 BomInputStreamReader (org.eclipse.scout.rt.platform.util.BomInputStreamReader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)1 CsvHelper (org.eclipse.scout.rt.shared.csv.CsvHelper)1