Esempio n. 1
0
  private static boolean date(Text t, List<Object> valueList) {
    final boolean result;

    final int start = t.cursor();
    if (t.consume('@') && date(t)) {
      final String string = t.getString(start + 1); // Jump the'@'
      final LocalDate value = DateU.parseStandardDate(string);
      valueList.add(value);
      result = true;
    } else {
      result = false;
    }

    return result;
  }
Esempio n. 2
0
  /**
   * There's a bug in this as it requires seconds in the string whereas the time() function does
   * not. It seems to be in the Java parser.
   *
   * @param tb Source to parse.
   * @return A Temporal or null if none found.
   */
  @Nullable
  private static Temporal temporal(Text t) {
    Temporal result;

    final int save = t.cursor();
    t.consume('@');
    final int start = save + 1;

    // yyyy/MM/dd HH:mm:ss

    try {
      if (date(t)) {
        final int save2 = t.cursor();
        t.ws();
        if (time(t)) {
          final String string = t.getString(start);
          result = DateU.parseStandardDatetime(string);
        } else {
          t.setCursor(save2);
          final String string = t.getString(start);
          result = DateU.parseStandardDate(string);
        }
      } else if (time(t)) {
        final String string = t.getString(start);
        result = DateU.parseStandardTime(string);
      } else {
        result = null;
        t.setCursor(save);
      }
    } catch (final DateTimeParseException e) {
      result = null;
      t.setCursor(save);
    }

    return result;
  }
Esempio n. 3
0
  /**
   * Parse a string and set the value accordingly.
   *
   * @param <T> Type of object to parse
   * @param string The string to parse. May be null. The string should be in Oak markup so "a
   *     string", `/Some/Path`, etc.
   * @return The parsed value.
   * @see #toString(List)
   * @throws ParsingException Thrown if the string cannot be parsed correctly.
   */
  @SuppressWarnings("unchecked")
  public <T> T parse(@Nullable String string) throws ParsingException {
    final T result;

    assert string != null : "Null string";

    if (string.equals("null")) {
      result = null;
    } else {
      switch (this) {
        case text: // "a string"
          try {
            result = (T) StringU.removeQuotes(string);
          } catch (final RuntimeException e) {
            throw new ParsingException("Missing quotes");
          }
          break;

        case identifier:
          // Verify
          Identifier.parse(string);
          result = (T) string;
          break;

        case path:
          result = (T) Path.parse(string);
          if (result == null) {
            throw new ParsingException("Invalid path: " + string);
          }
          break;

        case bool:
          // Boolean.parse() is not strict enough
          if (string.equals("true")) {
            result = (T) Boolean.TRUE;
          } else if (string.equals("false")) {
            result = (T) Boolean.FALSE;
          } else {
            throw new ParsingException("Invalid boolean, should be (true/false): " + string);
          }
          break;

        case z:
          try {
            // Remove optional trailing 'z'
            final int length = string.length();
            if (length > 1 && string.charAt(length - 1) == 'z') {
              result = (T) new Long(string.substring(0, length - 1));
            } else {
              result = (T) new Long(string);
            }
          } catch (final NumberFormatException e) {
            throw new ParsingException("Invalid integer (Long): " + string, e);
          }
          break;

        case Z:
          try {
            // Remove trailing 'Z'
            final int end = string.length() - 1;
            if (end < 0 || string.charAt(end) != 'Z') {
              throw new ParsingException("Invalid number");
            }
            result = (T) new Long(string.substring(0, end));
          } catch (final NumberFormatException e) {
            throw new ParsingException("Invalid INTEGER (BigInteger): " + string, e);
          }
          break;

        case f:
          try {
            // Remove optional trailing 'f'
            final int length = string.length();
            if (length > 1 && string.charAt(length - 1) == 'f') {
              result = (T) new Double(string.substring(0, length - 1));
            } else {
              result = (T) new Double(string);
            }
          } catch (final NumberFormatException e) {
            throw new ParsingException("Invalid float (Double): " + string, e);
          }
          break;

        case F:
          try {
            // Remove trailing 'F'
            final int end = string.length() - 1;
            if (end < 0 || string.charAt(end) != 'F') {
              throw new ParsingException("Invalid number");
            }
            result = (T) new Long(string.substring(0, end));
          } catch (final NumberFormatException e) {
            throw new ParsingException("Invalid FLOAT (BigDecimal): " + string, e);
          }
          break;

        case cardinality:
          try {
            result = (T) Cardinality.newInstance(string);
          } catch (final Cardinality.Exception e) {
            throw new ParsingException("Invalid cardinality: " + string, e);
          }
          break;

        case date:
          if (string.length() == 0 || string.charAt(0) != '@') {
            throw new ParsingException("Invalid date, missing leading '@': " + string);
          }
          try {
            result = (T) DateU.parseStandardDate(string.substring(1));
          } catch (final DateTimeParseException e) {
            throw new ParsingException("Invalid date: " + string);
          }
          break;

        case time:
          if (string.length() == 0 || string.charAt(0) != '@') {
            throw new ParsingException("Invalid time, missing leading '@': " + string);
          }
          try {
            result = (T) DateU.parseStandardDate(string.substring(1));
          } catch (final DateTimeParseException e) {
            throw new ParsingException("Invalid time: " + string);
          }
          break;

        case datetime:
          if (string.length() == 0 || string.charAt(0) != '@') {
            throw new ParsingException("Invalid datetime, missing leading '@': " + string);
          }
          try {
            result = (T) DateU.parseStandardDate(string.substring(1));
          } catch (final DateTimeParseException e) {
            throw new ParsingException("Invalid datetime: " + string);
          }
          break;

        case any:
        default:
          throw new UnexpectedException(string);
      }
    }

    return result;
  }