001: /**
002: * Caption: Zaval Java Resource Editor
003: * $Revision: 0.37 $
004: * $Date: 2002/03/28 9:24:42 $
005: *
006: * @author: Victor Krapivin
007: * @version: 2.0
008: *
009: * Zaval JRC Editor is a visual editor which allows you to manipulate
010: * localization strings for all Java based software with appropriate
011: * support embedded.
012: *
013: * For more info on this product read Zaval Java Resource Editor User's Guide
014: * (It comes within this package).
015: * The latest product version is always available from the product's homepage:
016: * http://www.zaval.org/products/jrc-editor/
017: * and from the SourceForge:
018: * http://sourceforge.net/projects/zaval0002/
019: *
020: * Contacts:
021: * Support : support@zaval.org
022: * Change Requests : change-request@zaval.org
023: * Feedback : feedback@zaval.org
024: * Other : info@zaval.org
025: *
026: * Copyright (C) 2001-2002 Zaval Creative Engineering Group (http://www.zaval.org)
027: *
028: * This program is free software; you can redistribute it and/or
029: * modify it under the terms of the GNU General Public License
030: * (version 2) as published by the Free Software Foundation.
031: *
032: * This program is distributed in the hope that it will be useful,
033: * but WITHOUT ANY WARRANTY; without even the implied warranty of
034: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
035: * GNU General Public License for more details.
036: *
037: * You should have received a copy of the GNU General Public License
038: * along with this program; if not, write to the Free Software
039: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
040: *
041: */package org.zaval.tools.i18n.translator;
042:
043: import java.io.*;
044: import java.util.*;
045:
046: class SrcGenerator {
047: private PrintStream out = null;
048: private String filename = null;
049:
050: SrcGenerator(String filename) throws IOException {
051: FileOutputStream fop = new FileOutputStream(filename);
052: out = new PrintStream(fop);
053: this .filename = filename;
054: }
055:
056: void perform(BundleSet set) throws IOException {
057: out.println("import java.util.*;\n\npublic class "
058: + baseName(filename) + "\n{");
059: int j, k = set.getItemCount();
060: for (j = 0; j < k; ++j) {
061: BundleItem bi = set.getItem(j);
062: out.println("\tprivate String " + makeVarName(bi) + ";");
063: }
064: out.println();
065: for (j = 0; j < k; ++j) {
066: BundleItem bi = set.getItem(j);
067: out.println("\tpublic final String get" + makeFunName(bi)
068: + "()\t{ return " + makeVarName(bi) + ";}");
069: }
070: out.println();
071: for (j = 0; j < k; ++j) {
072: BundleItem bi = set.getItem(j);
073: out.println("\tpublic final void set" + makeFunName(bi)
074: + "(String what)\t{ this." + makeVarName(bi)
075: + " = what;}");
076: }
077: out.println();
078: out
079: .println("\tpublic void loadFromResource(ResourceBundle rs)\n\t{");
080: for (j = 0; j < k; ++j) {
081: BundleItem bi = set.getItem(j);
082: out.println("\t\ttry{ set" + makeFunName(bi)
083: + "(rs.getString(\"" + bi.getId()
084: + "\")); } catch(Exception error){ reportNoRc(\""
085: + bi.getId() + "\", error); }");
086: }
087: out.println("\t}\n");
088: out
089: .println("\tprivate void reportNoRc(String what, Exception details)\n\t{\n"
090: + "\t\tSystem.err.println(what + \": unknown resource\");\n"
091: + "\t\tdetails.printStackTrace();\n\t}\n");
092: out.println("}");
093: out.close();
094: }
095:
096: private String makeVarName(BundleItem bi) {
097: String ask = bi.getTranslation("__var");
098: if (ask != null)
099: return ask;
100: ask = makeVarName(bi.getId());
101: bi.setTranslation("__var", ask);
102: return ask;
103: }
104:
105: private String makeFunName(BundleItem bi) {
106: String ask = bi.getTranslation("__varF");
107: if (ask != null)
108: return ask;
109: ask = capitalize(makeVarName(bi.getId()));
110: bi.setTranslation("__varF", ask);
111: return ask;
112: }
113:
114: private String makeVarName(String key) {
115: String s = key.toLowerCase();
116: int j1 = s.lastIndexOf('.');
117: if (j1 < 0)
118: return s;
119: int j2 = s.lastIndexOf('.', j1 - 1);
120: if (j2 < 0)
121: j2 = -1;
122: s = s.substring(j2 + 1, j1) + capitalize(s.substring(j1 + 1));
123: return s;
124: }
125:
126: private String capitalize(String s) {
127: if (s.length() < 2)
128: return s.toUpperCase();
129: return s.substring(0, 1).toUpperCase() + s.substring(1);
130: }
131:
132: private String baseName(String fn) {
133: int ind = fn.lastIndexOf('/');
134: fn = ind >= 0 ? fn.substring(ind + 1) : fn;
135: ind = fn.lastIndexOf('.');
136: return ind >= 0 ? fn.substring(0, ind) : fn;
137: }
138: }
|