Changeset 3241


Ignore:
Timestamp:
Apr 12, 2007, 11:30:53 PM (16 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #563: FlatFileParser? can't handle complex mappings

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

Legend:

Unmodified
Added
Removed
  • branches/2.2.3/src/core/net/sf/basedb/util/parser/CompoundMapper.java

    r2302 r3241  
    2424package net.sf.basedb.util.parser;
    2525
     26import java.text.NumberFormat;
    2627import java.util.List;
    2728
     29import net.sf.basedb.core.Type;
    2830import net.sf.basedb.util.parser.FlatFileParser.Data;
    2931
     
    4143{
    4244  private final List<Mapper> mappers;
     45  private final NumberFormat parser;
    4346 
    4447  /**
     
    4649    @param mappers A list of other mappers that will be invoked in
    4750      the order they appear in the list
    48    */
     51  */
    4952  public CompoundMapper(List<Mapper> mappers)
    5053  {
     54    this(mappers, null);
     55  }
     56 
     57  /**
     58    Create a new compound mapper, using a specific number formatter as it's parser.
     59    @param mappers A list of other mappers that will be invoked in
     60      the order they appear in the list
     61    @param parser The number format to use or null to use Float.valueOf()
     62    @since 2.2.3
     63   */
     64  public CompoundMapper(List<Mapper> mappers, NumberFormat parser)
     65  {
    5166    this.mappers = mappers;
     67    this.parser = parser;
    5268  }
    53 
    5469  /*
    5570    From the Mapper interface
     
    7388  {
    7489    String stringValue = getValue(data);
    75     return stringValue == null ? null : Integer.parseInt(stringValue);
     90    Integer intValue = null;
     91    if (stringValue != null)
     92    {
     93      if (parser != null)
     94      {
     95        intValue = (Integer)Type.INT.parseString(stringValue, parser);
     96      }
     97      else
     98      {
     99        intValue = Integer.parseInt(stringValue);
     100      }
     101    }
     102    return intValue;
    76103  }
    77104  public Float getFloat(Data data)
    78105  {
    79106    String stringValue = getValue(data);
    80     return stringValue == null ? null : Float.parseFloat(stringValue);
     107    Float floatValue = null;
     108    if (stringValue != null)
     109    {
     110      if (parser != null)
     111      {
     112        floatValue = (Float)Type.FLOAT.parseString(stringValue, parser);
     113      }
     114      else
     115      {
     116        floatValue = Float.parseFloat(stringValue);
     117      }
     118    }
     119    return floatValue;
    81120  }
    82121  // -------------------------------------------
  • branches/2.2.3/src/core/net/sf/basedb/util/parser/ConstantMapper.java

    r3023 r3241  
    2727import java.text.ParsePosition;
    2828
     29import net.sf.basedb.core.Type;
    2930import net.sf.basedb.util.parser.FlatFileParser.Data;
    3031
     
    4142{
    4243  private final String constant;
     44  private final NumberFormat parser;
     45  private boolean needToParseNumeric;
     46  private Float asFloat;
    4347  private Integer asInteger;
    44   private NumberFormatException integerException;
    45   private Float asFloat;
    46   private NumberFormatException floatException;
     48  private NumberFormatException parseException;
    4749 
    4850  /**
     
    6365  {
    6466    this.constant = constant;
    65     if (constant != null)
    66     {
    67       if (parser != null)
    68       {
    69         ParsePosition pos = new ParsePosition(0);
    70         asFloat = parser.parse(constant, pos).floatValue();
    71         if (pos.getIndex() < constant.length())
    72         {
    73           asFloat = null;
    74           floatException = new NumberFormatException("For input string: \"" + constant + "\"");
    75           integerException = floatException;
    76         }
    77         else
    78         {
    79           asInteger = asFloat.intValue();
    80         }
    81       }
    82       else
    83       {
    84         try
    85         {
    86           asFloat = Float.valueOf(constant);
    87         }
    88         catch (NumberFormatException ex)
    89         {
    90           floatException = ex;
    91         }
    92         if (asFloat != null)
    93         {
    94           asInteger = asFloat.intValue();
    95         }
    96         else
    97         {
    98           integerException = floatException;
    99         }
    100       }
    101     }
     67    this.parser = parser;
     68    this.needToParseNumeric = constant != null;
    10269  }
    10370 
     
    10774    this.asInteger = constant;
    10875    this.asFloat = new Float(constant.floatValue());
     76    this.parser = null;
     77    this.needToParseNumeric = false;
    10978  }
    11079 
     
    11483    this.asFloat = constant;
    11584    this.asInteger = new Integer(constant.intValue());
     85    this.parser = null;
     86    this.needToParseNumeric = false;
    11687  }
    11788 
     
    129100  public Integer getInt(Data data)
    130101  {
    131     if (integerException != null) throw integerException;
     102    if (parseException != null) throw parseException;
     103    if (needToParseNumeric) parseNumeric();
    132104    return asInteger;
    133105  }
    134106  public Float getFloat(Data data)
    135107  {
    136     if (floatException != null) throw floatException;
     108    if (parseException != null) throw parseException;
     109    if (needToParseNumeric) parseNumeric();
    137110    return asFloat;
    138111  }
     
    148121  }
    149122  // -------------------------------------------
     123
     124  private void parseNumeric()
     125  {
     126    assert constant != null : "constant == null";
     127    needToParseNumeric = false;
     128    try
     129    {
     130      if (parser != null)
     131      {
     132        asFloat = (Float)Type.FLOAT.parseString(constant, parser);
     133      }
     134      else
     135      {
     136        asFloat = Float.valueOf(constant);
     137      }
     138    }
     139    catch (NumberFormatException ex)
     140    {
     141      parseException = ex;
     142    }
     143    if (asFloat != null)
     144    {
     145      asInteger = asFloat.intValue();
     146    }
     147  }
    150148}
  • branches/2.2.3/src/core/net/sf/basedb/util/parser/FlatFileParser.java

    r3023 r3241  
    871871      else
    872872      {
    873         mapper = new CompoundMapper(mappers);
     873        mapper = new CompoundMapper(mappers, numberFormat);
    874874      }
    875875    }
Note: See TracChangeset for help on using the changeset viewer.