示例#1
0
文件: DataType.java 项目: inexas/oak
  private static boolean cardinality(Text t, List<Object> valueList) {
    final boolean result;

    final Cardinality value = Cardinality.parse(t);
    if (value == null) {
      result = false;
    } else {
      valueList.add(value);
      result = true;
    }

    return result;
  }
示例#2
0
文件: DataType.java 项目: inexas/oak
  /**
   * Convert a value to an Oak markup representation.
   *
   * @param value The value to represent.
   * @param tb The buffer in which to write the markup.
   */
  public void toMarkup(Object value, Text t) {
    if (value == null) {
      t.append("null");
    } else {
      switch (this) {
        case z:
        case Z:
        case f:
        case F:
        case bool:
          t.append(value.toString());
          break;

        case cardinality:
          ((Cardinality) value).toString(t);
          break;

        case text:
          escapeForOak((String) value, true, t);
          break;

        case identifier:
          t.append(value.toString());
          break;

        case path:
          ((Path) value).toString(t);
          break;

        case datetime:
          t.append('@');
          t.append(DateU.formatStandardDatetime((LocalDateTime) value));
          break;

        case date:
          t.append('@');
          t.append(DateU.formatStandardDate((LocalDate) value));
          break;

        case time:
          t.append('@');
          t.append(DateU.formatStandardTime((LocalTime) value));
          break;

        case any:
        default:
          throw new UnexpectedException("asString: " + this);
      }
    }
  }
示例#3
0
文件: DataType.java 项目: inexas/oak
  /**
   * 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;
  }