2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
33 import java.io.BufferedInputStream;
34 import java.io.BufferedOutputStream;
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;
43 import mir.util.ExceptionFunctions;
44 import mir.util.PropertiesManipulator;
46 public class BundleTool {
48 public static void compare(String aMaster, String aSlave) {
49 PropertiesManipulator master;
50 PropertiesManipulator slave;
51 PropertiesManipulator result;
54 master = PropertiesManipulator.readProperties(
55 new BufferedInputStream(
56 new FileInputStream(new File(aMaster)),8192));
59 System.out.println("Unable to read master properties: " + t.getMessage());
64 slave = PropertiesManipulator.readProperties(
65 new BufferedInputStream(
66 new FileInputStream(new File(aSlave)),8192));
68 catch (FileNotFoundException t) {
69 slave = new PropertiesManipulator();
72 System.out.println("Unable to read slave properties: " + t.getMessage());
78 Iterator i = master.getEntries();
82 if (e instanceof PropertiesManipulator.Entry) {
83 String key = ( (PropertiesManipulator.Entry) e).getKey();
85 if (!slave.containsKey(key) || slave.get(key) == null || slave.get(key).length()==0 ) {
87 System.out.println(aSlave+" is missing:");
89 System.out.println(" " + key);
96 System.out.println("total missing: " +missing);
99 i = slave.getEntries();
100 while (i.hasNext()) {
103 if (e instanceof PropertiesManipulator.Entry) {
104 String key = ( (PropertiesManipulator.Entry) e).getKey();
106 if (!master.containsKey(key)) {
108 System.out.println(aSlave+" has extra:");
110 System.out.println(" " + key);
116 System.out.println("total extra: " +missing);
119 public static void align(String aMaster, String aSlave) {
120 PropertiesManipulator master;
121 PropertiesManipulator slave;
122 PropertiesManipulator result;
125 master = PropertiesManipulator.readProperties(
126 new BufferedInputStream(
127 new FileInputStream(new File(aMaster)),8192));
129 catch (Throwable t) {
130 System.out.println("Unable to read master properties: " + t.getMessage());
135 slave = PropertiesManipulator.readProperties(
136 new BufferedInputStream(
137 new FileInputStream(new File(aSlave)),8192));
139 catch (FileNotFoundException t) {
140 slave = new PropertiesManipulator();
142 catch (Throwable t) {
143 System.out.println("Unable to read slave properties: " + t.getMessage());
147 result = new PropertiesManipulator();
149 // skip past the header in the slave bundle
150 Iterator i = slave.getEntries();
151 while (i.hasNext()) {
154 if (e instanceof PropertiesManipulator.EmptyLine) {
155 result.addEmptyLine();
157 else if (e instanceof PropertiesManipulator.Comment) {
158 result.addComment( ( (PropertiesManipulator.Comment) e).getComment());
161 if (! (e instanceof PropertiesManipulator.Comment))
165 boolean insideHeader = true;
166 i = master.getEntries();
167 while (i.hasNext()) {
170 if (!insideHeader && (e instanceof PropertiesManipulator.EmptyLine)) {
171 result.addEmptyLine();
173 else if (!insideHeader && e instanceof PropertiesManipulator.Comment) {
174 result.addComment( ( (PropertiesManipulator.Comment) e).getComment());
176 else if (e instanceof PropertiesManipulator.Entry) {
177 String key = ( (PropertiesManipulator.Entry) e).getKey();
178 String value = slave.get(key);
180 if (value==null || value.length()==0) {
181 result.addComment("# missing (master value = \"" +master.get(key)+"\")");
184 result.addEntry(key, value);
187 insideHeader = insideHeader && (e instanceof PropertiesManipulator.Comment);
191 PropertiesManipulator.writeProperties(result,
192 new BufferedOutputStream(new FileOutputStream(new File(aSlave)),8192));
194 catch (Throwable t) {
195 System.out.println("Unable to write slave properties: " + t.getMessage());
200 public static void encode(String aBundle, String anEncoding, String anOutputFile) {
201 PropertiesManipulator bundle;
204 bundle = PropertiesManipulator.readProperties(
205 new BufferedInputStream(
206 new FileInputStream(new File(aBundle)),8192));
208 PropertiesManipulator.writeProperties(bundle,
209 new BufferedOutputStream(new FileOutputStream(anOutputFile),8192), anEncoding, false);
211 catch (Throwable t) {
212 System.out.println("Unable to read master properties: " + t.getMessage());
217 public static void decode(String aBundle, String anEncoding, String aSourceFile) {
218 PropertiesManipulator bundle;
221 bundle = PropertiesManipulator.readProperties(
222 new BufferedInputStream(
223 new FileInputStream(new File(aSourceFile)),8192), anEncoding);
225 catch (Throwable t) {
226 Throwable s = ExceptionFunctions.traceCauseException(t);
228 System.out.println("Unable to read sourcefile: " + s.toString());
232 PropertiesManipulator.writeProperties(bundle,
233 new BufferedOutputStream(new FileOutputStream(aBundle),8192));
235 catch (Throwable t) {
236 System.out.println("Unable to write bundle: " + t.toString());
241 public static void rename(String anOldKeyName, String aNewKeyName, List aBundles) {
243 PropertiesManipulator bundle;
246 bundle = PropertiesManipulator.readProperties(new FileInputStream(new File(aBundle)));
248 PropertiesManipulator.writeProperties(bundle, new FileOutputStream(anOutputFile), anEncoding, false);
250 catch (Throwable t) {
251 System.out.println("Unable to read master properties: " + t.getMessage());
258 public static void main(String[] anArguments) {
259 String command = "help";
261 if (anArguments.length >= 1) {
262 command = anArguments[0];
264 if (command.equals("compare")) {
265 if (anArguments.length==3) {
266 compare(anArguments[1], anArguments[2]);
271 else if (command.equals("align")) {
272 if (anArguments.length==3) {
273 align(anArguments[1], anArguments[2]);
278 else if (command.equals("encode")) {
279 if (anArguments.length==4) {
280 encode(anArguments[1], anArguments[2], anArguments[3]);
285 else if (command.equals("decode")) {
286 if (anArguments.length==4) {
287 decode(anArguments[1], anArguments[2], anArguments[3]);
292 else if (command.equals("rename")) {
293 if (anArguments.length>=3) {
294 List arguments = Arrays.asList(anArguments);
296 rename(anArguments[0], anArguments[1], arguments.subList(2, arguments.size()));
299 decode(anArguments[1], anArguments[2], anArguments[3]);
306 System.out.println("Usage:");
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.");