]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - Tools/Source/PackageEditor/src/org/tianocore/common/Tools.java
Fix the problem "update action multiple times fail".
[mirror_edk2.git] / Tools / Source / PackageEditor / src / org / tianocore / common / Tools.java
... / ...
CommitLineData
1/** @file\r
2 Java class Tools contains common use procedures.\r
3 \r
4Copyright (c) 2006, Intel Corporation\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12**/\r
13package org.tianocore.common;\r
14\r
15import java.io.File;\r
16import java.text.SimpleDateFormat;\r
17import java.util.Calendar;\r
18import java.util.Date;\r
19import java.util.UUID;\r
20\r
21/**\r
22 This class contains static methods for some common operations\r
23 \r
24 @since PackageEditor 1.0\r
25**/\r
26public class Tools {\r
27 \r
28 public static final String guidArrayPat = "0x[a-fA-F0-9]{1,8},( )*0x[a-fA-F0-9]{1,4},( )*0x[a-fA-F0-9]{1,4}(,( )*\\{)?(,?( )*0x[a-fA-F0-9]{1,2}){8}( )*(\\})?";\r
29 public static final String guidRegistryPat = "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}";\r
30 /**\r
31 get current date and time, then return\r
32 @return String\r
33 **/\r
34 public static String getCurrentDateTime() {\r
35 Date now = new Date(System.currentTimeMillis());\r
36 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");\r
37 return sdf.format(now);\r
38 }\r
39 \r
40 /**\r
41 Delete a folder and all its files\r
42 @param strFolderName\r
43 @return boolean\r
44 **/\r
45 public static boolean deleteFolder(File fleFolderName) {\r
46 boolean blnIsDeleted = true;\r
47 File[] aryAllFiles = fleFolderName.listFiles();\r
48 \r
49 for (int indexI = 0; indexI < aryAllFiles.length; indexI++) {\r
50 if (blnIsDeleted) {\r
51 if (aryAllFiles[indexI].isDirectory()) {\r
52 blnIsDeleted = deleteFolder(aryAllFiles[indexI]);\r
53 } else if (aryAllFiles[indexI].isFile()) {\r
54 if (!aryAllFiles[indexI].delete()) {\r
55 blnIsDeleted = false;\r
56 }\r
57 }\r
58 }\r
59 }\r
60 if (blnIsDeleted) {\r
61 fleFolderName.delete();\r
62 }\r
63 return blnIsDeleted;\r
64 }\r
65 \r
66 /**\r
67 Get a new GUID\r
68 \r
69 @return String\r
70 **/\r
71 public static String generateUuidString() {\r
72 return UUID.randomUUID().toString();\r
73 }\r
74 \r
75 public static String formatGuidString (String guidNameConv) {\r
76 String[] strList;\r
77 String guid = "";\r
78 int index = 0;\r
79 if (guidNameConv\r
80 .matches(Tools.guidRegistryPat)) {\r
81 strList = guidNameConv.split("-");\r
82 guid = "0x" + strList[0] + ", ";\r
83 guid = guid + "0x" + strList[1] + ", ";\r
84 guid = guid + "0x" + strList[2] + ", ";\r
85// guid = guid + "{";\r
86 guid = guid + "0x" + strList[3].substring(0, 2) + ", ";\r
87 guid = guid + "0x" + strList[3].substring(2, 4);\r
88\r
89 while (index < strList[4].length()) {\r
90 guid = guid + ", ";\r
91 guid = guid + "0x" + strList[4].substring(index, index + 2);\r
92 index = index + 2;\r
93 }\r
94// guid = guid + "}";\r
95 return guid;\r
96 }\r
97 else if (guidNameConv\r
98 .matches(Tools.guidArrayPat)) {\r
99 strList = guidNameConv.split(",");\r
100 \r
101 //\r
102 // chang ANSI c form to registry form\r
103 //\r
104 for (int i = 0; i < strList.length; i++){\r
105 strList[i] = strList[i].substring(strList[i].lastIndexOf("x") + 1);\r
106 }\r
107 if (strList[strList.length - 1].endsWith("}")) {\r
108 strList[strList.length -1] = strList[strList.length-1].substring(0, strList[strList.length-1].length()-1); \r
109 }\r
110 //\r
111 //inserting necessary leading zeros\r
112 //\r
113 \r
114 int segLen = strList[0].length();\r
115 if (segLen < 8){\r
116 for (int i = 0; i < 8 - segLen; ++i){\r
117 strList[0] = "0" + strList[0];\r
118 }\r
119 }\r
120 \r
121 segLen = strList[1].length();\r
122 if (segLen < 4){\r
123 for (int i = 0; i < 4 - segLen; ++i){\r
124 strList[1] = "0" + strList[1];\r
125 }\r
126 }\r
127 segLen = strList[2].length();\r
128 if (segLen < 4){\r
129 for (int i = 0; i < 4 - segLen; ++i){\r
130 strList[2] = "0" + strList[2];\r
131 }\r
132 }\r
133 for (int i = 3; i < 11; ++i) {\r
134 segLen = strList[i].length();\r
135 if (segLen < 2){\r
136 strList[i] = "0" + strList[i];\r
137 }\r
138 }\r
139 \r
140 for (int i = 0; i < 3; i++){\r
141 guid += strList[i] + "-";\r
142 }\r
143 \r
144 guid += strList[3];\r
145 guid += strList[4] + "-";\r
146 \r
147 for (int i = 5; i < strList.length; ++i){\r
148 guid += strList[i];\r
149 }\r
150 \r
151 \r
152 return guid;\r
153 } else {\r
154 \r
155 return "0";\r
156\r
157 }\r
158 }\r
159}\r