87
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
* Icon conversion utility implemented using eSWT.
|
|
16 |
*
|
|
17 |
*/
|
|
18 |
|
|
19 |
|
|
20 |
package com.nokia.mj.impl.installer.utils;
|
|
21 |
|
|
22 |
import java.io.InputStream;
|
|
23 |
import java.io.IOException;
|
|
24 |
import java.io.OutputStream;
|
|
25 |
import java.util.jar.JarFile;
|
|
26 |
import java.util.jar.JarEntry;
|
|
27 |
|
|
28 |
import org.eclipse.swt.SWT;
|
|
29 |
import org.eclipse.swt.graphics.Image;
|
|
30 |
import org.eclipse.swt.graphics.ImageData;
|
|
31 |
import org.eclipse.swt.graphics.ImageLoader;
|
|
32 |
import org.eclipse.swt.graphics.Point;
|
|
33 |
import org.eclipse.swt.internal.extension.Toolkit;
|
|
34 |
import org.eclipse.swt.widgets.Display;
|
|
35 |
|
|
36 |
/**
|
|
37 |
* Icon conversion utility implemented using eSWT.
|
|
38 |
*/
|
|
39 |
public class IconConverterEswt
|
|
40 |
{
|
|
41 |
/** List of unsupported bitmap image format filename extensions. */
|
|
42 |
private static String[] iUnsupportedBitmapFormats =
|
|
43 |
new String[] { ".wbmp", ".wmf" };
|
|
44 |
|
|
45 |
/**
|
|
46 |
* Converts icon to platform specific format.
|
|
47 |
*
|
|
48 |
* @param aInputIconFilename file name for input icon file
|
|
49 |
* @param aOutputIconFilename file name for output icon file
|
|
50 |
* @param aJarFilename jar file name if aInputIconFilename specifies
|
|
51 |
* file inside jar file,
|
|
52 |
* or null if aInputIconFilename specifies file from disk
|
|
53 |
* @param aIconSuffix the correct suffix of the icon is returned through
|
|
54 |
* this parameter, will contain '.png' or '.svg' when function returns
|
|
55 |
* @return true if the conversion succeeds
|
|
56 |
*/
|
|
57 |
public static boolean convertIcon(
|
|
58 |
String aInputIconFilename, String aOutputIconFilename,
|
|
59 |
String aJarFilename, StringBuffer aIconSuffix)
|
|
60 |
{
|
|
61 |
Log.log("IconConverterEswt.convertIcon: " + aInputIconFilename +
|
|
62 |
" to " + aOutputIconFilename + " from " + aJarFilename);
|
|
63 |
boolean result = false;
|
|
64 |
try
|
|
65 |
{
|
|
66 |
if (isSvgIcon(aInputIconFilename, aJarFilename))
|
|
67 |
{
|
|
68 |
result = convertSvgIcon(
|
|
69 |
aInputIconFilename, aOutputIconFilename,
|
|
70 |
aJarFilename, aIconSuffix);
|
|
71 |
}
|
|
72 |
else
|
|
73 |
{
|
|
74 |
result = convertBitmapIcon(
|
|
75 |
aInputIconFilename, aOutputIconFilename,
|
|
76 |
aJarFilename, aIconSuffix);
|
|
77 |
}
|
|
78 |
}
|
|
79 |
catch (Throwable t)
|
|
80 |
{
|
|
81 |
Log.logWarning("IconConverterEswt.convertIcon: Exception when " +
|
|
82 |
"converting " + aInputIconFilename + " to " +
|
|
83 |
aOutputIconFilename + " from " + aJarFilename, t);
|
|
84 |
}
|
|
85 |
return result;
|
|
86 |
}
|
|
87 |
|
|
88 |
/**
|
|
89 |
* Converts bitmap icon to platform specific format.
|
|
90 |
*
|
|
91 |
* @param aInputIconFilename file name for input icon file
|
|
92 |
* @param aOutputIconFilename file name for output icon file
|
|
93 |
* @param aJarFilename jar file name if aInputIconFilename specifies
|
|
94 |
* file inside jar file,
|
|
95 |
* or null if aInputIconFilename specifies file from disk
|
|
96 |
* @param aIconSuffix the correct suffix of the icon is returned through
|
|
97 |
* this parameter
|
|
98 |
* @return true if the conversion succeeds
|
|
99 |
*/
|
|
100 |
private static boolean convertBitmapIcon(
|
|
101 |
String aInputIconFilename, String aOutputIconFilename,
|
|
102 |
String aJarFilename, StringBuffer aIconSuffix)
|
|
103 |
{
|
|
104 |
if (!isSupportedBitmapIcon(aInputIconFilename, aJarFilename))
|
|
105 |
{
|
|
106 |
Log.logWarning("IconConverterEswt.convertBitmapIcon: unsupported " +
|
|
107 |
"image format " + aInputIconFilename + " from " +
|
|
108 |
aJarFilename + " not converted");
|
|
109 |
return false;
|
|
110 |
}
|
|
111 |
|
|
112 |
final Display display = Toolkit.getInternalDisplay();
|
|
113 |
if (display == null)
|
|
114 |
{
|
|
115 |
Log.logWarning("IconConverterEswt: Getting Display failed, " +
|
|
116 |
"icon conversion aborted for " + aInputIconFilename +
|
|
117 |
" from " + aJarFilename);
|
|
118 |
return false;
|
|
119 |
}
|
|
120 |
|
|
121 |
final Point iconSize = new Point(88, 88);
|
|
122 |
if (iconSize != null)
|
|
123 |
{
|
|
124 |
display.syncExec(new Runnable()
|
|
125 |
{
|
|
126 |
public void run()
|
|
127 |
{
|
|
128 |
display.setData("org.eclipse.swt.internal.image.loadSize",
|
|
129 |
iconSize);
|
|
130 |
}
|
|
131 |
});
|
|
132 |
}
|
|
133 |
|
|
134 |
Image image = null;
|
|
135 |
if (aJarFilename == null)
|
|
136 |
{
|
|
137 |
// Load image from file.
|
|
138 |
image = new Image(display, aInputIconFilename);
|
|
139 |
}
|
|
140 |
else
|
|
141 |
{
|
|
142 |
// Load image from jar file.
|
|
143 |
JarFile jarFile = null;
|
|
144 |
try
|
|
145 |
{
|
|
146 |
jarFile = new JarFile(aJarFilename);
|
|
147 |
InputStream inputStream = jarFile.getInputStream(
|
|
148 |
new JarEntry(FileUtils.trimJarEntry(aInputIconFilename)));
|
|
149 |
image = new Image(display, inputStream);
|
|
150 |
}
|
|
151 |
catch (IOException ioe)
|
|
152 |
{
|
|
153 |
Log.logWarning("IconConverterEswt: Getting icon " +
|
|
154 |
aInputIconFilename + " from " +
|
|
155 |
aJarFilename + " failed", ioe);
|
|
156 |
}
|
|
157 |
finally
|
|
158 |
{
|
|
159 |
if (jarFile != null)
|
|
160 |
{
|
|
161 |
try
|
|
162 |
{
|
|
163 |
jarFile.close(); // Closes also InputStream.
|
|
164 |
jarFile = null;
|
|
165 |
}
|
|
166 |
catch (IOException ioe)
|
|
167 |
{
|
|
168 |
Log.logWarning("IconConverterEswt: Closing " +
|
|
169 |
aJarFilename + " failed", ioe);
|
|
170 |
}
|
|
171 |
}
|
|
172 |
}
|
|
173 |
}
|
|
174 |
|
|
175 |
boolean result = false;
|
|
176 |
if (image != null)
|
|
177 |
{
|
|
178 |
// Save image to file.
|
|
179 |
OutputStream outputStream = null;
|
|
180 |
try
|
|
181 |
{
|
|
182 |
outputStream = FileUtils.getOutputStream(aOutputIconFilename);
|
|
183 |
ImageLoader imageLoader = new ImageLoader();
|
|
184 |
imageLoader.data = new ImageData[] { image.getImageData() };
|
|
185 |
imageLoader.save(outputStream, SWT.IMAGE_PNG);
|
|
186 |
aIconSuffix.append(".png");
|
|
187 |
result = true;
|
|
188 |
}
|
|
189 |
catch (IOException ioe)
|
|
190 |
{
|
|
191 |
Log.logWarning("IconConverterEswt: Saving icon " +
|
|
192 |
aOutputIconFilename + " failed", ioe);
|
|
193 |
}
|
|
194 |
finally
|
|
195 |
{
|
|
196 |
if (outputStream != null)
|
|
197 |
{
|
|
198 |
try
|
|
199 |
{
|
|
200 |
outputStream.close();
|
|
201 |
outputStream = null;
|
|
202 |
}
|
|
203 |
catch (IOException ioe)
|
|
204 |
{
|
|
205 |
Log.logWarning("IconConverterEswt: Closing " +
|
|
206 |
aOutputIconFilename + " failed", ioe);
|
|
207 |
}
|
|
208 |
}
|
|
209 |
}
|
|
210 |
}
|
|
211 |
return result;
|
|
212 |
}
|
|
213 |
|
|
214 |
/**
|
|
215 |
* Converts SVG icon to platform specific format.
|
|
216 |
*
|
|
217 |
* @param aInputIconFilename file name for input icon file
|
|
218 |
* @param aOutputIconFilename file name for output icon file
|
|
219 |
* @param aJarFilename jar file name if aInputIconFilename specifies
|
|
220 |
* file inside jar file,
|
|
221 |
* or null if aInputIconFilename specifies file from disk
|
|
222 |
* @param aIconSuffix the correct suffix of the icon is returned through
|
|
223 |
* this parameter
|
|
224 |
* @return true if the conversion succeeds
|
|
225 |
*/
|
|
226 |
private static boolean convertSvgIcon(
|
|
227 |
String aInputIconFilename, String aOutputIconFilename,
|
|
228 |
String aJarFilename, StringBuffer aIconSuffix)
|
|
229 |
{
|
|
230 |
boolean result = false;
|
|
231 |
JarFile jarFile = null;
|
|
232 |
InputStream inputStream = null;
|
|
233 |
OutputStream outputStream = null;
|
|
234 |
try
|
|
235 |
{
|
|
236 |
if (aJarFilename == null)
|
|
237 |
{
|
|
238 |
inputStream = FileUtils.getInputStream(aInputIconFilename);
|
|
239 |
}
|
|
240 |
else
|
|
241 |
{
|
|
242 |
jarFile = new JarFile(aJarFilename);
|
|
243 |
inputStream = jarFile.getInputStream(
|
|
244 |
new JarEntry(FileUtils.trimJarEntry(aInputIconFilename)));
|
|
245 |
}
|
|
246 |
outputStream = FileUtils.getOutputStream(aOutputIconFilename);
|
|
247 |
// Copy the image data from InputStream to OutputStream.
|
|
248 |
byte[] buf = new byte[16384];
|
|
249 |
int i = 0;
|
|
250 |
while ((i = inputStream.read(buf)) != -1)
|
|
251 |
{
|
|
252 |
outputStream.write(buf, 0, i);
|
|
253 |
}
|
|
254 |
aIconSuffix.append(".svg");
|
|
255 |
result = true;
|
|
256 |
}
|
|
257 |
catch (IOException ioe)
|
|
258 |
{
|
|
259 |
Log.logWarning("IconConverterEswt: Saving SVG icon " +
|
|
260 |
aInputIconFilename + " from " +
|
|
261 |
aJarFilename + " to " + aOutputIconFilename +
|
|
262 |
" failed", ioe);
|
|
263 |
}
|
|
264 |
finally
|
|
265 |
{
|
|
266 |
if (outputStream != null)
|
|
267 |
{
|
|
268 |
try
|
|
269 |
{
|
|
270 |
outputStream.close();
|
|
271 |
outputStream = null;
|
|
272 |
}
|
|
273 |
catch (IOException ioe)
|
|
274 |
{
|
|
275 |
Log.logWarning("IconConverterEswt: Closing " +
|
|
276 |
aOutputIconFilename + " failed", ioe);
|
|
277 |
}
|
|
278 |
}
|
|
279 |
if (inputStream != null)
|
|
280 |
{
|
|
281 |
try
|
|
282 |
{
|
|
283 |
inputStream.close();
|
|
284 |
inputStream = null;
|
|
285 |
}
|
|
286 |
catch (IOException ioe)
|
|
287 |
{
|
|
288 |
Log.logWarning("IconConverterEswt: Closing " +
|
|
289 |
aInputIconFilename + " failed", ioe);
|
|
290 |
}
|
|
291 |
}
|
|
292 |
if (jarFile != null)
|
|
293 |
{
|
|
294 |
try
|
|
295 |
{
|
|
296 |
jarFile.close();
|
|
297 |
jarFile = null;
|
|
298 |
}
|
|
299 |
catch (IOException ioe)
|
|
300 |
{
|
|
301 |
Log.logWarning("IconConverterEswt: Closing " +
|
|
302 |
aJarFilename + " failed", ioe);
|
|
303 |
}
|
|
304 |
}
|
|
305 |
}
|
|
306 |
return result;
|
|
307 |
}
|
|
308 |
|
|
309 |
/**
|
|
310 |
* Returns true if given icon file is in SVG format.
|
|
311 |
*/
|
|
312 |
private static boolean isSvgIcon(String aIconFilename, String aJarFilename)
|
|
313 |
{
|
|
314 |
boolean result = false;
|
|
315 |
// Simple file type detection from filename extension.
|
|
316 |
//if (aIconFilename.toLowerCase().endsWith(".svg"))
|
|
317 |
//{
|
|
318 |
// result = true;
|
|
319 |
//}
|
|
320 |
//return result;
|
|
321 |
|
|
322 |
// Detect SVG files by checking if file begins with "<?xml".
|
|
323 |
JarFile jarFile = null;
|
|
324 |
InputStream inputStream = null;
|
|
325 |
try
|
|
326 |
{
|
|
327 |
if (aJarFilename == null)
|
|
328 |
{
|
|
329 |
inputStream = FileUtils.getInputStream(aIconFilename);
|
|
330 |
}
|
|
331 |
else
|
|
332 |
{
|
|
333 |
jarFile = new JarFile(aJarFilename);
|
|
334 |
inputStream = jarFile.getInputStream(
|
|
335 |
new JarEntry(FileUtils.trimJarEntry(aIconFilename)));
|
|
336 |
}
|
|
337 |
byte[] bytes = new byte[5];
|
|
338 |
int readCount = inputStream.read(bytes);
|
|
339 |
if (readCount == bytes.length)
|
|
340 |
{
|
|
341 |
result = true;
|
|
342 |
byte[] xmlDecl = { '<', '?', 'x', 'm', 'l' };
|
|
343 |
for (int i = 0; i < readCount; i++)
|
|
344 |
{
|
|
345 |
if (bytes[i] != xmlDecl[i])
|
|
346 |
{
|
|
347 |
result = false;
|
|
348 |
break;
|
|
349 |
}
|
|
350 |
}
|
|
351 |
}
|
|
352 |
}
|
|
353 |
catch (IOException ioe)
|
|
354 |
{
|
|
355 |
Log.logWarning("IconConverterEswt.isSvgIcon: Checking file type of " +
|
|
356 |
aIconFilename + " from " + aJarFilename +
|
|
357 |
" failed", ioe);
|
|
358 |
}
|
|
359 |
finally
|
|
360 |
{
|
|
361 |
if (inputStream != null)
|
|
362 |
{
|
|
363 |
try
|
|
364 |
{
|
|
365 |
inputStream.close();
|
|
366 |
inputStream = null;
|
|
367 |
}
|
|
368 |
catch (IOException ioe)
|
|
369 |
{
|
|
370 |
Log.logWarning("IconConverterEswt.isSvgIcon: Closing " +
|
|
371 |
aIconFilename + " failed", ioe);
|
|
372 |
}
|
|
373 |
}
|
|
374 |
if (jarFile != null)
|
|
375 |
{
|
|
376 |
try
|
|
377 |
{
|
|
378 |
jarFile.close();
|
|
379 |
jarFile = null;
|
|
380 |
}
|
|
381 |
catch (IOException ioe)
|
|
382 |
{
|
|
383 |
Log.logWarning("IconConverterEswt.isSvgIcon: Closing " +
|
|
384 |
aJarFilename + " failed", ioe);
|
|
385 |
}
|
|
386 |
}
|
|
387 |
}
|
|
388 |
return result;
|
|
389 |
}
|
|
390 |
|
|
391 |
/**
|
|
392 |
* Returns true if given icon file is in supported bitmap format.
|
|
393 |
*/
|
|
394 |
private static boolean isSupportedBitmapIcon(
|
|
395 |
String aIconFilename, String aJarFilename)
|
|
396 |
{
|
|
397 |
boolean result = true;
|
|
398 |
for (int i = 0; i < iUnsupportedBitmapFormats.length; i++)
|
|
399 |
{
|
|
400 |
if (aIconFilename.toLowerCase().endsWith(
|
|
401 |
iUnsupportedBitmapFormats[i]))
|
|
402 |
{
|
|
403 |
result = false;
|
|
404 |
break;
|
|
405 |
}
|
|
406 |
}
|
|
407 |
return result;
|
|
408 |
}
|
|
409 |
}
|