Changeset 7722


Ignore:
Timestamp:
Jun 4, 2019, 1:36:40 PM (2 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #2182: Add an option to trim away white-space when parsing files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.15-stable/src/core/net/sf/basedb/util/parser/FlatFileParser.java

    r7721 r7722  
    268268 
    269269  /**
     270    If white space should be trimmed from data values or not.
     271    Default is false.
     272    @since 3.15.1
     273  */
     274  private boolean trimWhiteSpace;
     275 
     276  /**
    270277    The regular expression for matching the data footer line.
    271278  */
     
    511518  {
    512519    this.trimQuotes = trimQuotes;
     520  }
     521 
     522  /**
     523    Set a flag indicating if white-space should be trimmed from
     524    start and end of data values. The default setting is false.
     525    @param trimWhiteSpace TRUE to remove white-space, FALSE to keep them
     526    @since 3.15.1
     527   */
     528  public void setTrimWhiteSpace(boolean trimWhiteSpace)
     529  {
     530    this.trimWhiteSpace = trimWhiteSpace;
    513531  }
    514532
     
    947965            lines.add(new Line(parsedLines, line, LineType.DATA));
    948966            parsedDataLines = 1;
    949             nextData = new Data(parsedLines, parsedDataLines, line, columns, emptyIsNull, nullIsNull);
     967            nextData = new Data(parsedLines, parsedDataLines, line, columns, emptyIsNull, nullIsNull, trimWhiteSpace);
    950968          }
    951969        }
     
    14721490              if (excelSheet != null)
    14731491              {
    1474                 nextData = new ExcelData(excelSheet, excelParsedLinesOffset, parsedLines, parsedDataLines, line, columns, emptyIsNull, nullIsNull);
     1492                nextData = new ExcelData(excelSheet, excelParsedLinesOffset, parsedLines, parsedDataLines, line, columns, emptyIsNull, nullIsNull, trimWhiteSpace);
    14751493              }
    14761494              else
    14771495              {
    1478                 nextData = new Data(parsedLines, parsedDataLines, line, columns, emptyIsNull, nullIsNull);
     1496                nextData = new Data(parsedLines, parsedDataLines, line, columns, emptyIsNull, nullIsNull, trimWhiteSpace);
    14791497              }
    14801498            }
     
    18971915    private final boolean emptyIsNull;
    18981916    private final boolean nullIsNull;
     1917    private final boolean trimWhiteSpace;
    18991918
    19001919    /**
    19011920      Create a new data object.
    19021921    */
    1903     private Data(int lineNo, int dataLineNo, String line, String[] columns, boolean emptyIsNull, boolean nullIsNull)
     1922    private Data(int lineNo, int dataLineNo, String line, String[] columns, boolean emptyIsNull, boolean nullIsNull, boolean trimWhiteSpace)
    19041923    {
    19051924      this.lineNo = lineNo;
     
    19091928      this.emptyIsNull = emptyIsNull;
    19101929      this.nullIsNull = nullIsNull;
     1930      this.trimWhiteSpace = trimWhiteSpace;
    19111931    }
    19121932   
     
    19912011    protected String fixString(String value)
    19922012    {
     2013      // TODO - In Java 11 we can use String.strip() which also handles other white-space than "space"
     2014      if (value != null && trimWhiteSpace) value = value.trim();     
     2015     
    19932016      return value == null ||
    19942017        emptyIsNull && value.length() == 0 ||
     
    19972020    }
    19982021   
    1999    
    20002022    /**
    20012023      Shortcut for getInt(index, null, true)
     
    20862108      Create a new data object.
    20872109    */
    2088     ExcelData(SheetInfo sheet, int lineOffset, int lineNo, int dataLineNo, String line, String[] columns, boolean emptyIsNull, boolean nullIsNull)
    2089     {
    2090       super(lineNo, dataLineNo, line, columns, emptyIsNull, nullIsNull);
     2110    ExcelData(SheetInfo sheet, int lineOffset, int lineNo, int dataLineNo, String line, String[] columns, boolean emptyIsNull, boolean nullIsNull, boolean trimWhiteSpace)
     2111    {
     2112      super(lineNo, dataLineNo, line, columns, emptyIsNull, nullIsNull, trimWhiteSpace);
    20912113      this.sheet = sheet;
    20922114      this.rowOffset = sheet.getFirstRow()-1-lineOffset;
Note: See TracChangeset for help on using the changeset viewer.