]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/MigrationTools/org/tianocore/migration/Common.java
func name changed & modify common.todoall
[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
21 public static final int BOTH = 0;\r
22 public static final int FILE = 1;\r
23 public static final int DIR = 2;\r
8c4eeeb6 24 \r
ac62aa9a 25 public static final String strseparate = "(.*)\\\\([^\\\\]*)";\r
5ea254f6 26 public static final Pattern ptnseparate = Pattern.compile("(.*)\\\\([^\\\\]*)");\r
27\r
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
32 if (mtr.find()) {\r
33 return mtr.replaceAll(des);\r
34 }\r
35 return line;\r
36 }\r
37\r
38 //-------------------------------------regex------------------------------------------//\r
39 \r
40 //-----------------------------------file&string---------------------------------------//\r
41 \r
42 public static final String file2string(String filename) throws Exception {\r
90503bad 43 BufferedReader rd = new BufferedReader(new FileReader(filename));\r
44 StringBuffer wholefile = new StringBuffer();\r
45 String line;\r
46 while ((line = rd.readLine()) != null) {\r
47 wholefile.append(line + "\n");\r
48 }\r
49 return wholefile.toString();\r
50 }\r
51\r
5ea254f6 52 public static final void string2file(String content, String filename) throws Exception {\r
a756211f 53 ensureDir(filename);\r
54 PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter(filename)));\r
55 outfile.append(content);\r
56 outfile.flush();\r
57 outfile.close();\r
58 }\r
59\r
5ea254f6 60 //-----------------------------------file&string---------------------------------------//\r
61\r
62 //--------------------------------------dir--------------------------------------------//\r
b678aaca 63 /*\r
64 public static final HashSet<String> walkDir(String path, int mode) throws Exception {\r
65 HashSet<String> pathlist = new HashSet<String>();\r
66 Common.toDoAll(path, Common.class.getMethod("walkDir", String.class), null, null, mode);\r
67 return pathlist;\r
68 }\r
69 */\r
5ea254f6 70 public static final void ensureDir(String objFileWhole) {\r
90503bad 71 File tempdir;\r
8c4eeeb6 72 Matcher mtrseparate = ptnseparate.matcher(objFileWhole);\r
90503bad 73 if (mtrseparate.find()) {\r
74 tempdir = new File(mtrseparate.group(1));\r
75 if (!tempdir.exists()) tempdir.mkdirs();\r
76 }\r
fed802b1 77 }\r
78 \r
5ea254f6 79 public static final void deleteDir(String objFileWhole) {\r
80 String[] list = new File(objFileWhole).list();\r
81 File temp;\r
fed802b1 82 for (int i = 0 ; i < list.length ; i++) {\r
5ea254f6 83 temp = new File(objFileWhole + File.separator + list[i]);\r
84 if (temp.isDirectory()) {\r
85 deleteDir(objFileWhole + File.separator + list[i]);\r
fed802b1 86 } else {\r
5ea254f6 87 temp.delete();\r
fed802b1 88 }\r
89 }\r
5ea254f6 90 new File(objFileWhole).delete();\r
a756211f 91 }\r
92 \r
5ea254f6 93 public static final String dirCopy_(String src) throws Exception {\r
8c4eeeb6 94 Matcher mtrseparate = Common.ptnseparate.matcher(src);\r
95 if (mtrseparate.find()) {\r
96 dirCopy(src, mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2));\r
97 }\r
98 return mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2);\r
99 }\r
100 \r
5ea254f6 101 public static final void dirCopy(String src, String des) throws Exception {\r
8c4eeeb6 102 String[] list = new File(src).list();\r
103 File test;\r
104\r
105 for (int i = 0 ; i < list.length ; i++) {\r
106 test = new File(src + File.separator + list[i]);\r
107 if (test.isDirectory()) {\r
108 dirCopy(src + File.separator + list[i], des + File.separator + list[i]);\r
109 } else {\r
110 ensureDir(des + File.separator + list[i]);\r
111 string2file(file2string(src + File.separator + list[i]), des + File.separator + list[i]);\r
112 }\r
113 }\r
114 }\r
5ea254f6 115\r
116 //--------------------------------------dir--------------------------------------------//\r
117\r
118 //-------------------------------like python walk-----------------------------------------//\r
119 \r
120 public static final void toDoAll(String path, Method md, Object obj, Object[] args, int type) throws Exception {\r
121 String[] list = new File(path).list();\r
122 ArrayList<Object> _args = new ArrayList<Object>();\r
123 \r
124 _args.add(path);\r
125 if (args != null) {\r
126 for (int i = 0; i < args.length; i++) {\r
127 _args.add(args[i]);\r
128 }\r
129 }\r
130\r
131 if (type == DIR || type == BOTH) {\r
132 md.invoke(obj, _args.toArray());\r
133 }\r
134 for (int i = 0 ; i < list.length ; i++) {\r
135 if (new File(path + File.separator + list[i]).isDirectory()) {\r
136 toDoAll(path + File.separator + list[i], md, obj, args, type);\r
137 } else {\r
138 if (type == FILE || type == BOTH) {\r
139 _args.set(0, path + File.separator + list[i]);\r
140 md.invoke(obj, _args.toArray());\r
141 }\r
142 }\r
143 }\r
144 }\r
fed802b1 145 \r
b678aaca 146 public static void toDoAll(String path, ForDoAll fda, int type) throws Exception { // filter of file type can be done in toDo\r
fed802b1 147 String[] list = new File(path).list();\r
148 File test;\r
149\r
b678aaca 150 if (type == DIR || type == BOTH) {\r
151 fda.toDo(path);\r
152 }\r
fed802b1 153 for (int i = 0 ; i < list.length ; i++) {\r
154 test = new File(path + File.separator + list[i]);\r
155 if (test.isDirectory()) {\r
b678aaca 156 toDoAll(path + File.separator + list[i], fda, type);\r
fed802b1 157 } else {\r
b678aaca 158 if (type == FILE || type == BOTH) {\r
159 fda.toDo(path + File.separator + list[i]);\r
160 }\r
fed802b1 161 }\r
162 }\r
163 }\r
164 \r
165 public static interface ForDoAll {\r
166 public void toDo(String filepath) throws Exception;\r
90503bad 167 }\r
b678aaca 168 /*\r
169 // this PathIterator is based on HashSet, an thread implementation is required.\r
170 private final class PathIterator implements ForDoAll{\r
171 PathIterator(String path) throws Exception {\r
172 startpath = path;\r
173 Common.toDoAll(startpath, this, mode);\r
174 }\r
175 PathIterator(String path, int md) throws Exception {\r
176 startpath = path;\r
177 mode = md;\r
178 Common.toDoAll(startpath, this, mode);\r
179 }\r
180 private String startpath;\r
181 private int mode = Common.BOTH;\r
182 private HashSet<String> pathlist = new HashSet<String>();\r
183 private Iterator<String> it = pathlist.iterator();\r
184 \r
185 public final void toDo(String path) throws Exception {\r
186 pathlist.add(path);\r
187 }\r
188 \r
189 public final String next() {\r
190 return it.next();\r
191 }\r
192 \r
193 public final boolean hasNext() {\r
194 return it.hasNext();\r
195 }\r
196 \r
197 public final String toString() {\r
198 return pathlist.toString();\r
199 }\r
200 }\r
201 \r
202 public final PathIterator getPathIterator(String path, int md) throws Exception {\r
203 return new PathIterator(path, md);\r
204 }\r
205 */\r
90503bad 206}\r