/** * Return {@code true} if the given string is surrounded by double quotes, and {@code false} * otherwise. * * @param value The string to inspect. * @return {@code true} if the given string is surrounded by double quotes, and {@code false} * otherwise. */ public static boolean isDoubleQuoted(String value) { return isQuoted(value, '\"'); }
/** * Return {@code true} if the given string is surrounded by single quotes, and {@code false} * otherwise. * * @param value The string to inspect. * @return {@code true} if the given string is surrounded by single quotes, and {@code false} * otherwise. */ public static boolean isQuoted(String value) { return isQuoted(value, '\''); }
@Override public InetAddress parse(String value) { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; value = value.trim(); if (!ParseUtils.isQuoted(value)) throw new InvalidTypeException( String.format("inet values must be enclosed in single quotes (\"%s\")", value)); try { return InetAddress.getByName(value.substring(1, value.length() - 1)); } catch (Exception e) { throw new InvalidTypeException(String.format("Cannot parse inet value from \"%s\"", value)); } }
@Override public String parse(String value) { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; if (!ParseUtils.isQuoted(value)) throw new InvalidTypeException("text or varchar values must be enclosed by single quotes"); return ParseUtils.unquote(value); }
if (!isQuoted(text, quoteChar)) return text;
@Override public Date parse(String value) { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; // strip enclosing single quotes, if any if (ParseUtils.isQuoted(value)) value = ParseUtils.unquote(value); if (ParseUtils.isLongLiteral(value)) { try { return new Date(Long.parseLong(value)); } catch (NumberFormatException e) { throw new InvalidTypeException( String.format("Cannot parse timestamp value from \"%s\"", value)); } } try { return ParseUtils.parseDate(value); } catch (ParseException e) { throw new InvalidTypeException( String.format("Cannot parse timestamp value from \"%s\"", value)); } }
@Override public Long parse(String value) { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; // enclosing single quotes required, even for long literals if (!ParseUtils.isQuoted(value)) throw new InvalidTypeException("time values must be enclosed by single quotes"); value = value.substring(1, value.length() - 1); if (ParseUtils.isLongLiteral(value)) { try { return Long.parseLong(value); } catch (NumberFormatException e) { throw new InvalidTypeException( String.format("Cannot parse time value from \"%s\"", value), e); } } try { return ParseUtils.parseTime(value); } catch (ParseException e) { throw new InvalidTypeException( String.format("Cannot parse time value from \"%s\"", value), e); } }
@Test(groups = "unit") public void testIsQuoted() { assertThat(ParseUtils.isQuoted(null)).isFalse(); assertThat(ParseUtils.isQuoted("")).isFalse(); assertThat(ParseUtils.isQuoted(" ")).isFalse(); assertThat(ParseUtils.isQuoted("'")).isFalse(); // malformed string considered unquoted assertThat(ParseUtils.isQuoted("foo")).isFalse(); assertThat(ParseUtils.isQuoted("''")).isTrue(); assertThat(ParseUtils.isQuoted("' '")).isTrue(); assertThat(ParseUtils.isQuoted("'foo")).isFalse(); // malformed string considered unquoted assertThat(ParseUtils.isQuoted("'foo'")).isTrue(); assertThat(ParseUtils.isQuoted(" 'foo' ")).isFalse(); // considered unquoted assertThat(ParseUtils.isQuoted("'''foo'''")).isTrue(); }
@Override public LocalDate parse(String value) { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; // single quotes are optional for long literals, mandatory for date patterns // strip enclosing single quotes, if any if (ParseUtils.isQuoted(value)) value = ParseUtils.unquote(value); if (ParseUtils.isLongLiteral(value)) { long unsigned; try { unsigned = Long.parseLong(value); } catch (NumberFormatException e) { throw new InvalidTypeException( String.format("Cannot parse date value from \"%s\"", value), e); } try { int days = CodecUtils.fromCqlDateToDaysSinceEpoch(unsigned); return LocalDate.fromDaysSinceEpoch(days); } catch (IllegalArgumentException e) { throw new InvalidTypeException( String.format("Cannot parse date value from \"%s\"", value), e); } } try { Date date = ParseUtils.parseDate(value, pattern); return LocalDate.fromMillisSinceEpoch(date.getTime()); } catch (ParseException e) { throw new InvalidTypeException( String.format("Cannot parse date value from \"%s\"", value), e); } }
/** * Return {@code true} if the given string is surrounded * by double quotes, and {@code false} otherwise. * * @param value The string to inspect. * @return {@code true} if the given string is surrounded * by double quotes, and {@code false} otherwise. */ public static boolean isDoubleQuoted(String value) { return isQuoted(value, '\"'); }
/** * Return {@code true} if the given string is surrounded * by single quotes, and {@code false} otherwise. * * @param value The string to inspect. * @return {@code true} if the given string is surrounded * by single quotes, and {@code false} otherwise. */ public static boolean isQuoted(String value) { return isQuoted(value, '\''); }
/** * Return {@code true} if the given string is surrounded * by double quotes, and {@code false} otherwise. * * @param value The string to inspect. * @return {@code true} if the given string is surrounded * by double quotes, and {@code false} otherwise. */ public static boolean isDoubleQuoted(String value) { return isQuoted(value, '\"'); }
/** * Return {@code true} if the given string is surrounded * by single quotes, and {@code false} otherwise. * * @param value The string to inspect. * @return {@code true} if the given string is surrounded * by single quotes, and {@code false} otherwise. */ public static boolean isQuoted(String value) { return isQuoted(value, '\''); }
/** * Return {@code true} if the given string is surrounded * by single quotes, and {@code false} otherwise. * * @param value The string to inspect. * @return {@code true} if the given string is surrounded * by single quotes, and {@code false} otherwise. */ public static boolean isQuoted(String value) { return isQuoted(value, '\''); }
/** * Return {@code true} if the given string is surrounded * by double quotes, and {@code false} otherwise. * * @param value The string to inspect. * @return {@code true} if the given string is surrounded * by double quotes, and {@code false} otherwise. */ public static boolean isDoubleQuoted(String value) { return isQuoted(value, '\"'); }
@Override public InetAddress parse(String value) { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; value = value.trim(); if (!ParseUtils.isQuoted(value)) throw new InvalidTypeException(String.format("inet values must be enclosed in single quotes (\"%s\")", value)); try { return InetAddress.getByName(value.substring(1, value.length() - 1)); } catch (Exception e) { throw new InvalidTypeException(String.format("Cannot parse inet value from \"%s\"", value)); } }
@Override public String parse(String value) { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; if (!ParseUtils.isQuoted(value)) throw new InvalidTypeException("text or varchar values must be enclosed by single quotes"); return ParseUtils.unquote(value); }
@Override public String parse(String value) { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; if (!ParseUtils.isQuoted(value)) throw new InvalidTypeException("text or varchar values must be enclosed by single quotes"); return ParseUtils.unquote(value); }
@Override public String parse(String value) { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; if (!ParseUtils.isQuoted(value)) throw new InvalidTypeException("text or varchar values must be enclosed by single quotes"); return ParseUtils.unquote(value); }
@Override @SuppressWarnings("unchecked") public T parse(String value) throws InvalidTypeException { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; if (!ParseUtils.isQuoted(value)) throw new InvalidTypeException("JSON strings must be enclosed by single quotes"); String json = ParseUtils.unquote(value); try { return (T) objectMapper.readValue(json, toJacksonJavaType()); } catch (IOException e) { throw new InvalidTypeException(e.getMessage(), e); } }