下面列出了org.apache.commons.lang3.StringUtils#splitPreserveAllTokens ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
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);
}
}
@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);
}
/**
* 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();
}
/**
* 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;
}
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]);
}
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;
}
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];
}
@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);
}
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);
}
}
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]);
}
/**
* 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];
}
}
/**
* 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);
}
@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);
}
}
/**
* 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;
}
/**
* 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;
}
/**
* {@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();
}
}
protected String getRegion() {
if(StringUtils.contains(host.getRegion(), ':')) {
return StringUtils.splitPreserveAllTokens(host.getRegion(), ':')[0];
}
return host.getRegion();
}
protected String getResource() {
if(StringUtils.contains(host.getRegion(), ':')) {
return StringUtils.splitPreserveAllTokens(host.getRegion(), ':')[1];
}
return StringUtils.EMPTY;
}
/**
* 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);
}