@Override public String getText() { if (currentNode == null) { return ""; } return StringResources.processStringResources(currentNode.getTextContent()); }
/** * Processes String resource values in the same way real Android does, namely:- * 1) Trim leading and trailing whitespace. * 2) Converts code points. * 3) Escapes */ public static String processStringResources(String inputValue) { return escape(whitespace().collapseFrom(inputValue.trim(), ' ')); }
int j = text.indexOf('\\', i); if (j == -1) { result.append(removeUnescapedDoubleQuotes(text.substring(i))); break; result.append(removeUnescapedDoubleQuotes(text.substring(i, j))); if (j == length - 1) { result.append(extractCodePoint(codePoint)); i = j + CODE_POINT_LENGTH;
/** * Processes String resource values in the same way real Android does, namely:- * 1) Trim leading and trailing whitespace. * 2) Converts code points. * 3) Escapes */ public static String processStringResources(String inputValue) { return escape(convertCodePoints(inputValue.trim() .replace("\\n", String.valueOf('\n')) .replace("\\t", String.valueOf('\t')) )); }
@Override public void onEnd(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { String s = buf.toString(); if (resType == ResType.CHAR_SEQUENCE) { s = StringResources.processStringResources(s); } else { s = s.trim(); } resourceTable.addResource(attrType, name, new TypedResource<>(s, resType, xmlContext)); } }
@Test public void escape_shouldEscapeStrings() { assertThat(StringResources.escape("\"This'll work\"")).isEqualTo("This'll work"); assertThat(StringResources.escape("This\\'ll also work")).isEqualTo("This'll also work"); assertThat(StringResources.escape("This is a \\\"good string\\\".")).isEqualTo("This is a \"good string\"."); assertThat(StringResources.escape("This is a \"bad string with unescaped double quotes\".")) .isEqualTo("This is a bad string with unescaped double quotes."); assertThat(StringResources.escape("Text with escaped backslash followed by an \\\\\"unescaped double quote.")) .isEqualTo("Text with escaped backslash followed by an \\unescaped double quote."); }
int j = text.indexOf('\\', i); if (j == -1) { result.append(removeUnescapedDoubleQuotes(text.substring(i))); break; result.append(removeUnescapedDoubleQuotes(text.substring(i, j))); if (j == length - 1) { result.append(extractCodePoint(codePoint)); i = j + CODE_POINT_LENGTH;
public String qualify(String value) { if (value == null) return null; if (AttributeResource.isResourceReference(value)) { return "@" + ResName.qualifyResourceName(value.trim().substring(1).replace("+", ""), packageName, "attr"); } else if (AttributeResource.isStyleReference(value)) { return "?" + ResName.qualifyResourceName(value.trim().substring(1), packageName, "attr"); } else { return StringResources.processStringResources(value); } }
/** * Processes String resource values in the same way real Android does, namely:- * 1) Trim leading and trailing whitespace. * 2) Converts code points. * 3) Escapes */ public static String processStringResources(String inputValue) { return escape(whitespace().collapseFrom(inputValue.trim(), ' ')); }
@Test public void escape_shouldRemoveUnescapedDoubleQuotes() { Map<String, String> tests = new HashMap<>(); tests.put("a\\\"b", "a\"b"); tests.put("a\\\\\"b", "a\\b"); tests.put("a\\\\\\\"b", "a\\\"b"); tests.put("a\\\\\\\\\"b", "a\\\\b"); for (Map.Entry<String, String> t : tests.entrySet()) { assertThat(StringResources.processStringResources(t.getKey())).isEqualTo(t.getValue()); } }
@Test public void escape_shouldEscapeCodePoints() { Map<String, String> tests = new HashMap<>(); tests.put("\\u0031", "1"); tests.put("1\\u0032", "12"); tests.put("\\u00312", "12"); tests.put("1\\u00323", "123"); tests.put("\\u005A", "Z"); tests.put("\\u005a", "Z"); for (Map.Entry<String, String> t : tests.entrySet()) { assertThat(StringResources.processStringResources(t.getKey())).isEqualTo(t.getValue()); } }
@Test public void shouldTrimWhitespace() { assertThat(StringResources.processStringResources(" ")).isEmpty(); assertThat(StringResources.processStringResources("Trailingwhitespace ")).isEqualTo("Trailingwhitespace"); assertThat(StringResources.processStringResources("Leadingwhitespace ")).isEqualTo("Leadingwhitespace"); }
@Test public void escape_shouldSupport() { Map<String, String> tests = new HashMap<>(); tests.put("\\\\", "\\"); tests.put("domain\\\\username", "domain\\username"); for (Map.Entry<String, String> t : tests.entrySet()) { assertThat(StringResources.processStringResources(t.getKey())).isEqualTo(t.getValue()); } }
@Test public void shouldCollapseInternalWhiteSpaces() { assertThat(StringResources.processStringResources("Whitespace in the middle")).isEqualTo("Whitespace in the middle"); assertThat(StringResources.processStringResources("Some\n\n\n\nNewlines")).isEqualTo("Some Newlines"); }
@Test public void testInvalidCodePoints() { List<String> tests = new ArrayList<>(); tests.add("\\u"); tests.add("\\u0"); tests.add("\\u00"); tests.add("\\u004"); tests.add("\\uzzzz"); tests.add("\\u0zzz"); tests.add("\\u00zz"); tests.add("\\u000z"); for (String t : tests) { try { StringResources.processStringResources(t); fail("expected IllegalArgumentException with test '" + t + "'"); } catch (IllegalArgumentException expected) { // cool } } } }
@Test public void escape_shouldIgnoreUnsupportedEscapeCodes() { assertThat(StringResources.processStringResources("\\ \\a\\b\\c\\d\\e\\ ")).isEqualTo(""); }
@Override public String getText() { if (currentNode == null) { return ""; } return StringResources.processStringResources(currentNode.getTextContent()); }
@Override public void onEnd(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { String s = buf.toString(); if (resType == ResType.CHAR_SEQUENCE) { s = StringResources.processStringResources(s); } else { s = s.trim(); } resourceTable.addResource(attrType, name, new TypedResource<>(s, resType, xmlContext)); } }
@Override public void onEnd(XMLStreamReader xml, XmlContext xmlContext) throws XMLStreamException { String s = buf.toString(); if (resType == ResType.CHAR_SEQUENCE) { s = StringResources.processStringResources(s); } resourceTable.addResource(attrType, name, new TypedResource<>(s, resType, xmlContext)); } }
public String qualify(String value) { if (value == null) return null; if (AttributeResource.isResourceReference(value)) { return "@" + ResName.qualifyResourceName(value.trim().substring(1).replace("+", ""), packageName, "attr"); } else if (AttributeResource.isStyleReference(value)) { return "?" + ResName.qualifyResourceName(value.trim().substring(1), packageName, "attr"); } else { return StringResources.processStringResources(value); } }