001: /**
002: * Caption: Zaval Java Resource Editor
003: * @author: Victor Krapivin
004: * @version: 2.0
005: *
006: * Zaval JRC Editor is a visual editor which allows you to manipulate
007: * localization strings for all Java based software with appropriate
008: * support embedded.
009: *
010: * For more info on this product read Zaval Java Resource Editor User's Guide
011: * (It comes within this package).
012: * The latest product version is always available from the product's homepage:
013: * http://www.zaval.org/products/jrc-editor/
014: * and from the SourceForge:
015: * http://sourceforge.net/projects/zaval0002/
016: *
017: * Contacts:
018: * Support : support@zaval.org
019: * Change Requests : change-request@zaval.org
020: * Feedback : feedback@zaval.org
021: * Other : info@zaval.org
022: *
023: * Copyright (C) 2001-2002 Zaval Creative Engineering Group (http://www.zaval.org)
024: *
025: * This program is free software; you can redistribute it and/or
026: * modify it under the terms of the GNU General Public License
027: * (version 2) as published by the Free Software Foundation.
028: *
029: * This program is distributed in the hope that it will be useful,
030: * but WITHOUT ANY WARRANTY; without even the implied warranty of
031: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
032: * GNU General Public License for more details.
033: *
034: * You should have received a copy of the GNU General Public License
035: * along with this program; if not, write to the Free Software
036: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
037: *
038: */package org.zaval.tools.i18n.translator;
039:
040: import java.io.*;
041: import java.util.*;
042: import org.zaval.util.SafeResourceBundle;
043:
044: public class Split {
045: private BundleManager bundle = new BundleManager();
046:
047: private Split(String srcName) {
048: try {
049: readResources(srcName, false);
050: } catch (Exception e) {
051: }
052: }
053:
054: private SafeResourceBundle rcTable = null;
055:
056: private String RC(String key) {
057: return rcTable.getString(key);
058: }
059:
060: private void join(BundleManager bundle2, boolean part) {
061: if (part) {
062: BundleSet set = bundle2.getBundle();
063: int items = set.getItemCount();
064: for (int i = 0; i < items; ++i) {
065: BundleItem bi = set.getItem(i);
066: bundle.getBundle().addKey(bi.getId());
067: Enumeration en = bi.getLanguages();
068: while (en.hasMoreElements()) {
069: String lang = (String) en.nextElement();
070: bundle.getBundle().addLanguage(lang);
071: bundle.getBundle().updateValue(bi.getId(), lang,
072: bi.getTranslation(lang));
073: }
074: }
075: } else
076: bundle = bundle2;
077: }
078:
079: public void readResources(String fileName, boolean part)
080: throws Exception {
081: try {
082: BundleManager bundle2 = new BundleManager(fileName);
083: join(bundle2, part);
084: } catch (Exception e) {
085: infoException(fileName, e);
086: throw e;
087: }
088: }
089:
090: public void onSaveAs(String fileName) {
091: String filename = fileName;
092: if (filename != null)
093: try {
094: bundle.store(filename);
095: } catch (Exception e) {
096: infoException(fileName, e);
097: }
098: }
099:
100: private void infoException(String fileName, Exception e) {
101: System.err.println(fileName + ":" + e.getMessage());
102: }
103:
104: private void onGenCode(String fileName) {
105: try {
106: String filename = fileName;
107: if (filename != null) {
108: SrcGenerator srcgen = new SrcGenerator(bundle.replace(
109: filename, "\\", "/"));
110: srcgen.perform(bundle.getBundle());
111: }
112: } catch (Exception e) {
113: infoException(fileName, e);
114: }
115: }
116:
117: private void onParseCode(String fileName) throws Exception {
118: try {
119: String filename = fileName;
120: if (fileName != null) {
121: filename = bundle.replace(filename, "\\", "/");
122: JavaParser parser = new JavaParser(new FileInputStream(
123: filename));
124: Hashtable ask = parser.parse();
125:
126: bundle.getBundle().addLanguage("en");
127: String rlng = bundle.getBundle().getLanguage(0)
128: .getLangId();
129:
130: Enumeration en = ask.keys();
131: while (en.hasMoreElements()) {
132: String key = (String) en.nextElement();
133: bundle.getBundle().addKey(key);
134: bundle.getBundle().updateValue(key, rlng,
135: (String) ask.get(key));
136: }
137: }
138: } catch (Exception e) {
139: //infoException(fileName, e);
140: throw e;
141: }
142: }
143:
144: private String stretchPath(String name) {
145: if (name.length() < 60)
146: return name;
147: return name.substring(0, 4)
148: + "..."
149: + name.substring(name.length()
150: - Math.min(name.length() - 7, 60 - 7));
151: }
152:
153: private String[] getLangSet(String options) {
154: StringTokenizer st = new StringTokenizer(options, ";,");
155: String[] ask = new String[st.countTokens()];
156: for (int i = 0; i < ask.length; ++i)
157: ask[i] = st.nextToken();
158: return ask;
159: }
160:
161: public void onSaveXml(String fileName, String[] parts) {
162: String filename = fileName;
163: if (filename != null)
164: try {
165: DataOutputStream out = new DataOutputStream(
166: new FileOutputStream(filename));
167: BundleSet set = bundle.getBundle();
168: int items = set.getItemCount();
169: out.writeChar((char) 0xFEFF);
170: out.writeChars("<xml>\n");
171: for (int i = 0; i < items; ++i) {
172: BundleItem bi = set.getItem(i);
173: Enumeration en = bi.getLanguages();
174: out.writeChars("\t<key name=\"" + bi.getId()
175: + "\">\n");
176: while (en.hasMoreElements()) {
177: String lang = (String) en.nextElement();
178: if (!inArray(parts, lang))
179: continue;
180: out.writeChars("\t\t<value lang=\"" + lang
181: + "\">" + bi.getTranslation(lang)
182: + "</value>\n");
183: }
184: out.writeChars("\t</key>\n");
185: }
186: out.writeChars("</xml>\n");
187: out.close();
188: } catch (Exception e) {
189: infoException(fileName, e);
190: }
191: }
192:
193: public void onSaveUtf(String fileName, String[] parts) {
194: String filename = fileName;
195: if (filename != null)
196: try {
197: DataOutputStream out = new DataOutputStream(
198: new FileOutputStream(filename));
199: BundleSet set = bundle.getBundle();
200: int items = set.getItemCount();
201: out.writeChar((char) 0xFEFF);
202: out
203: .writeChars("#JRC Editor 2.0: do not modify this line\r\n\r\n");
204: for (int i = 0; i < items; ++i) {
205: BundleItem bi = set.getItem(i);
206: Enumeration en = bi.getLanguages();
207: out.writeChars("KEY=\"" + bi.getId() + "\":\r\n");
208: while (en.hasMoreElements()) {
209: String lang = (String) en.nextElement();
210: if (!inArray(parts, lang))
211: continue;
212: out.writeChars("\t\"" + lang + "\"=\""
213: + bi.getTranslation(lang) + "\"\r\n");
214: }
215: out.writeChars("\r\n");
216: }
217: out.close();
218: } catch (Exception e) {
219: infoException(fileName, e);
220: }
221: }
222:
223: private boolean inArray(String[] array, String lang) {
224: for (int j = 0; array != null && j < array.length; ++j)
225: if (array[j] != null && array[j].equalsIgnoreCase(lang))
226: return true;
227: return false;
228: }
229:
230: /*
231: Reading unicode (UCS16) file stream into memory
232: */
233: private String getBody(String file) throws IOException {
234: char ch;
235: DataInputStream in = new DataInputStream(new FileInputStream(
236: file));
237: StringBuffer buf = new StringBuffer(in.available());
238:
239: try {
240: in.readChar(); // skip UCS16 marker FEFF
241: for (;;) {
242: ch = in.readChar();
243: buf.append(ch);
244: }
245: } catch (EOFException eof) {
246: }
247: return buf.toString();
248: }
249:
250: private void fillTable(Hashtable tbl) {
251: Enumeration en = tbl.keys();
252: while (en.hasMoreElements()) {
253: String k = (String) en.nextElement();
254: StringTokenizer st = new StringTokenizer(k, "!");
255: String key = st.nextToken();
256: if (!st.hasMoreTokens())
257: continue;
258: String lang = st.nextToken();
259:
260: if (bundle.getBundle().getLanguage(lang) == null)
261: bundle.getBundle().addLanguage(lang);
262:
263: bundle.getBundle().addKey(key);
264: bundle.getBundle().updateValue(key, lang,
265: (String) tbl.get(k));
266: }
267: }
268:
269: public void onLoadXml(String fileName) throws Exception {
270: String filename = fileName;
271: if (filename != null) {
272: bundle.getBundle().addLanguage("en");
273:
274: try {
275: XmlReader xml = new XmlReader(getBody(filename));
276: Hashtable tbl = xml.getTable();
277: fillTable(tbl);
278: } catch (Exception e) {
279: // infoException(fileName, e);
280: throw e;
281: }
282: }
283: }
284:
285: public void onLoadUtf(String fileName) throws Exception {
286: String filename = fileName;
287: if (filename != null) {
288: bundle.getBundle().addLanguage("en");
289: try {
290: UtfParser parser = new UtfParser(new StringReader(
291: getBody(filename)));
292: Hashtable tbl = parser.parse();
293: fillTable(tbl);
294: } catch (Exception e) {
295: // infoException(fileName, e);
296: throw e;
297: }
298: }
299: }
300:
301: private void infoError(String error) {
302: System.err.println(error);
303: }
304:
305: private void tryToLoad(String fileName) throws IOException {
306: try {
307: onParseCode(fileName);
308: return;
309: } catch (Exception e) {
310: }
311: try {
312: onLoadXml(fileName);
313: return;
314: } catch (Exception e) {
315: }
316: try {
317: onLoadUtf(fileName);
318: return;
319: } catch (Exception e) {
320: }
321: try {
322: readResources(fileName, true);
323: return;
324: } catch (Exception e) {
325: }
326: throw new IOException(fileName
327: + ": wrong file format or file unavailable");
328: }
329:
330: public static void main(String[] args) {
331: try {
332: String command = args[0];
333: String fileName = args[1];
334: String[] options = new String[args.length - 2];
335: for (int j = 0; j < options.length; ++j)
336: options[j] = args[j + 2];
337: Split obj = new Split(fileName);
338: if (command.equals("join")) {
339: for (int j = 0; j < options.length; ++j)
340: obj.tryToLoad(options[j]);
341: obj.onSaveAs(fileName);
342: } else if (command.equals("split")) {
343: String dstFile = options[0];
344: options[0] = null;
345: if (dstFile.endsWith(".txt"))
346: obj.onSaveUtf(dstFile, options);
347: else if (dstFile.endsWith(".xml"))
348: obj.onSaveXml(dstFile, options);
349: else if (dstFile.endsWith(".java"))
350: obj.onGenCode(dstFile);
351: else
352: throw new IOException(dstFile
353: + ": wrong file format or I/O error");
354: } else
355: throw new Exception();
356: } catch (IOException eio) {
357: System.err.println(eio.getMessage());
358: } catch (Exception e) {
359: // if(e.getMessage()!=null && e.getMessage().trim().length()>0)
360: // System.err.println(e.getMessage());
361: e.printStackTrace();
362: System.out
363: .println("Usage:\n"
364: + "\tjrc-split join srcFile ... addFile\n"
365: + "\tjrc-split split srcFile dstFile [lang ...]\n"
366: + "Where:\n"
367: + "\taddFile\t- XML, Java, other bundle set or UCS16 text file\n"
368: + "\tsrcFile\t- a root file of properties bundle set\n"
369: + "\tdstFile\t- XML, Java, other bundle set or UCS16 text file\n"
370: + "\tlang\t- locale abbreviation (suffix of slave properties files)\n");
371: }
372: }
373: }
|