Re: Non-ASCII characters in script

Daniel Wunsch <[email protected]> Thu, 21 Apr 2005 23:21:02 +0200
Newsgroups gmane.comp.java.beanshell.devel
Message-ID <[email protected]>
On Thursday 21 April 2005 22:35, Mario Ajzerle wrote:
> I have a simple script (that adds two variables)
>
> 	String x;
> 	String é;
> 	String result;
> 	result = é + x;
>
> that uses a non-ASCII character as a variable name. BSH parser does not
> parse correctly this variable.

beanshell always uses the platform default encoding. on linux, you 
could start the java-vm prefixed with something like 
LANG=en_US@UTF-8 , or you could try starting java with 
java -Dfile.encoding=UTF-8 which may work, too.

> In the code 
>
> 	java.io.FileReader reader = new java.io.FileReader(myFileName);
> 	Parser parser = new Parser(reader);
> 	while (!parser.Line())
> 	{
> 		SimpleNode node = parser.popNode();
> 	...
>
> I get 1, 3 and 4. row corectly parsed and for the 2. row a get
> BSHAmbiguousName. Is there a setting I can use to tell BeanShell Parser
> that I want to work with UTF characters.

the FileReader uses the system default encoding for files.
new InputStreamReader(new FileInputStream(file), "UTF-8");
instead should work.

sadly, the source(...) command and the methods in bsh.Interpreter
do not support an argument specifying the file encoding. i use 
something like this instead (careful, this doesn't close the
Reader in case of an Exception):

/** a replacement for source(URL url) with selectable charSet and closing the 
Reader afterwards */
var source(URL url, String charSet) {
    var in = new InputStreamReader(url.openStream(), charSet);
    var out = this.interpreter.eval(in, this.caller.namespace, 
                  "URL: "+url.toString());
    in.close(); return out; 
}

daniel


-------------------------------------------------------
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.
http://ads.osdn.com/?ad_ide95&alloc_id396&op=click