Loading resource from classpath

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Loading resource from classpath

frederick.verbist

Hi,

I would like to read a file from the classpath.
I add the the directory to the classpath but get a FileNotFoundException when trying to open it :-(
I tryed with both beanshell 1.3 and 2.0

here is my code :

bsh % print(getClassPath());
java.net.URL[]: {
file:/C:/java/beanshell-1.3/bsh-1.3.0.jar,
file:/C:/java/beanshell-1.3/,
file:/C:/java/beanshell/bsh-2.0b4.jar,
file:/C:/Program Files/Java/jdk1.5.0_04/lib/tools.jar,
file:/C:/Program Files/Java/jre1.5.0_04/lib/rt.jar,
file:/C:/Documents and Settings/xxx/src/conf/,
}
bsh % dir ("C:/Documents and Settings/xxx/src/conf");
rw_ Aug 16     3221 applicationContext.xml
rw_ Aug 16      387 jdbc.properties
rw_ Aug 12      376 log4j.properties
rw_ Aug 16      590 persistence.hbm.xml
bsh % b = new FileInputStream("applicationContext.xml");
// Error: // Uncaught Exception: Object constructor : at Line: 15 : in file: <unknown file> : new FileInputStream ( "applicationContext.xml" )

Target exception: java.io.FileNotFoundException: applicationContext.xml (The system cannot find the file specified)

java.io.FileNotFoundException: applicationContext.xml (The system cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at bsh.Reflect.constructObject(Unknown Source)
        at bsh.BSHAllocationExpression.constructObject(Unknown Source)
        at bsh.BSHAllocationExpression.objectAllocation(Unknown Source)
        at bsh.BSHAllocationExpression.eval(Unknown Source)
        at bsh.BSHPrimaryExpression.eval(Unknown Source)
        at bsh.BSHPrimaryExpression.eval(Unknown Source)
        at bsh.BSHAssignment.eval(Unknown Source)
        at bsh.Interpreter.run(Unknown Source)
        at bsh.Interpreter.main(Unknown Source)
bsh %

what am I doing wrong?

thanks,

Fred
Reply | Threaded
Open this post in threaded view
|

RE: Loading resource from classpath

Daniel Leuck

Hi Fredrick,

 

FileInputStream works with file paths, which have no relationship to your classpath.  Relative file paths are resolved against the user’s current directory.  You want:

 

url = getResource("/applicationContext.xml");

inputStream = url.openStream();

 

This will open a file called applicationContext.xml occurring directly under any directory in your classpath.

 

Cheers,

Dan

 


From: [hidden email] [mailto:[hidden email]] On Behalf Of [hidden email]
Sent: Wednesday, August 17, 2005 6:38 AM
To: [hidden email]
Subject: [Beanshell-users] Loading resource from classpath

 


Hi,

I would like to read a file from the classpath.
I add the the directory to the classpath but get a FileNotFoundException when trying to open it :-(
I tryed with both beanshell 1.3 and 2.0

here is my code :

bsh % print(getClassPath());
java.net.URL[]: {
file:/C:/java/beanshell-1.3/bsh-1.3.0.jar,
file:/C:/java/beanshell-1.3/,
file:/C:/java/beanshell/bsh-2.0b4.jar,
file:/C:/Program Files/Java/jdk1.5.0_04/lib/tools.jar,
file:/C:/Program Files/Java/jre1.5.0_04/lib/rt.jar,
file:/C:/Documents and Settings/xxx/src/conf/,
}
bsh % dir ("C:/Documents and Settings/xxx/src/conf");
rw_ Aug 16     3221 applicationContext.xml
rw_ Aug 16      387 jdbc.properties
rw_ Aug 12      376 log4j.properties
rw_ Aug 16      590 persistence.hbm.xml
bsh % b = new FileInputStream("applicationContext.xml");
// Error: // Uncaught Exception: Object constructor : at Line: 15 : in file: <unknown file> : new FileInputStream ( "applicationContext.xml" )

Target exception: java.io.FileNotFoundException: applicationContext.xml (The system cannot find the file specified)

java.io.FileNotFoundException: applicationContext.xml (The system cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at bsh.Reflect.constructObject(Unknown Source)
        at bsh.BSHAllocationExpression.constructObject(Unknown Source)
        at bsh.BSHAllocationExpression.objectAllocation(Unknown Source)
        at bsh.BSHAllocationExpression.eval(Unknown Source)
        at bsh.BSHPrimaryExpression.eval(Unknown Source)
        at bsh.BSHPrimaryExpression.eval(Unknown Source)
        at bsh.BSHAssignment.eval(Unknown Source)
        at bsh.Interpreter.run(Unknown Source)
        at bsh.Interpreter.main(Unknown Source)
bsh %

what am I doing wrong?

thanks,

Fred

Reply | Threaded
Open this post in threaded view
|

RE: Loading resource from classpath

frederick.verbist
In reply to this post by frederick.verbist

Thank you for the quick answer, it made my test work :-)
actualy I am trying to start an application of mine that uses the springframework and it cannot find his applicationcontext file
normaly it just does
new ClassPathXmlApplicationContext("applicationContext.xml");
in he constructor.

But when instanciating the class from beanshell, I get a FileNotFoundException...

Any ideas ?

thanks,

Fred
Reply | Threaded
Open this post in threaded view
|

RE: Loading resource from classpath

Daniel Leuck

Hi Fredrick,

 

Try

new ClassPathXmlApplicationContext("/applicationContext.xml");

Dan

 


From: [hidden email] [mailto:[hidden email]]
Sent: Wednesday, August 17, 2005 8:57 PM
To: Daniel Leuck
Cc: [hidden email]
Subject: RE: [Beanshell-users] Loading resource from classpath

 


Thank you for the quick answer, it made my test work :-)
actualy I am trying to start an application of mine that uses the springframework and it cannot find his applicationcontext file
normaly it just does
new ClassPathXmlApplicationContext("applicationContext.xml");
in he constructor.

But when instanciating the class from beanshell, I get a FileNotFoundException...

Any ideas ?

thanks,

Fred

Reply | Threaded
Open this post in threaded view
|

RE: Loading resource from classpath

frederick.verbist
In reply to this post by frederick.verbist

Yes I tryed that also ...
without success :-(

thanks anyway
Reply | Threaded
Open this post in threaded view
|

RE: Loading resource from classpath

Daniel Leuck

Hi Frederick,

 

I don’t know what the context of your application is, but if the classpath context isn’t working for you and you know where the file is in the filesystem you can use FileSystemXmlApplicationContext.  If it’s a web application you can use XmlWebApplicationContext and stick the file in WEB-INF/

 

Good luck with your app.

 

Dan

 


From: [hidden email] [mailto:[hidden email]] On Behalf Of [hidden email]
Sent: Thursday, August 18, 2005 1:52 AM
To: [hidden email]
Subject: RE: [Beanshell-users] Loading resource from classpath

 


Yes I tryed that also ...
without success :-(

thanks anyway



Thank you for the quick answer, it made my test work :-)
actualy I am trying to start an application of mine that uses the springframework and it cannot find his applicationcontext file
normaly it just does
new ClassPathXmlApplicationContext("applicationContext.xml");
in he constructor.

But when instanciating the class from beanshell, I get a FileNotFoundException...

Any ideas ?

thanks,

Fred
Reply | Threaded
Open this post in threaded view
|

Newbie Question

Sanjeev Saha-2

Hello:

 

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

 

Thanks,

Sanjeev

Reply | Threaded
Open this post in threaded view
|

Re: Newbie Question

Daniel Wunsch

> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

RE: Newbie Question

Furash Gary
In reply to this post by Sanjeev Saha-2
I was going to suggest that you should tell the script that it's good
and worthwhile, but someone appears to have provided the actual,
non-joke answer ;-)

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Daniel
Wunsch
Sent: Friday, August 19, 2005 10:39 AM
To: [hidden email]
Subject: Re: [Beanshell-users] Newbie Question



> 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 [hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Newbie Question

MICHAEL PITONIAK-2
In reply to this post by Daniel Wunsch
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 ([hidden email]).
*
* 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 <[hidden email]>
>To: [hidden email]
>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
>[hidden email]
>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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

RE: Newbie Question

Daniel Leuck
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: [hidden email] [mailto:beanshell-users-
> [hidden email]] On Behalf Of MICHAEL PITONIAK
> Sent: Saturday, August 20, 2005 12:58 AM
> To: [hidden email]; [hidden email]
> Cc: [hidden email]
> 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 ([hidden email]).
> *
> * 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 <[hidden email]>
> >To: [hidden email]
> >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
> >[hidden email]
> >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
> [hidden email]
> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

RE: Newbie Question

Sanjeev Saha-2

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: [hidden email] [mailto:[hidden email]] On Behalf Of Daniel Leuck
Sent: Saturday, August 20, 2005 8:40 AM
To: 'MICHAEL PITONIAK'; [hidden email]; [hidden email]
Cc: [hidden email]
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: [hidden email] [mailto:beanshell-users-

> [hidden email]] On Behalf Of MICHAEL PITONIAK

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

> To: [hidden email]; [hidden email]

> Cc: [hidden email]

> 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 ([hidden email]).

> *

> * 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 <[hidden email]>

> >To: [hidden email]

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

> >[hidden email]

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

> [hidden email]

> 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

[hidden email]

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