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
;
31 The class is used to provides some useful interfaces
37 // The dir user selected to create new package in
39 public static String dirForNewSpd
= null;
42 Get current date and time and format it as "yyyy-MM-dd HH:mm"
44 @return formatted current date and time
47 public static String
getCurrentDateTime() {
48 Date now
= new Date(System
.currentTimeMillis());
49 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm");
50 return sdf
.format(now
);
56 @return the created UUID
59 public static String
generateUuidString() {
60 return UUID
.randomUUID().toString();
64 Use current file separator in the path
70 public static String
convertPathToCurrentOsType(String strPath
) {
71 strPath
= strPath
.replace(DataType
.DOS_FILE_SEPARATOR
, DataType
.FILE_SEPARATOR
);
72 strPath
= strPath
.replace(DataType
.UNIX_FILE_SEPARATOR
, DataType
.FILE_SEPARATOR
);
77 Use Unix file separator in the path
83 public static String
convertPathToUnixType(String strPath
) {
84 strPath
= strPath
.replace(DataType
.DOS_FILE_SEPARATOR
, DataType
.UNIX_FILE_SEPARATOR
);
89 Use Dos file separator in the path
95 public static String
convertPathToDosType(String strPath
) {
96 strPath
= strPath
.replace(DataType
.UNIX_FILE_SEPARATOR
, DataType
.DOS_FILE_SEPARATOR
);
101 Get all system properties and output to the console
104 public static void getSystemProperties() {
105 System
.out
.println(System
.getProperty("java.class.version"));
106 System
.out
.println(System
.getProperty("java.class.path"));
107 System
.out
.println(System
.getProperty("java.ext.dirs"));
108 System
.out
.println(System
.getProperty("os.name"));
109 System
.out
.println(System
.getProperty("os.arch"));
110 System
.out
.println(System
.getProperty("os.version"));
111 System
.out
.println(System
.getProperty("file.separator"));
112 System
.out
.println(System
.getProperty("path.separator"));
113 System
.out
.println(System
.getProperty("line.separator"));
114 System
.out
.println(System
.getProperty("user.name"));
115 System
.out
.println(System
.getProperty("user.home"));
116 System
.out
.println(System
.getProperty("user.dir"));
117 System
.out
.println(System
.getProperty("PATH"));
119 System
.out
.println(System
.getenv("PROCESSOR_REVISION"));
123 Generate selection items for JComboBox by input vector
126 public static void generateComboBoxByVector(JComboBox jcb
, Vector
<String
> vector
) {
128 jcb
.removeAllItems();
130 if (vector
!= null) {
131 for (int index
= 0; index
< vector
.size(); index
++) {
132 jcb
.addItem(vector
.elementAt(index
));
138 Generate selection items for JList by input vector
141 public static void generateListByVector(JList jl
, Vector
<String
> vector
) {
143 DefaultListModel listModel
= (DefaultListModel
) jl
.getModel();
144 listModel
.removeAllElements();
146 if (vector
!= null) {
147 for (int index
= 0; index
< vector
.size(); index
++) {
148 listModel
.addElement(vector
.get(index
));
152 if (listModel
.size() > 0) {
153 jl
.setSelectedIndex(0);
159 Get path only from a path
165 public static String
getFilePathOnly(String filePath
) {
166 String path
= filePath
.substring(0, filePath
.length() - getFileNameOnly(filePath
).length());
167 if (path
.endsWith(DataType
.FILE_SEPARATOR
)) {
168 path
= path
.substring(0, path
.length() - DataType
.FILE_SEPARATOR
.length());
175 Get file name from a path
181 public static String
getFileNameOnly(String filePath
) {
182 File f
= new File(filePath
);
183 return f
.getAbsoluteFile().getName();
186 public static String
getFileNameWithoutExt(String filePath
) {
187 filePath
= getFileNameOnly(filePath
);
188 filePath
= filePath
.substring(0, filePath
.lastIndexOf(DataType
.FILE_EXT_SEPARATOR
));
197 @return wholePath - commonPath
200 public static String
getRelativePath(String wholePath
, String commonPath
) {
203 i
= wholePath
.indexOf(commonPath
);
205 i
= i
+ commonPath
.length();
209 path
= wholePath
.substring(i
);
211 // remove file separator of head
213 if (path
.indexOf(DataType
.DOS_FILE_SEPARATOR
) == 0) {
214 path
= path
.substring(0 + DataType
.DOS_FILE_SEPARATOR
.length());
216 if (path
.indexOf(DataType
.UNIX_FILE_SEPARATOR
) == 0) {
217 path
= path
.substring(0 + DataType
.DOS_FILE_SEPARATOR
.length());
220 // remove file separator of rear
222 if (path
.indexOf(DataType
.DOS_FILE_SEPARATOR
) == path
.length() - DataType
.DOS_FILE_SEPARATOR
.length()) {
223 path
= path
.substring(0, path
.length() - DataType
.DOS_FILE_SEPARATOR
.length());
225 if (path
.indexOf(DataType
.UNIX_FILE_SEPARATOR
) == path
.length() - DataType
.UNIX_FILE_SEPARATOR
.length()) {
226 path
= path
.substring(0, path
.length() - DataType
.DOS_FILE_SEPARATOR
.length());
229 // convert to UNIX format
231 path
= Tools
.convertPathToUnixType(path
);
236 Convert List ot Vector
242 public static Vector
<String
> convertListToVector(List list
) {
243 Vector
<String
> v
= new Vector
<String
>();
244 if (list
!= null && list
.size() > 0) {
245 for (int index
= 0; index
< list
.size(); index
++) {
246 v
.addElement(list
.get(index
).toString());
253 If the input path missing ext, append the ext to the path
260 public static String
addPathExt(String path
, int type
) {
262 if (type
== DataType
.RETURN_TYPE_MODULE_SURFACE_AREA
) {
263 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.MODULE_SURFACE_AREA_EXT
;
265 if (type
== DataType
.RETURN_TYPE_PACKAGE_SURFACE_AREA
) {
266 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PACKAGE_SURFACE_AREA_EXT
;
268 if (type
== DataType
.RETURN_TYPE_PLATFORM_SURFACE_AREA
) {
269 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PLATFORM_SURFACE_AREA_EXT
;
271 if (type
== DataType
.RETURN_TYPE_TEXT
) {
272 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.TEXT_FILE_EXT
;
274 if (type
== DataType
.RETURN_TYPE_FAR_SURFACE_AREA
) {
275 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.FAR_SURFACE_AREA_EXT
;
277 if (path
.length() <= match
.length()) {
281 if (!(path
.substring(path
.length() - match
.length())).equals(match
)) {
293 public static void showInformationMessage(String arg0
) {
294 JOptionPane
.showConfirmDialog(null, arg0
, "Info", JOptionPane
.DEFAULT_OPTION
, JOptionPane
.INFORMATION_MESSAGE
);
298 if the string doesn't end with a file separator, append it to the string
304 public static String
addFileSeparator(String arg0
) {
305 if (!arg0
.endsWith(DataType
.FILE_SEPARATOR
)) {
306 arg0
= arg0
+ DataType
.FILE_SEPARATOR
;
312 Wrap single line long input string to multiple short line string by word
314 @param arg0 input string
315 @return wraped string
318 public static String
wrapStringByWord(String arg0
) {
319 int intMaxLength
= 40;
320 String strReturn
= "";
322 boolean isCopied
= true;
325 // Convert string to array by " "
327 String s
[] = arg0
.split(" ");
328 if (arg0
.indexOf(" ") == -1) {
333 // Add each string of array one by one
335 for (int index
= 0; index
< s
.length
; index
++) {
336 String ss
= s
[index
];
339 // The word length > defined line length
341 if (ss
.length() > intMaxLength
) {
343 // Finish previous line
346 strReturn
= strReturn
+ strTemp
+ DataType
.UNIX_LINE_SEPARATOR
;
350 // Separater to short lines
352 while (ss
.length() > 0) {
353 if (ss
.length() > intMaxLength
) {
354 strReturn
= strReturn
+ s
[index
].substring(0, intMaxLength
- 1) + DataType
.UNIX_LINE_SEPARATOR
;
355 ss
= ss
.substring(intMaxLength
);
364 if ((strTemp
+ " " + ss
).length() <= intMaxLength
) {
365 strTemp
= strTemp
+ " " + ss
;
368 strReturn
= strReturn
+ strTemp
+ DataType
.UNIX_LINE_SEPARATOR
;
376 strReturn
= strReturn
+ strTemp
;
382 public static String
convertUnicodeHexStringToString(String str
) {
384 // Handle if str is null or empty
389 if (str
.equals("")) {
393 String returnString
= "";
394 String
[] strArray
= str
.split(" ");
395 for (int index
= 0; index
< strArray
.length
; index
++) {
396 String s
= strArray
[index
];
397 if (s
.length() == 6 && s
.indexOf(DataType
.HEX_STRING_HEADER
) == 0) {
398 s
= s
.substring(DataType
.HEX_STRING_HEADER
.length());
400 Log
.err("convertUnicodeHexStringToString", "Wrong input string: " + str
);
406 int dec
= Integer
.parseInt(s
, 16);
408 returnString
= returnString
+ (char)(dec
);
414 Convert input string to unicode hex string
416 @param str input string
417 @return unicode hex string
420 public static String
convertStringToUnicodeHexString(String str
) {
422 // Handle if str is null or empty
427 if (str
.equals("")) {
432 // convert string to hex string
434 String hexString
= "";
435 for (int index
= 0; index
< str
.length(); index
++) {
436 int codePoint
= str
.codePointAt(index
);
437 String s
= Integer
.toHexString(codePoint
);
439 // Make the string to four length
441 if (s
.length() == 3) {
443 } else if (s
.length() == 2) {
445 } else if (s
.length() == 1) {
450 // Add the string to return hex string
452 hexString
= hexString
+ DataType
.HEX_STRING_HEADER
+ s
+ " ";
458 return hexString
.trim();