Java Code Examples for org.apache.commons.lang3.StringUtils#splitPreserveAllTokens()

The following examples show how to use org.apache.commons.lang3.StringUtils#splitPreserveAllTokens() .
Example 1
Project: synopsys-detect   File: ListValueParser.java    License: Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public List<T> parse(@NotNull final String rawValue) throws ValueParseException {
    final List<T> parsedValues = new ArrayList<>();

    for (final String element : StringUtils.splitPreserveAllTokens(rawValue, delimiter)) {
        final String trimmedElement = element.trim();

        if (StringUtils.isBlank(trimmedElement)) {
            throw new ValueParseException(rawValue, "List",
                String.format("Failed to parse list '%s'. The list must be comma separated and each element in the list must not be empty (at least one character that is not whitespace).", rawValue));
        } else {
            parsedValues.add(valueParser.parse(trimmedElement));
        }
    }

    return parsedValues;
}
 
Example 2
Project: riiablo   File: TxtParser.java    License: Apache License 2.0 6 votes vote down vote up
private TxtParser(BufferedReader in) {
  this.in = in;

  try {
    line = in.readLine();
    columns = StringUtils.splitPreserveAllTokens(line, '\t');
    if (DEBUG_COLS) Gdx.app.debug(TAG, "cols=" + Arrays.toString(columns));

    ids = new ObjectIntMap<>();
    for (int i = 0; i < columns.length; i++) {
      String key = columns[i].toLowerCase();
      if (!ids.containsKey(key)) ids.put(key, i);
    }
  } catch (Throwable t) {
    throw new GdxRuntimeException("Couldn't read txt", t);
  }
}
 
Example 3
Project: cyberduck   File: IRODSSession.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setUserName(final String input) {
    final String user;
    final AuthScheme scheme;
    if(StringUtils.contains(input, ':')) {
        // Support non default auth scheme (PAM)
        user = StringUtils.splitPreserveAllTokens(input, ':')[1];
        // Defaults to standard if not found
        scheme = AuthScheme.findTypeByString(StringUtils.splitPreserveAllTokens(input, ':')[0]);
    }
    else {
        user = input;
        if(StringUtils.isNotBlank(host.getProtocol().getAuthorization())) {
            scheme = AuthScheme.findTypeByString(host.getProtocol().getAuthorization());
        }
        else {
            // We can default to Standard if not specified
            scheme = AuthScheme.STANDARD;
        }
    }
    super.setUserName(user);
    this.setAuthenticationScheme(scheme);
}
 
