]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Tools.java
a5345019ea2bb20e616265591dbcb3f84453c124
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / common / Tools.java
1 /** @file
2
3 The file is used to provides some useful interfaces
4
5 Copyright (c) 2006, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 package org.tianocore.frameworkwizard.common;
17
18 import java.io.File;
19 import java.text.SimpleDateFormat;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.UUID;
23 import java.util.Vector;
24
25 import javax.swing.DefaultListModel;
26 import javax.swing.JComboBox;
27 import javax.swing.JList;
28 import javax.swing.JOptionPane;
29
30 import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;
31 import org.tianocore.MsaHeaderDocument.MsaHeader;
32 import org.tianocore.PackageSurfaceAreaDocument.PackageSurfaceArea;
33 import org.tianocore.PlatformHeaderDocument.PlatformHeader;
34 import org.tianocore.PlatformSurfaceAreaDocument.PlatformSurfaceArea;
35 import org.tianocore.SpdHeaderDocument.SpdHeader;
36 import org.tianocore.frameworkwizard.module.Identifications.ModuleIdentification;
37 import org.tianocore.frameworkwizard.packaging.PackageIdentification;
38 import org.tianocore.frameworkwizard.platform.PlatformIdentification;
39
40 /**
41 The class is used to provides some useful interfaces
42
43 **/
44 public class Tools {
45
46 //
47 // The dir user selected to create new package in
48 //
49 public static String dirForNewSpd = null;
50
51 /**
52 Get current date and time and format it as "yyyy-MM-dd HH:mm"
53
54 @return formatted current date and time
55
56 **/
57 public static String getCurrentDateTime() {
58 Date now = new Date(System.currentTimeMillis());
59 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
60 return sdf.format(now);
61 }
62
63 /**
64 Generate a UUID
65
66 @return the created UUID
67
68 **/
69 public static String generateUuidString() {
70 return UUID.randomUUID().toString();
71 }
72
73 /**
74 Use current file separator in the path
75
76 @param strPath
77 @return
78
79 **/
80 public static String convertPathToCurrentOsType(String strPath) {
81 strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.FILE_SEPARATOR);
82 strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.FILE_SEPARATOR);
83 return strPath;
84 }
85
86 /**
87 Use Unix file separator in the path
88
89 @param strPath
90 @return
91
92 **/
93 public static String convertPathToUnixType(String strPath) {
94 strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.UNIX_FILE_SEPARATOR);
95 return strPath;
96 }
97
98 /**
99 Use Dos file separator in the path
100
101 @param strPath
102 @return
103
104 **/
105 public static String convertPathToDosType(String strPath) {
106 strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.DOS_FILE_SEPARATOR);
107 return strPath;
108 }
109
110 /**
111 Get all system properties and output to the console
112
113 **/
114 public static void getSystemProperties() {
115 System.out.println(System.getProperty("java.class.version"));
116 System.out.println(System.getProperty("java.class.path"));
117 System.out.println(System.getProperty("java.ext.dirs"));
118 System.out.println(System.getProperty("os.name"));
119 System.out.println(System.getProperty("os.arch"));
120 System.out.println(System.getProperty("os.version"));
121 System.out.println(System.getProperty("file.separator"));
122 System.out.println(System.getProperty("path.separator"));
123 System.out.println(System.getProperty("line.separator"));
124 System.out.println(System.getProperty("user.name"));
125 System.out.println(System.getProperty("user.home"));
126 System.out.println(System.getProperty("user.dir"));
127 System.out.println(System.getProperty("PATH"));
128
129 System.out.println(System.getenv("PROCESSOR_REVISION"));
130 }
131
132 /**
133 Generate selection items for JComboBox by input vector
134
135 **/
136 public static void generateComboBoxByVector(JComboBox jcb, Vector<String> vector) {
137 if (jcb != null) {
138 jcb.removeAllItems();
139 }
140 if (vector != null) {
141 for (int index = 0; index < vector.size(); index++) {
142 jcb.addItem(vector.elementAt(index));
143 }
144 }
145 }
146
147 /**
148 Generate selection items for JList by input vector
149
150 **/
151 public static void generateListByVector(JList jl, Vector<String> vector) {
152 if (jl != null) {
153 DefaultListModel listModel = (DefaultListModel) jl.getModel();
154 listModel.removeAllElements();
155
156 if (vector != null) {
157 for (int index = 0; index < vector.size(); index++) {
158 listModel.addElement(vector.get(index));
159 }
160 }
161
162 if (listModel.size() > 0) {
163 jl.setSelectedIndex(0);
164 }
165 }
166 }
167
168 /**
169 Get path only from a path
170
171 @param filePath
172 @return
173
174 **/
175 public static String getFilePathOnly(String filePath) {
176 String path = filePath.substring(0, filePath.length() - getFileNameOnly(filePath).length());
177 if (path.endsWith(DataType.FILE_SEPARATOR)) {
178 path = path.substring(0, path.length() - DataType.FILE_SEPARATOR.length());
179 }
180
181 return path;
182 }
183
184 /**
185 Get file name from a path
186
187 @param filePath
188 @return
189
190 **/
191 public static String getFileNameOnly(String filePath) {
192 File f = new File(filePath);
193 return f.getAbsoluteFile().getName();
194 }
195
196 public static String getFileNameWithoutExt(String filePath) {
197 filePath = getFileNameOnly(filePath);
198 filePath = filePath.substring(0, filePath.lastIndexOf(DataType.FILE_EXT_SEPARATOR));
199 return filePath;
200 }
201
202 /**
203 Get relative path
204
205 @param wholePath
206 @param commonPath
207 @return wholePath - commonPath
208
209 **/
210 public static String getRelativePath(String wholePath, String commonPath) {
211 String path = "";
212 int i = 0;
213 i = wholePath.indexOf(commonPath);
214 if (i > -1) {
215 i = i + commonPath.length();
216 } else {
217 return "";
218 }
219 path = wholePath.substring(i);
220 //
221 // remove file separator of head
222 //
223 if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == 0) {
224 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());
225 }
226 if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == 0) {
227 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());
228 }
229 //
230 // remove file separator of rear
231 //
232 if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == path.length() - DataType.DOS_FILE_SEPARATOR.length()) {
233 path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());
234 }
235 if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == path.length() - DataType.UNIX_FILE_SEPARATOR.length()) {
236 path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());
237 }
238 //
239 // convert to UNIX format
240 //
241 path = Tools.convertPathToUnixType(path);
242 return path;
243 }
244
245 /**
246 Convert List ot Vector
247
248 @param list
249 @return
250
251 **/
252 public static Vector<String> convertListToVector(List list) {
253 Vector<String> v = new Vector<String>();
254 if (list != null && list.size() > 0) {
255 for (int index = 0; index < list.size(); index++) {
256 v.addElement(list.get(index).toString());
257 }
258 }
259 return v;
260 }
261
262 /**
263 If the input path missing ext, append the ext to the path
264
265 @param path
266 @param type
267 @return
268
269 **/
270 public static String addPathExt(String path, int type) {
271 String match = "";
272 if (type == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
273 match = DataType.FILE_EXT_SEPARATOR + DataType.MODULE_SURFACE_AREA_EXT;
274 }
275 if (type == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
276 match = DataType.FILE_EXT_SEPARATOR + DataType.PACKAGE_SURFACE_AREA_EXT;
277 }
278 if (type == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
279 match = DataType.FILE_EXT_SEPARATOR + DataType.PLATFORM_SURFACE_AREA_EXT;
280 }
281 if (type == DataType.RETURN_TYPE_TEXT) {
282 match = DataType.FILE_EXT_SEPARATOR + DataType.TEXT_FILE_EXT;
283 }
284 if (type == DataType.RETURN_TYPE_FAR_SURFACE_AREA) {
285 match = DataType.FILE_EXT_SEPARATOR + DataType.FAR_SURFACE_AREA_EXT;
286 }
287 if (path.length() <= match.length()) {
288 path = path + match;
289 return path;
290 }
291 if (!(path.substring(path.length() - match.length())).equals(match)) {
292 path = path + match;
293 }
294 return path;
295 }
296
297 /**
298 Show a message box
299
300 @param arg0
301
302 **/
303 public static void showInformationMessage(String arg0) {
304 JOptionPane.showConfirmDialog(null, arg0, "Info", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
305 }
306
307 /**
308 if the string doesn't end with a file separator, append it to the string
309
310 @param arg0
311 @return
312
313 **/
314 public static String addFileSeparator(String arg0) {
315 if (!arg0.endsWith(DataType.FILE_SEPARATOR)) {
316 arg0 = arg0 + DataType.FILE_SEPARATOR;
317 }
318 return arg0;
319 }
320
321 /**
322 Wrap single line long input string to multiple short line string by word
323
324 @param arg0 input string
325 @return wraped string
326
327 **/
328 public static String wrapStringByWord(String arg0) {
329 int intMaxLength = 40;
330 String strReturn = "";
331 String strTemp = "";
332 boolean isCopied = true;
333
334 //
335 // Convert string to array by " "
336 //
337 String s[] = arg0.split(" ");
338 if (arg0.indexOf(" ") == -1) {
339 s[0] = arg0;
340 }
341
342 //
343 // Add each string of array one by one
344 //
345 for (int index = 0; index < s.length; index++) {
346 String ss = s[index];
347 isCopied = false;
348 //
349 // The word length > defined line length
350 //
351 if (ss.length() > intMaxLength) {
352 //
353 // Finish previous line
354 //
355 if (!isCopied) {
356 strReturn = strReturn + strTemp + DataType.UNIX_LINE_SEPARATOR;
357 strTemp = "";
358 }
359 //
360 // Separater to short lines
361 //
362 while (ss.length() > 0) {
363 if (ss.length() > intMaxLength) {
364 strReturn = strReturn + s[index].substring(0, intMaxLength - 1) + DataType.UNIX_LINE_SEPARATOR;
365 ss = ss.substring(intMaxLength);
366 isCopied = true;
367 } else {
368 strTemp = ss;
369 ss = "";
370 isCopied = false;
371 }
372 }
373 } else {
374 if ((strTemp + " " + ss).length() <= intMaxLength) {
375 strTemp = strTemp + " " + ss;
376 continue;
377 } else {
378 strReturn = strReturn + strTemp + DataType.UNIX_LINE_SEPARATOR;
379 strTemp = ss + " ";
380 isCopied = true;
381 }
382 }
383 }
384
385 if (!isCopied) {
386 strReturn = strReturn + strTemp;
387 }
388
389 return strReturn;
390 }
391
392 public static String convertUnicodeHexStringToString(String str) {
393 //
394 // Handle if str is null or empty
395 //
396 if (str == null) {
397 return "";
398 }
399 if (str.equals("")) {
400 return "";
401 }
402
403 String returnString = "";
404 String[] strArray = str.split(" ");
405 for (int index = 0; index < strArray.length; index++) {
406 String s = strArray[index];
407 if (s.length() == 6 && s.indexOf(DataType.HEX_STRING_HEADER) == 0) {
408 s = s.substring(DataType.HEX_STRING_HEADER.length());
409 } else {
410 Log.err("convertUnicodeHexStringToString", "Wrong input string: " + str);
411 continue;
412 }
413 //
414 // Change hex to dec
415 //
416 int dec = Integer.parseInt(s, 16);
417
418 returnString = returnString + (char)(dec);
419 }
420 return returnString;
421 }
422
423 /**
424 Convert input string to unicode hex string
425
426 @param str input string
427 @return unicode hex string
428
429 **/
430 public static String convertStringToUnicodeHexString(String str) {
431 //
432 // Handle if str is null or empty
433 //
434 if (str == null) {
435 return "";
436 }
437 if (str.equals("")) {
438 return "";
439 }
440
441 //
442 // convert string to hex string
443 //
444 String hexString = "";
445 for (int index = 0; index < str.length(); index++) {
446 int codePoint = str.codePointAt(index);
447 String s = Integer.toHexString(codePoint);
448 //
449 // Make the string to four length
450 //
451 if (s.length() == 3) {
452 s = "0" + s;
453 } else if (s.length() == 2) {
454 s = "00" + s;
455 } else if (s.length() == 1) {
456 s = "000" + s;
457 }
458
459 //
460 // Add the string to return hex string
461 //
462 hexString = hexString + DataType.HEX_STRING_HEADER + s + " ";
463 }
464
465 //
466 // return hex string
467 //
468 return hexString.trim();
469 }
470
471 public static ModuleIdentification getId(String path, ModuleSurfaceArea msa) {
472 MsaHeader head = msa.getMsaHeader();
473 String name = head.getModuleName();
474 String guid = head.getGuidValue();
475 String version = head.getVersion();
476 ModuleIdentification id = new ModuleIdentification(name, guid, version, path);
477 return id;
478 }
479
480 public static PackageIdentification getId(String path, PackageSurfaceArea spd) {
481 SpdHeader head = spd.getSpdHeader();
482 String name = head.getPackageName();
483 String guid = head.getGuidValue();
484 String version = head.getVersion();
485 PackageIdentification id = new PackageIdentification(name, guid, version, path);
486 return id;
487 }
488
489 public static PlatformIdentification getId(String path, PlatformSurfaceArea fpd) {
490 PlatformHeader head = fpd.getPlatformHeader();
491 String name = head.getPlatformName();
492 String guid = head.getGuidValue();
493 String version = head.getVersion();
494 PlatformIdentification id = new PlatformIdentification(name, guid, version, path);
495 return id;
496 }
497 }