here is my compiler class:

any comments on solving the problem better are welcome.



 * Copyright (C) 2002 by Michael Pitoniak (
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to:
 *   The Free Software Foundation, Inc.,
 *   59 Temple Place - Suite 330
 *   Boston, MA 02111-1307
 *   USA

package harness.parsers.beanShell;

import harness.common.CommonConstants;
import harness.utils.classServices.ClassServices;


import bsh.ParseException;
import bsh.Parser;

 * @author  Michael_Pitoniak
public class BeanShellScriptCompiler extends Thread implements CommonConstants{
    private String m_Script = null;
    private FileReader m_FileReader = null;
    private Parser m_BshParser = null;
    private StringBuffer m_ResponseBuffer = null;
    private StringBuffer m_ErrorBuffer = null;
    private boolean m_bSuccess = false;
    /** Creates a new instance of BeanShellScriptCompiler */
    public BeanShellScriptCompiler(String script) {
        m_Script = script;

    public void run(){
            m_bSuccess = compileScript(m_Script);
        }catch(Exception e){
            //we need to catch Exception here because occasionally
            //parseException in compileScript() has incomplete data,
            //and throws null ptr Exception when dereferenced.
            m_ErrorBuffer.append(ClassServices.getCurrentMethodName() + e + NEWLINE_CHAR);
    /* compileScript had to be Threaded, and compile one script at a time
     *  because the bsh.Parser will crash when it encounters an  
     * "Un-Quoted" String. By Threading we circumvent this*/
    private boolean compileScript(String script){
        m_ResponseBuffer = new StringBuffer();
        m_ErrorBuffer = new StringBuffer();
        m_bSuccess = false;
            m_FileReader = new FileReader(script);
            m_BshParser = new Parser(m_FileReader);
            while(!m_BshParser.Line()/*eof*/ ){
                m_ResponseBuffer.append(m_BshParser.popNode() + NEWLINE_CHAR);
            m_bSuccess = true;
        }catch(Error error){
            m_ErrorBuffer.append(ClassServices.getCurrentMethodName() + error + NEWLINE_CHAR);
        }catch(ParseException parseException){
            m_ErrorBuffer.append(ClassServices.getCurrentMethodName() + "PARSE EXCEPTION: " + parseException.toString()+ NEWLINE_CHAR);
            m_ErrorBuffer.append(ClassServices.getCurrentMethodName() + "ERROR SOURCE FILE: " + parseException.getErrorSourceFile()+ NEWLINE_CHAR);
            m_ErrorBuffer.append(ClassServices.getCurrentMethodName() + "ERROR MESSAGE: " + parseException.getMessage()+ NEWLINE_CHAR);
            m_ErrorBuffer.append(ClassServices.getCurrentMethodName() + "ERROR LINE NUMBER: " + parseException.getErrorLineNumber()+ NEWLINE_CHAR);
            m_ErrorBuffer.append(ClassServices.getCurrentMethodName() + "ERROR TEXT: " + parseException.getErrorText()+ NEWLINE_CHAR);
            m_ErrorBuffer.append(ClassServices.getCurrentMethodName() + "SCRIPT STACK TRACE: " + parseException.getScriptStackTrace()+ NEWLINE_CHAR);
        }catch(Exception e){
            m_ErrorBuffer.append(ClassServices.getCurrentMethodName() + e + NEWLINE_CHAR);
                if(m_FileReader != null){
            }catch(Exception e){
            }catch(Error error){}
        return m_bSuccess;
    public String getResponseBuffer(){
        return m_ResponseBuffer.toString();
    public String getErrorBuffer(){
        return m_ErrorBuffer.toString();
    public boolean getSuccess(){
        return m_bSuccess;

David Spectar <>
Sent by:

03/21/2005 12:44 PM

[Beanshell-users] Validating Scripts from Source

According to the beanshell javadoc "You can use the
Parser from the command line to do basic structural
validation of BeanShell files without actually
executing them."

e.g. java bsh.Parser [ -p ] file [ file ] [ ... ]

Is there a way to do this from code?

The thing I am working on loads a script at
initialization but doesn't execute it because none of
the variables are ready yet. I wanted to be able to
validate the script to some extent during
initialization time.

Do you Yahoo!?
Make Yahoo! your home page

SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
Beanshell-users mailing list