List of usage examples for org.apache.commons.lang3.text WordUtils wrap
public static String wrap(final String str, int wrapLength, String newLineStr, final boolean wrapLongWords)
Wraps a single line of text, identifying words by ' '
.
Leading spaces on a new line are stripped.
From source file:com.github.tomakehurst.wiremock.common.Strings.java
public static String wrapIfLongestLineExceedsLimit(String s, int maxLineLength) { int longestLength = findLongestLineLength(s); if (longestLength > maxLineLength) { String wrapped = WordUtils.wrap(s, maxLineLength, null, true); return wrapped.replaceAll("(?m)^[ \t]*\r?\n", ""); }//from w ww . j av a 2s .co m return s; }
From source file:derpatiel.progressivediff.util.TextHelper.java
public static String[] cutLongString(String string, int characters) { return WordUtils.wrap(string, characters, "/cut", false).split("/cut"); }
From source file:ke.co.tawi.babblesms.server.utils.StringUtil.java
/** * Breaks up a long String into shorter strings of a specified length. * <p>/*from www.j a va2 s. co m*/ * If the String to be broken is shorter than the length, then the size of * the returned array is one. * <p> * An example of where this function is useful is breaking up a long SMS into * shorter ones. * * @param toBreak * @param length * @return an array of Strings */ public static String[] breakupStr(String toBreak, int length) { String tokenStr = RandomStringUtils.randomAlphabetic(RANDOM_STRING_SIZE); String wrappedStr = WordUtils.wrap(toBreak, length, tokenStr, true); return wrappedStr.split(tokenStr); }
From source file:com.avatarproject.core.command.CommandHelp.java
@Override public void execute(CommandSender sender, List<String> args) { if (!isPlayer(sender) || !hasPermission(sender) || !correctLength(sender, args.size(), 0, 1)) { return;/*w w w.j a v a2 s . co m*/ } Player player = (Player) sender; List<String> order = new ArrayList<String>(); HashMap<String, TextComponent> components = new HashMap<String, TextComponent>(); for (APCommand command : instances.values()) { if (hasPermissionHelp(sender, command.getName())) { TextComponent tc = new TextComponent(ChatColor.GRAY + command.getProperUse()); String tooltip = WordUtils.wrap(ChatColor.GRAY + command.getDescription(), 40, "\n" + ChatColor.GRAY, false); tc.setHoverEvent( new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(tooltip).create())); tc.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, command.getProperUse())); order.add(command.getName()); components.put(command.getName(), tc); } } order.remove(APCommand.instances.get("help").getName()); Collections.sort(order); Collections.reverse(order); order.add(APCommand.instances.get("help").getName()); Collections.reverse(order); List<TextComponent> neworder = new ArrayList<TextComponent>(); for (String s : order) { neworder.add(components.get(s)); } if (args.size() == 0 || isNumeric(args.get(0))) { int page = 0; if (args.size() > 0 && isNumeric(args.get(0))) { page = Integer.valueOf(args.get(0)) - 1; } for (TextComponent tc : getPage(Strings.COMMAND_HELP_TITLE.toString(), page, neworder)) { player.spigot().sendMessage(tc); } return; } String arg = args.get(0); if (instances.keySet().contains(arg.toLowerCase())) { instances.get(arg).help(sender, true); } else { sender.sendMessage("Invalid command!"); } }
From source file:com.mirth.connect.util.CodeTemplateUtil.java
public static String updateCode(String code) { if (StringUtils.isNotBlank(code)) { code = StringUtils.trim(code);//w w w .ja va2 s . c o m int endIndex = 0; Matcher matcher = COMMENT_PATTERN.matcher(code); if (matcher.find()) { endIndex = matcher.end(); } CodeTemplateDocumentation documentation = getDocumentation(code); String description = documentation.getDescription(); CodeTemplateFunctionDefinition functionDefinition = documentation.getFunctionDefinition(); if (StringUtils.isBlank(description)) { description = "Modify the description here. Modify the function name and parameters as needed. One function per template is recommended; create a new code template for each new function."; } StringBuilder builder = new StringBuilder("/**"); for (String descriptionLine : description.split("\r\n|\r|\n")) { builder.append("\n\t").append(WordUtils.wrap(descriptionLine, 100, "\n\t", false)); } if (functionDefinition != null) { builder.append('\n'); if (CollectionUtils.isNotEmpty(functionDefinition.getParameters())) { for (Parameter parameter : functionDefinition.getParameters()) { StringBuilder parameterBuilder = new StringBuilder("\n\t@param {"); parameterBuilder.append(StringUtils.defaultString(parameter.getType(), "Any")); parameterBuilder.append("} ").append(parameter.getName()).append(" - ") .append(StringUtils.trimToEmpty(parameter.getDescription())); builder.append(WordUtils.wrap(parameterBuilder.toString(), 100, "\n\t\t", false)); } } StringBuilder returnBuilder = new StringBuilder("\n\t@return {") .append(StringUtils.defaultString(functionDefinition.getReturnType(), "Any")).append("} ") .append(StringUtils.trimToEmpty(functionDefinition.getReturnDescription())); builder.append(WordUtils.wrap(returnBuilder.toString(), 100, "\n\t\t", false)); } builder.append("\n*/\n"); return builder.toString() + code.substring(endIndex); } return code; }
From source file:com.mirth.connect.model.CodeTemplate.java
private static String addComment(String code, String description) { if (StringUtils.isNotBlank(description)) { return new StringBuilder("/**\n\t").append(WordUtils.wrap(description, 80, "\n\t", false)) .append("\n*/\n").append(code).toString(); }//from w ww . j av a 2 s .co m return code; }
From source file:com.qwazr.utils.HtmlUtils.java
public final static String htmlWrap(String text, int wrapLength) { if (StringUtils.isEmpty(text)) return text; if (text.length() < wrapLength) return text; text = StringUtils.replace(text, "­", ""); return WordUtils.wrap(text, wrapLength, "­", true); }
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 .j a va 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:net.dacce.commons.cli.HelpFormatter.java
private String generateUsage() { StringBuffer buff = new StringBuffer("Usage: ").append(cmdLineSyntax).append(" "); Collection<ExclusiveOptions> processedGroups = new ArrayList<ExclusiveOptions>(); boolean first = true; for (Option option : options.getAllOptions()) { if (first) { first = false;/*from w ww . j a v a 2s . c o m*/ } else { buff.append(" "); } ExclusiveOptions group = options.getExclusiveOptionGroup(option); if (group != null) { if (!processedGroups.contains(group)) { processedGroups.add(group); appendOptionGroupUsage(buff, group); } } else { appendOptionUsage(buff, option, option.isRequired()); } } return WordUtils.wrap(buff.toString(), width - leftPad, newLine + createPadding(leftPad), true); }
From source file:io.anserini.doc.DataModel.java
public String generateIndexingCommand(String collection) { Map<String, Object> config = this.collections.get(collection); ObjectMapper oMapper = new ObjectMapper(); StringBuilder builder = new StringBuilder(); builder.append("nohup sh "); builder.append(safeGet(config, "index_command")); builder.append(" -collection ").append(safeGet(config, "collection")); builder.append(" -generator ").append(safeGet(config, "generator")); builder.append(" -threads ").append(safeGet(config, "threads")); builder.append(" -input ").append("/path/to/" + collection); builder.append(" -index ").append("lucene-index." + safeGet(config, "name") + ".pos+docvectors"); List indexParams = oMapper.convertValue(safeGet(config, "index_options"), List.class); for (Object option : indexParams) { builder.append(" ").append(option); }// w w w . ja v a2 s .co m builder.append(String.format(" >& log.%s.pos+docvectors%s &", collection, indexParams.contains("-storeRawDocs") ? "+rawdocs" : "")); return WordUtils.wrap(builder.toString(), 80, " \\\n", false); }