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