/** * Unquote the given string if it is quoted; single quotes are unescaped. If the given string is * not quoted, it is returned without any modification. * * @param value The string to unquote. * @return The unquoted string. */ public static String unquote(String value) { return unquote(value, '\''); }
/** * Unquote the given string if it is double quoted; double quotes are unescaped. If the given * string is not double quoted, it is returned without any modification. * * @param value The string to un-double quote. * @return The un-double quoted string. */ public static String unDoubleQuote(String value) { return 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 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)); } }
@Test(groups = "unit") public void testUnquote() { assertThat(ParseUtils.unquote(null)).isNull(); assertThat(ParseUtils.unquote("")).isEqualTo(""); assertThat(ParseUtils.unquote(" ")).isEqualTo(" "); assertThat(ParseUtils.unquote("'")).isEqualTo("'"); // malformed string left untouched assertThat(ParseUtils.unquote("foo")).isEqualTo("foo"); assertThat(ParseUtils.unquote("''")).isEqualTo(""); assertThat(ParseUtils.unquote("' '")).isEqualTo(" "); assertThat(ParseUtils.unquote("'foo")).isEqualTo("'foo"); // malformed string left untouched assertThat(ParseUtils.unquote("'foo'")).isEqualTo("foo"); assertThat(ParseUtils.unquote(" 'foo' ")).isEqualTo(" 'foo' "); // considered unquoted assertThat(ParseUtils.unquote("'''foo'''")).isEqualTo("'foo'"); assertThat(ParseUtils.unquote("'''")).isEqualTo("'"); assertThat(ParseUtils.unquote("''foo'")).isEqualTo("'foo"); assertThat(ParseUtils.unquote("'foo''")).isEqualTo("foo'"); }
@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); } }
/** * Unquote the given string if it is quoted; single quotes are unescaped. * If the given string is not quoted, it is returned without any modification. * * @param value The string to unquote. * @return The unquoted string. */ public static String unquote(String value) { return unquote(value, '\''); }
/** * Unquote the given string if it is quoted; single quotes are unescaped. * If the given string is not quoted, it is returned without any modification. * * @param value The string to unquote. * @return The unquoted string. */ public static String unquote(String value) { return unquote(value, '\''); }
/** * Unquote the given string if it is double quoted; double quotes are unescaped. * If the given string is not double quoted, it is returned without any modification. * * @param value The string to un-double quote. * @return The un-double quoted string. */ public static String unDoubleQuote(String value) { return unquote(value, '"'); }
/** * Unquote the given string if it is quoted; single quotes are unescaped. * If the given string is not quoted, it is returned without any modification. * * @param value The string to unquote. * @return The unquoted string. */ public static String unquote(String value) { return unquote(value, '\''); }
/** * Unquote the given string if it is double quoted; double quotes are unescaped. * If the given string is not double quoted, it is returned without any modification. * * @param value The string to un-double quote. * @return The un-double quoted string. */ public static String unDoubleQuote(String value) { return unquote(value, '"'); }
/** * Unquote the given string if it is double quoted; double quotes are unescaped. * If the given string is not double quoted, it is returned without any modification. * * @param value The string to un-double quote. * @return The un-double quoted string. */ public static String unDoubleQuote(String value) { return unquote(value, '"'); }
@Override public String parse(String value) { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; // strip enclosing single quotes, if any if (isQuoted(value)) value = unquote(value); return value; } }
@Override public String 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 (isQuoted(value)) { value = unquote(value); } return 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); } }
@Override @SuppressWarnings("unchecked") public JsonStructure 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); StringReader sr = new StringReader(json); try { JsonReader reader = readerFactory.createReader(sr); return reader.read(); } catch (JsonException e) { throw new InvalidTypeException(e.getMessage(), e); } finally { sr.close(); } } }
@Test(groups = "unit") public void testUnquote() { assertThat(ParseUtils.unquote(null)).isNull(); assertThat(ParseUtils.unquote("")).isEqualTo(""); assertThat(ParseUtils.unquote(" ")).isEqualTo(" "); assertThat(ParseUtils.unquote("'")).isEqualTo("'"); // malformed string left untouched assertThat(ParseUtils.unquote("foo")).isEqualTo("foo"); assertThat(ParseUtils.unquote("''")).isEqualTo(""); assertThat(ParseUtils.unquote("' '")).isEqualTo(" "); assertThat(ParseUtils.unquote("'foo")).isEqualTo("'foo"); // malformed string left untouched assertThat(ParseUtils.unquote("'foo'")).isEqualTo("foo"); assertThat(ParseUtils.unquote(" 'foo' ")).isEqualTo(" 'foo' "); // considered unquoted assertThat(ParseUtils.unquote("'''foo'''")).isEqualTo("'foo'"); assertThat(ParseUtils.unquote("'''")).isEqualTo("'"); assertThat(ParseUtils.unquote("''foo'")).isEqualTo("'foo"); assertThat(ParseUtils.unquote("'foo''")).isEqualTo("foo'"); }