]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/MigrationTools/org/tianocore/migration/Common.java
Changed spelling to manifest
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / Common.java
CommitLineData
8c4eeeb6 1/** @file\r
2 \r
3 Copyright (c) 2006, Intel Corporation\r
4 All rights reserved. This program and the accompanying materials\r
5 are licensed and made available under the terms and conditions of the BSD License\r
6 which accompanies this distribution. The full text of the license may be found at\r
7 http://opensource.org/licenses/bsd-license.php\r
8 \r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11 \r
12 **/\r
90503bad 13package org.tianocore.migration;\r
14\r
fed802b1 15import java.io.*;\r
16import java.util.regex.*;\r
17import java.util.*;\r
5ea254f6 18import java.lang.reflect.*;\r
90503bad 19\r
5ea254f6 20public final class Common {\r
27e0221a 21 public static final int BOTH = 0;\r
22 public static final int FILE = 1;\r
23 public static final int DIR = 2;\r
24 \r
39e5e412 25 public static final String STRSEPARATER = "(.*)\\\\([^\\\\]*)";\r
26 public static final Pattern PTNSEPARATER = Pattern.compile("(.*)\\\\([^\\\\]*)");\r
5ea254f6 27\r
27e0221a 28 //-------------------------------------regex------------------------------------------//\r
29 \r
30 public static final String replaceAll(String line, Pattern ptn, String des) {\r
31 Matcher mtr = ptn.matcher(line);\r
a817ecbf 32\r
27e0221a 33 if (mtr.find()) {\r
34 return mtr.replaceAll(des);\r
35 }\r
36 \r
37 return line;\r
38 }\r
5ea254f6 39\r
06d1ff5b 40 public static final boolean find (String line, String regex) {\r
41 Pattern ptn = Pattern.compile(regex);\r
42\r
43 return ptn.matcher (line).find (); \r
44 }\r
27e0221a 45 //-------------------------------------regex------------------------------------------//\r
46 \r
47 //-----------------------------------file&string---------------------------------------//\r
48 \r
49 public static final String file2string(String filename) throws Exception {\r
50 BufferedReader rd = new BufferedReader(new FileReader(filename));\r
51 StringBuffer wholefile = new StringBuffer();\r
52 String line;\r
53 while ((line = rd.readLine()) != null) {\r
54 wholefile.append(line + "\n");\r
55 }\r
39e5e412 56 rd.close();\r
27e0221a 57 return wholefile.toString();\r
58 }\r
90503bad 59\r
27e0221a 60 public static final void string2file(String content, String filename) throws Exception {\r
61 ensureDir(filename);\r
62 PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter(filename)));\r
63 outfile.append(content);\r
64 outfile.flush();\r
65 outfile.close();\r
66 }\r
a756211f 67\r
39e5e412 68 public static final void fileCopy(String src, String des) throws Exception {\r
372a4642 69 string2file(file2string(src), des);\r
39e5e412 70 }\r
71 \r
27e0221a 72 //-----------------------------------file&string---------------------------------------//\r
5ea254f6 73\r
27e0221a 74 //--------------------------------------dir--------------------------------------------//\r
75 /*\r
76 public static final HashSet<String> walkDir(String path, int mode) throws Exception {\r
77 HashSet<String> pathlist = new HashSet<String>();\r
78 Common.toDoAll(path, Common.class.getMethod("walkDir", String.class), null, null, mode);\r
79 return pathlist;\r
80 }\r
81 */\r
82 public static final void ensureDir(String objFileWhole) {\r
83 File tempdir;\r
39e5e412 84 Matcher mtrseparate = PTNSEPARATER.matcher(objFileWhole);\r
27e0221a 85 if (mtrseparate.find()) {\r
86 tempdir = new File(mtrseparate.group(1));\r
87 if (!tempdir.exists()) tempdir.mkdirs();\r
88 }\r
89 }\r
90 \r
91 public static final void deleteDir(String objFileWhole) {\r
92 String[] list = new File(objFileWhole).list();\r
93 File temp;\r
94 for (int i = 0 ; i < list.length ; i++) {\r
95 temp = new File(objFileWhole + File.separator + list[i]);\r
96 if (temp.isDirectory()) {\r
97 deleteDir(objFileWhole + File.separator + list[i]);\r
98 } else {\r
99 temp.delete();\r
100 }\r
101 }\r
102 new File(objFileWhole).delete();\r
103 }\r
104 \r
105 public static final String dirCopy_(String src) throws Exception {\r
39e5e412 106 Matcher mtrseparate = Common.PTNSEPARATER.matcher(src);\r
27e0221a 107 if (mtrseparate.find()) {\r
108 dirCopy(src, mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2));\r
109 }\r
110 return mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2);\r
111 }\r
112 \r
113 public static final void dirCopy(String src, String des) throws Exception {\r
114 String[] list = new File(src).list();\r
115 File test;\r
8c4eeeb6 116\r
39e5e412 117 ensureDir(des);\r
27e0221a 118 for (int i = 0 ; i < list.length ; i++) {\r
119 test = new File(src + File.separator + list[i]);\r
120 if (test.isDirectory()) {\r
121 dirCopy(src + File.separator + list[i], des + File.separator + list[i]);\r
122 } else {\r
39e5e412 123 //ensureDir(des + File.separator + list[i]);\r
27e0221a 124 string2file(file2string(src + File.separator + list[i]), des + File.separator + list[i]);\r
125 }\r
126 }\r
127 }\r
39e5e412 128 \r
129 public static final void oneLevelDirCopy(String src, String des, String type) throws Exception {\r
372a4642 130 String[] list = new File(src).list();\r
131 \r
132 ensureDir(des);\r
133 for (int i = 0; i < list.length; i++) {\r
134 if (list[i].contains(type)) {\r
39e5e412 135 string2file(file2string(src + File.separator + list[i]), des + File.separator + list[i]);\r
372a4642 136 }\r
137 }\r
39e5e412 138 }\r
5ea254f6 139\r
27e0221a 140 //--------------------------------------dir--------------------------------------------//\r
5ea254f6 141\r
27e0221a 142 //-------------------------------like python walk-----------------------------------------//\r
143 \r
144 public static final void toDoAll(String path, Method md, Object obj, Object[] args, int type) throws Exception {\r
145 String[] list = new File(path).list();\r
146 ArrayList<Object> _args = new ArrayList<Object>();\r
147 \r
148 _args.add(path);\r
149 if (args != null) {\r
150 for (int i = 0; i < args.length; i++) {\r
151 _args.add(args[i]);\r
152 }\r
153 }\r
5ea254f6 154\r
27e0221a 155 if (type == DIR || type == BOTH) {\r
156 md.invoke(obj, _args.toArray());\r
157 }\r
158 for (int i = 0 ; i < list.length ; i++) {\r
159 if (new File(path + File.separator + list[i]).isDirectory()) {\r
160 toDoAll(path + File.separator + list[i], md, obj, args, type);\r
161 } else {\r
162 if (type == FILE || type == BOTH) {\r
163 _args.set(0, path + File.separator + list[i]);\r
164 md.invoke(obj, _args.toArray());\r
165 }\r
166 }\r
167 }\r
168 }\r
16b7eeef 169\r
27e0221a 170 public static final void toDoAll(Set<String> set, ForDoAll fda) throws Exception {\r
171 Iterator<String> di = set.iterator();\r
172 while (di.hasNext()) {\r
173 fda.run(di.next());\r
174 }\r
175 }\r
176 \r
177 public static final void toDoAll(String path, ForDoAll fda, int type) throws Exception { // filter of file type can be done in toDo\r
178 String[] list = new File(path).list();\r
179 File test;\r
fed802b1 180\r
27e0221a 181 if (type == DIR || type == BOTH) {\r
182 fda.run(path);\r
183 }\r
184 for (int i = 0 ; i < list.length ; i++) {\r
185 test = new File(path + File.separator + list[i]);\r
186 if (test.isDirectory()) {\r
187 if (fda.filter(test)) {\r
188 toDoAll(path + File.separator + list[i], fda, type);\r
189 }\r
190 } else {\r
191 if (type == FILE || type == BOTH) {\r
192 fda.run(path + File.separator + list[i]);\r
193 }\r
194 }\r
195 }\r
196 }\r
197 \r
198 public static interface ForDoAll {\r
199 public void run(String filepath) throws Exception;\r
200 \r
201 public boolean filter(File dir);\r
202 }\r
203 \r
204 public static abstract class Laplace {\r
205 public void transform(String src, String des) throws Exception {\r
206 Common.string2file(operation(Common.file2string(src)), des);\r
207 }\r
208 \r
209 public abstract String operation(String wholeline);\r
210 \r
211 public abstract boolean recognize(String filename);\r
212 \r
213 public abstract String namechange(String oldname);\r
214 }\r
215 \r
216 public static interface Element {\r
217 \r
218// public int replace = 0;\r
219// public int type = 1;\r
220 \r
221 public String getReplace(String key);\r
222 \r
223// public void getType(String key);\r
224// \r
225// public void setReplace(int num);\r
226// \r
227// public void setType(int num);\r
228 \r
229 }\r
90503bad 230}\r