1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.i18n;
21
22 import java.io.*;
23 import java.util.*;
24
25 public class MergeKeys {
26
27 /***
28 *
29 */
30 public static void main(String[] argv)
31 {
32 if (argv.length < 1) {
33 System.err.println("usage: MergeKeys file [file...]");
34 System.exit(1);
35 }
36
37 HashSet keys = I18nHelper.readKeys();
38
39 for (int i = 0; i < argv.length; i++) {
40 try {
41 System.out.println("Merging keys into file: " + argv[i]);
42 merge(argv[i], keys);
43 }
44 catch (IOException e) {
45 System.err.println("Error: " + e.getMessage());
46 }
47 }
48 }
49
50 public static void merge(String filename, HashSet keys) throws IOException
51 {
52
53
54
55 Hashtable fileTable = I18nHelper.readI18nFile(filename, false);
56
57 System.out.println(" total keys: " + fileTable.size());
58
59 HashSet deprecatedKeys = new HashSet(fileTable.keySet());
60 deprecatedKeys.removeAll(keys);
61
62 HashSet keepKeys = new HashSet(keys);
63 keepKeys.retainAll(fileTable.keySet());
64
65 HashSet newKeys = new HashSet(keys);
66 newKeys.removeAll(fileTable.keySet());
67
68 System.out.println(" keys: "
69 + deprecatedKeys.size() + " deprecated "
70 + keepKeys.size() + " kept "
71 + newKeys.size() + " new");
72
73 BufferedWriter out = new BufferedWriter
74 (new OutputStreamWriter(new FileOutputStream(new File(filename)),
75 I18nHelper.getI18nCharset(filename)));
76
77 write(out, fileTable, deprecatedKeys, "#", false);
78 write(out, fileTable, keepKeys, "", false);
79 write(out, fileTable, newKeys, "", filename.endsWith("XNap.i18n"));
80
81 out.close();
82 }
83
84 public static void write(BufferedWriter out, Hashtable table,
85 HashSet keySet, String prefix,
86 boolean keyEqualsValue)
87 throws IOException
88 {
89 LinkedList keys = new LinkedList(keySet);
90 Collections.sort(keys);
91
92 for (Iterator i = keys.iterator(); i.hasNext();) {
93 String key = (String)i.next();
94 out.write(prefix);
95 out.write(I18nHelper.i18nKeyToRaw(key));
96 out.write("=");
97 String value = (String)table.get(key);
98 if (value != null) {
99 out.write(value);
100 }
101 else if (keyEqualsValue) {
102 out.write(key);
103 }
104 out.newLine();
105 }
106 }
107
108 }
109
110