2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with the com.oreilly.servlet library, any library
\r
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
\r
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
\r
24 * the above that use the same license as the above), and distribute linked
\r
25 * combinations including the two. You must obey the GNU General Public
\r
26 * License in all respects for all of the code used other than the above
\r
27 * mentioned libraries. If you modify this file, you may extend this exception
\r
28 * to your version of the file, but you are not obligated to do so. If you do
\r
29 * not wish to do so, delete this exception statement from your version.
\r
36 import org.apache.commons.collections.*;
\r
40 public class BundleTool {
\r
42 public static void compare(String aMaster, String aSlave) {
\r
43 PropertiesManipulator master;
\r
44 PropertiesManipulator slave;
\r
45 PropertiesManipulator result;
\r
48 master = PropertiesManipulator.readProperties(new FileInputStream(new File(aMaster)));
\r
50 catch (Throwable t) {
\r
51 System.out.println("Unable to read master properties: " + t.getMessage());
\r
56 slave = PropertiesManipulator.readProperties(new FileInputStream(new File(aSlave)));
\r
58 catch (FileNotFoundException t) {
\r
59 slave = new PropertiesManipulator();
\r
61 catch (Throwable t) {
\r
62 System.out.println("Unable to read slave properties: " + t.getMessage());
\r
68 Iterator i = master.getEntries();
\r
69 while (i.hasNext()) {
\r
70 Object e = i.next();
\r
72 if (e instanceof PropertiesManipulator.Entry) {
\r
73 String key = ( (PropertiesManipulator.Entry) e).getKey();
\r
75 if (!slave.containsKey(key) || slave.get(key) == null || slave.get(key).length()==0 ) {
\r
77 System.out.println(aSlave+" is missing:");
\r
79 System.out.println(" " + key);
\r
86 System.out.println("total missing: " +missing);
\r
89 i = slave.getEntries();
\r
90 while (i.hasNext()) {
\r
91 Object e = i.next();
\r
93 if (e instanceof PropertiesManipulator.Entry) {
\r
94 String key = ( (PropertiesManipulator.Entry) e).getKey();
\r
96 if (!master.containsKey(key)) {
\r
98 System.out.println(aSlave+" has extra:");
\r
100 System.out.println(" " + key);
\r
106 System.out.println("total extra: " +missing);
\r
109 public static void align(String aMaster, String aSlave) {
\r
110 PropertiesManipulator master;
\r
111 PropertiesManipulator slave;
\r
112 PropertiesManipulator result;
\r
115 master = PropertiesManipulator.readProperties(new FileInputStream(new File(aMaster)));
\r
117 catch (Throwable t) {
\r
118 System.out.println("Unable to read master properties: " + t.getMessage());
\r
123 slave = PropertiesManipulator.readProperties(new FileInputStream(new File(aSlave)));
\r
125 catch (FileNotFoundException t) {
\r
126 slave = new PropertiesManipulator();
\r
128 catch (Throwable t) {
\r
129 System.out.println("Unable to read slave properties: " + t.getMessage());
\r
133 result = new PropertiesManipulator();
\r
135 // skip past the header in the slave bundle
\r
136 Iterator i = slave.getEntries();
\r
137 while (i.hasNext()) {
\r
138 Object e = i.next();
\r
140 if (e instanceof PropertiesManipulator.EmptyLine) {
\r
141 result.addEmptyLine();
\r
143 else if (e instanceof PropertiesManipulator.Comment) {
\r
144 result.addComment( ( (PropertiesManipulator.Comment) e).getComment());
\r
147 if (! (e instanceof PropertiesManipulator.Comment))
\r
151 boolean insideHeader = true;
\r
152 i = master.getEntries();
\r
153 while (i.hasNext()) {
\r
154 Object e = i.next();
\r
156 if (!insideHeader && (e instanceof PropertiesManipulator.EmptyLine)) {
\r
157 result.addEmptyLine();
\r
159 else if (!insideHeader && e instanceof PropertiesManipulator.Comment) {
\r
160 result.addComment( ( (PropertiesManipulator.Comment) e).getComment());
\r
162 else if (e instanceof PropertiesManipulator.Entry) {
\r
163 String key = ( (PropertiesManipulator.Entry) e).getKey();
\r
164 String value = slave.get(key);
\r
166 if (value==null || value.length()==0) {
\r
167 result.addComment("# missing (master value = \"" +master.get(key)+"\")");
\r
170 result.addEntry(key, value);
\r
173 insideHeader = insideHeader && (e instanceof PropertiesManipulator.Comment);
\r
177 PropertiesManipulator.writeProperties(result, new FileOutputStream(new File(aSlave)));
\r
179 catch (Throwable t) {
\r
180 System.out.println("Unable to write slave properties: " + t.getMessage());
\r
185 public static void encode(String aBundle, String anEncoding, String anOutputFile) {
\r
186 PropertiesManipulator bundle;
\r
189 bundle = PropertiesManipulator.readProperties(new FileInputStream(new File(aBundle)));
\r
191 PropertiesManipulator.writeProperties(bundle, new FileOutputStream(anOutputFile), anEncoding, false);
\r
193 catch (Throwable t) {
\r
194 System.out.println("Unable to read master properties: " + t.getMessage());
\r
199 public static void decode(String aBundle, String anEncoding, String aSourceFile) {
\r
200 PropertiesManipulator bundle;
\r
203 bundle = PropertiesManipulator.readProperties(new FileInputStream(new File(aSourceFile)), anEncoding);
\r
205 catch (Throwable t) {
\r
206 Throwable s = ExceptionFunctions.traceCauseException(t);
\r
208 System.out.println("Unable to read sourcefile: " + s.toString());
\r
212 PropertiesManipulator.writeProperties(bundle, new FileOutputStream(aBundle));
\r
214 catch (Throwable t) {
\r
215 System.out.println("Unable to write bundle: " + t.toString());
\r
220 public static void main(String[] anArguments) {
\r
221 String command = "help";
\r
223 if (anArguments.length >= 1) {
\r
224 command = anArguments[0];
\r
226 if (command.equals("compare")) {
\r
227 if (anArguments.length==3) {
\r
228 compare(anArguments[1], anArguments[2]);
\r
233 else if (command.equals("align")) {
\r
234 if (anArguments.length==3) {
\r
235 align(anArguments[1], anArguments[2]);
\r
240 else if (command.equals("encode")) {
\r
241 if (anArguments.length==4) {
\r
242 encode(anArguments[1], anArguments[2], anArguments[3]);
\r
247 else if (command.equals("decode")) {
\r
248 if (anArguments.length==4) {
\r
249 decode(anArguments[1], anArguments[2], anArguments[3]);
\r
258 System.out.println("Usage:");
\r
260 System.out.println(" BundleTool align <master bundle> <slave bundle>");
\r
261 System.out.println("");
\r
262 System.out.println(" Reorders keys/values in a slave bundle according to a master bundle.");
\r
263 System.out.println("");
\r
264 System.out.println(" BundleTool compare <master bundle> <slave bundle>");
\r
265 System.out.println("");
\r
266 System.out.println(" Compares availability of bundle keys.");
\r
267 System.out.println("");
\r
268 System.out.println(" BundleTool encode <bundle> <encoding> <destinationfile>");
\r
269 System.out.println("");
\r
270 System.out.println(" Encodes the keys/values with a custom encoding.");
\r
271 System.out.println("");
\r
272 System.out.println(" BundleTool decode <bundle> <encoding> <sourcefile>");
\r
273 System.out.println("");
\r
274 System.out.println(" Decodes the keys/values with a custom encoding.");
\r