]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Tools.java
1. Fix bug in get industry std headers
[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.frameworkwizard.module.Identifications.ModuleIdentification;
31 import org.tianocore.frameworkwizard.packaging.PackageIdentification;
32 import org.tianocore.frameworkwizard.platform.PlatformIdentification;
33
34 /**
35 The class is used to provides some useful interfaces
36
37 **/
38 public class Tools {
39
40 //
41 // The dir user selected to create new package in
42 //
43 public static String dirForNewSpd = null;
44
45 /**
46 Used for test
47
48 @param args
49
50 **/
51 public static void main(String[] args) {
52 System.out.println(getCurrentDateTime());
53 // Vector<String> v = new Vector<String>();
54 // Vector<String> v1 = new Vector<String>();
55 //
56 // v.addElement("CAC");
57 // v1.addElement("1111");
58 // v.addElement("1AC");
59 // v1.addElement("2222");
60 // v.addElement("ABC");
61 // v1.addElement("3333");
62 // v.addElement("0C");
63 // v1.addElement("4444");
64 // v.addElement("AAC");
65 // v1.addElement("5555");
66 // Vector<Integer> vs = new Vector<Integer>();
67 // vs = Tools.getVectorSortSequence(v, DataType.Sort_Type_Ascending);
68 // Tools.sortVectorString(v1, Tools.getVectorSortSequence(v, DataType.Sort_Type_Ascending));
69 //
70 // Tools.sortVectorString(v, DataType.Sort_Type_Ascending);
71 // Tools.sortVectorString(v, DataType.Sort_Type_Descending);
72 }
73
74 /**
75 Get current date and time and format it as "yyyy-MM-dd HH:mm"
76
77 @return formatted current date and time
78
79 **/
80 public static String getCurrentDateTime() {
81 Date now = new Date(System.currentTimeMillis());
82 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
83 return sdf.format(now);
84 }
85
86 /**
87 Generate a UUID
88
89 @return the created UUID
90
91 **/
92 public static String generateUuidString() {
93 return UUID.randomUUID().toString();
94 }
95
96 /**
97 Use current file separator in the path
98
99 @param strPath
100 @return
101
102 **/
103 public static String convertPathToCurrentOsType(String strPath) {
104 strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.FILE_SEPARATOR);
105 strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.FILE_SEPARATOR);
106 return strPath;
107 }
108
109 /**
110 Use Unix file separator in the path
111
112 @param strPath
113 @return
114
115 **/
116 public static String convertPathToUnixType(String strPath) {
117 strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.UNIX_FILE_SEPARATOR);
118 return strPath;
119 }
120
121 /**
122 Use Dos file separator in the path
123
124 @param strPath
125 @return
126
127 **/
128 public static String convertPathToDosType(String strPath) {
129 strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.DOS_FILE_SEPARATOR);
130 return strPath;
131 }
132
133 /**
134 Get all system properties and output to the console
135
136 **/
137 public static void getSystemProperties() {
138 System.out.println(System.getProperty("java.class.version"));
139 System.out.println(System.getProperty("java.class.path"));
140 System.out.println(System.getProperty("java.ext.dirs"));
141 System.out.println(System.getProperty("os.name"));
142 System.out.println(System.getProperty("os.arch"));
143 System.out.println(System.getProperty("os.version"));
144 System.out.println(System.getProperty("file.separator"));
145 System.out.println(System.getProperty("path.separator"));
146 System.out.println(System.getProperty("line.separator"));
147 System.out.println(System.getProperty("user.name"));
148 System.out.println(System.getProperty("user.home"));
149 System.out.println(System.getProperty("user.dir"));
150 System.out.println(System.getProperty("PATH"));
151
152 System.out.println(System.getenv("PROCESSOR_REVISION"));
153 }
154
155 /**
156 Generate selection items for JComboBox by input vector
157
158 **/
159 public static void generateComboBoxByVector(JComboBox jcb, Vector<String> vector) {
160 if (jcb != null) {
161 jcb.removeAllItems();
162 }
163 if (vector != null) {
164 for (int index = 0; index < vector.size(); index++) {
165 jcb.addItem(vector.elementAt(index));
166 }
167 }
168 }
169
170 /**
171 Generate selection items for JList by input vector
172
173 **/
174 public static void generateListByVector(JList jl, Vector<String> vector) {
175 if (jl != null) {
176 DefaultListModel listModel = (DefaultListModel) jl.getModel();
177 listModel.removeAllElements();
178
179 if (vector != null) {
180 for (int index = 0; index < vector.size(); index++) {
181 listModel.addElement(vector.get(index));
182 }
183 }
184
185 if (listModel.size() > 0) {
186 jl.setSelectedIndex(0);
187 }
188 }
189 }
190
191 /**
192 Get path only from a path
193
194 @param filePath
195 @return
196
197 **/
198 public static String getFilePathOnly(String filePath) {
199 String path = filePath.substring(0, filePath.length() - getFileNameOnly(filePath).length());
200 if (path.endsWith(DataType.FILE_SEPARATOR)) {
201 path = path.substring(0, path.length() - DataType.FILE_SEPARATOR.length());
202 }
203
204 return path;
205 }
206
207 /**
208 Get file name from a path
209
210 @param filePath
211 @return
212
213 **/
214 public static String getFileNameOnly(String filePath) {
215 File f = new File(filePath);
216 return f.getAbsoluteFile().getName();
217 }
218
219 public static String getFileNameWithoutExt(String filePath) {
220 filePath = getFileNameOnly(filePath);
221 filePath = filePath.substring(0, filePath.lastIndexOf(DataType.FILE_EXT_SEPARATOR));
222 return filePath;
223 }
224
225 /**
226 Get relative path
227
228 @param wholePath
229 @param commonPath
230 @return wholePath - commonPath
231
232 **/
233 public static String getRelativePath(String wholePath, String commonPath) {
234 String path = "";
235 int i = 0;
236 i = wholePath.indexOf(commonPath);
237 if (i > -1) {
238 i = i + commonPath.length();
239 } else {
240 return "";
241 }
242 path = wholePath.substring(i);
243 //
244 // remove file separator of head
245 //
246 if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == 0) {
247 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());
248 }
249 if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == 0) {
250 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());
251 }
252 //
253 // remove file separator of rear
254 //
255 if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == path.length() - DataType.DOS_FILE_SEPARATOR.length()) {
256 path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());
257 }
258 if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == path.length() - DataType.UNIX_FILE_SEPARATOR.length()) {
259 path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());
260 }
261 //
262 // convert to UNIX format
263 //
264 path = Tools.convertPathToUnixType(path);
265 return path;
266 }
267
268 /**
269 Convert List ot Vector
270
271 @param list
272 @return
273
274 **/
275 public static Vector<String> convertListToVector(List list) {
276 Vector<String> v = new Vector<String>();
277 if (list != null && list.size() > 0) {
278 for (int index = 0; index < list.size(); index++) {
279 v.addElement(list.get(index).toString());
280 }
281 }
282 return v;
283 }
284
285 /**
286 If the input path missing ext, append the ext to the path
287
288 @param path
289 @param type
290 @return
291
292 **/
293 public static String addPathExt(String path, int type) {
294 String match = "";
295 if (type == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
296 match = DataType.FILE_EXT_SEPARATOR + DataType.MODULE_SURFACE_AREA_EXT;
297 }
298 if (type == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
299 match = DataType.FILE_EXT_SEPARATOR + DataType.PACKAGE_SURFACE_AREA_EXT;
300 }
301 if (type == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
302 match = DataType.FILE_EXT_SEPARATOR + DataType.PLATFORM_SURFACE_AREA_EXT;
303 }
304 if (type == DataType.RETURN_TYPE_TEXT) {
305 match = DataType.FILE_EXT_SEPARATOR + DataType.TEXT_FILE_EXT;
306 }
307 if (type == DataType.RETURN_TYPE_FAR_SURFACE_AREA) {
308 match = DataType.FILE_EXT_SEPARATOR + DataType.FAR_SURFACE_AREA_EXT;
309 }
310 if (path.length() <= match.length()) {
311 path = path + match;
312 return path;
313 }
314 if (!(path.substring(path.length() - match.length())).equals(match)) {
315 path = path + match;
316 }
317 return path;
318 }
319
320 /**
321 Show a message box
322
323 @param arg0
324
325 **/
326 public static void showInformationMessage(String arg0) {
327 JOptionPane.showConfirmDialog(null, arg0, "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
328 }
329
330 /**
331 if the string doesn't end with a file separator, append it to the string
332
333 @param arg0
334 @return
335
336 **/
337 public static String addFileSeparator(String arg0) {
338 if (!arg0.endsWith(DataType.FILE_SEPARATOR)) {
339 arg0 = arg0 + DataType.FILE_SEPARATOR;
340 }
341 return arg0;
342 }
343
344 /**
345 Sort all elements in the vector as the specific sort type
346
347 @param v The vector need to be sorted
348 @param mode Sort type DataType.Sort_Type_Ascendin and DataType.Sort_Type_Descending
349
350 **/
351 public static void sortVectorString(Vector<String> v, int mode) {
352 if (v != null) {
353 for (int indexI = 0; indexI < v.size(); indexI++) {
354 for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {
355 if ((v.get(indexJ).compareTo(v.get(indexI)) < 0 && mode == DataType.SORT_TYPE_ASCENDING)
356 || (v.get(indexI).compareTo(v.get(indexJ)) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {
357 String temp = v.get(indexI);
358 v.setElementAt(v.get(indexJ), indexI);
359 v.setElementAt(temp, indexJ);
360 }
361 }
362 }
363 }
364 }
365
366 /**
367 Sort all elements of vector and return sorted sequence
368
369 @param v The vector need to be sorted
370 @param mode Sort type DataType.Sort_Type_Ascendin and DataType.Sort_Type_Descending
371 @return Vector<Integer> The sorted sequence
372
373 **/
374 public static Vector<Integer> getVectorSortSequence(Vector<String> v, int mode) {
375 Vector<Integer> vSequence = new Vector<Integer>();
376 //
377 // Init sequence
378 //
379 if (v != null) {
380 for (int index = 0; index < v.size(); index++) {
381 vSequence.addElement(index);
382 }
383 }
384
385 //
386 // sort and get new sequence
387 //
388 for (int indexI = 0; indexI < v.size(); indexI++) {
389 for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {
390 if ((v.get(indexJ).compareTo(v.get(indexI)) < 0 && mode == DataType.SORT_TYPE_ASCENDING)
391 || (v.get(indexI).compareTo(v.get(indexJ)) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {
392 //
393 // Swap strings
394 //
395 String tempStr = v.get(indexI);
396 v.setElementAt(v.get(indexJ), indexI);
397 v.setElementAt(tempStr, indexJ);
398
399 //
400 // Swap sequences
401 //
402 int tempInt = vSequence.get(indexI);
403 vSequence.setElementAt(vSequence.get(indexJ), indexI);
404 vSequence.setElementAt(tempInt, indexJ);
405 }
406 }
407 }
408
409 return vSequence;
410 }
411
412 /**
413 Sort all elements of vector as input sequence
414
415 @param v The vector need to be sorted
416 @param vSequence The sort sequence should be followed
417
418 **/
419 public static void sortVectorString(Vector<String> v, Vector<Integer> vSequence) {
420 if (v != null && vSequence != null && v.size() == vSequence.size()) {
421 Vector<String> tempV = new Vector<String>();
422 for (int index = 0; index < v.size(); index++) {
423 tempV.addElement(v.get(index));
424 }
425 for (int index = 0; index < v.size(); index++) {
426 v.setElementAt(tempV.get(vSequence.get(index)), index);
427 }
428 }
429 }
430
431 /**
432 Sort all modules
433
434 @param v
435 @param mode
436
437 **/
438 public static void sortModules(Vector<ModuleIdentification> v, int mode) {
439 if (v != null) {
440 //
441 // sort by name
442 //
443 for (int indexI = 0; indexI < v.size(); indexI++) {
444 for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {
445 if ((v.get(indexJ).getName().compareTo(v.get(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)
446 || (v.get(indexI).getName().compareTo(v.get(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {
447 ModuleIdentification temp = v.get(indexI);
448 v.setElementAt(v.get(indexJ), indexI);
449 v.setElementAt(temp, indexJ);
450 }
451 }
452 }
453 }
454 }
455
456 /**
457 Sort all packages
458
459 @param v
460 @param mode
461
462 **/
463 public static void sortPackages(Vector<PackageIdentification> v, int mode) {
464 if (v != null) {
465 //
466 // sort by name
467 //
468 for (int indexI = 0; indexI < v.size(); indexI++) {
469 for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {
470 if ((v.get(indexJ).getName().compareTo(v.get(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)
471 || (v.get(indexI).getName().compareTo(v.get(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {
472 PackageIdentification temp = v.get(indexI);
473 v.setElementAt(v.get(indexJ), indexI);
474 v.setElementAt(temp, indexJ);
475 }
476 }
477 }
478 }
479 }
480
481 /**
482 Sort all platforms
483
484 @param v
485 @param mode
486
487 **/
488 public static void sortPlatforms(Vector<PlatformIdentification> v, int mode) {
489 if (v != null) {
490 //
491 // sort by name
492 //
493 for (int indexI = 0; indexI < v.size(); indexI++) {
494 for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {
495 if ((v.get(indexJ).getName().compareTo(v.get(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)
496 || (v.get(indexI).getName().compareTo(v.get(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {
497 PlatformIdentification temp = v.get(indexI);
498 v.setElementAt(v.get(indexJ), indexI);
499 v.setElementAt(temp, indexJ);
500 }
501 }
502 }
503 }
504 }
505 }