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