All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mozilla.javascript.tools.idswitch.README Maven / Gradle / Ivy

Go to download

Rhino is an open-source implementation of JavaScript written entirely in Java. It is typically embedded into Java applications to provide scripting to end users.

The newest version!
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.

USING IDSWITCH GENERATOR TOOL

Usage:
java org.mozilla.javascript.tools.idswitch.Main 

The main purpose of this utility is to generate Java code to map strings to some ids that can be used, for example, in a switch statement.

The utility scans the input file for lines with the following structure:

// #string_id_map#
... 
// #generated#
...
// #/generated#
... 
// #/string_id_map#

Then every line in  is scanned for the pattern:
^[ \t]*Id_([0-9a-zA-Z_]+)[ \t]*=.*$

Each such patterns adds a mapping form string $1 to Id_\$ or if the line also contains the pattern //\s*#string=\s*([^#]+)\s*#, then it adds map of $1 in this pattern to Id_\$

After that lines in  are replaced by a code block that sets variable "id" to Id_ if variable "s" equals  (or value defined by //#string=...# construction in the line with Id_) or 0 otherwise.
If the new code for  is identical to old one, the file is not touched otherwise  is overwritten by the new code and a time stamp is appended after #generated#.

For example, if file x.java contains:

// #string_id_map#

    private int getId(String s) {
        int id;
// #generated# Initial version
// #/generated#
        return id;
    }

    private static final int
        Id_x       = 1,
        Id_y       = 2,
        Id_hello   = 3, // #string = Hello, World! #
        Id_symbols = 4, // #string=<<*Symbols*>>#
        Id_nice    = 5,
        Id_for     = 6,
        Id_bar     = 7;

// #/string_id_map#

....

    private double getFieldValue(String s) {

// #string_id_map#
    final int
        Id_field1 = 1,
        Id_field2 = 2,
        Id_field3 = 3,
        Id_one_more_field = 4; // #string = ONE%MORE%FIELD#

        int id;
// #generated# Initial version
// #/generated#
// #/string_id_map#
        switch (id) {
            case Id_field1: return field1;
            case Id_field2: return field2;
            case Id_field3: return field3;
            case Id_one_more_field: return one_more_field;
        }
        throw new RuntimeException("No such field");
    }


then invocation

java org.mozilla.javascript.tools.idswitch.Main x.java

would replace that by a code fragment similar to:

// #string_id_map#

    private int getId(String s) {
        int id;
// #generated# Last update: 2001-05-25 18:00:24 GMT+02:00
        L0: { id = 0; String X = null; int c;
            L: switch (s.length()) {
            case 1: c=s.charAt(0);
                if (c=='x') { id=Id_x; break L0; }
                else if (c=='y') { id=Id_y; break L0; }
                break L;
            case 3: c=s.charAt(0);
                if (c=='b') { if (s.charAt(2)=='r' && s.charAt(1)=='a') {id=Id_bar; break L0;} }
                else if (c=='f') { if (s.charAt(2)=='r' && s.charAt(1)=='o') {id=Id_for; break L0;} }
                break L;
            case 4: X="nice";id=Id_nice; break L;
            case 13: c=s.charAt(0);
                if (c=='<') { X="<<*Symbols*>>";id=Id_symbols; }
                else if (c=='H') { X="Hello, World!";id=Id_hello; }
                break L;
            }
            if (X!=null && X!=s && !X.equals(s)) id = 0;
        }
// #/generated#
        return id;
    }

    private static final int
        Id_x       = 1,
        Id_y       = 2,
        Id_hello   = 3, // #string = Hello, World! #
        Id_symbols = 4, // #string=<<*Symbols*>>#
        Id_nice    = 5,
        Id_for     = 6,
        Id_bar     = 7;

// #/string_id_map#

....

    private double getFieldValue(String s) {

// #string_id_map#
    final int
        Id_field1 = 1,
        Id_field2 = 2,
        Id_field3 = 3,
        Id_one_more_field = 4; // #string = ONE%MORE%FIELD#

        int id;
// #generated# Last update: 2001-05-25 16:48:50 GMT+02:00
        L0: { id = 0; String X = null; int c;
            int s_length = s.length();
            if (s_length==6) {
                c=s.charAt(5);
                if (c=='1') { X="field1";id=Id_field1; }
                else if (c=='2') { X="field2";id=Id_field2; }
                else if (c=='3') { X="field3";id=Id_field3; }
            }
            else if (s_length==14) { X="ONE%MORE%FIELD";id=Id_one_more_field; }
            if (X!=null && X!=s && !X.equals(s)) id = 0;
        }
// #/generated#
// #/string_id_map#
        switch (id) {
            case Id_field1: return field1;
            case Id_field2: return field2;
            case Id_field3: return field3;
            case Id_one_more_field: return one_more_field;
        }
        throw new RuntimeException("No such field");
    }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy