]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Tools.java
1. Fix bug for wrong Variable name in msa file
[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 /**
31 The class is used to provides some useful interfaces
32
33 **/
34 public class Tools {
35
36 //
37 // The dir user selected to create new package in
38 //
39 public static String dirForNewSpd = null;
40
41 /**
42 Get current date and time and format it as "yyyy-MM-dd HH:mm"
43
44 @return formatted current date and time
45
46 **/
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);
51 }
52
53 /**
54 Generate a UUID
55
56 @return the created UUID
57
58 **/
59 public static String generateUuidString() {
60 return UUID.randomUUID().toString();
61 }
62
63 /**
64 Use current file separator in the path
65
66 @param strPath
67 @return
68
69 **/
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);
73 return strPath;
74 }
75
76 /**
77 Use Unix file separator in the path
78
79 @param strPath
80 @return
81
82 **/
83 public static String convertPathToUnixType(String strPath) {
84 strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.UNIX_FILE_SEPARATOR);
85 return strPath;
86 }
87
88 /**
89 Use Dos file separator in the path
90
91 @param strPath
92 @return
93
94 **/
95 public static String convertPathToDosType(String strPath) {
96 strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.DOS_FILE_SEPARATOR);
97 return strPath;
98 }
99
100 /**
101 Get all system properties and output to the console
102
103 **/
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"));
118
119 System.out.println(System.getenv("PROCESSOR_REVISION"));
120 }
121
122 /**
123 Generate selection items for JComboBox by input vector
124
125 **/
126 public static void generateComboBoxByVector(JComboBox jcb, Vector<String> vector) {
127 if (jcb != null) {
128 jcb.removeAllItems();
129 }
130 if (vector != null) {
131 for (int index = 0; index < vector.size(); index++) {
132 jcb.addItem(vector.elementAt(index));
133 }
134 }
135 }
136
137 /**
138 Generate selection items for JList by input vector
139
140 **/
141 public static void generateListByVector(JList jl, Vector<String> vector) {
142 if (jl != null) {
143 DefaultListModel listModel = (DefaultListModel) jl.getModel();
144 listModel.removeAllElements();
145
146 if (vector != null) {
147 for (int index = 0; index < vector.size(); index++) {
148 listModel.addElement(vector.get(index));
149 }
150 }
151
152 if (listModel.size() > 0) {
153 jl.setSelectedIndex(0);
154 }
155 }
156 }
157
158 /**
159 Get path only from a path
160
161 @param filePath
162 @return
163
164 **/
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());
169 }
170
171 return path;
172 }
173
174 /**
175 Get file name from a path
176
177 @param filePath
178 @return
179
180 **/
181 public static String getFileNameOnly(String filePath) {
182 File f = new File(filePath);
183 return f.getAbsoluteFile().getName();
184 }
185
186 public static String getFileNameWithoutExt(String filePath) {
187 filePath = getFileNameOnly(filePath);
188 filePath = filePath.substring(0, filePath.lastIndexOf(DataType.FILE_EXT_SEPARATOR));
189 return filePath;
190 }
191
192 /**
193 Get relative path
194
195 @param wholePath
196 @param commonPath
197 @return wholePath - commonPath
198
199 **/
200 public static String getRelativePath(String wholePath, String commonPath) {
201 String path = "";
202 int i = 0;
203 i = wholePath.indexOf(commonPath);
204 if (i > -1) {
205 i = i + commonPath.length();
206 } else {
207 return "";
208 }
209 path = wholePath.substring(i);
210 //
211 // remove file separator of head
212 //
213 if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == 0) {
214 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());
215 }
216 if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == 0) {
217 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());
218 }
219 //
220 // remove file separator of rear
221 //
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());
224 }
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());
227 }
228 //
229 // convert to UNIX format
230 //
231 path = Tools.convertPathToUnixType(path);
232 return path;
233 }
234
235 /**
236 Convert List ot Vector
237
238 @param list
239 @return
240
241 **/
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());
247 }
248 }
249 return v;
250 }
251
252 /**
253 If the input path missing ext, append the ext to the path
254
255 @param path
256 @param type
257 @return
258
259 **/
260 public static String addPathExt(String path, int type) {
261 String match = "";
262 if (type == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
263 match = DataType.FILE_EXT_SEPARATOR + DataType.MODULE_SURFACE_AREA_EXT;
264 }
265 if (type == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
266 match = DataType.FILE_EXT_SEPARATOR + DataType.PACKAGE_SURFACE_AREA_EXT;
267 }
268 if (type == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
269 match = DataType.FILE_EXT_SEPARATOR + DataType.PLATFORM_SURFACE_AREA_EXT;
270 }
271 if (type == DataType.RETURN_TYPE_TEXT) {
272 match = DataType.FILE_EXT_SEPARATOR + DataType.TEXT_FILE_EXT;
273 }
274 if (type == DataType.RETURN_TYPE_FAR_SURFACE_AREA) {
275 match = DataType.FILE_EXT_SEPARATOR + DataType.FAR_SURFACE_AREA_EXT;
276 }
277 if (path.length() <= match.length()) {
278 path = path + match;
279 return path;
280 }
281 if (!(path.substring(path.length() - match.length())).equals(match)) {
282 path = path + match;
283 }
284 return path;
285 }
286
287 /**
288 Show a message box
289
290 @param arg0
291
292 **/
293 public static void showInformationMessage(String arg0) {
294 JOptionPane.showConfirmDialog(null, arg0, "Info", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
295 }
296
297 /**
298 if the string doesn't end with a file separator, append it to the string
299
300 @param arg0
301 @return
302
303 **/
304 public static String addFileSeparator(String arg0) {
305 if (!arg0.endsWith(DataType.FILE_SEPARATOR)) {
306 arg0 = arg0 + DataType.FILE_SEPARATOR;
307 }
308 return arg0;
309 }
310
311 /**
312 Wrap single line long input string to multiple short line string by word
313
314 @param arg0 input string
315 @return wraped string
316
317 **/
318 public static String wrapStringByWord(String arg0) {
319 int intMaxLength = 40;
320 String strReturn = "";
321 String strTemp = "";
322 boolean isCopied = true;
323
324 //
325 // Convert string to array by " "
326 //
327 String s[] = arg0.split(" ");
328 if (arg0.indexOf(" ") == -1) {
329 s[0] = arg0;
330 }
331
332 //
333 // Add each string of array one by one
334 //
335 for (int index = 0; index < s.length; index++) {
336 String ss = s[index];
337 isCopied = false;
338 //
339 // The word length > defined line length
340 //
341 if (ss.length() > intMaxLength) {
342 //
343 // Finish previous line
344 //
345 if (!isCopied) {
346 strReturn = strReturn + strTemp + DataType.UNIX_LINE_SEPARATOR;
347 strTemp = "";
348 }
349 //
350 // Separater to short lines
351 //
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);
356 isCopied = true;
357 } else {
358 strTemp = ss;
359 ss = "";
360 isCopied = false;
361 }
362 }
363 } else {
364 if ((strTemp + " " + ss).length() <= intMaxLength) {
365 strTemp = strTemp + " " + ss;
366 continue;
367 } else {
368 strReturn = strReturn + strTemp + DataType.UNIX_LINE_SEPARATOR;
369 strTemp = ss + " ";
370 isCopied = true;
371 }
372 }
373 }
374
375 if (!isCopied) {
376 strReturn = strReturn + strTemp;
377 }
378
379 return strReturn;
380 }
381
382 public static String convertUnicodeHexStringToString(String str) {
383 //
384 // Handle if str is null or empty
385 //
386 if (str == null) {
387 return "";
388 }
389 if (str.equals("")) {
390 return "";
391 }
392
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());
399 } else {
400 Log.err("convertUnicodeHexStringToString", "Wrong input string: " + str);
401 continue;
402 }
403 //
404 // Change hex to dec
405 //
406 int dec = Integer.parseInt(s, 16);
407
408 returnString = returnString + (char)(dec);
409 }
410 return returnString;
411 }
412
413 /**
414 Convert input string to unicode hex string
415
416 @param str input string
417 @return unicode hex string
418
419 **/
420 public static String convertStringToUnicodeHexString(String str) {
421 //
422 // Handle if str is null or empty
423 //
424 if (str == null) {
425 return "";
426 }
427 if (str.equals("")) {
428 return "";
429 }
430
431 //
432 // convert string to hex string
433 //
434 String hexString = "";
435 for (int index = 0; index < str.length(); index++) {
436 int codePoint = str.codePointAt(index);
437 String s = Integer.toHexString(codePoint);
438 //
439 // Make the string to four length
440 //
441 if (s.length() == 3) {
442 s = "0" + s;
443 } else if (s.length() == 2) {
444 s = "00" + s;
445 } else if (s.length() == 1) {
446 s = "000" + s;
447 }
448
449 //
450 // Add the string to return hex string
451 //
452 hexString = hexString + DataType.HEX_STRING_HEADER + s + " ";
453 }
454
455 //
456 // return hex string
457 //
458 return hexString.trim();
459 }
460 }