3 The file is used to provides some useful interfaces
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
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.
16 package org
.tianocore
.frameworkwizard
.common
;
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
;
25 import javax
.swing
.DefaultListModel
;
26 import javax
.swing
.JComboBox
;
27 import javax
.swing
.JList
;
28 import javax
.swing
.JOptionPane
;
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
;
41 The class is used to provides some useful interfaces
47 // The dir user selected to create new package in
49 public static String dirForNewSpd
= null;
52 Get current date and time and format it as "yyyy-MM-dd HH:mm"
54 @return formatted current date and time
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
);
66 @return the created UUID
69 public static String
generateUuidString() {
70 return UUID
.randomUUID().toString();
74 Use current file separator in the path
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
);
87 Use Unix file separator in the path
93 public static String
convertPathToUnixType(String strPath
) {
94 strPath
= strPath
.replace(DataType
.DOS_FILE_SEPARATOR
, DataType
.UNIX_FILE_SEPARATOR
);
99 Use Dos file separator in the path
105 public static String
convertPathToDosType(String strPath
) {
106 strPath
= strPath
.replace(DataType
.UNIX_FILE_SEPARATOR
, DataType
.DOS_FILE_SEPARATOR
);
111 Get all system properties and output to the console
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"));
129 System
.out
.println(System
.getenv("PROCESSOR_REVISION"));
133 Generate selection items for JComboBox by input vector
136 public static void generateComboBoxByVector(JComboBox jcb
, Vector
<String
> vector
) {
138 jcb
.removeAllItems();
140 if (vector
!= null) {
141 for (int index
= 0; index
< vector
.size(); index
++) {
142 jcb
.addItem(vector
.elementAt(index
));
148 Generate selection items for JList by input vector
151 public static void generateListByVector(JList jl
, Vector
<String
> vector
) {
153 DefaultListModel listModel
= (DefaultListModel
) jl
.getModel();
154 listModel
.removeAllElements();
156 if (vector
!= null) {
157 for (int index
= 0; index
< vector
.size(); index
++) {
158 listModel
.addElement(vector
.get(index
));
162 if (listModel
.size() > 0) {
163 jl
.setSelectedIndex(0);
169 Get path only from a path
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());
185 Get file name from a path
191 public static String
getFileNameOnly(String filePath
) {
192 File f
= new File(filePath
);
193 return f
.getAbsoluteFile().getName();
196 public static String
getFileNameWithoutExt(String filePath
) {
197 filePath
= getFileNameOnly(filePath
);
198 filePath
= filePath
.substring(0, filePath
.lastIndexOf(DataType
.FILE_EXT_SEPARATOR
));
207 @return wholePath - commonPath
210 public static String
getRelativePath(String wholePath
, String commonPath
) {
213 i
= wholePath
.indexOf(commonPath
);
215 i
= i
+ commonPath
.length();
219 path
= wholePath
.substring(i
);
221 // remove file separator of head
223 if (path
.indexOf(DataType
.DOS_FILE_SEPARATOR
) == 0) {
224 path
= path
.substring(0 + DataType
.DOS_FILE_SEPARATOR
.length());
226 if (path
.indexOf(DataType
.UNIX_FILE_SEPARATOR
) == 0) {
227 path
= path
.substring(0 + DataType
.DOS_FILE_SEPARATOR
.length());
230 // remove file separator of rear
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());
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());
239 // convert to UNIX format
241 path
= Tools
.convertPathToUnixType(path
);
246 Convert List ot Vector
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());
263 If the input path missing ext, append the ext to the path
270 public static String
addPathExt(String path
, int type
) {
272 if (type
== DataType
.RETURN_TYPE_MODULE_SURFACE_AREA
) {
273 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.MODULE_SURFACE_AREA_EXT
;
275 if (type
== DataType
.RETURN_TYPE_PACKAGE_SURFACE_AREA
) {
276 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PACKAGE_SURFACE_AREA_EXT
;
278 if (type
== DataType
.RETURN_TYPE_PLATFORM_SURFACE_AREA
) {
279 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PLATFORM_SURFACE_AREA_EXT
;
281 if (type
== DataType
.RETURN_TYPE_TEXT
) {
282 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.TEXT_FILE_EXT
;
284 if (type
== DataType
.RETURN_TYPE_FAR_SURFACE_AREA
) {
285 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.FAR_SURFACE_AREA_EXT
;
287 if (path
.length() <= match
.length()) {
291 if (!(path
.substring(path
.length() - match
.length())).equals(match
)) {
303 public static void showInformationMessage(String arg0
) {
304 JOptionPane
.showConfirmDialog(null, arg0
, "Info", JOptionPane
.DEFAULT_OPTION
, JOptionPane
.INFORMATION_MESSAGE
);
308 if the string doesn't end with a file separator, append it to the string
314 public static String
addFileSeparator(String arg0
) {
315 if (!arg0
.endsWith(DataType
.FILE_SEPARATOR
)) {
316 arg0
= arg0
+ DataType
.FILE_SEPARATOR
;
322 Wrap single line long input string to multiple short line string by word
324 @param arg0 input string
325 @return wraped string
328 public static String
wrapStringByWord(String arg0
) {
329 int intMaxLength
= 40;
330 String strReturn
= "";
332 boolean isCopied
= true;
335 // Convert string to array by " "
337 String s
[] = arg0
.split(" ");
338 if (arg0
.indexOf(" ") == -1) {
343 // Add each string of array one by one
345 for (int index
= 0; index
< s
.length
; index
++) {
346 String ss
= s
[index
];
349 // The word length > defined line length
351 if (ss
.length() > intMaxLength
) {
353 // Finish previous line
356 strReturn
= strReturn
+ strTemp
+ DataType
.UNIX_LINE_SEPARATOR
;
360 // Separater to short lines
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
);
374 if ((strTemp
+ " " + ss
).length() <= intMaxLength
) {
375 strTemp
= strTemp
+ " " + ss
;
378 strReturn
= strReturn
+ strTemp
+ DataType
.UNIX_LINE_SEPARATOR
;
386 strReturn
= strReturn
+ strTemp
;
392 public static String
convertUnicodeHexStringToString(String str
) {
394 // Handle if str is null or empty
399 if (str
.equals("")) {
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());
410 Log
.err("convertUnicodeHexStringToString", "Wrong input string: " + str
);
416 int dec
= Integer
.parseInt(s
, 16);
418 returnString
= returnString
+ (char)(dec
);
424 Convert input string to unicode hex string
426 @param str input string
427 @return unicode hex string
430 public static String
convertStringToUnicodeHexString(String str
) {
432 // Handle if str is null or empty
437 if (str
.equals("")) {
442 // convert string to hex string
444 String hexString
= "";
445 for (int index
= 0; index
< str
.length(); index
++) {
446 int codePoint
= str
.codePointAt(index
);
447 String s
= Integer
.toHexString(codePoint
);
449 // Make the string to four length
451 if (s
.length() == 3) {
453 } else if (s
.length() == 2) {
455 } else if (s
.length() == 1) {
460 // Add the string to return hex string
462 hexString
= hexString
+ DataType
.HEX_STRING_HEADER
+ s
+ " ";
468 return hexString
.trim();
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
);
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
);
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
);