View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  //		BufferedReader in = new BufferedReader
53  //			(new InputStreamReader(new FileInputStream(new File(filename))));
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