e44345b019282c6d95211147cfd35f189444522a
[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.File;
34 import java.io.FileInputStream;
35 import java.io.FileNotFoundException;
36 import java.io.FileOutputStream;
37 import java.util.Iterator;
38
39 import mir.util.ExceptionFunctions;
40 import mir.util.PropertiesManipulator;
41
42 public class BundleTool {
43
44   public static void compare(String aMaster, String aSlave) {
45     PropertiesManipulator master;
46     PropertiesManipulator slave;
47     PropertiesManipulator result;
48
49     try {
50       master = PropertiesManipulator.readProperties(new FileInputStream(new File(aMaster)));
51     }
52     catch (Throwable t) {
53       System.out.println("Unable to read master properties: " + t.getMessage());
54       return;
55     }
56
57     try {
58       slave = PropertiesManipulator.readProperties(new FileInputStream(new File(aSlave)));
59     }
60     catch (FileNotFoundException t) {
61       slave = new PropertiesManipulator();
62     }
63     catch (Throwable t) {
64       System.out.println("Unable to read slave properties: " + t.getMessage());
65       return;
66     }
67
68     int missing=0;
69
70     Iterator i = master.getEntries();
71     while (i.hasNext()) {
72       Object e = i.next();
73
74       if (e instanceof PropertiesManipulator.Entry) {
75         String key = ( (PropertiesManipulator.Entry) e).getKey();
76
77         if (!slave.containsKey(key) || slave.get(key) == null || slave.get(key).length()==0 ) {
78           if (missing==0) {
79             System.out.println(aSlave+" is missing:");
80           }
81           System.out.println("  " + key);
82           missing++;
83         }
84       }
85     }
86
87     if (missing>0)
88       System.out.println("total missing: " +missing);
89
90     missing=0;
91     i = slave.getEntries();
92     while (i.hasNext()) {
93       Object e = i.next();
94
95       if (e instanceof PropertiesManipulator.Entry) {
96         String key = ( (PropertiesManipulator.Entry) e).getKey();
97
98         if (!master.containsKey(key)) {
99           if (missing==0) {
100             System.out.println(aSlave+" has extra:");
101           }
102           System.out.println("  " + key);
103           missing++;
104         }
105       }
106     }
107     if (missing>0)
108       System.out.println("total extra: " +missing);
109   }
110
111   public static void align(String aMaster, String aSlave) {
112     PropertiesManipulator master;
113     PropertiesManipulator slave;
114     PropertiesManipulator result;
115
116     try {
117       master = PropertiesManipulator.readProperties(new FileInputStream(new File(aMaster)));
118     }
119     catch (Throwable t) {
120       System.out.println("Unable to read master properties: " + t.getMessage());
121       return;
122     }
123
124     try {
125       slave = PropertiesManipulator.readProperties(new FileInputStream(new File(aSlave)));
126     }
127     catch (FileNotFoundException t) {
128       slave = new PropertiesManipulator();
129     }
130     catch (Throwable t) {
131       System.out.println("Unable to read slave properties: " + t.getMessage());
132       return;
133     }
134
135     result = new PropertiesManipulator();
136
137     // skip past the header in the slave bundle
138     Iterator i = slave.getEntries();
139     while (i.hasNext()) {
140       Object e = i.next();
141
142       if (e instanceof PropertiesManipulator.EmptyLine) {
143         result.addEmptyLine();
144       }
145       else if (e instanceof PropertiesManipulator.Comment) {
146         result.addComment( ( (PropertiesManipulator.Comment) e).getComment());
147       }
148
149       if (! (e instanceof PropertiesManipulator.Comment))
150         break;
151     }
152
153     boolean insideHeader = true;
154     i = master.getEntries();
155     while (i.hasNext()) {
156       Object e = i.next();
157
158       if (!insideHeader && (e instanceof PropertiesManipulator.EmptyLine)) {
159         result.addEmptyLine();
160       }
161       else if (!insideHeader && e instanceof PropertiesManipulator.Comment) {
162         result.addComment( ( (PropertiesManipulator.Comment) e).getComment());
163       }
164       else if (e instanceof PropertiesManipulator.Entry) {
165         String key = ( (PropertiesManipulator.Entry) e).getKey();
166         String value = slave.get(key);
167
168         if (value==null || value.length()==0) {
169           result.addComment("# missing (master value = \"" +master.get(key)+"\")");
170         }
171
172         result.addEntry(key, value);
173       }
174
175       insideHeader = insideHeader && (e instanceof PropertiesManipulator.Comment);
176     }
177
178     try {
179       PropertiesManipulator.writeProperties(result, new FileOutputStream(new File(aSlave)));
180     }
181     catch (Throwable t) {
182       System.out.println("Unable to write slave properties: " + t.getMessage());
183       return;
184     }
185   }
186
187   public static void encode(String aBundle, String anEncoding, String anOutputFile) {
188     PropertiesManipulator bundle;
189
190     try {
191       bundle = PropertiesManipulator.readProperties(new FileInputStream(new File(aBundle)));
192
193       PropertiesManipulator.writeProperties(bundle, new FileOutputStream(anOutputFile), anEncoding, false);
194     }
195     catch (Throwable t) {
196       System.out.println("Unable to read master properties: " + t.getMessage());
197       return;
198     }
199   }
200
201   public static void decode(String aBundle, String anEncoding, String aSourceFile) {
202     PropertiesManipulator bundle;
203
204     try {
205       bundle = PropertiesManipulator.readProperties(new FileInputStream(new File(aSourceFile)), anEncoding);
206     }
207     catch (Throwable t) {
208       Throwable s = ExceptionFunctions.traceCauseException(t);
209
210       System.out.println("Unable to read sourcefile: " + s.toString());
211       return;
212     }
213     try {
214       PropertiesManipulator.writeProperties(bundle, new FileOutputStream(aBundle));
215     }
216     catch (Throwable t) {
217       System.out.println("Unable to write bundle: " + t.toString());
218       return;
219     }
220   }
221
222   public static void main(String[] anArguments) {
223     String command = "help";
224
225     if (anArguments.length >= 1) {
226       command = anArguments[0];
227
228       if (command.equals("compare")) {
229         if (anArguments.length==3) {
230           compare(anArguments[1], anArguments[2]);
231
232           return;
233         }
234       }
235       else if (command.equals("align")) {
236         if (anArguments.length==3) {
237           align(anArguments[1], anArguments[2]);
238
239           return;
240         }
241       }
242       else if (command.equals("encode")) {
243         if (anArguments.length==4) {
244           encode(anArguments[1], anArguments[2], anArguments[3]);
245
246           return;
247         }
248       }
249       else if (command.equals("decode")) {
250         if (anArguments.length==4) {
251           decode(anArguments[1], anArguments[2], anArguments[3]);
252
253           return;
254         }
255       }
256     }
257
258
259
260     System.out.println("Usage:");
261
262     System.out.println("  BundleTool align <master bundle> <slave bundle>");
263     System.out.println("");
264     System.out.println("      Reorders keys/values in a slave bundle according to a master bundle.");
265     System.out.println("");
266     System.out.println("  BundleTool compare  <master bundle> <slave bundle>");
267     System.out.println("");
268     System.out.println("      Compares availability of bundle keys.");
269     System.out.println("");
270     System.out.println("  BundleTool encode <bundle> <encoding> <destinationfile>");
271     System.out.println("");
272     System.out.println("      Encodes the keys/values with a custom encoding.");
273     System.out.println("");
274     System.out.println("  BundleTool decode <bundle> <encoding> <sourcefile>");
275     System.out.println("");
276     System.out.println("      Decodes the keys/values with a custom encoding.");
277   }
278
279 }