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.ExceptionRoutines;
44 import mir.util.PropertiesManipulator;
46 public class BundleTool {
48 public static void compare(String aMaster, String aSlave) {
49 PropertiesManipulator master;
50 PropertiesManipulator slave;
53 master = PropertiesManipulator.readProperties(
54 new BufferedInputStream(
55 new FileInputStream(new File(aMaster)),8192));
58 System.out.println("Unable to read master properties: " + t.getMessage());
63 slave = PropertiesManipulator.readProperties(
64 new BufferedInputStream(
65 new FileInputStream(new File(aSlave)),8192));
67 catch (FileNotFoundException t) {
68 slave = new PropertiesManipulator();
71 System.out.println("Unable to read slave properties: " + t.getMessage());
77 Iterator i = master.getEntries();
81 if (e instanceof PropertiesManipulator.Entry) {
82 String key = ( (PropertiesManipulator.Entry) e).getKey();
84 if (!slave.containsKey(key) || slave.get(key) == null || slave.get(key).length()==0 ) {
86 System.out.println(aSlave+" is missing:");
88 System.out.println(" " + key);
95 System.out.println("total missing: " +missing);
98 i = slave.getEntries();
102 if (e instanceof PropertiesManipulator.Entry) {
103 String key = ( (PropertiesManipulator.Entry) e).getKey();
105 if (!master.containsKey(key)) {
107 System.out.println(aSlave+" has extra:");
109 System.out.println(" " + key);
115 System.out.println("total extra: " +missing);
118 public static void align(String aMaster, String aSlave) {
119 PropertiesManipulator master;
120 PropertiesManipulator slave;
121 PropertiesManipulator result;
124 master = PropertiesManipulator.readProperties(
125 new BufferedInputStream(
126 new FileInputStream(new File(aMaster)),8192));
128 catch (Throwable t) {
129 System.out.println("Unable to read master properties: " + t.getMessage());
134 slave = PropertiesManipulator.readProperties(
135 new BufferedInputStream(
136 new FileInputStream(new File(aSlave)),8192));
138 catch (FileNotFoundException t) {
139 slave = new PropertiesManipulator();
141 catch (Throwable t) {
142 System.out.println("Unable to read slave properties: " + t.getMessage());
146 result = new PropertiesManipulator();
148 // skip past the header in the slave bundle
149 Iterator i = slave.getEntries();
150 while (i.hasNext()) {
153 if (e instanceof PropertiesManipulator.EmptyLine) {
154 result.addEmptyLine();
156 else if (e instanceof PropertiesManipulator.Comment) {
157 result.addComment( ( (PropertiesManipulator.Comment) e).getComment());
160 if (! (e instanceof PropertiesManipulator.Comment))
164 boolean insideHeader = true;
165 i = master.getEntries();
166 while (i.hasNext()) {
169 if (!insideHeader && (e instanceof PropertiesManipulator.EmptyLine)) {
170 result.addEmptyLine();
172 else if (!insideHeader && e instanceof PropertiesManipulator.Comment) {
173 result.addComment( ( (PropertiesManipulator.Comment) e).getComment());
175 else if (e instanceof PropertiesManipulator.Entry) {
176 String key = ( (PropertiesManipulator.Entry) e).getKey();
177 String value = slave.get(key);
179 if (value==null || value.length()==0) {
180 result.addComment("# missing (master value = \"" +master.get(key)+"\")");
183 result.addEntry(key, value);
186 insideHeader = insideHeader && (e instanceof PropertiesManipulator.Comment);
190 PropertiesManipulator.writeProperties(result,
191 new BufferedOutputStream(new FileOutputStream(new File(aSlave)),8192));
193 catch (Throwable t) {
194 System.out.println("Unable to write slave properties: " + t.getMessage());
199 public static void encode(String aBundle, String anEncoding, String anOutputFile) {
200 PropertiesManipulator bundle;
203 bundle = PropertiesManipulator.readProperties(
204 new BufferedInputStream(
205 new FileInputStream(new File(aBundle)),8192));
207 PropertiesManipulator.writeProperties(bundle,
208 new BufferedOutputStream(new FileOutputStream(anOutputFile),8192), anEncoding, false);
210 catch (Throwable t) {
211 System.out.println("Unable to read master properties: " + t.getMessage());
216 public static void decode(String aBundle, String anEncoding, String aSourceFile) {
217 PropertiesManipulator bundle;
220 bundle = PropertiesManipulator.readProperties(
221 new BufferedInputStream(
222 new FileInputStream(new File(aSourceFile)),8192), anEncoding);
224 catch (Throwable t) {
225 Throwable s = ExceptionRoutines.traceCauseException(t);
227 System.out.println("Unable to read sourcefile: " + s.toString());
231 PropertiesManipulator.writeProperties(bundle,
232 new BufferedOutputStream(new FileOutputStream(aBundle),8192));
234 catch (Throwable t) {
235 System.out.println("Unable to write bundle: " + t.toString());
240 public static void rename(String anOldKeyName, String aNewKeyName, List aBundles) {
242 PropertiesManipulator bundle;
245 bundle = PropertiesManipulator.readProperties(new FileInputStream(new File(aBundle)));
247 PropertiesManipulator.writeProperties(bundle, new FileOutputStream(anOutputFile), anEncoding, false);
249 catch (Throwable t) {
250 System.out.println("Unable to read master properties: " + t.getMessage());
257 public static void main(String[] anArguments) {
258 String command = "help";
260 if (anArguments.length >= 1) {
261 command = anArguments[0];
263 if (command.equals("compare")) {
264 if (anArguments.length==3) {
265 compare(anArguments[1], anArguments[2]);
270 else if (command.equals("align")) {
271 if (anArguments.length==3) {
272 align(anArguments[1], anArguments[2]);
277 else if (command.equals("encode")) {
278 if (anArguments.length==4) {
279 encode(anArguments[1], anArguments[2], anArguments[3]);
284 else if (command.equals("decode")) {
285 if (anArguments.length==4) {
286 decode(anArguments[1], anArguments[2], anArguments[3]);
291 else if (command.equals("rename")) {
292 if (anArguments.length>=3) {
293 List arguments = Arrays.asList(anArguments);
295 rename(anArguments[0], anArguments[1], arguments.subList(2, arguments.size()));
298 decode(anArguments[1], anArguments[2], anArguments[3]);
305 System.out.println("Usage:");
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.");