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.
34 import java.io.FileInputStream;
35 import java.io.FileNotFoundException;
36 import java.io.FileOutputStream;
37 import java.util.Iterator;
39 import mir.util.ExceptionFunctions;
40 import mir.util.PropertiesManipulator;
42 public class BundleTool {
44 public static void compare(String aMaster, String aSlave) {
45 PropertiesManipulator master;
46 PropertiesManipulator slave;
47 PropertiesManipulator result;
50 master = PropertiesManipulator.readProperties(new FileInputStream(new File(aMaster)));
53 System.out.println("Unable to read master properties: " + t.getMessage());
58 slave = PropertiesManipulator.readProperties(new FileInputStream(new File(aSlave)));
60 catch (FileNotFoundException t) {
61 slave = new PropertiesManipulator();
64 System.out.println("Unable to read slave properties: " + t.getMessage());
70 Iterator i = master.getEntries();
74 if (e instanceof PropertiesManipulator.Entry) {
75 String key = ( (PropertiesManipulator.Entry) e).getKey();
77 if (!slave.containsKey(key) || slave.get(key) == null || slave.get(key).length()==0 ) {
79 System.out.println(aSlave+" is missing:");
81 System.out.println(" " + key);
88 System.out.println("total missing: " +missing);
91 i = slave.getEntries();
95 if (e instanceof PropertiesManipulator.Entry) {
96 String key = ( (PropertiesManipulator.Entry) e).getKey();
98 if (!master.containsKey(key)) {
100 System.out.println(aSlave+" has extra:");
102 System.out.println(" " + key);
108 System.out.println("total extra: " +missing);
111 public static void align(String aMaster, String aSlave) {
112 PropertiesManipulator master;
113 PropertiesManipulator slave;
114 PropertiesManipulator result;
117 master = PropertiesManipulator.readProperties(new FileInputStream(new File(aMaster)));
119 catch (Throwable t) {
120 System.out.println("Unable to read master properties: " + t.getMessage());
125 slave = PropertiesManipulator.readProperties(new FileInputStream(new File(aSlave)));
127 catch (FileNotFoundException t) {
128 slave = new PropertiesManipulator();
130 catch (Throwable t) {
131 System.out.println("Unable to read slave properties: " + t.getMessage());
135 result = new PropertiesManipulator();
137 // skip past the header in the slave bundle
138 Iterator i = slave.getEntries();
139 while (i.hasNext()) {
142 if (e instanceof PropertiesManipulator.EmptyLine) {
143 result.addEmptyLine();
145 else if (e instanceof PropertiesManipulator.Comment) {
146 result.addComment( ( (PropertiesManipulator.Comment) e).getComment());
149 if (! (e instanceof PropertiesManipulator.Comment))
153 boolean insideHeader = true;
154 i = master.getEntries();
155 while (i.hasNext()) {
158 if (!insideHeader && (e instanceof PropertiesManipulator.EmptyLine)) {
159 result.addEmptyLine();
161 else if (!insideHeader && e instanceof PropertiesManipulator.Comment) {
162 result.addComment( ( (PropertiesManipulator.Comment) e).getComment());
164 else if (e instanceof PropertiesManipulator.Entry) {
165 String key = ( (PropertiesManipulator.Entry) e).getKey();
166 String value = slave.get(key);
168 if (value==null || value.length()==0) {
169 result.addComment("# missing (master value = \"" +master.get(key)+"\")");
172 result.addEntry(key, value);
175 insideHeader = insideHeader && (e instanceof PropertiesManipulator.Comment);
179 PropertiesManipulator.writeProperties(result, new FileOutputStream(new File(aSlave)));
181 catch (Throwable t) {
182 System.out.println("Unable to write slave properties: " + t.getMessage());
187 public static void encode(String aBundle, String anEncoding, String anOutputFile) {
188 PropertiesManipulator bundle;
191 bundle = PropertiesManipulator.readProperties(new FileInputStream(new File(aBundle)));
193 PropertiesManipulator.writeProperties(bundle, new FileOutputStream(anOutputFile), anEncoding, false);
195 catch (Throwable t) {
196 System.out.println("Unable to read master properties: " + t.getMessage());
201 public static void decode(String aBundle, String anEncoding, String aSourceFile) {
202 PropertiesManipulator bundle;
205 bundle = PropertiesManipulator.readProperties(new FileInputStream(new File(aSourceFile)), anEncoding);
207 catch (Throwable t) {
208 Throwable s = ExceptionFunctions.traceCauseException(t);
210 System.out.println("Unable to read sourcefile: " + s.toString());
214 PropertiesManipulator.writeProperties(bundle, new FileOutputStream(aBundle));
216 catch (Throwable t) {
217 System.out.println("Unable to write bundle: " + t.toString());
222 public static void main(String[] anArguments) {
223 String command = "help";
225 if (anArguments.length >= 1) {
226 command = anArguments[0];
228 if (command.equals("compare")) {
229 if (anArguments.length==3) {
230 compare(anArguments[1], anArguments[2]);
235 else if (command.equals("align")) {
236 if (anArguments.length==3) {
237 align(anArguments[1], anArguments[2]);
242 else if (command.equals("encode")) {
243 if (anArguments.length==4) {
244 encode(anArguments[1], anArguments[2], anArguments[3]);
249 else if (command.equals("decode")) {
250 if (anArguments.length==4) {
251 decode(anArguments[1], anArguments[2], anArguments[3]);
260 System.out.println("Usage:");
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.");