FileDocCategorySizeDatePackage
UpgradeL10N.javaAPI DocphoneME MR2 API (J2ME)12460Wed May 02 18:00:26 BST 2007None

UpgradeL10N

public class UpgradeL10N extends Object
This tool upgrades a LocalizedStrings.java file to the new XML file format. An example file is src/configuration/configurator/share/l10n/en-US.xml.

Usage: javac UpgradeL10N.java java -cp . UpgradeL10N [inencoding] [infile] \ [outencoding] [outfile] [outclass] You must specify an [inencoding] that's the same as the character encoding used by [infile]. E.g., java -cp . UpgradeL10N ISO-8859-1 LocalizedStrings.java \ ISO-8859-1 en-US.xml LocalizedStringsBase

Fields Summary
static BufferedReader
reader
static PrintWriter
writer
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)
Run the tool

        String inEncoding  = args[0];
        String inFile      = args[1];
        String outEncoding = args[2];
        String outFile     = args[3];
        String outClass    = args[4];

        FileInputStream fin = new FileInputStream(inFile);
        InputStreamReader r = new InputStreamReader(fin, inEncoding);
        reader = new BufferedReader(r);

        FileOutputStream fout = new FileOutputStream(outFile);
        OutputStreamWriter w = new OutputStreamWriter(fout, outEncoding);
        writer = new PrintWriter(w);

        // Write the XML file prolog.
        pl("<?xml version=\"1.0\" encoding=\"" + outEncoding + "\"?>");
        pl("<!DOCTYPE configuration SYSTEM \"../configuration.dtd\">");
        pl("<!--");
        pl("         ");
        pl("");
        pl("  Copyright  1990-2007 Sun Microsystems, Inc. All Rights Reserved.");
        pl("  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER");
        pl("  ");
        pl("  This program is free software; you can redistribute it and/or");
        pl("  modify it under the terms of the GNU General Public License version");
        pl("  2 only, as published by the Free Software Foundation.");
        pl("  ");
        pl("  This program is distributed in the hope that it will be useful, but");
        pl("  WITHOUT ANY WARRANTY; without even the implied warranty of");
        pl("  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU");
        pl("  General Public License version 2 for more details (a copy is");
        pl("  included at /legal/license.txt).");
        pl("  ");
        pl("  You should have received a copy of the GNU General Public License");
        pl("  version 2 along with this work; if not, write to the Free Software");
        pl("  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA");
        pl("  02110-1301 USA");
        pl("  ");
        pl("  Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa");
        pl("  Clara, CA 95054 or visit www.sun.com if you need additional");
        pl("  information or have any questions.");
        pl("-->");
        pl("<configuration>");
        pl("<localized_strings Package=\"com.sun.midp.l10n\" Name=\"" +
           outClass + "\">");

        StringBuffer sbuf = new StringBuffer();
        String line;
        while ((line = reader.readLine()) != null) {
            sbuf.append(line);
            sbuf.append("\n");
        }

        // Find all occurrences of 
        // new Integer(ResourceConstants.DONE), "\u5B8C\u6210"
        //                               ^^^^    ^^^^^^^^^^^^
        //                               key     value
        Parser p = new Parser(sbuf.toString());
        while (p.advance("Integer")) {
            p.mark();
            try {
                p.skipSpaces(); p.skip('(");
                p.skipSpaces(); p.skip("ResourceConstants");
                p.skipSpaces(); p.skip(".");
                p.skipSpaces();

                String key = p.readSymbol();

                p.skipSpaces(); p.skip(')");
                p.skipSpaces(); p.skip(',");
                p.skipSpaces();

                String value = p.readStringLiteral();

                while (true) {
                    p.mark();

                    try {
                        // Handle any "xxx" + "yyy" cases
                        p.skipSpaces(); p.skip('+");
                        p.skipSpaces();
                        String more = p.readStringLiteral();
                        value += more;
                        p.pop();
                    } catch (Error t) {
                        p.reset();
                        break;
                    }
                }
                pl("<localized_string Key=\"" + key + "\"");
                pl("                Value=\"" + quote(value) + "\"/>");
            } catch (UnsupportedOperationException t) {
                System.out.println("Error: " + t.getMessage());
                System.out.println("at line " + p.countLine());
                System.out.println("at character " + p.countPos());
                System.exit(1);
            } catch (Error t) {
                // This loop will eventually terminate, since we at least
                // consume ".*Integer" from each iteration.
                p.reset();
            }
        }
        pl("</localized_strings>");
        pl("</configuration>");
        writer.close();
    
static voidpl(java.lang.String s)
Short-hand for printint a line into the output file

        writer.println(s);
    
static java.lang.Stringquote(java.lang.String s)
Quote characters that must be escaped for valid XML documents

        if (s.indexOf('&") != -1 || 
            s.indexOf('<") != -1 || 
            s.indexOf('>") != -1 || 
            s.indexOf('\n") != -1 || 
            s.indexOf('"") != -1) {
            StringBuffer sbuf = new StringBuffer();
            for (int i=0; i<s.length(); i++) {
                char c = s.charAt(i);
                if (c == '"") {
                    sbuf.append(""");
                } else if (c == '&") {
                    sbuf.append("&");
                } else if (c == '>") {
                    sbuf.append(">");
                } else if (c == '<") {
                    sbuf.append("<");
                } else if (c == '\n") {
                    sbuf.append("
");
                } else {
                    sbuf.append(c);
                }
            }
            return sbuf.toString();
        }
        return s;