Example 4
Project: swagger   File: AntPathMatcher.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Given a pattern and a full path, determine the pattern-mapped part. <p>For example: <ul>
 * <li>'{@code /docs/cvs/commit.html}' and '{@code /docs/cvs/commit.html} -> ''</li>
 * <li>'{@code /docs/*}' and '{@code /docs/cvs/commit} -> '{@code cvs/commit}'</li>
 * <li>'{@code /docs/cvs/*.html}' and '{@code /docs/cvs/commit.html} -> '{@code commit.html}'</li>
 * <li>'{@code /docs/**}' and '{@code /docs/cvs/commit} -> '{@code cvs/commit}'</li>
 * <li>'{@code /docs/**\/*.html}' and '{@code /docs/cvs/commit.html} -> '{@code cvs/commit.html}'</li>
 * <li>'{@code /*.html}' and '{@code /docs/cvs/commit.html} -> '{@code docs/cvs/commit.html}'</li>
 * <li>'{@code *.html}' and '{@code /docs/cvs/commit.html} -> '{@code /docs/cvs/commit.html}'</li>
 * <li>'{@code *}' and '{@code /docs/cvs/commit.html} -> '{@code /docs/cvs/commit.html}'</li> </ul>
 * <p>Assumes that {@link #match} returns {@code true} for '{@code pattern}' and '{@code path}', but
 * does <strong>not</strong> enforce this.
 */
@Override
public String extractPathWithinPattern(String pattern, String path) {
    String[] patternParts = StringUtils.splitPreserveAllTokens(pattern, this.pathSeparator);
    String[] pathParts = StringUtils.splitPreserveAllTokens(path, this.pathSeparator);
    StringBuilder builder = new StringBuilder();
    boolean pathStarted = false;

    for (int segment = 0; segment < patternParts.length; segment++) {
        String patternPart = patternParts[segment];
        if (patternPart.indexOf('*') > -1 || patternPart.indexOf('?') > -1) {
            for (; segment < pathParts.length; segment++) {
                if (pathStarted || (segment == 0 && !pattern.startsWith(this.pathSeparator))) {
                    builder.append(this.pathSeparator);
                }
                builder.append(pathParts[segment]);
                pathStarted = true;
            }
        }
    }

    return builder.toString();
}
 
Example 5
/**
 * Using a recursive call to check if a next line exists, may lead to an over flow.
 * Use a while loop with a function call, which has a tri-state return value.
 * 
 * @return ReadState
 * @throws IOException
 */
private ReadState loadNext() throws IOException{
	if(reader != null){
		currentRow  = reader.readLine();
		if(currentRow == null){
			return ReadState.no;
		}

		lineNumber++;

		final String trimmedLine = StringUtils.trimToEmpty(currentRow);
		if (trimmedLine.length() == 0) {
			return ReadState.next;
		}
		else if (trimmedLine.startsWith(commentPrefix)) {
			if (isHeaderMetaData(trimmedLine)) {
				handleHeaderMetaData(trimmedLine);
			}
			else {
				fireComment();
			}
			return ReadState.next;
		}
		else{
			fireParsing();
			this.currentCols = StringUtils.splitPreserveAllTokens(this.currentRow, '\t');
			return validateLine(currentCols);
		}
	}
	return ReadState.no;
}
 
Example 6
Project: sakai   File: CourseSectionImpl.java    License: Educational Community License v2.0 5 votes vote down vote up
private boolean getIndexedBooleanProperty(int index, String complexString) {
	String[] sa = StringUtils.splitPreserveAllTokens(complexString, CourseSectionImpl.SEP_CHARACTER);
	if(sa == null) {
		return false;
	}
	if(index >=sa.length) {
		log.debug("Can not get " + index + " index from string " + complexString);
		return false;
	}
	return Boolean.parseBoolean(sa[index]);
}
 
Example 7
Project: hivemq-community-edition   File: DefaultPermissionsEvaluator.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean checkPublish(@Nullable final ModifiableDefaultPermissions permissions, @NotNull final String topic,
                                    @NotNull final QoS qos, final boolean retain) {
    if (permissions == null) {
        //no permissions set -> default to DENY
        return false;
    }

    final List<TopicPermission> topicPermissions = permissions.asList();

    if (topicPermissions.size() < 1) {
        return permissions.getDefaultBehaviour() == DefaultAuthorizationBehaviour.ALLOW;
    }

    final String[] splitTopic = StringUtils.splitPreserveAllTokens(topic, "/");
    final String stripedTopic;
    if (topic.length() > 1) {
        stripedTopic = StringUtils.stripEnd(topic, "/");
    } else {
        stripedTopic = topic;
    }
    for (final TopicPermission topicPermission : permissions.asList()) {
        if (implied(topicPermission, stripedTopic, splitTopic, qos, TopicPermission.MqttActivity.PUBLISH, retain)) {
            return topicPermission.getType() == TopicPermission.PermissionType.ALLOW;
        }
    }

    return permissions.getDefaultBehaviour() == DefaultAuthorizationBehaviour.ALLOW;
}
 
Example 8
Project: sakai   File: CourseSectionImpl.java    License: Educational Community License v2.0 5 votes vote down vote up
private String getIndexedStringProperty(int index, String complexString) {
	if(complexString == null || "".equals(complexString.trim())) {
		return null;
	}
	String[] sa = StringUtils.splitPreserveAllTokens(complexString, CourseSectionImpl.SEP_CHARACTER);
	if(index >=sa.length) {
		log.warn("Can not get " + index + " index from string " + complexString);
		return null;
	}
	return sa[index];
}
 
Example 9
Project: hivemq-community-edition   File: PermissionTopicMatcher.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(@NotNull final String permissionTopic, @NotNull final String actualTopic) throws InvalidTopicException {
    final String stripedPermissionTopic = StringUtils.stripEnd(permissionTopic, "/");
    final String[] splitPermissionTopic = StringUtils.splitPreserveAllTokens(stripedPermissionTopic, "/");
    final boolean nonWildCard = StringUtils.containsNone(stripedPermissionTopic, "#+");
    final boolean rootWildCard = stripedPermissionTopic.contains("#");
    final boolean endsWithWildCard = StringUtils.endsWith(stripedPermissionTopic, "/#");

    final String stripedActualTopic = StringUtils.stripEnd(actualTopic, "/");
    final String[] splitActualTopic = StringUtils.splitPreserveAllTokens(stripedActualTopic, "/");
    return matches(stripedPermissionTopic, splitPermissionTopic, nonWildCard, endsWithWildCard, rootWildCard, stripedActualTopic, splitActualTopic);
}
 
Example 10
Project: riiablo   File: TXT.java    License: Apache License 2.0 5 votes vote down vote up
public static TXT loadFromStream(InputStream in) {
  BufferedReader reader = null;
  try {
    reader = IOUtils.buffer(new InputStreamReader(in, "US-ASCII"));
    ObjectIntMap<String> columns = new ObjectIntMap<>();
    String[] columnNames = StringUtils.splitPreserveAllTokens(reader.readLine(), '\t');
    for (int i = 0; i < columnNames.length; i++) {
      String key = columnNames[i].toLowerCase();
      if (!columns.containsKey(key)) columns.put(key, i);
    }
    if (DEBUG_COLS) {
      ObjectSet<String> duplicates = new ObjectSet<>();
      String[] colNames = new String[columns.size];
      for (int i = 0, j = 0; i < columnNames.length; i++) {
        String columnName = columnNames[i];
        if (!duplicates.add(columnName)) continue;
        colNames[j++] = columnName;
      }
      Gdx.app.debug(TAG, "cols=" + Arrays.toString(colNames));
    }

    Array<String[]> data = new Array<>(String[].class);
    for (String line; (line = reader.readLine()) != null;) {
      String[] tmp = split(columnNames.length, line, '\t');
      if (tmp.length != columnNames.length) {
        if (DEBUG_ROWS) Gdx.app.debug(TAG, "Skipping row " + Arrays.toString(tmp));
        continue;
      }

      data.add(tmp);
      if (DEBUG_ROWS) Gdx.app.debug(TAG, data.size - 1 + ": " + Arrays.toString(tmp));
    }

    return new TXT(columns, data);
  } catch (Throwable t) {
    throw new GdxRuntimeException("Couldn't read TXT", t);
  } finally {
    IOUtils.closeQuietly(reader);
  }
}
 
Example 11
Project: sakai   File: CourseSectionImpl.java    License: Educational Community License v2.0 5 votes vote down vote up
private boolean getIndexedBooleanProperty(int index, String complexString) {
	String[] sa = StringUtils.splitPreserveAllTokens(complexString, CourseSectionImpl.SEP_CHARACTER);
	if(sa == null) {
		return false;
	}
	if(index >=sa.length) {
		log.debug("Can not get " + index + " index from string " + complexString);
		return false;
	}
	return Boolean.parseBoolean(sa[index]);
}
 
Example 12
Project: flink   File: SqlFunctionUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Split target string with custom separator and pick the index-th(start with 0) result.
 *
 * @param str   target string.
 * @param character int value of the separator character
 * @param index index of the result which you want.
 * @return the string at the index of split results.
 */
public static String splitIndex(String str, int character, int index) {
	if (character > 255 || character < 1 || index < 0) {
		return null;
	}
	String[] values = StringUtils.splitPreserveAllTokens(str, (char) character);
	if (index >= values.length) {
		return null;
	} else {
		return values[index];
	}
}
 
Example 13
Project: owltools   File: SplitTest.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Assert that the regex and the {@link StringUtils} produce the same result.
 */
@Test
public void testSplit() {
	final String source = "a\t\t\ta";
	String[] splitOld = source.split("\\t");
	String[] splitNew = StringUtils.splitPreserveAllTokens(source, '\t');
	assertArrayEquals(splitOld, splitNew);
}
 
Example 14
Project: vscrawler   File: Split.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected  String[] split(String str, String separatorChars, int max, boolean preserveAllTokens) {
    if (preserveAllTokens) {
        return StringUtils.splitPreserveAllTokens(str, separatorChars, max);
    } else {
        return StringUtils.split(str, separatorChars, max);
    }
}
 
Example 15
Project: cuba   File: MetadataTools.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Return a collection of properties included into entity's name pattern (see {@link NamePattern}).
 *
 * @param metaClass   entity metaclass
 * @param useOriginal if true, and if the given metaclass doesn't define a {@link NamePattern} and if it is an
 *                    extended entity, this method tries to find a name pattern in an original entity
 * @return collection of the name pattern properties
 */
@Nonnull
public Collection<MetaProperty> getNamePatternProperties(MetaClass metaClass, boolean useOriginal) {
    Collection<MetaProperty> properties = new ArrayList<>();
    String pattern = (String) getMetaAnnotationAttributes(metaClass.getAnnotations(), NamePattern.class).get("value");
    if (pattern == null && useOriginal) {
        MetaClass original = metadata.getExtendedEntities().getOriginalMetaClass(metaClass);
        if (original != null) {
            pattern = (String) getMetaAnnotationAttributes(original.getAnnotations(), NamePattern.class).get("value");
        }
    }
    if (!StringUtils.isBlank(pattern)) {
        String value = StringUtils.substringAfter(pattern, "|");
        String[] fields = StringUtils.splitPreserveAllTokens(value, ",");
        for (String field : fields) {
            String fieldName = StringUtils.trim(field);

            MetaProperty property = metaClass.getProperty(fieldName);
            if (property != null) {
                properties.add(metaClass.getProperty(fieldName));
            } else {
                throw new DevelopmentException(
                        String.format("Property '%s' is not found in %s", field, metaClass.toString()),
                        "NamePattern", pattern);
            }
        }
    }
    return properties;
}
 
Example 16
Project: owltools   File: GAFParser.java    License: BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
	 * Using a recursive call to check if a next line exists, may lead to an over flow.
	 * Use a while loop with a function call, which has a tri-state return value.
	 * 
	 * @return ReadState
	 * @throws IOException
	 */
	private ReadState loadNext() throws IOException{
		if(reader != null){
			currentRow  = reader.readLine();
			if(currentRow == null){
				return ReadState.no;
			}

			lineNumber++;
//			if(DEBUG)
//				LOG.debug("Parsing Row: " +lineNumber + " -- " +currentRow);

			final String trimmedLine = StringUtils.trimToEmpty(currentRow);
			if (trimmedLine.length() == 0) {
				LOG.warn("Blank Line");
				return ReadState.next;
			}else if (trimmedLine.startsWith(GAF_COMMENT)) {
				
				fireComment();
				
				if(gafVersion<1){
				
					if (isFormatDeclaration(trimmedLine)) {
						gafVersion = parseGafVersion(trimmedLine);
						if (gafVersion >= 2.0) {
							expectedNumCols = 17;
						}
					}
				}
				return ReadState.next;
			}else{
				
				
				fireParsing();
				
				// use more efficient implementation to split line
				// this.currentRow.split("\\t", -1);
				this.currentCols = StringUtils.splitPreserveAllTokens(this.currentRow, '\t');
				if (expectedNumCols == 17 && currentCols.length == 16) {
					//LOG.warn("Fix missing tab for GAF "+Double.toString(gafVersion)+" format in line: "+lineNumber);
					// repair
					// add an empty "" to the array
					this.currentCols = Arrays.copyOf(currentCols, 17);
					this.currentCols[16] = "";
					fireParsingWarning("Fix missing tab for GAF "+Double.toString(gafVersion)+" format, expected 17 columns but found only 16.");
				}
				if (expectedNumCols == 17 && currentCols.length == 15) {
					//LOG.warn("Fix missing tabs for GAF "+Double.toString(gafVersion)+" format in line: "+lineNumber);
					// repair
					// add two empty "" to the array
					this.currentCols = Arrays.copyOf(currentCols, 17);
					this.currentCols[15] = "";
					this.currentCols[16] = "";
					fireParsingWarning("Fix missing tab for GAF "+Double.toString(gafVersion)+" format, expected 17 columns but found only 15.");
				}
				if (currentCols.length != expectedNumCols) {

					String error = "Got invalid number of columns for row '"+lineNumber+"' (expected "
						+ expectedNumCols
						+ ", got "
						+ currentCols.length
						+ ")";
	
					if(currentCols.length<expectedNumCols){
						String v =error;
						voilations.add(v);
						fireParsingError(error);
						LOG.error(error + "  The row is ignored: " + this.currentRow);
						return ReadState.next;
					}else{
						fireParsingWarning(error);
						LOG.info(error + " : " + this.currentRow);
					}
				}
				return ReadState.success;
			}
			
		}
		
		return ReadState.no;
			
	}
 
Example 17
Project: hivemq-community-edition   File: TopicTreeImpl.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void removeSubscriber(@NotNull final String subscriber, @NotNull final String topic, @Nullable final String sharedName) {
    checkNotNull(subscriber);
    checkNotNull(topic);

    if ("#".equals(topic)) {
        removeRootWildcardSubscriber(subscriber, sharedName);
        return;
    }
    //We can shortcut here in case we don't have any segments
    if (segments.isEmpty()) {
        return;
    }

    if (topic.isEmpty()) {
        log.debug("Tried to remove an empty topic from the topic tree.");
        return;
    }

    final String[] topicPart = StringUtils.splitPreserveAllTokens(topic, "/");

    final Node[] nodes = new Node[topicPart.length];
    final String segmentKey = topicPart[0];
    final Lock lock = segmentLocks.get(segmentKey).writeLock();
    lock.lock();
    try {
        //The segment doesn't exist, we can abort
        final Node segmentNode = segments.get(segmentKey);
        if (segmentNode == null) {
            return;
        }

        if (topicPart.length == 1) {
            segmentNode.removeExactSubscriber(subscriber, sharedName);
        }

        if (topicPart.length == 2 && topicPart[1].equals("#")) {
            segmentNode.removeWildcardSubscriber(subscriber, sharedName);
        }

        iterateChildNodesForSubscriberRemoval(segmentNode, topicPart, nodes, 0);

        final Node lastFoundNode = getLastNode(nodes);
        if (lastFoundNode != null) {
            final String lastTopicPart = topicPart[topicPart.length - 1];
            if (lastTopicPart.equals("#")) {
                lastFoundNode.removeWildcardSubscriber(subscriber, sharedName);

            } else if (lastTopicPart.equals(lastFoundNode.getTopicPart())) {
                lastFoundNode.removeExactSubscriber(subscriber, sharedName);
            }
        }

        //Delete all nodes recursively if they are not needed anymore

        for (int i = nodes.length - 1; i > 0; i--) {
            final Node node = nodes[i];
            if (node != null) {

                if (isNodeDeletable(node)) {
                    Node parent = nodes[i - 1];
                    if (parent == null) {
                        parent = segmentNode;
                    }
                    final Node[] childrenOfParent = parent.getChildren();
                    if (childrenOfParent != null) {
                        for (int j = 0; j < childrenOfParent.length; j++) {
                            if (childrenOfParent[j] == node) {
                                childrenOfParent[j] = null;
                            }
                        }
                    } else if (parent.getChildrenMap() != null) {
                        final Node childOfParent = parent.getChildrenMap().get(node.getTopicPart());
                        if (childOfParent == node) {
                            parent.getChildrenMap().remove(childOfParent.getTopicPart());
                        }
                    }
                }
            }
        }
        //We can remove the segment if it's not needed anymore
        if (NodeUtils.getChildrenCount(segmentNode) == 0 &&
                NodeUtils.getExactSubscriberCount(segmentNode) == 0 &&
                NodeUtils.getWildcardSubscriberCount(segmentNode) == 0) {
            segments.remove(segmentNode.getTopicPart());
        }

    } finally {
        lock.unlock();
    }
}
 
Example 18
Project: cyberduck   File: IRODSSession.java    License: GNU General Public License v3.0 4 votes vote down vote up
protected String getRegion() {
    if(StringUtils.contains(host.getRegion(), ':')) {
        return StringUtils.splitPreserveAllTokens(host.getRegion(), ':')[0];
    }
    return host.getRegion();
}
 
Example 19
Project: cyberduck   File: IRODSSession.java    License: GNU General Public License v3.0 4 votes vote down vote up
protected String getResource() {
    if(StringUtils.contains(host.getRegion(), ':')) {
        return StringUtils.splitPreserveAllTokens(host.getRegion(), ':')[1];
    }
    return StringUtils.EMPTY;
}
 
Example 20
Project: swagger   File: AntPathMatcher.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Tokenize the given path String into parts, based on this matcher's settings.
 * @param path the path to tokenize
 * @return the tokenized path parts
 */
protected String[] tokenizePath(String path) {
    return StringUtils.splitPreserveAllTokens(path, this.pathSeparator);
}