]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Tools.java
1. Restructure some folders and files
[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.JComboBox;
26 import javax.swing.JOptionPane;
27
28 /**
29 The class is used to provides some useful interfaces
30
31 **/
32 public class Tools {
33
34 //
35 // The dir user selected to create new package in
36 //
37 public static String dirForNewSpd = null;
38
39 /**
40 Used for test
41
42 @param args
43
44 **/
45 public static void main(String[] args) {
46 System.out.println(getCurrentDateTime());
47 }
48
49 /**
50 Get current date and time and format it as "yyyy-MM-dd HH:mm"
51
52 @return formatted current date and time
53
54 **/
55 public static String getCurrentDateTime() {
56 Date now = new Date(System.currentTimeMillis());
57 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
58 return sdf.format(now);
59 }
60
61 /**
62 Generate a UUID
63
64 @return the created UUID
65
66 **/
67 public static String generateUuidString() {
68 return UUID.randomUUID().toString();
69 }
70
71 /**
72 Use current file separator in the path
73
74 @param strPath
75 @return
76
77 **/
78 public static String convertPathToCurrentOsType(String strPath) {
79 strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.FILE_SEPARATOR);
80 strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.FILE_SEPARATOR);
81 return strPath;
82 }
83
84 /**
85 Use Unix file separator in the path
86
87 @param strPath
88 @return
89
90 **/
91 public static String convertPathToUnixType(String strPath) {
92 strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.UNIX_FILE_SEPARATOR);
93 return strPath;
94 }
95
96 /**
97 Use Dos file separator in the path
98
99 @param strPath
100 @return
101
102 **/
103 public static String convertPathToDosType(String strPath) {
104 strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.DOS_FILE_SEPARATOR);
105 return strPath;
106 }
107
108 /**
109 Get all system properties and output to the console
110
111 **/
112 public static void getSystemProperties() {
113 System.out.println(System.getProperty("java.class.version"));
114 System.out.println(System.getProperty("java.class.path"));
115 System.out.println(System.getProperty("java.ext.dirs"));
116 System.out.println(System.getProperty("os.name"));
117 System.out.println(System.getProperty("os.arch"));
118 System.out.println(System.getProperty("os.version"));
119 System.out.println(System.getProperty("file.separator"));
120 System.out.println(System.getProperty("path.separator"));
121 System.out.println(System.getProperty("line.separator"));
122 System.out.println(System.getProperty("user.name"));
123 System.out.println(System.getProperty("user.home"));
124 System.out.println(System.getProperty("user.dir"));
125 System.out.println(System.getProperty("PATH"));
126
127 System.out.println(System.getenv("PROCESSOR_REVISION"));
128 }
129
130 /**
131 Generate selection items for JComboBox by input vector
132
133 **/
134 public static void generateComboBoxByVector(JComboBox jcb, Vector<String> vector) {
135 if (jcb != null) {
136 jcb.removeAllItems();
137 }
138 if (vector != null) {
139 for (int index = 0; index < vector.size(); index++) {
140 jcb.addItem(vector.elementAt(index));
141 }
142 }
143 }
144
145 /**
146 Get path only from a path
147
148 @param filePath
149 @return
150
151 **/
152 public static String getFilePathOnly(String filePath) {
153 String path = filePath.substring(0, filePath.length() - getFileNameOnly(filePath).length());
154 if (path.endsWith(DataType.FILE_SEPARATOR)) {
155 path = path.substring(0, path.length() - DataType.FILE_SEPARATOR.length());
156 }
157
158 return path;
159 }
160
161 /**
162 Get file name from a path
163
164 @param filePath
165 @return
166
167 **/
168 public static String getFileNameOnly(String filePath) {
169 File f = new File(filePath);
170 return f.getAbsoluteFile().getName();
171 }
172
173 public static String getFileNameWithoutExt(String filePath) {
174 filePath = getFileNameOnly(filePath);
175 filePath = filePath.substring(0, filePath.lastIndexOf(DataType.FILE_EXT_SEPARATOR));
176 return filePath;
177 }
178
179 /**
180 Get relative path
181
182 @param wholePath
183 @param commonPath
184 @return wholePath - commonPath
185
186 **/
187 public static String getRelativePath(String wholePath, String commonPath) {
188 String path = "";
189 int i = 0;
190 i = wholePath.indexOf(commonPath);
191 if (i > -1) {
192 i = i + commonPath.length();
193 } else {
194 return "";
195 }
196 path = wholePath.substring(i);
197 //
198 // remove file separator of head
199 //
200 if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == 0) {
201 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());
202 }
203 if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == 0) {
204 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());
205 }
206 //
207 // remove file separator of rear
208 //
209 if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == path.length() - DataType.DOS_FILE_SEPARATOR.length()) {
210 path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());
211 }
212 if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == path.length() - DataType.UNIX_FILE_SEPARATOR.length()) {
213 path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());
214 }
215 //
216 // convert to UNIX format
217 //
218 path = Tools.convertPathToUnixType(path);
219 return path;
220 }
221
222 /**
223 Convert List ot Vector
224
225 @param list
226 @return
227
228 **/
229 public static Vector<String> convertListToVector(List list) {
230 Vector<String> v = new Vector<String>();
231 if (list != null && list.size() > 0) {
232 for (int index = 0; index < list.size(); index++) {
233 v.addElement(list.get(index).toString());
234 }
235 }
236 return v;
237 }
238
239 /**
240 If the input path missing ext, append the ext to the path
241
242 @param path
243 @param type
244 @return
245
246 **/
247 public static String addPathExt(String path, int type) {
248 String match = "";
249 if (type == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
250 match = DataType.FILE_EXT_SEPARATOR + DataType.MODULE_SURFACE_AREA_EXT;
251 }
252 if (type == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
253 match = DataType.FILE_EXT_SEPARATOR + DataType.PACKAGE_SURFACE_AREA_EXT;
254 }
255 if (type == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
256 match = DataType.FILE_EXT_SEPARATOR + DataType.PLATFORM_SURFACE_AREA_EXT;
257 }
258 if (path.length() <= match.length()) {
259 path = path + match;
260 return path;
261 }
262 if (!(path.substring(path.length() - match.length())).equals(match)) {
263 path = path + match;
264 }
265 return path;
266 }
267
268 /**
269 Show a message box
270
271 @param arg0
272
273 **/
274 public static void showInformationMessage(String arg0) {
275 JOptionPane.showConfirmDialog(null, arg0, "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
276 }
277
278 /**
279 if the string doesn't end with a file separator, append it to the string
280
281 @param arg0
282 @return
283
284 **/
285 public static String addFileSeparator(String arg0) {
286 if (!arg0.endsWith(DataType.FILE_SEPARATOR)) {
287 arg0 = arg0 + DataType.FILE_SEPARATOR;
288 }
289 return arg0;
290 }
291 }