]>
Commit | Line | Data |
---|---|---|
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 | 13 | package org.tianocore.migration;\r |
14 | \r | |
fed802b1 | 15 | import java.io.*;\r |
16 | import java.util.regex.*;\r | |
17 | import java.util.*;\r | |
5ea254f6 | 18 | import java.lang.reflect.*;\r |
90503bad | 19 | \r |
5ea254f6 | 20 | public 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 | |
25 | public static final String strseparate = "(.*)\\\\([^\\\\]*)";\r | |
26 | public static final Pattern ptnseparate = 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 | |
56 | return wholefile.toString();\r | |
57 | }\r | |
90503bad | 58 | \r |
27e0221a | 59 | public static final void string2file(String content, String filename) throws Exception {\r |
60 | ensureDir(filename);\r | |
61 | PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter(filename)));\r | |
62 | outfile.append(content);\r | |
63 | outfile.flush();\r | |
64 | outfile.close();\r | |
65 | }\r | |
a756211f | 66 | \r |
27e0221a | 67 | //-----------------------------------file&string---------------------------------------//\r |
5ea254f6 | 68 | \r |
27e0221a | 69 | //--------------------------------------dir--------------------------------------------//\r |
70 | /*\r | |
71 | public static final HashSet<String> walkDir(String path, int mode) throws Exception {\r | |
72 | HashSet<String> pathlist = new HashSet<String>();\r | |
73 | Common.toDoAll(path, Common.class.getMethod("walkDir", String.class), null, null, mode);\r | |
74 | return pathlist;\r | |
75 | }\r | |
76 | */\r | |
77 | public static final void ensureDir(String objFileWhole) {\r | |
78 | File tempdir;\r | |
79 | Matcher mtrseparate = ptnseparate.matcher(objFileWhole);\r | |
80 | if (mtrseparate.find()) {\r | |
81 | tempdir = new File(mtrseparate.group(1));\r | |
82 | if (!tempdir.exists()) tempdir.mkdirs();\r | |
83 | }\r | |
84 | }\r | |
85 | \r | |
86 | public static final void deleteDir(String objFileWhole) {\r | |
87 | String[] list = new File(objFileWhole).list();\r | |
88 | File temp;\r | |
89 | for (int i = 0 ; i < list.length ; i++) {\r | |
90 | temp = new File(objFileWhole + File.separator + list[i]);\r | |
91 | if (temp.isDirectory()) {\r | |
92 | deleteDir(objFileWhole + File.separator + list[i]);\r | |
93 | } else {\r | |
94 | temp.delete();\r | |
95 | }\r | |
96 | }\r | |
97 | new File(objFileWhole).delete();\r | |
98 | }\r | |
99 | \r | |
100 | public static final String dirCopy_(String src) throws Exception {\r | |
101 | Matcher mtrseparate = Common.ptnseparate.matcher(src);\r | |
102 | if (mtrseparate.find()) {\r | |
103 | dirCopy(src, mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2));\r | |
104 | }\r | |
105 | return mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2);\r | |
106 | }\r | |
107 | \r | |
108 | public static final void dirCopy(String src, String des) throws Exception {\r | |
109 | String[] list = new File(src).list();\r | |
110 | File test;\r | |
8c4eeeb6 | 111 | \r |
27e0221a | 112 | for (int i = 0 ; i < list.length ; i++) {\r |
113 | test = new File(src + File.separator + list[i]);\r | |
114 | if (test.isDirectory()) {\r | |
115 | dirCopy(src + File.separator + list[i], des + File.separator + list[i]);\r | |
116 | } else {\r | |
117 | ensureDir(des + File.separator + list[i]);\r | |
118 | string2file(file2string(src + File.separator + list[i]), des + File.separator + list[i]);\r | |
119 | }\r | |
120 | }\r | |
121 | }\r | |
5ea254f6 | 122 | \r |
27e0221a | 123 | //--------------------------------------dir--------------------------------------------//\r |
5ea254f6 | 124 | \r |
27e0221a | 125 | //-------------------------------like python walk-----------------------------------------//\r |
126 | \r | |
127 | public static final void toDoAll(String path, Method md, Object obj, Object[] args, int type) throws Exception {\r | |
128 | String[] list = new File(path).list();\r | |
129 | ArrayList<Object> _args = new ArrayList<Object>();\r | |
130 | \r | |
131 | _args.add(path);\r | |
132 | if (args != null) {\r | |
133 | for (int i = 0; i < args.length; i++) {\r | |
134 | _args.add(args[i]);\r | |
135 | }\r | |
136 | }\r | |
5ea254f6 | 137 | \r |
27e0221a | 138 | if (type == DIR || type == BOTH) {\r |
139 | md.invoke(obj, _args.toArray());\r | |
140 | }\r | |
141 | for (int i = 0 ; i < list.length ; i++) {\r | |
142 | if (new File(path + File.separator + list[i]).isDirectory()) {\r | |
143 | toDoAll(path + File.separator + list[i], md, obj, args, type);\r | |
144 | } else {\r | |
145 | if (type == FILE || type == BOTH) {\r | |
146 | _args.set(0, path + File.separator + list[i]);\r | |
147 | md.invoke(obj, _args.toArray());\r | |
148 | }\r | |
149 | }\r | |
150 | }\r | |
151 | }\r | |
16b7eeef | 152 | \r |
27e0221a | 153 | public static final void toDoAll(Set<String> set, ForDoAll fda) throws Exception {\r |
154 | Iterator<String> di = set.iterator();\r | |
155 | while (di.hasNext()) {\r | |
156 | fda.run(di.next());\r | |
157 | }\r | |
158 | }\r | |
159 | \r | |
160 | public static final void toDoAll(String path, ForDoAll fda, int type) throws Exception { // filter of file type can be done in toDo\r | |
161 | String[] list = new File(path).list();\r | |
162 | File test;\r | |
fed802b1 | 163 | \r |
27e0221a | 164 | if (type == DIR || type == BOTH) {\r |
165 | fda.run(path);\r | |
166 | }\r | |
167 | for (int i = 0 ; i < list.length ; i++) {\r | |
168 | test = new File(path + File.separator + list[i]);\r | |
169 | if (test.isDirectory()) {\r | |
170 | if (fda.filter(test)) {\r | |
171 | toDoAll(path + File.separator + list[i], fda, type);\r | |
172 | }\r | |
173 | } else {\r | |
174 | if (type == FILE || type == BOTH) {\r | |
175 | fda.run(path + File.separator + list[i]);\r | |
176 | }\r | |
177 | }\r | |
178 | }\r | |
179 | }\r | |
180 | \r | |
181 | public static interface ForDoAll {\r | |
182 | public void run(String filepath) throws Exception;\r | |
183 | \r | |
184 | public boolean filter(File dir);\r | |
185 | }\r | |
186 | \r | |
187 | public static abstract class Laplace {\r | |
188 | public void transform(String src, String des) throws Exception {\r | |
189 | Common.string2file(operation(Common.file2string(src)), des);\r | |
190 | }\r | |
191 | \r | |
192 | public abstract String operation(String wholeline);\r | |
193 | \r | |
194 | public abstract boolean recognize(String filename);\r | |
195 | \r | |
196 | public abstract String namechange(String oldname);\r | |
197 | }\r | |
198 | \r | |
199 | public static interface Element {\r | |
200 | \r | |
201 | // public int replace = 0;\r | |
202 | // public int type = 1;\r | |
203 | \r | |
204 | public String getReplace(String key);\r | |
205 | \r | |
206 | // public void getType(String key);\r | |
207 | // \r | |
208 | // public void setReplace(int num);\r | |
209 | // \r | |
210 | // public void setType(int num);\r | |
211 | \r | |
212 | }\r | |
90503bad | 213 | }\r |