1.1 restoration
[mir.git] / source / tool / BundleTool.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir 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; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30
31 package tool;
32
33 import java.io.BufferedInputStream;
34 import java.io.BufferedOutputStream;
35 import java.io.File;
36 import java.io.FileInputStream;
37 import java.io.FileNotFoundException;
38 import java.io.FileOutputStream;
39 import java.util.Arrays;
40 import java.util.Iterator;
41 import java.util.List;
42
43 import mir.util.ExceptionFunctions;
44 import mir.util.PropertiesManipulator;
45
46 public class BundleTool {
47
48   public static void compare(String aMaster, String aSlave) {
49     PropertiesManipulator master;
50     PropertiesManipulator slave;
51     PropertiesManipulator result;
52
53     try {
54       master = PropertiesManipulator.readProperties(
55         new BufferedInputStream(
56           new FileInputStream(new File(aMaster)),8192));
57     }
58     catch (Throwable t) {
59       System.out.println("Unable to read master properties: " + t.getMessage());
60       return;
61     }
62
63     try {
64       slave = PropertiesManipulator.readProperties(
65         new BufferedInputStream(
66           new FileInputStream(new File(aSlave)),8192));
67     }
68     catch (FileNotFoundException t) {
69       slave = new PropertiesManipulator();
70     }
71     catch (Throwable t) {
72       System.out.println("Unable to read slave properties: " + t.getMessage());
73       return;
74     }
75
76     int missing=0;
77
78     Iterator i = master.getEntries();
79     while (i.hasNext()) {
80       Object e = i.next();
81
82       if (e instanceof PropertiesManipulator.Entry) {
83         String key = ( (PropertiesManipulator.Entry) e).getKey();
84
85         if (!slave.containsKey(key) || slave.get(key) == null || slave.get(key).length()==0 ) {
86           if (missing==0) {
87             System.out.println(aSlave+" is missing:");
88           }
89           System.out.println("  " + key);
90           missing++;
91         }
92       }
93     }
94
95     if (missing>0)
96       System.out.println("total missing: " +missing);
97
98     missing=0;
99     i = slave.getEntries();
100     while (i.hasNext()) {
101       Object e = i.next();
102
103       if (e instanceof PropertiesManipulator.Entry) {
104         String key = ( (PropertiesManipulator.Entry) e).getKey();
105
106         if (!master.containsKey(key)) {
107           if (missing==0) {
108             System.out.println(aSlave+" has extra:");
109           }
110           System.out.println("  " + key);
111           missing++;
112         }
113       }
114     }
115     if (missing>0)
116       System.out.println("total extra: " +missing);
117   }
118
119   public static void align(String aMaster, String aSlave) {
120     PropertiesManipulator master;
121     PropertiesManipulator slave;
122     PropertiesManipulator result;
123
124     try {
125       master = PropertiesManipulator.readProperties(
126         new BufferedInputStream(
127           new FileInputStream(new File(aMaster)),8192));
128     }
129     catch (Throwable t) {
130       System.out.println("Unable to read master properties: " + t.getMessage());
131       return;
132     }
133
134     try {
135       slave = PropertiesManipulator.readProperties(
136         new BufferedInputStream(
137           new FileInputStream(new File(aSlave)),8192));
138     }
139     catch (FileNotFoundException t) {
140       slave = new PropertiesManipulator();
141     }
142     catch (Throwable t) {
143       System.out.println("Unable to read slave properties: " + t.getMessage());
144       return;
145     }
146
147     result = new PropertiesManipulator();
148
149     // skip past the header in the slave bundle
150     Iterator i = slave.getEntries();
151     while (i.hasNext()) {
152       Object e = i.next();
153
154       if (e instanceof PropertiesManipulator.EmptyLine) {
155         result.addEmptyLine();
156       }
157       else if (e instanceof PropertiesManipulator.Comment) {
158         result.addComment( ( (PropertiesManipulator.Comment) e).getComment());
159       }
160
161       if (! (e instanceof PropertiesManipulator.Comment))
162         break;
163     }
164
165     boolean insideHeader = true;
166     i = master.getEntries();
167     while (i.hasNext()) {
168       Object e = i.next();
169
170       if (!insideHeader && (e instanceof PropertiesManipulator.EmptyLine)) {
171         result.addEmptyLine();
172       }
173       else if (!insideHeader && e instanceof PropertiesManipulator.Comment) {
174         result.addComment( ( (PropertiesManipulator.Comment) e).getComment());
175       }
176       else if (e instanceof PropertiesManipulator.Entry) {
177         String key = ( (PropertiesManipulator.Entry) e).getKey();
178         String value = slave.get(key);
179
180         if (value==null || value.length()==0) {
181           result.addComment("# missing (master value = \"" +master.get(key)+"\")");
182         }
183
184         result.addEntry(key, value);
185       }
186
187       insideHeader = insideHeader && (e instanceof PropertiesManipulator.Comment);
188     }
189
190     try {
191       PropertiesManipulator.writeProperties(result, 
192         new BufferedOutputStream(new FileOutputStream(new File(aSlave)),8192));
193     }
194     catch (Throwable t) {
195       System.out.println("Unable to write slave properties: " + t.getMessage());
196       return;
197     }
198   }
199
200   public static void encode(String aBundle, String anEncoding, String anOutputFile) {
201     PropertiesManipulator bundle;
202
203     try {
204       bundle = PropertiesManipulator.readProperties(
205         new BufferedInputStream(
206           new FileInputStream(new File(aBundle)),8192));
207
208       PropertiesManipulator.writeProperties(bundle, 
209         new BufferedOutputStream(new FileOutputStream(anOutputFile),8192), anEncoding, false);
210     }
211     catch (Throwable t) {
212       System.out.println("Unable to read master properties: " + t.getMessage());
213       return;
214     }
215   }
216
217   public static void decode(String aBundle, String anEncoding, String aSourceFile) {
218     PropertiesManipulator bundle;
219
220     try {
221       bundle = PropertiesManipulator.readProperties(
222         new BufferedInputStream(
223           new FileInputStream(new File(aSourceFile)),8192), anEncoding);
224     }
225     catch (Throwable t) {
226       Throwable s = ExceptionFunctions.traceCauseException(t);
227
228       System.out.println("Unable to read sourcefile: " + s.toString());
229       return;
230     }
231     try {
232       PropertiesManipulator.writeProperties(bundle, 
233         new BufferedOutputStream(new FileOutputStream(aBundle),8192));
234     }
235     catch (Throwable t) {
236       System.out.println("Unable to write bundle: " + t.toString());
237       return;
238     }
239   }
240
241   public static void rename(String anOldKeyName, String aNewKeyName, List aBundles) {
242     /*
243     PropertiesManipulator bundle;
244
245     try {
246       bundle = PropertiesManipulator.readProperties(new FileInputStream(new File(aBundle)));
247
248       PropertiesManipulator.writeProperties(bundle, new FileOutputStream(anOutputFile), anEncoding, false);
249     }
250     catch (Throwable t) {
251       System.out.println("Unable to read master properties: " + t.getMessage());
252       return;
253     }
254 */
255   }
256
257
258   public static void main(String[] anArguments) {
259     String command = "help";
260
261     if (anArguments.length >= 1) {
262       command = anArguments[0];
263
264       if (command.equals("compare")) {
265         if (anArguments.length==3) {
266           compare(anArguments[1], anArguments[2]);
267
268           return;
269         }
270       }
271       else if (command.equals("align")) {
272         if (anArguments.length==3) {
273           align(anArguments[1], anArguments[2]);
274
275           return;
276         }
277       }
278       else if (command.equals("encode")) {
279         if (anArguments.length==4) {
280           encode(anArguments[1], anArguments[2], anArguments[3]);
281
282           return;
283         }
284       }
285       else if (command.equals("decode")) {
286         if (anArguments.length==4) {
287           decode(anArguments[1], anArguments[2], anArguments[3]);
288
289           return;
290         }
291       }
292       else if (command.equals("rename")) {
293         if (anArguments.length>=3) {
294           List arguments = Arrays.asList(anArguments);
295
296           rename(anArguments[0], anArguments[1], arguments.subList(2, arguments.size()));
297
298 /*
299           decode(anArguments[1], anArguments[2], anArguments[3]);
300 */
301           return;
302         }
303       }
304     }
305
306     System.out.println("Usage:");
307
308     System.out.println("  BundleTool align <master bundle> <slave bundle>");
309     System.out.println("");
310     System.out.println("      Reorders keys/values in a slave bundle according to a master bundle.");
311     System.out.println("");
312     System.out.println("  BundleTool compare  <master bundle> <slave bundle>");
313     System.out.println("");
314     System.out.println("      Compares availability of bundle keys.");
315     System.out.println("");
316     System.out.println("  BundleTool encode <bundle> <encoding> <destinationfile>");
317     System.out.println("");
318     System.out.println("      Encodes the keys/values with a custom encoding.");
319     System.out.println("");
320     System.out.println("  BundleTool decode <bundle> <encoding> <sourcefile>");
321     System.out.println("");
322     System.out.println("      Decodes the keys/values with a custom encoding.");
323     System.out.println("  BundleTool rename <old key> <new key> <bundle> [<bundle> [ ... ]]");
324     System.out.println("");
325     System.out.println("      Decodes the keys/values with a custom encoding.");
326   }
327
328 }