public String filter ( String input ) { StringBuilder output = new StringBuilder ( ) ; Matcher tokenMatcher = tokenPattern . matcher ( input ) ; // Track last regex match int endOfLastMatch = 0 ; // For each possible token while ( tokenMatcher . find ( ) ) { // Pull possible leading text and first char before possible token String literal = tokenMatcher . group ( LEADING_TEXT_GROUP ) ; String escape = tokenMatcher . group ( ESCAPE_CHAR_GROUP ) ; // Append leading non-token text output . append ( literal ) ; // If char before token is '$', the token itself is escaped if ( "$" . equals ( escape ) ) { String notToken = tokenMatcher . group ( TOKEN_GROUP ) ; output . append ( notToken ) ; } // If char is not '$', interpret as a token else { // The char before the token, if any, is a literal output . append ( escape ) ; // Pull token value String tokenName = tokenMatcher . group ( TOKEN_NAME_GROUP ) ; String tokenValue = getToken ( tokenName ) ; // If token is unknown, interpret as literal if ( tokenValue == null ) { String notToken = tokenMatcher . group ( TOKEN_GROUP ) ; output . append ( notToken ) ; } // Otherwise, substitute value else output . append ( tokenValue ) ; } // Update last regex match endOfLastMatch = tokenMatcher . end ( ) ; } // Append any remaining non-token text output . append ( input . substring ( endOfLastMatch ) ) ; return output . toString ( ) ; }
Filters the given string replacing any tokens with their corresponding values .