cleanup / abuse system fix / prepping for a release
[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.ExceptionRoutines;
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
52     try {
53       master = PropertiesManipulator.readProperties(
54         new BufferedInputStream(
55           new FileInputStream(new File(aMaster)),8192));
56     }
57     catch (Throwable t) {
58       System.out.println("Unable to read master properties: " + t.getMessage());
59       return;
60     }
61
62     try {
63       slave = PropertiesManipulator.readProperties(
64         new BufferedInputStream(
65           new FileInputStream(new File(aSlave)),8192));
66     }
67     catch (FileNotFoundException t) {
68       slave = new PropertiesManipulator();
69     }
70     catch (Throwable t) {
71       System.out.println("Unable to read slave properties: " + t.getMessage());
72       return;
73     }
74
75     int missing=0;
76
77     Iterator i = master.getEntries();
78     while (i.hasNext()) {
79       Object e = i.next();
80
81       if (e instanceof PropertiesManipulator.Entry) {
82         String key = ( (PropertiesManipulator.Entry) e).getKey();
83
84         if (!slave.containsKey(key) || slave.get(key) == null || slave.get(key).length()==0 ) {
85           if (missing==0) {
86             System.out.println(aSlave+" is missing:");
87           }
88           System.out.println("  " + key);
89           missing++;
90         }
91       }
92     }
93
94     if (missing>0)
95       System.out.println("total missing: " +missing);
96
97     missing=0;
98     i = slave.getEntries();
99     while (i.hasNext()) {
100       Object e = i.next();
101
102       if (e instanceof PropertiesManipulator.Entry) {
103         String key = ( (PropertiesManipulator.Entry) e).getKey();
104
105         if (!master.containsKey(key)) {
106           if (missing==0) {
107             System.out.println(aSlave+" has extra:");
108           }
109           System.out.println("  " + key);
110           missing++;
111         }
112       }
113     }
114     if (missing>0)
115       System.out.println("total extra: " +missing);
116   }
117
118   public static void align(String aMaster, String aSlave) {
119     PropertiesManipulator master;
120     PropertiesManipulator slave;
121     PropertiesManipulator result;
122
123     try {
124       master = PropertiesManipulator.readProperties(
125         new BufferedInputStream(
126           new FileInputStream(new File(aMaster)),8192));
127     }
128     catch (Throwable t) {
129       System.out.println("Unable to read master properties: " + t.getMessage());
130       return;
131     }
132
133     try {
134       slave = PropertiesManipulator.readProperties(
135         new BufferedInputStream(
136           new FileInputStream(new File(aSlave)),8192));
137     }
138     catch (FileNotFoundException t) {
139       slave = new PropertiesManipulator();
140     }
141     catch (Throwable t) {
142       System.out.println("Unable to read slave properties: " + t.getMessage());
143       return;
144     }
145
146     result = new PropertiesManipulator();
147
148     // skip past the header in the slave bundle
149     Iterator i = slave.getEntries();
150     while (i.hasNext()) {
151       Object e = i.next();
152
153       if (e instanceof PropertiesManipulator.EmptyLine) {
154         result.addEmptyLine();
155       }
156       else if (e instanceof PropertiesManipulator.Comment) {
157         result.addComment( ( (PropertiesManipulator.Comment) e).getComment());
158       }
159
160       if (! (e instanceof PropertiesManipulator.Comment))
161         break;
162     }
163
164     boolean insideHeader = true;
165     i = master.getEntries();
166     while (i.hasNext()) {
167       Object e = i.next();
168
169       if (!insideHeader && (e instanceof PropertiesManipulator.EmptyLine)) {
170         result.addEmptyLine();
171       }
172       else if (!insideHeader && e instanceof PropertiesManipulator.Comment) {
173         result.addComment( ( (PropertiesManipulator.Comment) e).getComment());
174       }
175       else if (e instanceof PropertiesManipulator.Entry) {
176         String key = ( (PropertiesManipulator.Entry) e).getKey();
177         String value = slave.get(key);
178
179         if (value==null || value.length()==0) {
180           result.addComment("# missing (master value = \"" +master.get(key)+"\")");
181         }
182
183         result.addEntry(key, value);
184       }
185
186       insideHeader = insideHeader && (e instanceof PropertiesManipulator.Comment);
187     }
188
189     try {
190       PropertiesManipulator.writeProperties(result, 
191         new BufferedOutputStream(new FileOutputStream(new File(aSlave)),8192));
192     }
193     catch (Throwable t) {
194       System.out.println("Unable to write slave properties: " + t.getMessage());
195       return;
196     }
197   }
198
199   public static void encode(String aBundle, String anEncoding, String anOutputFile) {
200     PropertiesManipulator bundle;
201
202     try {
203       bundle = PropertiesManipulator.readProperties(
204         new BufferedInputStream(
205           new FileInputStream(new File(aBundle)),8192));
206
207       PropertiesManipulator.writeProperties(bundle, 
208         new BufferedOutputStream(new FileOutputStream(anOutputFile),8192), anEncoding, false);
209     }
210     catch (Throwable t) {
211       System.out.println("Unable to read master properties: " + t.getMessage());
212       return;
213     }
214   }
215
216   public static void decode(String aBundle, String anEncoding, String aSourceFile) {
217     PropertiesManipulator bundle;
218
219     try {
220       bundle = PropertiesManipulator.readProperties(
221         new BufferedInputStream(
222           new FileInputStream(new File(aSourceFile)),8192), anEncoding);
223     }
224     catch (Throwable t) {
225       Throwable s = ExceptionRoutines.traceCauseException(t);
226
227       System.out.println("Unable to read sourcefile: " + s.toString());
228       return;
229     }
230     try {
231       PropertiesManipulator.writeProperties(bundle, 
232         new BufferedOutputStream(new FileOutputStream(aBundle),8192));
233     }
234     catch (Throwable t) {
235       System.out.println("Unable to write bundle: " + t.toString());
236       return;
237     }
238   }
239
240   public static void rename(String anOldKeyName, String aNewKeyName, List aBundles) {
241     /*
242     PropertiesManipulator bundle;
243
244     try {
245       bundle = PropertiesManipulator.readProperties(new FileInputStream(new File(aBundle)));
246
247       PropertiesManipulator.writeProperties(bundle, new FileOutputStream(anOutputFile), anEncoding, false);
248     }
249     catch (Throwable t) {
250       System.out.println("Unable to read master properties: " + t.getMessage());
251       return;
252     }
253 */
254   }
255
256
257   public static void main(String[] anArguments) {
258     String command = "help";
259
260     if (anArguments.length >= 1) {
261       command = anArguments[0];
262
263       if (command.equals("compare")) {
264         if (anArguments.length==3) {
265           compare(anArguments[1], anArguments[2]);
266
267           return;
268         }
269       }
270       else if (command.equals("align")) {
271         if (anArguments.length==3) {
272           align(anArguments[1], anArguments[2]);
273
274           return;
275         }
276       }
277       else if (command.equals("encode")) {
278         if (anArguments.length==4) {
279           encode(anArguments[1], anArguments[2], anArguments[3]);
280
281           return;
282         }
283       }
284       else if (command.equals("decode")) {
285         if (anArguments.length==4) {
286           decode(anArguments[1], anArguments[2], anArguments[3]);
287
288           return;
289         }
290       }
291       else if (command.equals("rename")) {
292         if (anArguments.length>=3) {
293           List arguments = Arrays.asList(anArguments);
294
295           rename(anArguments[0], anArguments[1], arguments.subList(2, arguments.size()));
296
297 /*
298           decode(anArguments[1], anArguments[2], anArguments[3]);
299 */
300           return;
301         }
302       }
303     }
304
305     System.out.println("Usage:");
306
307     System.out.println("  BundleTool align <master bundle> <slave bundle>");
308     System.out.println("");
309     System.out.println("      Reorders keys/values in a slave bundle according to a master bundle.");
310     System.out.println("");
311     System.out.println("  BundleTool compare  <master bundle> <slave bundle>");
312     System.out.println("");
313     System.out.println("      Compares availability of bundle keys.");
314     System.out.println("");
315     System.out.println("  BundleTool encode <bundle> <encoding> <destinationfile>");
316     System.out.println("");
317     System.out.println("      Encodes the keys/values with a custom encoding.");
318     System.out.println("");
319     System.out.println("  BundleTool decode <bundle> <encoding> <sourcefile>");
320     System.out.println("");
321     System.out.println("      Decodes the keys/values with a custom encoding.");
322     System.out.println("  BundleTool rename <old key> <new key> <bundle> [<bundle> [ ... ]]");
323     System.out.println("");
324     System.out.println("      Decodes the keys/values with a custom encoding.");
325   }
326
327 }