/** * Unwraps a sequence of quoted java strings, that are each separated by the given separator. * @param trimmedArg * @return */ public static List<String> unwrapQuotedJavaStringList(String s, String separator) { List<String> result = new ArrayList<String>(); String remaining = s.trim(); while (remaining.length() > 0) { int endIndex = findNextClosingQuoteOf(remaining); result.add(unwrapJavaString(remaining.substring(0, endIndex+1))); remaining = remaining.substring(endIndex+1).trim(); if (remaining.startsWith(separator)) { remaining = remaining.substring(separator.length()).trim(); } else if (remaining.length() > 0) { throw new IllegalArgumentException("String '"+s+"' has invalid separators, should be '"+separator+"'"); } } return result; } private static int findNextClosingQuoteOf(String s) {
/** as {@link #unwrapJsonishListIfPossible(String)} but throwing errors * if something which looks like a string or set of brackets is not well-formed * (this does the work for that method) * @throws IllegalArgumentException if looks to have quoted list or surrounding brackets but they are not syntactically valid */ public static List<String> unwrapOptionallyQuotedJavaStringList(String input) { if (input==null) return null; MutableList<String> result = MutableList.of(); String i1 = input.trim(); boolean inBrackets = (i1.startsWith("[") && i1.endsWith("]")); if (inBrackets) i1 = i1.substring(1, i1.length()-1).trim(); QuotedStringTokenizer qst = new QuotedStringTokenizer(i1, "\"", true, ",", false); while (qst.hasMoreTokens()) { String t = qst.nextToken().trim(); if (isWrappedInDoubleQuotes(t)) result.add(unwrapJavaString(t)); else { if (inBrackets && (t.indexOf('[')>=0 || t.indexOf(']')>=0)) throw new IllegalArgumentException("Literal square brackets must be quoted, in element '"+t+"'"); result.add(t.trim()); } } return result; } }
@Test public void testJavaUnwrap() { Assert.assertEquals(JavaStringEscapes.unwrapJavaString("\"Hello World\""), "Hello World"); Assert.assertEquals(JavaStringEscapes.unwrapJavaString("\"Hello \\\"Bob\\\"\""), "Hello \"Bob\""); try { JavaStringEscapes.unwrapJavaString("Hello World"); Assert.fail("Should have thrown"); } catch (Exception e) { /* expected */ } try { // missing final quote JavaStringEscapes.unwrapJavaString("\"Hello \\\"Bob\\\""); Assert.fail("Should have thrown"); } catch (Exception e) { /* expected */ } Assert.assertEquals(JavaStringEscapes.unwrapJavaStringIfWrapped("\"Hello World\""), "Hello World"); Assert.assertEquals(JavaStringEscapes.unwrapJavaStringIfWrapped("\"Hello \\\"Bob\\\"\""), "Hello \"Bob\""); Assert.assertEquals(JavaStringEscapes.unwrapJavaStringIfWrapped("Hello World"), "Hello World"); try { // missing final quote JavaStringEscapes.unwrapJavaStringIfWrapped("\"Hello \\\"Bob\\\""); Assert.fail("Should have thrown"); } catch (Exception e) { /* expected */ } }
/** as {@link #unwrapJavaString(String)} if the given string is wrapped in double quotes; * otherwise just returns the given string */ public static String unwrapJavaStringIfWrapped(String s) { if (!StringEscapes.isWrappedInDoubleQuotes(s)) return s; return unwrapJavaString(s); }