Hello:

 

Thanks to Daniel Wunsch, the following simple steps did the trick for me. My intent was to parse a string(not read from a file).

 

Regards,

Sanjeev

 

    public String parse(final String script) {

        try {

            Parser p = new Parser(new StringReader(script));

            for (;;) {

                if (p.Line()) {

                    break;

                } else {

                    p.popNode();

                }

            }

        } catch (Exception ex) {

            return ex.getMessage();

        } catch (Error er) {

            return er.getMessage();

        }

        return "Syntax check successfully completed.";

    }

 

-----Original Message-----
From: beanshell-users-admin@lists.sourceforge.net [mailto:beanshell-users-admin@lists.sourceforge.net] On Behalf Of Daniel Leuck
Sent: Saturday, August 20, 2005 8:40 AM
To: 'MICHAEL PITONIAK'; the.gray@gmx.net; beanshell-users@lists.sourceforge.net
Cc: michael_pitoniak@symantec.com
Subject: RE: [Beanshell-users] Newbie Question

 

Hi Micheal,

 

It looks good, except I don't see any reason to squash errors as in

(catch(Error error){}) - just catch Throwable and print the stack trace.

 

Cheers,

Dan

 

> -----Original Message-----

> From: beanshell-users-admin@lists.sourceforge.net [mailto:beanshell-users-

> admin@lists.sourceforge.net] On Behalf Of MICHAEL PITONIAK

> Sent: Saturday, August 20, 2005 12:58 AM

> To: the.gray@gmx.net; beanshell-users@lists.sourceforge.net

> Cc: michael_pitoniak@symantec.com

> Subject: Re: [Beanshell-users] Newbie Question

>

> Dan,

>

> I threaded it. Please feel free to comment if my code could be better, as

> i

> am always looking to improve it.

>

> cheers,

>

> mp

>

> * Copyright (C) 2002 by Michael Pitoniak (pitoniakm@msn.com).

> *

> * 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

> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

> * 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 java.io.FileReader;

>

> 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(){

>         try{

>             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;

>

>         try{

>             m_FileReader = new FileReader(script);

>             m_BshParser = new Parser(m_FileReader);

>             m_BshParser.setRetainComments(true);

>             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);

>         }finally{

>             try{

>                 if(m_FileReader != null){

>                     m_FileReader.close();

>                 }

>             }catch(Exception e){

>                 e.printStackTrace();

>             }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;

>     }

>

> }

>

>

>

> >From: Daniel Wunsch <the.gray@gmx.net>

> >To: beanshell-users@lists.sourceforge.net

> >Subject: Re: [Beanshell-users] Newbie Question

> >Date: Fri, 19 Aug 2005 19:39:02 +0200

> >

> >

> > > How does one go about validating a bsh script without evaluating it?

> >

> >something like this..

> >daniel

> >

> >import bsh.*;

> >

> >String   text  = "print(\"\"hello, world!\");";

> >try {

> >   var   in          = new StringReader(text);

> >   var   parser      = new Parser(in);

> >   for (;;) {

> >         if (parser.Line())      break;

> >         // bsh.BSH* classes

> >         SimpleNode  node  = parser.popNode();

> >         //node.dump("");

> >   }

> >   in.close();

> >}

> >catch (Exception e) {

> >   System.err.println("parser error: " + e.Message);

> >}

> >

> >

> >-------------------------------------------------------

> >SF.Net email is Sponsored by the Better Software Conference & EXPO

> >September 19-22, 2005 * San Francisco, CA * Development Lifecycle

> Practices

> >Agile & Plan-Driven Development * Managing Projects & Teams * Testing &

> QA

> >Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf

> >_______________________________________________

> >Beanshell-users mailing list

> >Beanshell-users@lists.sourceforge.net

> >https://lists.sourceforge.net/lists/listinfo/beanshell-users

>

>

>

>

> -------------------------------------------------------

> SF.Net email is Sponsored by the Better Software Conference & EXPO

> September 19-22, 2005 * San Francisco, CA * Development Lifecycle

> Practices

> Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA

> Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf

> _______________________________________________

> Beanshell-users mailing list

> Beanshell-users@lists.sourceforge.net

> https://lists.sourceforge.net/lists/listinfo/beanshell-users

 

 

 

-------------------------------------------------------

SF.Net email is Sponsored by the Better Software Conference & EXPO

September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices

Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA

Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf

_______________________________________________

Beanshell-users mailing list

Beanshell-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/beanshell-users