Changeset 4104


Ignore:
Timestamp:
Jan 28, 2008, 1:29:10 PM (15 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #906: FlatFileParser? should not always throw an exception if a column header is not found

Location:
trunk/src/core/net/sf/basedb/util/parser
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/util/parser/ColFunction.java

    r3911 r4104  
    6666  private final ParsePosition pos;
    6767  private FlatFileParser.Data data;
     68  private boolean ignoreNonExistingColumns = false;
    6869 
    6970  public ColFunction(Map<String, Integer> columnHeaders, NumberFormat numberFormat)
     
    138139      if (column == null)
    139140      {
    140         throw new BaseException("Column '" + colName + "' not found in column headers.");
    141       }
    142       value = data.get(column);
     141        if (!ignoreNonExistingColumns)
     142        {
     143          throw new BaseException("Column '" + colName + "' not found in column headers.");
     144        }
     145        value = null;
     146      }
     147      else
     148      {
     149        value = data.get(column);
     150      }
    143151    }
    144152    else
     
    176184  // -------------------------------------------
    177185
     186  /**
     187    Set to TRUE to ignore non-existing columns. Set to FALSE to
     188    throw an exception (default).
     189    @since 2.6
     190  */
     191  public void setIgnoreNonExistingColumns(boolean ignoreNonExistingColumns)
     192  {
     193    this.ignoreNonExistingColumns = ignoreNonExistingColumns;
     194  }
     195 
    178196  public void setData(FlatFileParser.Data data)
    179197  {
  • trunk/src/core/net/sf/basedb/util/parser/FlatFileParser.java

    r4095 r4104  
    247247
    248248  /**
     249    If non-existing columns should be ignored (true) or result in an
     250    exception (false)
     251  */
     252  private boolean ignoreNonExistingColumns = false;
     253
     254  /**
    249255    If <code>null</code> should be returned for the string NULL (ignoring case)
    250256    or not.
     
    849855 
    850856  /**
     857    Specify if trying to create a mapper with one of the {@link #getMapper(String)}
     858    methods for an expression which references a non-existing column should
     859    result in an exception or be ignored.
     860    @param ignoreNonExistingColumns TRUE to ignore, or FALSE to throw an exception
     861    @since 2.6
     862  */
     863  public void setIgnoreNonExistingColumns(boolean ignoreNonExistingColumns)
     864  {
     865    this.ignoreNonExistingColumns = ignoreNonExistingColumns;
     866  }
     867 
     868  /**
    851869    Get a mapper using the default number format.
    852870    @see #getMapper(String, NumberFormat, boolean)
     
    917935    else if (expression.startsWith("="))
    918936    {
    919       mapper = new JepMapper(expression.substring(1), columnHeaders, numberFormat);
     937      mapper = new JepMapper(expression.substring(1), columnHeaders, numberFormat, ignoreNonExistingColumns);
    920938      if (nullIfException) mapper = new NullIfExceptionMapper(mapper);
    921939    }
     
    946964            if (column == null)
    947965            {
    948               throw new BaseException("Column '" + name + "' not found in column headers.");
     966              if (!ignoreNonExistingColumns)
     967              {
     968                throw new BaseException("Column '" + name + "' not found in column headers.");
     969              }
    949970            }
    950             mappers.add(new ColumnMapper(column, name, numberFormat, nullIfException));
     971            else
     972            {
     973              mappers.add(new ColumnMapper(column, name, numberFormat, nullIfException));
     974            }
    951975          }
    952976        }
     
    957981        mappers.add(new ConstantMapper(expression.substring(nextStart), numberFormat, nullIfException));
    958982      }
    959       if (mappers.size() == 1)
     983      if (mappers.size() == 0)
     984      {
     985        mapper = new ConstantMapper(emptyIsNull ? null : "", numberFormat, nullIfException);
     986      }
     987      else if (mappers.size() == 1)
    960988      {
    961989        mapper = mappers.get(0);
  • trunk/src/core/net/sf/basedb/util/parser/JepMapper.java

    r3675 r4104  
    7070  public JepMapper(String expression, List<String> columnHeaders)
    7171  {
    72     this(expression, columnHeaders, null);
     72    this(expression, columnHeaders, null, false);
    7373  }
    7474 
    7575  public JepMapper(String expression, List<String> columnHeaders, NumberFormat numberFormat)
    7676  {
     77    this(expression, columnHeaders, numberFormat, false);
     78  }
     79
     80  public JepMapper(String expression, List<String> columnHeaders, NumberFormat numberFormat,
     81    boolean ignoreNonExistingColumns)
     82  {
    7783    this.expression = expression;
    7884    this.numberFormat = numberFormat;
    7985    this.colFunction = new ColFunction(columnHeaders, numberFormat);
     86    colFunction.setIgnoreNonExistingColumns(ignoreNonExistingColumns);
    8087    this.lineNoFunction = expression.contains("lineNo()") ? new LineNoFunction() : null;
    8188    this.hasLineNo = lineNoFunction != null;
     
    8693    this.parser = Jep.newJep(expression, colFunction, lineNoFunction, dataNoFunction);
    8794  }
     95 
    8896 
    8997  /*
Note: See TracChangeset for help on using the changeset viewer.