List of usage examples for org.apache.commons.lang3.text StrBuilder clear
public StrBuilder clear()
From source file:com.zergiu.tvman.init.SQLFileImporter.java
/** * @param statements/*from w ww. j a va 2 s.c o m*/ * @param line * @param statementBuilder */ private void buildStatement(List<String> statements, String line, StrBuilder statementBuilder) { int indexOfSemicolon = StringUtils.indexOf(line, ";"); if (indexOfSemicolon >= 0) { statementBuilder.append(line.substring(0, indexOfSemicolon));//add contents of the line up to ; statements.add(statementBuilder.toString()); //add the statement to the list statementBuilder.clear();//clear whatever we had before statementBuilder.append(line.substring(indexOfSemicolon + 1));//add whatever is left on that line after ; } else { //we're not done yet //always prepend a space before adding a new line statementBuilder.append(" " + line); } }
From source file:candr.yoclip.DefaultParserHelpFactoryTest.java
@Test public void testWrap() { final ParserHelpFactory<TestCase> testCase = new DefaultParserHelpFactory<TestCase>(); final int width = 30; assertThat("empty", StringUtils.isEmpty(testCase.wrap("", width)), is(true)); assertThat("null", StringUtils.isEmpty(testCase.wrap(null, width)), is(true)); StrBuilder builder = new StrBuilder(); builder.appendln(StringUtils.repeat("abc", 10)).append(StringUtils.repeat("abc", 5)); assertThat("!whitespace", testCase.wrap(StringUtils.repeat("abc", 15), width), is(builder.toString())); final String pangram = "The quick brown fox jumps over the lazy dog"; builder.clear(); builder.appendln("The quick brown fox jumps over").append("the lazy dog"); assertThat("pangram", testCase.wrap(pangram, width), is(builder.toString())); final String newLines = "\nExpected first line without indent.\n\nFollowed by more that are not indented.\n"; builder.clear();/*w w w. j a v a 2s . co m*/ builder.appendNewLine().appendln("Expected first line without").appendln("indent.").appendNewLine() .appendln("Followed by more that are not").appendln("indented."); assertThat("new lines", testCase.wrap(newLines, width), is(builder.toString())); }
From source file:candr.yoclip.DefaultParserHelpFactoryTest.java
@Test public void testHangingIndentWrap() { final ParserHelpFactory<TestCase> testCase = new DefaultParserHelpFactory<TestCase>(); final int width = 30; final int indent = 10; assertThat("empty", StringUtils.isEmpty(testCase.hangingIndentWrap("", indent, width)), is(true)); assertThat("null", StringUtils.isEmpty(testCase.hangingIndentWrap(null, indent, width)), is(true)); StrBuilder builder = new StrBuilder(); builder.appendln(StringUtils.repeat("abc", 10)).appendPadding(indent, ' ') .append(StringUtils.repeat("abc", 5)); assertThat("!whitespace", testCase.hangingIndentWrap(StringUtils.repeat("abc", 15), indent, width), is(builder.toString()));/*w w w.j ava 2 s .c o m*/ final String pangram = "The quick brown fox jumps over the lazy dog"; builder.clear(); builder.appendln("The quick brown fox jumps over").appendPadding(indent, ' ').append("the lazy dog"); assertThat("pangram", testCase.hangingIndentWrap(pangram, indent, width), is(builder.toString())); final String newLines = "\nExpected text without indent.\n\nFollowed by more that is indented.\n"; builder.clear(); builder.appendNewLine().appendln("Expected text without indent.").appendNewLine().appendPadding(indent, ' ') .appendln("Followed by more").appendPadding(indent, ' ').appendln("that is indented."); assertThat("new lines", testCase.hangingIndentWrap(newLines, indent, width), is(builder.toString())); }
From source file:de.vandermeer.skb.interfaces.transformers.textformat.Text_To_WrappedFormat.java
@Override default Pair<ArrayList<String>, ArrayList<String>> transform(String input) { Validate.notBlank(input);// w ww . ja v a2s . co m Validate.isTrue(this.getWidth() > 0); ArrayList<String> topList = new ArrayList<>(); ArrayList<String> bottomList = new ArrayList<>(); //an emergency break, counting loops to avoid endless loops int count; String text = StringUtils.replacePattern(input, "\\r\\n|\\r|\\n", LINEBREAK); text = StringUtils.replace(text, "<br>", LINEBREAK); text = StringUtils.replace(text, "<br/>", LINEBREAK); StrBuilder sb = new StrBuilder(text); if (this.getTopSettings() != null) { //we have a top request, do that one first Validate.notNull(this.getTopSettings().getLeft()); Validate.notNull(this.getTopSettings().getRight()); Validate.isTrue(this.getTopSettings().getLeft() > 0); Validate.isTrue(this.getTopSettings().getRight() > 0); int topLines = this.getTopSettings().getLeft(); int topWidth = this.getTopSettings().getRight(); count = 0; while (sb.size() > 0 && topLines > 0 && count++ < 200) { if (sb.startsWith(LINEBREAK)) { sb.replaceFirst(LINEBREAK, ""); } String s = null; boolean wln = false; if (sb.indexOf(LINEBREAK) > 0) { s = sb.substring(0, sb.indexOf(LINEBREAK)); wln = true; //sb.replace(0, sb.indexOf(LINEBREAK) + LINEBREAK.length(), ""); } else { s = sb.toString(); //sb.clear(); } String wrap = WordUtils.wrap(s, topWidth, LINEBREAK, true); StrTokenizer tok = new StrTokenizer(wrap, LINEBREAK).setIgnoreEmptyTokens(false); String[] ar = tok.getTokenArray(); if (ar.length <= topLines) { //all lines done, cleanup for (String str : ar) { topList.add(str.trim()); } if (wln == true) { //if we had a conditional linebreak there might be more text, remove the line we processed sb.replace(0, sb.indexOf(LINEBREAK) + LINEBREAK.length(), ""); } else { //no conditional line break, clean builder sb.clear(); } topLines = 0; } else { //we have more lines than we need, so remove the text we have from the builder and copy processed lines StrBuilder replace = new StrBuilder(); for (int i = 0; i < topLines; i++) { topList.add(ar[i].trim()); replace.appendSeparator(' ').append(ar[i]); } if (wln == true) { replace.append(LINEBREAK); } sb.replaceFirst(replace.toString(), ""); topLines = 0; } } } //no top, simple wrapping with recognition of conditional line breaks count = 0; while (sb.size() > 0 && count++ < 200) { if (sb.startsWith(LINEBREAK)) { sb.replaceFirst(LINEBREAK, ""); } String s = null; if (sb.indexOf(LINEBREAK) > 0) { s = sb.substring(0, sb.indexOf(LINEBREAK)); sb.replace(0, sb.indexOf(LINEBREAK) + LINEBREAK.length(), ""); } else { s = sb.toString(); sb.clear(); } s = WordUtils.wrap(s, this.getWidth(), LINEBREAK, true); StrTokenizer tok = new StrTokenizer(s, LINEBREAK).setIgnoreEmptyTokens(false); for (String str : tok.getTokenArray()) { bottomList.add(str.trim()); } } return Pair.of(topList, bottomList); }