Skip to content

Commit a98f310

Browse files
committed
Strip whitespace from network spec
1 parent 50349cd commit a98f310

2 files changed

Lines changed: 36 additions & 22 deletions

File tree

plugin/src/main/java/jenkins/plugins/openstack/compute/internal/TokenGroup.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
*/
2424
package jenkins.plugins.openstack.compute.internal;
2525

26+
import org.apache.commons.lang.StringUtils;
2627
import org.kohsuke.accmod.Restricted;
2728
import org.kohsuke.accmod.restrictions.NoExternalUse;
2829

2930
import javax.annotation.Nonnull;
3031
import java.util.ArrayList;
32+
import java.util.Collections;
3133
import java.util.List;
3234

3335
/**
@@ -37,18 +39,19 @@
3739
*/
3840
@Restricted(NoExternalUse.class)
3941
public class TokenGroup {
42+
43+
public static final String STRIP_ALL_WHITESPACE = null;
44+
4045
/**
4146
* Get list of tokens separated by delim.
4247
*/
43-
public static @Nonnull List<String> from(@Nonnull final String csv, char delim) {
44-
List<String> strings = breakInput(csv, new char[] { delim });
45-
46-
if (strings.size() == 1) return strings;
48+
public static @Nonnull List<String> from(@Nonnull final String input, char delim) {
49+
List<String> strings = breakInput(input, new char[] { delim });
4750

4851
ArrayList<String> ret = new ArrayList<>(strings.size() / 2 + 1);
49-
// Remove delimiters
52+
// Remove delimiters - odd members
5053
for (int i = 0; i < strings.size(); i+=2) {
51-
ret.add(strings.get(i));
54+
ret.add(clean(strings.get(i)));
5255
}
5356

5457
return ret;
@@ -60,19 +63,19 @@ public class TokenGroup {
6063
* The inner lists are never empty and contain tokens delimited by delim2. The outer list contains list on the inner
6164
* lists that ware separated by delim1. IOW, it turns "foo&lt;D1&gt;bar&lt;D2&gt;baz&lt;D1&gt;bax" into "((foo),(bar,baz),(bax))"
6265
*/
63-
public static @Nonnull List<List<String>> from(@Nonnull final String csv, char delim1, char delim2) {
66+
public static @Nonnull List<List<String>> from(@Nonnull final String input, char delim1, char delim2) {
6467
ArrayList<List<String>> ret = new ArrayList<>();
6568

66-
List<String> input = breakInput(csv, new char[] { delim1, delim2 });
69+
List<String> strings = breakInput(input, new char[] { delim1, delim2 });
6770

6871
ArrayList<String> chunks = new ArrayList<>();
69-
for (int i = 0; i < input.size(); i+=2) {
70-
String token = input.get(i);
71-
chunks.add(token);
72+
for (int i = 0; i < strings.size(); i+=2) {
73+
String token = strings.get(i);
74+
chunks.add(clean(token));
7275

73-
if (i + 1 == input.size()) break; // last element followed by no delimiter
76+
if (i + 1 == strings.size()) break; // last element followed by no delimiter
7477

75-
char delimiter = input.get(i + 1).charAt(0);
78+
char delimiter = strings.get(i + 1).charAt(0);
7679

7780
if (delimiter == delim1) {
7881
ret.add(chunks);
@@ -84,7 +87,14 @@ public class TokenGroup {
8487
return ret;
8588
}
8689

87-
private static @Nonnull List<String> breakInput(String csv, char[] delims) {
90+
/**
91+
* Remove unwanted characters, line breaks, newlines, etc.
92+
*/
93+
private static String clean(String in) {
94+
return StringUtils.strip(in, STRIP_ALL_WHITESPACE);
95+
}
96+
97+
private static @Nonnull List<String> breakInput(String input, char[] delims) {
8898
List<Integer> delimiters = new ArrayList<>();
8999
for (char delim : delims) {
90100
delimiters.add((int) delim);
@@ -95,7 +105,7 @@ public class TokenGroup {
95105
StringBuilder token = new StringBuilder();
96106
boolean inQuotes = false;
97107
boolean escaped = false;
98-
for (int c : csv.codePoints().toArray()) {
108+
for (int c : input.codePoints().toArray()) {
99109
if (c == '\\') {
100110
if (escaped) {
101111
token.append('\\');

plugin/src/test/java/jenkins/plugins/openstack/compute/internal/TokenGroupTest.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ public void securityGroups() {
4242
assertEquals(l("foo bar", "baz"), TokenGroup.from("foo bar,baz", ','));
4343
assertEquals(l("a,b|c\"\\"), TokenGroup.from("\"a,b|c\\\"\\\\\"", ','));
4444

45-
// Space not trimmed
46-
assertEquals(l("foo", " bar"), TokenGroup.from("\"foo\", bar", ','));
45+
// Whitespace trimmed
46+
assertEquals(l("foo"), TokenGroup.from(" foo\n", ','));
47+
assertEquals(l("foo", "bar"), TokenGroup.from(" \"foo\" , bar ", ','));
48+
assertEquals(l("foo", "bar"), TokenGroup.from("\n\"foo\"\n,\nbar\n", ','));
4749
// Single quotes are not quotes
4850
assertEquals(l("'foo'"), TokenGroup.from("'foo'", ','));
4951
assertEquals(l("foo\\bar"), TokenGroup.from("foo\\\\bar", ','));
@@ -56,8 +58,10 @@ public void networks() {
5658
assertEquals(l(l("foo bar"), l("baz")), TokenGroup.from("foo bar,baz", ',', '|'));
5759
assertEquals(l(l("a,b|c\"\\")), TokenGroup.from("\"a,b|c\\\"\\\\\"", ',', '|'));
5860

59-
// Space not trimmed
60-
assertEquals(l(l("foo"), l(" bar")), TokenGroup.from("\"foo\", bar", ',', '|'));
61+
// Whitespace trimmed
62+
assertEquals(l(l("foo")), TokenGroup.from("\tfoo\n", ',', '|'));
63+
assertEquals(l(l("foo", "bar")), TokenGroup.from(" \"foo\" | bar ", ',', '|'));
64+
assertEquals(l(l("foo"), l("bar")), TokenGroup.from("\n\"foo\"\n,\nbar\n", ',', '|'));
6165
// Single quotes are not quotes
6266
assertEquals(l(l("'foo'")), TokenGroup.from("'foo'", ',', '|'));
6367
assertEquals(l(l("foo\\bar")), TokenGroup.from("foo\\\\bar", ',', '|'));
@@ -67,8 +71,8 @@ public void networks() {
6771
assertEquals(l(l("foo bar", "baz")), TokenGroup.from("foo bar|baz", ',', '|'));
6872
}
6973

70-
private static <A, R extends List<A>> R l(A... args) {
71-
List<A> ret = new ArrayList<>(Arrays.asList(args));
72-
return (R) ret;
74+
@SafeVarargs
75+
private static <A> List<A> l(A... args) {
76+
return new ArrayList<>(Arrays.asList(args));
7377
}
7478
}

0 commit comments

Comments
 (0)