StringNode extends LeafNode<String> { public static String unescapeQuotedString(String string) { StringBuilder sb = new StringBuilder(string); for (int i = 0; i < sb.length(); i++) { if (sb.charAt(i) == '\\') { sb.deleteCharAt(i); if (i == sb.length()) { throw new IllegalArgumentException("Parse error" + string); } switch (sb.charAt(i)) { case'n': sb.setCharAt(i, '\n'); break; case'r': sb.setCharAt(i, '\r'); break; case't': sb.setCharAt(i, '\t'); break; case'f': sb.setCharAt(i, '\f'); break; case'x': if (i + 2 >= sb.length()) { throw new IllegalArgumentException ("Could not parse hex value " + string); } sb.setCharAt(i, (char) Integer.parseInt (sb.substring(i + 1, i + 3), 16)); sb.delete(i + 1, i + 3); break; case'\\': sb.setCharAt(i, '\\'); break; } } } if (sb.length() > 0 && (sb.charAt(0) == '"') && sb.charAt(sb.length() - 1) == '"') { sb.deleteCharAt(sb.length() - 1); if (sb.length() > 0) { sb.deleteCharAt(0); } } return sb.toString(); } StringNode();  StringNode(String value); String value(); @Override String getValue(); @Override String toString(); static String unescapeQuotedString(String string);  }
@Test public void testUnescapeQuotedString() { String a = "\"Hei\""; assertThat(StringNode.unescapeQuotedString(a), is("Hei")); assertThat(StringNode.unescapeQuotedString("foo\"bar\""), is("foo\"bar\"")); assertThat(StringNode.unescapeQuotedString("foo\\\"bar\\\""), is("foo\"bar\"")); assertThat(StringNode.unescapeQuotedString("a\\rb\\tc\\fd"), is("a\rb\tc\fd")); assertThat(StringNode.unescapeQuotedString("\\x55"), is("U")); }
@Test(expected = IllegalArgumentException.class) public void testUnescapedQuotedStringExceptions() { StringNode.unescapeQuotedString("foo\\"); }