 ***** BEGIN LICENSE BLOCK *****
 Version: MPL 1.1/GPL 2.0

 The contents of this file are subject to the Mozilla Public License Version
 1.1 (the "License"); you may not use this file except in compliance with
 the License. You may obtain a copy of the License at
 http://www.mozilla.org/MPL/

 Software distributed under the License is distributed on an "AS IS" basis,
 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 for the specific language governing rights and limitations under the
 License.

 The Original Code is Rhino code, released May 6, 1999.

 The Initial Developer of the Original Code is
 Netscape Communications Corporation.
 Portions created by the Initial Developer are Copyright (C) 1997-1999
 the Initial Developer. All Rights Reserved.

 Contributor(s):

 Alternatively, the contents of this file may be used under the terms of
 the GNU General Public License Version 2 or later (the "GPL"), in which
 case the provisions of the GPL are applicable instead of those above. If
 you wish to allow use of your version of this file only under the terms of
 the GPL and not to allow others to use your version of this file under the
 MPL, indicate your decision by deleting the provisions above and replacing
 them with the notice and other provisions required by the GPL. If you do
 not delete the provisions above, a recipient may use your version of this
 file under either the MPL or the GPL.

 ***** END LICENSE BLOCK *****


USING IDSWITCH GENERATOR TOOL

Usage:
java org.mozilla.javascript.tools.idswitch.Main <SINGLE-JAVA-SOURCE>

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#
... <DEFINITION AREA>
// #generated#
...<GENERATED AREA>
// #/generated#
... <DEFINITION AREA>
// #/string_id_map#

Then every line in <DEFINITION AREA> 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 <GENERATED AREA> are replaced by a code block that sets variable "id" to Id_<name> if variable "s" equals <name> (or value defined by //#string=...# construction in the line with Id_<name>) or 0 otherwise.
If the new code for <GENERATED AREA> is identical to old one, the file is not touched otherwise <GENERATED AREA> 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");
    }
