@Test public void write() throws IOException { ByteArrayOutputStream out = null; try { out = new ByteArrayOutputStream(); TrimWriter writer = new TrimWriter(); @SuppressWarnings("nls") String text = "xyz\r\n" + "\tfoo\r\n" + " <pre> single </pre> \r\n" + " bar \r\n" + "<pre class=\"xyz\"> a b c \r\n" + " d e \r\n" + " f g </pre>\r\n" + " \t \r\n" + "<textarea class=\"test\"> a b c \r\n" + " d e \r\n" + " f g </textarea>\r\n" + " zz<!-- hello world-->z \t \r\n" + "\t<div class=\"code-view-wrapper\"><!--__NOTRIM__--><div class=\"code-view\"> a \r\n" + " b \r\n" + " c </div><!--__/NOTRIM__--></div>\t\r\n"; writer.write(text, out, Charsets.UTF_8); } finally { Util.closeQuietly(out); } String result = new String(out.toByteArray(), Charsets.UTF_8); @SuppressWarnings("nls") String expected = "xyz\n" + "foo\n" + "<pre> single </pre>\n" + "bar\n" + "<pre class=\"xyz\"> a b c \n" + " d e \n" + " f g </pre>\n" + "<textarea class=\"test\"> a b c \n" + " d e \n" + " f g </textarea>\n" + "zzz\n" + "<div class=\"code-view-wrapper\"><div class=\"code-view\"> a \n" + " b \n" + " c </div></div>\n"; assertEquals(expected, result); }
void write(String text, OutputStream out, Charset charset) throws IOException { BufferedReader in = null; try { in = new BufferedReader(new StringReader(text)); String line; boolean textareaTag = false; boolean preTag = false; boolean notrimTag = false; boolean trim = true; while ((line = in.readLine()) != null) { String origLine = line; line = StringUtils.replace(line, "__NOTRIM__", StringUtils.EMPTY); line = StringUtils.replace(line, "__/NOTRIM__", StringUtils.EMPTY); line = REMOVE_COMMENT.replaceAll(line); if (trim) { line = TRIM_LEFT.replaceAll(line); } if (origLine.contains("<textarea")) { textareaTag = true; } if (origLine.contains("<pre")) { preTag = true; } if (origLine.contains("__NOTRIM__")) { notrimTag = true; } if (textareaTag || preTag || notrimTag) { trim = false; } if (origLine.contains("</textarea")) { textareaTag = false; } if (origLine.contains("</pre")) { preTag = false; } if (origLine.contains("__/NOTRIM__")) { notrimTag = false; } if (!textareaTag && !preTag && !notrimTag) { trim = true; } if (trim) { line = TRIM_RIGHT.replaceAll(line); } boolean doWrite = !trim || StringUtils.isNotBlank(line); if (doWrite) { writeln(line, out, charset); } } } finally { Util.closeQuietly(in); } }
TrimWriter { void write(String text, OutputStream out, Charset charset) throws IOException { BufferedReader in = null; try { in = new BufferedReader(new StringReader(text)); String line; boolean textareaTag = false; boolean preTag = false; boolean notrimTag = false; boolean trim = true; while ((line = in.readLine()) != null) { String origLine = line; line = StringUtils.replace(line, "__NOTRIM__", StringUtils.EMPTY); line = StringUtils.replace(line, "__/NOTRIM__", StringUtils.EMPTY); line = REMOVE_COMMENT.replaceAll(line); if (trim) { line = TRIM_LEFT.replaceAll(line); } if (origLine.contains("<textarea")) { textareaTag = true; } if (origLine.contains("<pre")) { preTag = true; } if (origLine.contains("__NOTRIM__")) { notrimTag = true; } if (textareaTag || preTag || notrimTag) { trim = false; } if (origLine.contains("</textarea")) { textareaTag = false; } if (origLine.contains("</pre")) { preTag = false; } if (origLine.contains("__/NOTRIM__")) { notrimTag = false; } if (!textareaTag && !preTag && !notrimTag) { trim = true; } if (trim) { line = TRIM_RIGHT.replaceAll(line); } boolean doWrite = !trim || StringUtils.isNotBlank(line); if (doWrite) { writeln(line, out, charset); } } } finally { Util.closeQuietly(in); } } }
TrimWriter { void write(String text, OutputStream out, Charset charset) throws IOException { BufferedReader in = null; try { in = new BufferedReader(new StringReader(text)); String line; boolean textareaTag = false; boolean preTag = false; boolean notrimTag = false; boolean trim = true; while ((line = in.readLine()) != null) { String origLine = line; line = StringUtils.replace(line, "__NOTRIM__", StringUtils.EMPTY); line = StringUtils.replace(line, "__/NOTRIM__", StringUtils.EMPTY); line = REMOVE_COMMENT.replaceAll(line); if (trim) { line = TRIM_LEFT.replaceAll(line); } if (origLine.contains("<textarea")) { textareaTag = true; } if (origLine.contains("<pre")) { preTag = true; } if (origLine.contains("__NOTRIM__")) { notrimTag = true; } if (textareaTag || preTag || notrimTag) { trim = false; } if (origLine.contains("</textarea")) { textareaTag = false; } if (origLine.contains("</pre")) { preTag = false; } if (origLine.contains("__/NOTRIM__")) { notrimTag = false; } if (!textareaTag && !preTag && !notrimTag) { trim = true; } if (trim) { line = TRIM_RIGHT.replaceAll(line); } boolean doWrite = !trim || StringUtils.isNotBlank(line); if (doWrite) { writeln(line, out, charset); } } } finally { Util.closeQuietly(in); } }  }
TrimWriter { void write(String text, OutputStream out, Charset charset) throws IOException { BufferedReader in = null; try { in = new BufferedReader(new StringReader(text)); String line; boolean textareaTag = false; boolean preTag = false; boolean notrimTag = false; boolean trim = true; while ((line = in.readLine()) != null) { String origLine = line; line = StringUtils.replace(line, "__NOTRIM__", StringUtils.EMPTY); line = StringUtils.replace(line, "__/NOTRIM__", StringUtils.EMPTY); line = REMOVE_COMMENT.replaceAll(line); if (trim) { line = TRIM_LEFT.replaceAll(line); } if (origLine.contains("<textarea")) { textareaTag = true; } if (origLine.contains("<pre")) { preTag = true; } if (origLine.contains("__NOTRIM__")) { notrimTag = true; } if (textareaTag || preTag || notrimTag) { trim = false; } if (origLine.contains("</textarea")) { textareaTag = false; } if (origLine.contains("</pre")) { preTag = false; } if (origLine.contains("__/NOTRIM__")) { notrimTag = false; } if (!textareaTag && !preTag && !notrimTag) { trim = true; } if (trim) { line = TRIM_RIGHT.replaceAll(line); } boolean doWrite = !trim || StringUtils.isNotBlank(line); if (doWrite) { writeln(line, out, charset); } } } finally { Util.closeQuietly(in); } }   }
TrimWriter { void write(String text, OutputStream out, Charset charset) throws IOException { BufferedReader in = null; try { in = new BufferedReader(new StringReader(text)); String line; boolean textareaTag = false; boolean preTag = false; boolean notrimTag = false; boolean trim = true; while ((line = in.readLine()) != null) { String origLine = line; line = StringUtils.replace(line, "__NOTRIM__", StringUtils.EMPTY); line = StringUtils.replace(line, "__/NOTRIM__", StringUtils.EMPTY); line = REMOVE_COMMENT.replaceAll(line); if (trim) { line = TRIM_LEFT.replaceAll(line); } if (origLine.contains("<textarea")) { textareaTag = true; } if (origLine.contains("<pre")) { preTag = true; } if (origLine.contains("__NOTRIM__")) { notrimTag = true; } if (textareaTag || preTag || notrimTag) { trim = false; } if (origLine.contains("</textarea")) { textareaTag = false; } if (origLine.contains("</pre")) { preTag = false; } if (origLine.contains("__/NOTRIM__")) { notrimTag = false; } if (!textareaTag && !preTag && !notrimTag) { trim = true; } if (trim) { line = TRIM_RIGHT.replaceAll(line); } boolean doWrite = !trim || StringUtils.isNotBlank(line); if (doWrite) { writeln(line, out, charset); } } } finally { Util.closeQuietly(in); } }    }