| 240 | } |
| 241 | |
| 242 | Object evalScript( |
| 243 | String script, StringBuffer scriptOutput, boolean captureOutErr, |
| 244 | HttpServletRequest request, HttpServletResponse response ) |
| 245 | throws EvalError |
| 246 | { |
| 247 | // Create a PrintStream to capture output |
| 248 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 249 | PrintStream pout = new PrintStream( baos ); |
| 250 | |
| 251 | // Create an interpreter instance with a null inputstream, |
| 252 | // the capture out/err stream, non-interactive |
| 253 | Interpreter bsh = new Interpreter( null, pout, pout, false ); |
| 254 | |
| 255 | // set up interpreter |
| 256 | bsh.set( "bsh.httpServletRequest", request ); |
| 257 | bsh.set( "bsh.httpServletResponse", response ); |
| 258 | |
| 259 | // Eval the text, gathering the return value or any error. |
| 260 | Object result = null; |
| 261 | String error = null; |
| 262 | PrintStream sout = System.out; |
| 263 | PrintStream serr = System.err; |
| 264 | if ( captureOutErr ) { |
| 265 | System.setOut( pout ); |
| 266 | System.setErr( pout ); |
| 267 | } |
| 268 | try { |
| 269 | // Eval the user text |
| 270 | result = bsh.eval( script ); |
| 271 | } finally { |
| 272 | if ( captureOutErr ) { |
| 273 | System.setOut( sout ); |
| 274 | System.setErr( serr ); |
| 275 | } |
| 276 | } |
| 277 | pout.flush(); |
| 278 | scriptOutput.append( baos.toString() ); |
| 279 | return result; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Convert special characters to entities for XML output |