public String getSubstitutedString ( String [ ] args , boolean stripAmpersand ) { String stringId = args [ 0 ] ; String rawString = getString ( stringId , stripAmpersand ) ; logger . finer ( String . format ( "String \"%s\" fetched as \"%s\"" , stringId , rawString ) ) ; StringBuffer buf = new StringBuffer ( ) ; int curArg = 0 ; int formattersCount = 0 ; boolean orderedMode ; // Matches %1, %2, ... String orderedFormatterRegexp = "%(\\d+)" ; Pattern pattern = Pattern . compile ( SUBSTITUTED_STRING_REGEXP ) ; Matcher matcher = pattern . matcher ( rawString ) ; // Check if there is at least one "standard" formatter if ( matcher . find ( ) ) { // There is, assume there are no ordered formatters in the string logger . finer ( "Parsing with standard formatters" ) ; // Reset the search since we'll also need to substitute the first occurrence later on. matcher . reset ( ) ; orderedMode = false ; } else { // There is none, assume there are only ordered formatters in the string logger . finer ( "Parsing with ordered formatters" ) ; pattern = Pattern . compile ( orderedFormatterRegexp ) ; matcher = pattern . matcher ( rawString ) ; orderedMode = true ; } // For each formatter occurrence found in string... while ( matcher . find ( ) ) { formattersCount ++ ; String replaceStr = matcher . group ( ) ; String substitution = "" ; if ( replaceStr . equals ( "%%" ) ) { substitution = "%" ; } else { if ( orderedMode ) { // Choose the argument basing on the ordered argument number, i.e. %3 will fetch the third // element from args curArg = Integer . parseInt ( replaceStr . substring ( 1 ) ) ; } else { // Just go one by one curArg ++ ; } if ( curArg < args . length ) { // If args is long enough to contain the argument we want, substitute it substitution = args [ curArg ] ; if ( substitution . isEmpty ( ) ) { substitution = WatirUtils . ANY_MATCHER ; } } else { // If args is too short, leave the formatter intact substitution = replaceStr ; } } matcher . appendReplacement ( buf , substitution ) ; } matcher . appendTail ( buf ) ; if ( formattersCount != ( args . length - 1 ) ) { logger . warning ( String . format ( "Argument count incorrect for %s, got %d, expected %d" , stringId , ( args . length - 1 ) , formattersCount ) ) ; } String result = buf . toString ( ) ; logger . finer ( "Final string: '" + result + "'" ) ; return result ; }
Fetches a translated string fetched basing on a string id and substitutes the printf formatters in it . Each substitution argument needs to be a string . The printf formatter types are ignored and the given string argument is substituted .