From:  Kenneth <teranews@mail1.ttak.org>
Date:  25 Aug 2006 00:01:42 Hong Kong Time
Newsgroup:  news.mozilla.org/netscape.public.mozilla.jseng
Subject:  

Java String and switch/case statements

NNTP-Posting-Host:  null

I am having troubles getting the switch/case statements to work with
Java Strings.

To execute my script I have the following code:

public void runScript(String script) {
	Controller controller = new Controller();
	RhinoScriptEngineFactory factory = new RhinoScriptEngineFactory();
	ScriptEngine jsEngine = factory.getScriptEngine();
	jsEngine.eval(script);
	Invocable jsInvoke = (Invocable) jsEngine;
	jsInvoke.call("run", new Object[]{controller});
}

If I try the following script, I get "it worked" in the debug logs:

function run(c) {
	var action = "begin";
	switch(action) {
		case "begin":
			c.debug("it worked");
			break;
		default:
			c.debug("it failed");
			break;
	}
}

But, if I try the following script, I get "it failed" in the debug logs:

function run(c) {
	var action = c.getAction();  // returns Java String "begin"
	...
}

I did find a reference that if I convert the Java String to a JavaScript
String, that it should work.  I tried the following, which did work:

function run(c) {
	var action = String( c.getAction() );  // returns Java String "begin"
	...
}

So my question is, I would prefer to not have my users have to know this
quirk.  Is there a way to either have my Java method return a JavaScript
string, or is there possibly a patch for Rhino that addresses this issue?

Thanks in advance,
Kenneth



-- 
Posted via a free Usenet account from http://www.teranews.com