Changeset 3023
- Timestamp:
- Dec 12, 2006, 1:32:16 PM (16 years ago)
- Location:
- trunk/src
- Files:
-
- 1 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/net/sf/basedb/core/ExtendedProperty.java
r2535 r3023 24 24 package net.sf.basedb.core; 25 25 26 import java.text.NumberFormat; 26 27 import java.util.List; 27 28 … … 179 180 } 180 181 182 /** 183 Parse a string and return an object of the correct type for this property. 184 Numeric properties are parsed with the specified number format. 185 @param value The value to parse 186 @param numberFormat The number format, or null to use Float.valueOf or Double.valueOf 187 @return An object 188 @throws InvalidDataException If the string cannot be converted to the correct type 189 @see #getType() 190 @since 2.2 191 */ 192 public Object parseString(String value, NumberFormat numberFormat) 193 { 194 return getType().parseString(value, numberFormat); 195 } 196 181 197 /** 182 198 Check if an object is valid according to the specifications of this property. -
trunk/src/core/net/sf/basedb/core/Type.java
r2751 r3023 37 37 import org.hibernate.type.NullableType; 38 38 39 import java.text.NumberFormat; 40 import java.text.ParsePosition; 39 41 import java.util.Date; 40 42 import java.util.Map; … … 70 72 return new Integer(Float.valueOf(value).intValue()); 71 73 } 74 public Number convertNumber(Number num) 75 { 76 return Integer.valueOf(num.intValue()); 77 } 72 78 }, 73 79 … … 90 96 return new Long(Float.valueOf(value).intValue()); 91 97 } 98 public Number convertNumber(Number num) 99 { 100 return Long.valueOf(num.longValue()); 101 } 92 102 }, 93 103 … … 105 115 return value == null ? 0 : 4; 106 116 } 117 public Number convertNumber(Number num) 118 { 119 return Float.valueOf(num.floatValue()); 120 } 107 121 }, 108 122 … … 119 133 { 120 134 return value == null ? 0 : 8; 135 } 136 public Number convertNumber(Number num) 137 { 138 return Double.valueOf(num.doubleValue()); 121 139 } 122 140 }, … … 351 369 352 370 /** 371 Convert a number to the appropriate class. The default implementation 372 returns null. Each numeric type overrides this method to create a number of 373 the correct type. 374 @since 2.2 375 */ 376 public Number convertNumber(Number num) 377 { 378 return null; 379 } 380 381 /** 353 382 Parse a string and return a value of the correct type. 354 383 */ … … 363 392 { 364 393 return getHibernateType().fromStringValue(value); 394 } 395 } 396 397 /** 398 Parse a string and return a value of the correct type using a specific 399 number format for numerical values. 400 @param value The string value to pars 401 @param numberFormat The number format, or null to use {@link #parseString(String)} 402 @since 2.2 403 */ 404 public Object parseString(String value, NumberFormat numberFormat) 405 { 406 if (value == null) 407 { 408 return null; 409 } 410 else if (!isNumerical || numberFormat == null) 411 { 412 return parseString(value); 413 } 414 else 415 { 416 ParsePosition pos = new ParsePosition(0); 417 Number num = numberFormat.parse(value, pos); 418 if (pos.getIndex() < value.length()) 419 { 420 throw new NumberFormatException("For input string: \"" + value + "\""); 421 } 422 return convertNumber(num); 365 423 } 366 424 } -
trunk/src/core/net/sf/basedb/util/parser/ColFunction.java
r2655 r3023 24 24 package net.sf.basedb.util.parser; 25 25 26 import java.text.NumberFormat; 27 import java.text.ParsePosition; 26 28 import java.util.HashMap; 27 29 import java.util.List; … … 59 61 60 62 private final Map<String, Integer> columnHeaders; 63 private final NumberFormat numberFormat; 64 private final ParsePosition pos; 61 65 private FlatFileParser.Data data; 62 66 63 public ColFunction(Map<String, Integer> columnHeaders )67 public ColFunction(Map<String, Integer> columnHeaders, NumberFormat numberFormat) 64 68 { 65 69 this.columnHeaders = columnHeaders; 70 this.numberFormat = numberFormat; 71 this.pos = new ParsePosition(0); 66 72 } 67 73 68 public ColFunction(List<String> columnHeaders )74 public ColFunction(List<String> columnHeaders, NumberFormat numberFormat) 69 75 { 76 this.numberFormat = numberFormat; 77 this.pos = new ParsePosition(0); 70 78 this.columnHeaders = new HashMap<String, Integer>(columnHeaders.size()); 71 79 int index = 0; … … 138 146 throw new ParseException("Invalid parameter type: " + argument + "; expected number"); 139 147 } 140 try 148 Double d = null; 149 if (value != null) 141 150 { 142 stack.push(value == null ? null : Double.valueOf(value)); 151 if (numberFormat == null) 152 { 153 try 154 { 155 d = Double.valueOf(value); 156 } 157 catch (NumberFormatException ex) 158 { 159 stack.push(value); 160 return; 161 } 162 } 163 else 164 { 165 pos.setIndex(0); 166 d = numberFormat.parse(value, pos).doubleValue(); 167 if (pos.getIndex() < value.length()) 168 { 169 stack.push(value); 170 return; 171 } 172 } 143 173 } 144 catch (NumberFormatException ex) 145 { 146 stack.push(value); 147 } 174 stack.push(d); 148 175 } 149 176 // ------------------------------------------- -
trunk/src/core/net/sf/basedb/util/parser/ColumnMapper.java
r2655 r3023 24 24 package net.sf.basedb.util.parser; 25 25 26 import java.text.NumberFormat; 27 import java.text.ParsePosition; 28 26 29 import net.sf.basedb.util.parser.FlatFileParser.Data; 27 30 … … 40 43 private final int index; 41 44 private final String name; 45 private final NumberFormat parser; 46 private final ParsePosition pos; 42 47 43 48 /** … … 49 54 public ColumnMapper(int index, String name) 50 55 { 56 this(index, name, null); 57 } 58 59 /** 60 Create a new column mapper using a specific number format. 61 @param index The index of the data column to use, starting at 0 62 @param name An optional name of the column, use in the <code>toString</code> 63 method 64 @param parser The parser to use or null to use Float.valueOf() 65 */ 66 public ColumnMapper(int index, String name, NumberFormat parser) 67 { 51 68 this.index = index; 52 69 this.name = name; 70 this.parser = parser; 71 this.pos = parser == null ? null : new ParsePosition(0); 53 72 } 54 73 … … 68 87 public Integer getInt(Data data) 69 88 { 70 String stringValue = getValue(data); 71 return stringValue == null ? null : Float.valueOf(stringValue).intValue(); 89 return getFloat(getValue(data)).intValue(); 72 90 } 73 91 public Float getFloat(Data data) 74 92 { 75 String stringValue = getValue(data); 76 return stringValue == null ? null : Float.valueOf(stringValue); 93 return getFloat(getValue(data)); 77 94 } 78 95 // ------------------------------------------- … … 87 104 } 88 105 // ------------------------------------------- 106 107 private Float getFloat(String value) 108 { 109 if (value == null) return null; 110 Float f = null; 111 if (parser == null) 112 { 113 f = Float.valueOf(value); 114 } 115 else 116 { 117 pos.setIndex(0); 118 f = parser.parse(value, pos).floatValue(); 119 if (pos.getIndex() < value.length()) 120 { 121 throw new NumberFormatException("For input string: \"" + value + "\""); 122 } 123 } 124 return f; 125 } 126 89 127 } 90 128 -
trunk/src/core/net/sf/basedb/util/parser/ConstantMapper.java
r2792 r3023 24 24 package net.sf.basedb.util.parser; 25 25 26 import java.text.NumberFormat; 27 import java.text.ParsePosition; 28 26 29 import net.sf.basedb.util.parser.FlatFileParser.Data; 27 30 … … 43 46 private NumberFormatException floatException; 44 47 48 /** 49 Create a constant mapper. 50 */ 45 51 public ConstantMapper(String constant) 52 { 53 this(constant, null); 54 } 55 56 /** 57 Create a constant mapper using a specific number formatter as it's parser. 58 @param constant The constant expression 59 @param parser The number format to use or null to use Float.valueOf() 60 @since 2.2 61 */ 62 public ConstantMapper(String constant, NumberFormat parser) 46 63 { 47 64 this.constant = constant; 48 65 if (constant != null) 49 66 { 50 try67 if (parser != null) 51 68 { 52 asFloat = Float.valueOf(constant); 53 } 54 catch (NumberFormatException ex) 55 { 56 floatException = ex; 57 } 58 if (asFloat != null) 59 { 60 asInteger = asFloat.intValue(); 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 } 61 81 } 62 82 else 63 83 { 64 integerException = floatException; 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 } 65 100 } 66 101 } -
trunk/src/core/net/sf/basedb/util/parser/FlatFileParser.java
r2992 r3023 30 30 import java.nio.charset.Charset; 31 31 32 import java.text.NumberFormat; 32 33 import java.util.regex.Pattern; 33 34 import java.util.regex.Matcher; … … 238 239 239 240 /** 241 The default number formatter to use for creating mappers. 242 */ 243 private NumberFormat numberFormat = null; 244 245 /** 240 246 List of lines parsed by the {@link #parseHeaders()} method. 241 247 */ … … 753 759 754 760 /** 761 Set the default number format to use when creating mappers. 762 @param numberFormat The number format to use, or null to parse 763 numbers with Float.valueOf or Double.valueOf 764 @since 2.2 765 @see #getMapper(String) 766 @see #getMapper(String, NumberFormat) 767 */ 768 public void setDefaultNumberFormat(NumberFormat numberFormat) 769 { 770 this.numberFormat = numberFormat; 771 } 772 773 /** 774 Get the default number format. 775 @return The number format, or null if none has been specified 776 @since 2.2 777 */ 778 public NumberFormat getDefaultNumberFormat() 779 { 780 return numberFormat; 781 } 782 783 /** 784 Get a mapper using the default number format. 785 @see #getMapper(String, NumberFormat) 786 */ 787 public Mapper getMapper(String expression) 788 { 789 return getMapper(expression, numberFormat); 790 } 791 792 /** 755 793 Create a mapper object that maps an expression string to a value. 756 794 An expression string is a regular string which contains placeholders … … 772 810 773 811 @param expression The string containing the mapping expression 812 @param numberFormat The number format the mapper should use for 813 parsing numbers, or null to use Float.valueOf or Double.valueOf 774 814 @return A mapper object 775 */ 776 public Mapper getMapper(String expression) 815 @since 2.2 816 */ 817 public Mapper getMapper(String expression, NumberFormat numberFormat) 777 818 { 778 819 Mapper mapper = null; 779 820 if (expression == null || expression.length() == 0) 780 821 { 781 mapper = new ConstantMapper(emptyIsNull ? null : "" );822 mapper = new ConstantMapper(emptyIsNull ? null : "", numberFormat); 782 823 } 783 824 else if (expression.startsWith("=")) 784 825 { 785 mapper = new JepMapper(expression.substring(1), columnHeaders );826 mapper = new JepMapper(expression.substring(1), columnHeaders, numberFormat); 786 827 } 787 828 else … … 795 836 if (matchStart > nextStart) 796 837 { 797 mappers.add(new ConstantMapper(expression.substring(nextStart, matchStart) ));838 mappers.add(new ConstantMapper(expression.substring(nextStart, matchStart), numberFormat)); 798 839 } 799 840 try 800 841 { 801 842 int column = Integer.parseInt(m.group(1)); 802 mappers.add(new ColumnMapper(column, null ));843 mappers.add(new ColumnMapper(column, null, numberFormat)); 803 844 } 804 845 catch (NumberFormatException ex) … … 810 851 if (column >= 0) 811 852 { 812 mappers.add(new ColumnMapper(column, name ));853 mappers.add(new ColumnMapper(column, name, numberFormat)); 813 854 } 814 855 else … … 822 863 if (nextStart < expression.length()) 823 864 { 824 mappers.add(new ConstantMapper(expression.substring(nextStart) ));865 mappers.add(new ConstantMapper(expression.substring(nextStart), numberFormat)); 825 866 } 826 867 if (mappers.size() == 1) -
trunk/src/core/net/sf/basedb/util/parser/JepMapper.java
r2655 r3023 24 24 package net.sf.basedb.util.parser; 25 25 26 import java.text.NumberFormat; 26 27 import java.util.List; 27 28 import org.nfunk.jep.JEP; … … 56 57 private final JEP parser; 57 58 private final ColFunction colFunction; 59 private final NumberFormat numberFormat; 58 60 59 61 /** … … 64 66 public JepMapper(String expression, List<String> columnHeaders) 65 67 { 68 this(expression, columnHeaders, null); 69 } 70 71 public JepMapper(String expression, List<String> columnHeaders, NumberFormat numberFormat) 72 { 66 73 this.expression = expression; 67 this.colFunction = new ColFunction(columnHeaders); 74 this.numberFormat = numberFormat; 75 this.colFunction = new ColFunction(columnHeaders, numberFormat); 68 76 // Replace: \ColumnName\ with: col('ColumnName') 69 77 expression = expression.replaceAll("\\\\([^\\\\]*)\\\\", "col('$1')"); … … 88 96 colFunction.setData(data); 89 97 if (parser.hasError()) Jep.clearErrorList(parser); 90 return new Integer((int)parser.getValue());98 return Integer.valueOf((int)parser.getValue()); 91 99 } 92 100 public Float getFloat(Data data) … … 94 102 colFunction.setData(data); 95 103 if (parser.hasError()) Jep.clearErrorList(parser); 96 return new Float(parser.getValue());104 return Float.valueOf((float)parser.getValue()); 97 105 } 98 106 // ------------------------------------------- -
trunk/src/plugins/core/net/sf/basedb/plugins/AbstractFlatFileImporter.java
r2992 r3023 51 51 import net.sf.basedb.core.plugin.Plugin; 52 52 53 import net.sf.basedb.util.NumberFormatUtil; 53 54 import net.sf.basedb.util.error.ClassMapErrorHandler; 54 55 import net.sf.basedb.util.error.ErrorHandler; … … 58 59 import net.sf.basedb.util.parser.Mapper; 59 60 61 import java.text.NumberFormat; 60 62 import java.util.ArrayList; 61 63 import java.util.Arrays; … … 272 274 new StringParameterType(255, Config.getCharset(), false, 1, 0, 0, 273 275 new ArrayList<String>(Charset.availableCharsets().keySet())); 276 277 /** 278 Type for selecting decimal separator. 279 */ 280 protected static final StringParameterType decimalSeparatorType = 281 new StringParameterType(255, "dot", false, 1, 0, 0, 282 Arrays.asList(new String[] { "dot", "comma" }) 283 ); 274 284 275 285 /** … … 482 492 FlatFileParser.Data dataline = null; 483 493 ffp.setInputStream(in, getCharset()); 494 ffp.setDefaultNumberFormat(getNumberFormat()); 484 495 boolean isSuccess = false; // false until we have finished parsing 485 496 try … … 714 725 715 726 /** 727 Get the decimal separator used by numbers in the file. This method first 728 checks the job parameters for a value, then the configuration parameters. 729 If not found null is returned. 730 @return The decimal separator or null to use Float.valueOf() or Double.valueOf() 731 @since 2.2 732 */ 733 protected String getDecimalSeparator() 734 { 735 String ds = null; 736 if (job != null) ds = (String)job.getValue(DECIMAL_SEPARATOR); 737 if (ds == null && configuration != null) 738 { 739 ds = (String)configuration.getValue(DECIMAL_SEPARATOR); 740 } 741 return ds; 742 } 743 744 /** 745 Get a number formatter that is able to parse numbers with the 746 specified decimal separator. Returns null if no decimal separator 747 has been specified which causes numbers to be parsed with Float.valueOf() 748 or Double.valueOf(). 749 @return The number format or null to use Float.valueOf() or Double.valueOf() 750 @since 2.2 751 */ 752 protected NumberFormat getNumberFormat() 753 { 754 String decimalSeparator = getDecimalSeparator(); 755 char ds = 0; 756 if ("dot".equals(decimalSeparator)) 757 { 758 ds = '.'; 759 } 760 else if ("comma".equals(decimalSeparator)) 761 { 762 ds = ','; 763 } 764 return ds == 0 ? null : NumberFormatUtil.getNumberFormat(ds, ';'); 765 } 766 767 /** 716 768 The name of the parameter that asks for the character set. 717 769 @see #getCharsetParameter(String, String, String) … … 740 792 return new PluginParameter<String>( 741 793 CHARSET, label, description, defaultValue, charsetType 794 ); 795 } 796 797 /** 798 The name of the parameter that asks for the decimal separator. 799 @see #getCharsetParameter(String, String, String) 800 */ 801 protected static final String DECIMAL_SEPARATOR = "decimalSeparator"; 802 803 /** 804 Parameter definition that asks for the decimal separator used by numeric values 805 in the file that is imported. This can be both a job parameter and a configuration 806 parameter. The implementation first checks in the job parameter and 807 if not found in the configuration parameter. If still not found the 808 no decimal separator is used and number parsing is done by Float.valueOf 809 or Double.valueOf. 810 @param label The label to use for the parameter or null to use the default label 811 (Decimal separator) 812 @param description The description to use for the parameter or null to use the 813 default description 814 @param defaultValue The default value for the decimal separator 815 @since 2.2 816 */ 817 protected PluginParameter<String> getDecimalSeparatorParameter(String label, String description, String defaultValue) 818 { 819 if (label == null) label = "Decimal separator"; 820 if (description == null) 821 { 822 description = "The decimal separator used in numeric values, " + 823 "if not specified dot is assumed."; 824 } 825 return new PluginParameter<String>( 826 DECIMAL_SEPARATOR, label, description, defaultValue, decimalSeparatorType 742 827 ); 743 828 } -
trunk/src/plugins/core/net/sf/basedb/plugins/RawDataFlatFileImporter.java
r2992 r3023 59 59 import net.sf.basedb.core.Job; 60 60 61 import java.text.NumberFormat; 61 62 import java.util.ArrayList; 62 63 import java.util.Arrays; … … 208 209 private int numInserted; 209 210 private FlatFileParser ffp; 211 private NumberFormat numberFormat; 210 212 private RawDataBatcher batcher; 211 213 private RawBioAssay rawBioAssay; … … 374 376 storeValue(configuration, request, maxDataColumnsParameter); 375 377 storeValue(configuration, request, ri.getParameter(CHARSET)); 378 storeValue(configuration, request, ri.getParameter(DECIMAL_SEPARATOR)); 376 379 377 380 // Column mappings … … 396 399 storeValue(job, request, fileParameter); 397 400 storeValue(job, request, ri.getParameter(CHARSET)); 401 storeValue(job, request, ri.getParameter(DECIMAL_SEPARATOR)); 398 402 399 403 // Error handling parameters … … 437 441 numInserted = 0; 438 442 this.ffp = ffp; 443 this.numberFormat = ffp.getDefaultNumberFormat(); 439 444 440 445 // Set up error handling … … 534 539 RawDataProperty ep = entry.getKey(); 535 540 Mapper m = entry.getValue(); 536 raw.setExtended(ep.getName(), ep.parseString(m.getValue(data) ));541 raw.setExtended(ep.getName(), ep.parseString(m.getValue(data), numberFormat)); 537 542 } 538 543 … … 613 618 parameters.add(fileParameter); 614 619 parameters.add(getCharsetParameter(null, null, (String)configuration.getValue(CHARSET))); 620 parameters.add(getDecimalSeparatorParameter(null, null, (String)configuration.getValue(DECIMAL_SEPARATOR))); 615 621 616 622 // Error handling parameters … … 712 718 parameters.add(maxDataColumnsParameter); 713 719 parameters.add(getCharsetParameter(null, null, null)); 720 parameters.add(getDecimalSeparatorParameter(null, null, null)); 714 721 715 722 // Column mappings -
trunk/src/plugins/core/net/sf/basedb/plugins/ReporterFlatFileImporter.java
r2992 r3023 66 66 import net.sf.basedb.util.parser.Mapper; 67 67 68 import java.text.NumberFormat; 68 69 import java.util.Collection; 69 70 import java.util.EnumSet; … … 305 306 storeValue(configuration, request, maxDataColumnsParameter); 306 307 storeValue(configuration, request, ri.getParameter(CHARSET)); 308 storeValue(configuration, request, ri.getParameter(DECIMAL_SEPARATOR)); 307 309 308 310 // Column mappings … … 332 334 storeValue(job, request, fileParameter); 333 335 storeValue(job, request, ri.getParameter(CHARSET)); 336 storeValue(job, request, ri.getParameter(DECIMAL_SEPARATOR)); 334 337 storeValue(job, request, updateExistingParameter); 335 338 … … 372 375 private Map<String, Float> deferred; 373 376 private FlatFileParser ffp; 377 private NumberFormat numberFormat; 374 378 375 379 /** … … 395 399 updateExisting = (Boolean)job.getValue("updateExisting"); 396 400 this.ffp = ffp; 401 this.numberFormat = ffp.getDefaultNumberFormat(); 397 402 numInserted = 0; 398 403 numUpdated = 0; … … 508 513 Float score = scoreMapper == null ? 509 514 reporterList.getScore(reporter) : 510 (Float)Type.FLOAT.parseString(scoreMapper.getValue(data) );515 (Float)Type.FLOAT.parseString(scoreMapper.getValue(data), numberFormat); 511 516 if (currentId == 0) 512 517 { … … 542 547 ExtendedProperty ep = entry.getKey(); 543 548 Mapper m = entry.getValue(); 544 reporter.setExtended(ep.getName(), ep.parseString(m.getValue(data) ));549 reporter.setExtended(ep.getName(), ep.parseString(m.getValue(data), numberFormat)); 545 550 } 546 551 if (currentId != 0) … … 620 625 } 621 626 parameters.add(fileParameter); 627 parameters.add(updateExistingParameter); 622 628 parameters.add(getCharsetParameter(null, null, (String)configuration.getValue(CHARSET))); 623 parameters.add( updateExistingParameter);629 parameters.add(getDecimalSeparatorParameter(null, null, (String)configuration.getValue(DECIMAL_SEPARATOR))); 624 630 625 631 parameters.add(errorSection); … … 677 683 parameters.add(maxDataColumnsParameter); 678 684 parameters.add(getCharsetParameter(null, null, null)); 685 parameters.add(getDecimalSeparatorParameter(null, null, null)); 679 686 680 687 // Column mappings
Note: See TracChangeset
for help on using the changeset viewer.