GroovyScriptEngine return messy code at Windows

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

GroovyScriptEngine return messy code at Windows

xxli79
In Windows (XP or 2003, I did't test Win7) with locale as China, when run a script in a new GSE instance, it will return the correct result, but when re-run the script with the old GSE instance, the result will be messy code.
But it will be all right in Linux (mine is Ubuntu 11.04).

Below is my test code, they can run in GroovyConsole directly, and my groovy version is 1.8.6.
The test step is:
1. run code1: that will create a new GSE instance;
2. run code2: the result will be "你好", it is correct;
3. run code2 again: the result will be "浣犲ソ", it's messy.

code 1

class GseHolder {
   static GroovyScriptEngine gse = new GroovyScriptEngine("D:\\test", Thread.currentThread().contextClassLoader);
}
return;

code 2

String script = "return '你好';"
String filename = "test1.groovy";
def gse = GseHolder.gse;
//gse.config.setSourceEncoding("UTF-8");
String fullname = "D:\\test\\" + filename;
new File(fullname).write(script, "UTF-8");
gse.run(filename, new Binding())
Reply | Threaded
Open this post in threaded view
|

Re: GroovyScriptEngine return messy code at Windows

Jochen Theodorou
Am 13.06.2012 09:25, schrieb xxli79:

> In Windows (XP or 2003, I did't test Win7) with locale as China, when
> run a script in a new GSE instance, it will return the correct result,
> but when re-run the script with the old GSE instance, the result will be
> messy code.
> But it will be all right in Linux (mine is Ubuntu 11.04).
>
> Below is my test code, they can run in GroovyConsole directly, and my
> groovy version is 1.8.6.
> The test step is:
> 1. run code1: that will create a new GSE instance;
> 2. run code2: the result will be *"你好"*, it is correct;
> 3. run code2 again: the result will be *"浣犲ソ"*, it's messy.

since you use 你好 directly in the code... did you try the variant with
\u to exclude encoding issues? Another point... how do you run code1 and
code2? Also why is the UTF-8 setting for the encoding commented out?

bye blackdrag

--
Jochen "blackdrag" Theodorou - Groovy Project Tech Lead
blog: http://blackdragsview.blogspot.com/
german groovy discussion newsgroup: de.comp.lang.misc
For Groovy programming sources visit http://groovy-lang.org


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: GroovyScriptEngine return messy code at Windows

xxli79
Great, encode the script to unicode before wite to file make it works!
Now the file contents look like "....\uXXXX\uXXXX...".
Thanks very much.

BTW: commented out UTF-8 setting is because it is useless for this problem.

Below is my encode code, hope this will help others:)
    private static String toUnicode(String theString) {
        StringBuilder sb = new StringBuilder(theString.length() * 2);
        for ( int x=0; x<theString.length(); x++ ) {
            char aChar = theString.charAt(x);
            if ( aChar > 0x00FF ) {
                sb.append('\\').append('u');
                sb.append(toHex((aChar >> 12) & 0xF));
                sb.append(toHex((aChar >>  8) & 0xF));
                sb.append(toHex((aChar >>  4) & 0xF));
                sb.append(toHex( aChar        & 0xF));
            } else {
                sb.append(aChar);
            }
        }
        return sb.toString();
    }
    private static final char[] hexDigit = [
        '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
    ] as char[];
    private static char toHex(int nibble) {
        return hexDigit[(nibble & 0xF)];
    }