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