2323 */
2424package jenkins .plugins .openstack .compute .internal ;
2525
26+ import org .apache .commons .lang .StringUtils ;
2627import org .kohsuke .accmod .Restricted ;
2728import org .kohsuke .accmod .restrictions .NoExternalUse ;
2829
2930import javax .annotation .Nonnull ;
3031import java .util .ArrayList ;
32+ import java .util .Collections ;
3133import java .util .List ;
3234
3335/**
3739 */
3840@ Restricted (NoExternalUse .class )
3941public 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<D1>bar<D2>baz<D1>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 ('\\' );
0 commit comments