private static final String [ ] decodeArray ( String encoded ) { char [ ] chars = encoded . trim ( ) . toCharArray ( ) ; //--Parse the String //(state) char quoteCloseChar = ( char ) 0 ; List < StringBuilder > terms = new LinkedList < StringBuilder > ( ) ; StringBuilder current = new StringBuilder ( ) ; //(start/stop overhead) int start = 0 ; int end = chars . length ; if ( chars [ 0 ] == ' ' ) { start += 1 ; end -= 1 ; if ( chars [ end ] != ' ' ) throw new IllegalArgumentException ( "Unclosed paren in encoded array: " + encoded ) ; } if ( chars [ 0 ] == ' ' ) { start += 1 ; end -= 1 ; if ( chars [ end ] != ' ' ) throw new IllegalArgumentException ( "Unclosed bracket in encoded array: " + encoded ) ; } //(finite state automata) for ( int i = start ; i < end ; i ++ ) { if ( chars [ i ] == ' ' ) { //(case: escaped character) if ( i == chars . length - 1 ) throw new IllegalArgumentException ( "Last character of encoded pair is escape character: " + encoded ) ; current . append ( chars [ i + 1 ] ) ; i += 1 ; } else if ( quoteCloseChar != 0 ) { //(case: in quotes) if ( chars [ i ] == quoteCloseChar ) { quoteCloseChar = ( char ) 0 ; } else { current . append ( chars [ i ] ) ; } } else { //(case: normal) if ( chars [ i ] == ' ' ) { quoteCloseChar = ' ' ; } else if ( chars [ i ] == ' ' ) { quoteCloseChar = ' ' ; } else if ( chars [ i ] == ' ' ) { //break terms . add ( current ) ; current = new StringBuilder ( ) ; } else { current . append ( chars [ i ] ) ; } } } //--Return if ( current . length ( ) > 0 ) terms . add ( current ) ; String [ ] rtn = new String [ terms . size ( ) ] ; int i = 0 ; for ( StringBuilder b : terms ) { rtn [ i ] = b . toString ( ) . trim ( ) ; i += 1 ; } return rtn ; }
Decode an array encoded as a String . This entails a comma separated value enclosed in brackets or parentheses