protected void chunkString ( final List < CharacterEvent > characterEvents , final CharSequence buffer , int patternIndex ) { // Iterate over the chunking patterns for ( ; patternIndex < this . chunkingPatterns . length ; patternIndex ++ ) { final Pattern pattern = this . chunkingPatterns [ patternIndex ] ; final Matcher matcher = pattern . matcher ( buffer ) ; if ( matcher . find ( ) ) { final CharacterEventSource eventSource = this . chunkingPatternEventSources . get ( pattern ) ; int prevMatchEnd = 0 ; do { // Add all of the text up to the match as a new chunk, use subSequence to avoid // extra string alloc this . chunkString ( characterEvents , buffer . subSequence ( prevMatchEnd , matcher . start ( ) ) , patternIndex + 1 ) ; // Get the generated CharacterEvents for the match final MatchResult matchResult = matcher . toMatchResult ( ) ; eventSource . generateCharacterEvents ( this . request , matchResult , characterEvents ) ; prevMatchEnd = matcher . end ( ) ; } while ( matcher . find ( ) ) ; // Add any remaining text from the original CharacterDataEvent if ( prevMatchEnd < buffer . length ( ) ) { this . chunkString ( characterEvents , buffer . subSequence ( prevMatchEnd , buffer . length ( ) ) , patternIndex + 1 ) ; } return ; } } // Buffer didn't match anything, just append the string data // de-duplication of the event string data final String eventString = buffer . toString ( ) ; characterEvents . add ( CharacterDataEventImpl . create ( eventString ) ) ; }
Breaks up the String into a List of CharacterEvents based on the configured Map of Patterns to CharacterEventSources