You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringValue.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Copyright (c) 2016 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl.expr;
  14. import java.math.BigDecimal;
  15. import java.text.DecimalFormatSymbols;
  16. import com.healthmarketscience.jackcess.expr.EvalException;
  17. import com.healthmarketscience.jackcess.expr.LocaleContext;
  18. import com.healthmarketscience.jackcess.expr.Value;
  19. import org.apache.commons.lang3.StringUtils;
  20. /**
  21. *
  22. * @author James Ahlborn
  23. */
  24. public class StringValue extends BaseValue
  25. {
  26. private static final Object NOT_A_NUMBER = new Object();
  27. private final String _val;
  28. private Object _num;
  29. public StringValue(String val)
  30. {
  31. _val = val;
  32. }
  33. @Override
  34. public Type getType() {
  35. return Type.STRING;
  36. }
  37. @Override
  38. public Object get() {
  39. return _val;
  40. }
  41. @Override
  42. public boolean getAsBoolean(LocaleContext ctx) {
  43. // ms access seems to treat strings as "true"
  44. return true;
  45. }
  46. @Override
  47. public String getAsString(LocaleContext ctx) {
  48. return _val;
  49. }
  50. @Override
  51. public Integer getAsLongInt(LocaleContext ctx) {
  52. return roundToLongInt(ctx);
  53. }
  54. @Override
  55. public Double getAsDouble(LocaleContext ctx) {
  56. return getNumber(ctx).doubleValue();
  57. }
  58. @Override
  59. public BigDecimal getAsBigDecimal(LocaleContext ctx) {
  60. return getNumber(ctx);
  61. }
  62. @Override
  63. public Value getAsDateTimeValue(LocaleContext ctx) {
  64. Value dateValue = DefaultDateFunctions.stringToDateValue(ctx, _val);
  65. if(dateValue == null) {
  66. // see if string can be coerced to number and then to value date (note,
  67. // numberToDateValue may return null for out of range numbers)
  68. try {
  69. dateValue = DefaultDateFunctions.numberToDateValue(
  70. getNumber(ctx).doubleValue());
  71. } catch(EvalException ignored) {
  72. // not a number, not a date/time
  73. }
  74. if(dateValue == null) {
  75. throw invalidConversion(Type.DATE_TIME);
  76. }
  77. }
  78. // TODO, for now, we can't cache the date value becuase it could be an
  79. // "implicit" date which would need to be re-calculated on each call
  80. return dateValue;
  81. }
  82. protected BigDecimal getNumber(LocaleContext ctx) {
  83. if(_num instanceof BigDecimal) {
  84. return (BigDecimal)_num;
  85. }
  86. if(_num == null) {
  87. // see if it is parseable as a number
  88. try {
  89. // ignore extraneous whitespace whitespace and handle "&[hH]" or
  90. // "&[oO]" prefix (only supports integers)
  91. String tmpVal = _val.trim();
  92. if(tmpVal.length() > 0) {
  93. if(tmpVal.charAt(0) != ValueSupport.NUMBER_BASE_PREFIX) {
  94. // convert to standard numeric support for parsing
  95. tmpVal = toCanonicalNumberFormat(ctx, tmpVal);
  96. _num = ValueSupport.normalize(new BigDecimal(tmpVal));
  97. return (BigDecimal)_num;
  98. }
  99. // parse as hex/octal symbolic value
  100. if(ValueSupport.HEX_PAT.matcher(tmpVal).matches()) {
  101. return parseIntegerString(tmpVal, 16);
  102. } else if(ValueSupport.OCTAL_PAT.matcher(tmpVal).matches()) {
  103. return parseIntegerString(tmpVal, 8);
  104. }
  105. // fall through to NaN
  106. }
  107. } catch(NumberFormatException nfe) {
  108. // fall through to NaN...
  109. }
  110. _num = NOT_A_NUMBER;
  111. }
  112. throw invalidConversion(Type.DOUBLE);
  113. }
  114. private BigDecimal parseIntegerString(String tmpVal, int radix) {
  115. _num = new BigDecimal(ValueSupport.parseIntegerString(tmpVal, radix));
  116. return (BigDecimal)_num;
  117. }
  118. private static String toCanonicalNumberFormat(LocaleContext ctx, String tmpVal)
  119. {
  120. // convert to standard numeric format:
  121. // - discard any grouping separators
  122. // - convert decimal separator to '.'
  123. DecimalFormatSymbols syms = ctx.getNumericConfig().getDecimalFormatSymbols();
  124. char groupSepChar = syms.getGroupingSeparator();
  125. tmpVal = StringUtils.remove(tmpVal, groupSepChar);
  126. char decSepChar = syms.getDecimalSeparator();
  127. if((decSepChar != ValueSupport.CANON_DEC_SEP) && (tmpVal.indexOf(decSepChar) >= 0)) {
  128. tmpVal = tmpVal.replace(decSepChar, ValueSupport.CANON_DEC_SEP);
  129. }
  130. return tmpVal;
  131. }
  132. }