String readStringLiteral() { skip('"'); StringBuffer sbuf = new StringBuffer(); while (index < s.length()) { char c = next(); switch (c) { case '\\': switch (next()) { case '\\': sbuf.append("\\"); break; case '\'': sbuf.append("\'"); break; case '\"': sbuf.append("\""); break; case 'n': sbuf.append("\n"); break; case 'r': sbuf.append("\r"); break; case 't': sbuf.append("\t"); break; case 'x': sbuf.append((char) ((nextHex() << 4) | nextHex())); break; case 'u': sbuf.append( (char) ((nextHex() << 12) | (nextHex() << 8) | (nextHex() << 4) | (nextHex() << 0))); break; default: throw new UnsupportedOperationException("Invalid escape sequence"); } break; case '"': return sbuf.toString(); default: sbuf.append(c); } } throw new UnsupportedOperationException("unterminated string"); }
/** Run the tool */ public static void main(String args[]) throws Throwable { 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-2009 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(); }
public static void skip(Parser p, boolean withAlpha) { if (withAlpha) p.skip(4); else p.skip(3); }
void skip(String token) { for (int i = 0; i < token.length(); i++) { skip(token.charAt(i)); } }
public static void skip(Parser p) { p.skip(3); }