List of usage examples for org.apache.commons.lang3 StringUtils splitByWholeSeparatorPreserveAllTokens
public static String[] splitByWholeSeparatorPreserveAllTokens(final String str, final String separator)
Splits the provided text into an array, separator string specified.
From source file:com.kazuki43zoo.apistub.domain.model.KeyGeneratingStrategy.java
public static List<String> split(String keys) { return Arrays.asList(StringUtils.splitByWholeSeparatorPreserveAllTokens(keys, KEY_DELIMITER)); }
From source file:io.cloudslang.content.utils.CollectionUtilities.java
/** * Splits the stringArray by the delimiter into an array of strings without ignoring the escaped delimiters * * @param stringArray the string to be split * @param delimiter the delimiter by which to split the stringArray * @return an array of Strings/*from ww w . ja v a 2 s. co m*/ */ @NotNull public static String[] toArrayWithEscaped(@Nullable final String stringArray, @NotNull final String delimiter) { if (StringUtils.isEmpty(stringArray)) { return new String[0]; } return StringUtils.splitByWholeSeparatorPreserveAllTokens(stringArray, delimiter); }
From source file:com.kazuki43zoo.apistub.api.key.FixedLengthKeyExtractor.java
@Override public List<Object> extract(HttpServletRequest request, byte[] requestBody, String... expressions) { if (requestBody == null || requestBody.length == 0) { return Collections.emptyList(); }//from w ww . j a v a 2 s . c o m Charset defaultCharset = Optional.ofNullable(request.getContentType()).map(MediaType::parseMediaType) .map(MediaType::getCharset).orElse(StandardCharsets.UTF_8); return Stream.of(expressions).map(expression -> { String[] defines = StringUtils.splitByWholeSeparatorPreserveAllTokens(expression, ","); if (defines.length <= 2) { return null; } int offset = Integer.parseInt(defines[0].trim()); int length = Integer.parseInt(defines[1].trim()); String type = defines[2].trim().toLowerCase(); final Charset charset; if (defines.length >= 4) { charset = Charset.forName(defines[3].trim()); } else { charset = defaultCharset; } if (!(requestBody.length >= offset && requestBody.length >= offset + length)) { return null; } return Optional.ofNullable(FUNCTIONS.get(type)).orElseThrow(() -> new IllegalArgumentException( "A bad expression is detected. The specified type does not support. expression: '" + expression + "', specified type: '" + type + "', allowing types: " + FUNCTIONS.keySet())) .apply(Arrays.copyOfRange(requestBody, offset, offset + length), charset); }).filter(Objects::nonNull).collect(Collectors.toList()); }
From source file:com.act.lcms.db.io.parser.PlateCompositionParser.java
public void processFile(File inFile) throws IOException { try (BufferedReader br = new BufferedReader(new FileReader(inFile))) { String line;/* w w w. j a va 2s .co m*/ boolean readingCompositionTable = false; String compositionTableName = null; List<String> compositionTableColumns = null; int rowIndexInCompositionTable = 0; while ((line = br.readLine()) != null) { if (line.startsWith(">>")) { // TODO: add max table width based on plate type. String[] fields = StringUtils.splitByWholeSeparatorPreserveAllTokens(line, "\t"); readingCompositionTable = true; if (fields.length < 2) { throw new RuntimeException( String.format("Found malformed composition table header: %s", line)); } compositionTableColumns = Arrays.asList(fields); compositionTableName = fields[0].replaceFirst("^>>", ""); rowIndexInCompositionTable = 0; } else if (line.startsWith(">")) { String[] fields = StringUtils.split(line, "\t", 2); // Found a plate attribute. if (fields.length != 2) { System.err.format("Too few fields: %s\n", StringUtils.join(fields, ", ")); System.err.flush(); throw new RuntimeException(String.format("Found malformed plate attribute line: %s", line)); } plateProperties.put(fields[0].replaceFirst("^>", ""), fields[1]); } else if (line.trim().length() == 0) { // Assume a blank line terminates a composition table. readingCompositionTable = false; compositionTableName = null; compositionTableColumns = null; rowIndexInCompositionTable = 0; } else if (readingCompositionTable) { // This split method with a very long name preserves blanks and doesn't merge consecutive delimiters. String[] fields = StringUtils.splitByWholeSeparatorPreserveAllTokens(line, "\t"); // The split ^^ preserves blanks, so we can exactly compare the lengths. if (fields.length != compositionTableColumns.size()) { throw new RuntimeException(String.format( "Found %d fields where %d were expected in composition table line:\n '%s'\n", fields.length, compositionTableColumns.size(), line)); } for (int i = 1; i < fields.length; i++) { String val = compositionTableColumns.get(i); // No need to store empty values; if (val == null || val.isEmpty()) { continue; } Pair<String, String> coordinates = Pair.of(fields[0], val); // Note: assumes every row appears in each composition table (even empty ones). coordinatesToIndices.put(coordinates, Pair.of(rowIndexInCompositionTable, i - 1)); Map<Pair<String, String>, String> thisTable = compositionTables.get(compositionTableName); if (thisTable == null) { thisTable = new HashMap<>(); compositionTables.put(compositionTableName, thisTable); } // TODO: add paranoid check for repeated keys? Shouldn't be possible unless tables are repeated. thisTable.put(coordinates, fields[i]); } rowIndexInCompositionTable++; } } } }
From source file:com.google.dart.engine.internal.index.AbstractDartTest.java
/** * Prints given multi-line source in the way ready to paste back into Java test source. *///from w w w.ja v a2 s . co m protected static void printSourceLines(String source) { String[] lines = StringUtils.splitByWholeSeparatorPreserveAllTokens(source, EOL); for (int i = 0; i < lines.length; i++) { String line = lines[i]; line = StringUtils.replace(line, "\"", "\\\""); System.out.print("\""); System.out.print(line); if (i != lines.length - 1) { System.out.println("\","); } else { System.out.println("\""); } } }
From source file:io.wcm.handler.richtext.impl.RichTextHandlerImpl.java
private List<Content> processPlainText(String text) { if (StringUtils.isBlank(text)) { return ImmutableList.of(); }/* ww w . j a va 2 s .c o m*/ List<Content> content = new ArrayList<>(); String[] lines = StringUtils.splitByWholeSeparatorPreserveAllTokens(text, "\n"); for (int i = 0; i < lines.length; i++) { if (i > 0) { content.add(new Element("br")); } content.add(new Text(lines[i])); } return ImmutableList.copyOf(content); }
From source file:com.norconex.importer.handler.tagger.impl.HierarchyTagger.java
private void breakSegments(ImporterMetadata metadata, HierarchyDetails details) { List<String> nodes = new ArrayList<String>(); String sep = details.fromSeparator; if (StringUtils.isNotEmpty(details.toSeparator)) { sep = details.toSeparator;/*from w ww .j a va 2 s . c o m*/ } for (String value : metadata.getStrings(details.fromField)) { String[] segs = StringUtils.splitByWholeSeparatorPreserveAllTokens(value, details.fromSeparator); StringBuilder b = new StringBuilder(); for (String seg : segs) { if (seg.equals(details.fromSeparator)) { b.append(sep); } else { b.append(seg); } nodes.add(b.toString()); } } String field = details.fromField; if (StringUtils.isNotBlank(details.toField)) { field = details.toField; } String[] nodesArray = nodes.toArray(ArrayUtils.EMPTY_STRING_ARRAY); if (details.overwrite) { metadata.setString(field, nodesArray); } else { metadata.addString(field, nodesArray); } }
From source file:candr.yoclip.DefaultParserHelpFactory.java
@Override public String hangingIndentWrap(String text, int hangingIndent, int width) { if (StringUtils.isEmpty(text)) { return StringUtils.EMPTY; }//from w w w . j ava 2 s.c o m // make sure the hanging indent is reasonable hangingIndent = Math.max(hangingIndent, 0); int availableTextWidth = width - hangingIndent; if (availableTextWidth < 1) { final String error = String.format("Indent (%d) too large for width (%d).", hangingIndent, width); throw new OptionsParseException(error); } final StrBuilder builder = new StrBuilder(); // break the string into pieces based on the new line marker boolean builderWhitespaceOnly = true; final String[] lines = StringUtils.splitByWholeSeparatorPreserveAllTokens(text, Options.LINE_BREAK); for (int lineIndex = 0; lineIndex < lines.length; lineIndex++) { // account for the new line markers if (lineIndex > 0) { builder.appendNewLine(); } // for each line make sure it is wrapped to the line width String line = lines[lineIndex]; for (int wrapCount = 0; !StringUtils.isEmpty(line); wrapCount++) { // account for the text being wrapped if (wrapCount > 0) { builder.appendNewLine(); } // once text is added to the builder the line width changes based on the hanging indent int lineWidth; if (builderWhitespaceOnly) { lineWidth = width; builderWhitespaceOnly = false; } else { lineWidth = availableTextWidth; if (hangingIndent > 0) { builder.appendPadding(hangingIndent, ' '); } } if (line.length() <= lineWidth) { builder.append(line); break; } // look for a natural break in the text, otherwise force one int wrapPosition = line.lastIndexOf(' ', lineWidth); if (wrapPosition < 0) { wrapPosition = lineWidth; } builder.append(line.substring(0, wrapPosition)); line = line.substring(wrapPosition).trim(); } } return builder.toString(); }
From source file:jp.terasoluna.fw.file.dao.standard.VariableFileLineIterator.java
/** * ????? ???? ????<br>//from w ww. jav a2 s . c o m * <code>fileLineString</code>?<code>null</code>???? ?????????<code>String</code>???? * @param fileLineString ??1? * @return ? */ @Override protected String[] separateColumns(String fileLineString) { if (fileLineString == null || "".equals(fileLineString)) { return new String[0]; } // 1??? StringBuilder columnBuilder = new StringBuilder(); // ???? char previousChar = Character.MIN_VALUE; // ?????? List<String> columnList = new ArrayList<String>(); boolean isEnclosed = true; boolean isEscaped = false; int fieldCount = 0; char[] columnEncloseChar = getColumnEncloseChar(); if (!isEnclosed()) { return StringUtils.splitByWholeSeparatorPreserveAllTokens(fileLineString, Character.toString(delimiter)); } else { for (char currentChar : fileLineString.toCharArray()) { if (previousChar == Character.MIN_VALUE) { previousChar = currentChar; } if (previousChar == getEncloseCharcter(columnEncloseChar, fieldCount)) { if (isEnclosed) { if (currentChar == getEncloseCharcter(columnEncloseChar, fieldCount)) { isEnclosed = false; } } else { if (currentChar == getEncloseCharcter(columnEncloseChar, fieldCount)) { if (isEscaped) { columnBuilder.append(currentChar); isEscaped = false; } else { isEscaped = true; } } else if (currentChar == getDelimiter()) { if (isEscaped) { columnList.add(columnBuilder.toString()); previousChar = Character.MIN_VALUE; columnBuilder.delete(0, columnBuilder.length()); isEnclosed = true; isEscaped = false; fieldCount++; } else { columnBuilder.append(currentChar); isEscaped = false; } } else { columnBuilder.append(currentChar); } } } else { if (currentChar != getDelimiter()) { columnBuilder.append(currentChar); } else { columnList.add(columnBuilder.toString()); previousChar = Character.MIN_VALUE; columnBuilder.delete(0, columnBuilder.length()); fieldCount++; } } } columnList.add(columnBuilder.toString()); return columnList.toArray(new String[columnList.size()]); } }
From source file:com.addthis.hydra.job.spawn.JobAlertUtil.java
/** * Split a path up and replace any {{now-1}}-style elements with the YYMMDD equivalent * @param path The input path to process * @return The path with the relevant tokens replaced *///from w ww .j av a 2s .c om @VisibleForTesting static String expandDateMacro(String path) { String[] parts = StringUtils.splitByWholeSeparatorPreserveAllTokens(path, DateUtil.NOW_PREFIX); StringBuilder sb = new StringBuilder(parts[0]); for (int i = 1; i < parts.length; i++) { String part = parts[i]; int end = part.indexOf(DateUtil.NOW_POSTFIX); if (end > -1) { String dateEnd = part.substring(0, end + 2); sb.append(DateUtil.getDateTime(ymdFormatter, DateUtil.NOW_PREFIX + dateEnd).toString(ymdFormatter)); sb.append(part.substring(end + 2)); } else { sb.append(DateUtil.NOW_PREFIX); sb.append(part); } } return sb.toString(); }