Changeset 7675


Ignore:
Timestamp:
Mar 27, 2019, 9:34:35 AM (3 years ago)
Author:
Nicklas Nordborg
Message:

References #2161: Import data from Excel files

Added XlsxTableWriter which is an extension of the TableWriter that write data to an excel file instead of to an CSV file. Existing code should be able to use the either writer with only minor changes to the code.

Location:
trunk/src/core/net/sf/basedb/util
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/util/export/TableWriter.java

    r5193 r7675  
    2424import java.io.PrintWriter;
    2525import java.io.Writer;
     26import java.util.Collections;
     27import java.util.HashMap;
     28import java.util.Map;
    2629
    2730import net.sf.basedb.util.encode.EncoderDecoder;
    2831import net.sf.basedb.util.encode.IdentityEncoderDecoder;
     32import net.sf.basedb.util.formatter.Formatter;
    2933
    3034/**
     
    4953  private String nullValue = "";
    5054  private EncoderDecoder encoder;
     55  private final Map<Integer, Formatter<?>> formatters;
    5156
    5257  /**
     
    5863  {
    5964    super(out);
     65    this.formatters = new HashMap<>();
    6066  }
    6167
     
    134140
    135141  /**
     142    Set a data formatter for a given column. From now on, the
     143    given formatter will be used instead of the toString() method
     144    to convert data values in that column. It is important that
     145    the data is of the correct type for the formatter.
     146    @since 3.15
     147  */
     148  public void setColumnFormatter(int index, Formatter<?> formatter)
     149  {
     150    this.formatters.put(index, formatter);
     151  }
     152 
     153  /**
     154    @see #setColumnFormatter(int, Formatter)
     155    @since 3.15
     156  */
     157  public Formatter<?> getColumnFormatter(int index)
     158  {
     159    return formatters.get(index);
     160  }
     161
     162 
     163  /**
     164    Print a header line to the parent writer. The only difference between
     165    this method and the {@link #tablePrintData(Object...)} method is that
     166    no column formatters are used (since they operate on data and the headers
     167    are typically strings).
     168    @since 3.15
     169  */
     170  public void tablePrintHeaders(Object... headers)
     171  {
     172    tablePrintData(getEncoder(), null, headers);
     173  }
     174 
     175  /**
    136176    Print a data line to the parent writer using the
    137     default encoder.
     177    default encoder and column formatters.
    138178
    139179    @param data The values to write
     
    141181  public void tablePrintData(Object... data)
    142182  {
    143     tablePrintData(getEncoder(), data);
     183    tablePrintData(getEncoder(), formatters, data);
    144184  }
    145185 
     
    148188    data separator string ('tab' by default). Null values are replaced
    149189    with the null value string (empty string by default). Non-null objects
    150     are converted to strings values by calling their toString() method and,
    151     if an encoder has been specified, then encoded with {@link
    152     EncoderDecoder#encode(String)}.
     190    are converted to strings values by calling either their toString() method or
     191    using the formatter assigned for each column. If an encoder has been specified,
     192    the value is finally encoded with {@link EncoderDecoder#encode(String)}.
    153193 
    154194    @param data The values to write
     
    158198  public void tablePrintData(EncoderDecoder encoder, Object... data)
    159199  {
    160     boolean first = true;
     200    tablePrintData(encoder, formatters, data);
     201  }
     202 
     203  /**
     204    Print a data line to the parent writer. Each value is separated by the
     205    data separator string ('tab' by default). Null values are replaced
     206    with the null value string (empty string by default). Non-null objects
     207    are converted to strings values by calling either their toString() method or
     208    using the formatter assigned for each column. If an encoder has been specified,
     209    the value is finally encoded with {@link EncoderDecoder#encode(String)}.
     210 
     211    @param data The values to write
     212    @param formatters Column formatters to use (null to always use the toString() method)
     213    @param encoder The encoder to use, or null to not use any encoder
     214    @since 2.15
     215  */
     216  @SuppressWarnings({ "rawtypes", "unchecked" })
     217  public void tablePrintData(EncoderDecoder encoder, Map<Integer, Formatter<?>> formatters, Object... data)
     218  {
    161219    String ds = getDataSeparator();
    162220    String nv = getNullValue();
    163     EncoderDecoder e = getEncoder();
    164     if (e == null) e = new IdentityEncoderDecoder();
    165     for (Object d : data)
     221    if (encoder == null) encoder = new IdentityEncoderDecoder();
     222    if (formatters == null) formatters = Collections.emptyMap();
     223    for (int col = 0; col < data.length; col++)
    166224    {
    167       if (!first) print(ds);
    168       print(d == null ? nv : e.encode(d.toString()));
    169       first = false;
     225      Object d = data[col];
     226      if (col > 0) print(ds);
     227      Formatter fmt = formatters.get(col);
     228      print(d == null ? nv : encoder.encode(fmt == null ? d.toString() : fmt.format(d)));
    170229    }
    171230    print("\n");
  • trunk/src/core/net/sf/basedb/util/parser/Mapper.java

    r7668 r7675  
    2525
    2626import net.sf.basedb.core.Type;
     27import net.sf.basedb.util.Values;
    2728import net.sf.basedb.util.parser.FlatFileParser.Data;
    2829
     
    127128      value = (T)getDate(data);
    128129    }
     130    else if (type == Type.BOOLEAN)
     131    {
     132      value = (T)(Boolean)Values.getBoolean(getString(data));
     133    }
    129134    else
    130135    {
Note: See TracChangeset for help on using the changeset viewer.