List of usage examples for org.apache.commons.lang3.text StrBuilder replace
public StrBuilder replace(final int startIndex, int endIndex, final String replaceStr)
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);/*from w w w . j av a 2s . 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); }
From source file:com.mgmtp.jfunk.data.generator.util.GeneratingExpression.java
/** * Resolves alternations by randomly choosing one at a time and adapting the pattern accordingly * //from w w w . j ava 2s. c o m * @param expression * the pattern * @return the reduced pattern */ private String chooseAlternations(final String expression) { StrBuilder sb = new StrBuilder(expression); int i = 0; // Loop until an unescaped pipe symbol appears. while (UNESCAPED_PIPE_PATTERN.matcher(sb.toString()).find()) { for (; i < sb.length(); ++i) { if (sb.charAt(i) == '|') { if (sb.charAt(i - 1) == '\\') { // escapet continue; } int start = i; // Backtrack until an opening bracket is found // to limit the context of alternatives. for (int closingCount = 0; start >= 0; --start) { char c = sb.charAt(start); if (c == '(') { if (closingCount == 0) { break; } --closingCount; } else if (c == ')') { ++closingCount; } } if (start >= 0) { // If an opening brace was found // search for a closing bracket. int end = i; for (int openingCount = 0; end < sb.length(); ++end) { char c = sb.charAt(end); if (c == '(') { ++openingCount; } else if (c == ')') { if (openingCount == 0) { break; } --openingCount; } } String alternative = random.getBoolean() ? sb.substring(start + 1, i) : sb.substring(i + 1, end); sb.replace(start, end + 1, alternative); i = start + alternative.length(); break; } String alternative = random.getBoolean() ? sb.substring(0, i) : sb.substring(i + 1); sb.replace(0, sb.length() + 1, alternative); break; } } } return sb.toString(); }