XmlSanitizer { public static String sanitize(final String input, final String tag) { Pattern p = Pattern.compile("<" + tag + ">(.+?)</" + tag + ">", Pattern.DOTALL); if (BuildConfig.DEBUG) Log.d(TAG, "sanitize(): Using pattern \"" + p.pattern() + "\""); Matcher m = p.matcher(input); StringBuffer sb = new StringBuffer(); if (!m.find()) { return input; } int pos = 0; while (m.find(pos)) { sb = sb.append(input, pos, m.start(1)); sb = sb.append(escapeXml(m.group(1))); pos = m.end(1); } sb = sb.append(input, pos, input.length()); if (BuildConfig.DEBUG_DATA) { Log.d(TAG, "Sanitized string: "); BufferedReader dbr = new BufferedReader(new StringReader(sb.toString()), 1024); String dl; int ln = 0; try { while ((dl = dbr.readLine()) != null) { ++ln; Log.d(TAG, String.format("%4d: %s", ln, dl)); } } catch (IOException ioe) { } } return sb.toString(); }  static String sanitize(final String input, final String tag); static String escapeXml(final String s); static String unescapeXml(final String s);  }
@Test public void sanitizeEmptyString() { final String received = ""; String sanitized = XmlSanitizer.sanitize(received, "body"); assertThat(sanitized, is(equalTo(received))); }
@Test public void sanitizeEmptyBody() { final String received = "<body></body>"; String sanitized = XmlSanitizer.sanitize(received, "body"); assertThat(sanitized, is(equalTo(received))); }
@Test public void sanitizeTagNotFound() { String sanitized = XmlSanitizer.sanitize(rawText1, "arbitrary"); assertThat(sanitized, is(equalTo(rawText1))); }
@Test public void sanitizeDifferentTag() { String sanitized = XmlSanitizer.sanitize(rawText1, "prj"); assertThat(sanitized, is(equalTo(rawText1))); }
@Test public void sanitizeNullTag() { String sanitized = XmlSanitizer.sanitize(rawText1, ""); assertThat(sanitized, is(equalTo(rawText1))); }
@Test public void sanitizeBoincNewVersion() { String sanitized = XmlSanitizer.sanitize(rawText1, "body"); assertThat(sanitized, is(equalTo(sanitizedText1))); }