protected String parse ( Reader reader ) throws IOException { if ( ! reader . markSupported ( ) ) { reader = new BufferedReader ( reader ) ; } StringWriter sw = new StringWriter ( ) ; startScript ( sw ) ; int c ; while ( ( c = reader . read ( ) ) != - 1 ) { if ( c == ' ' ) { reader . mark ( 1 ) ; c = reader . read ( ) ; if ( c != ' ' ) { sw . write ( ' ' ) ; reader . reset ( ) ; } else { reader . mark ( 1 ) ; c = reader . read ( ) ; if ( c == ' ' ) { groovyExpression ( reader , sw ) ; } else { reader . reset ( ) ; groovySection ( reader , sw ) ; } } continue ; // at least '<' is consumed ... read next chars. } if ( c == ' ' ) { reader . mark ( 1 ) ; c = reader . read ( ) ; if ( c != ' ' ) { sw . write ( ' ' ) ; reader . reset ( ) ; } else { reader . mark ( 1 ) ; sw . write ( "${" ) ; processGSstring ( reader , sw ) ; } continue ; // at least '$' is consumed ... read next chars. } if ( c == ' ' ) { sw . write ( ' ' ) ; } /* * Handle raw new line characters. */ if ( c == ' ' || c == ' ' ) { if ( c == ' ' ) { // on Windows, "\r\n" is a new line. reader . mark ( 1 ) ; c = reader . read ( ) ; if ( c != ' ' ) { reader . reset ( ) ; } } sw . write ( "\n" ) ; continue ; } sw . write ( c ) ; } endScript ( sw ) ; return sw . toString ( ) ; }
Parse the text document looking for <% or <% = and then call out to the appropriate handler otherwise copy the text directly into the script while escaping quotes .