]>
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 |
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 |
5ea254f6 | 25 | public static final Pattern ptnseparate = Pattern.compile("(.*)\\\\([^\\\\]*)");\r |
26 | \r | |
27 | //-------------------------------------regex------------------------------------------//\r | |
28 | \r | |
29 | public static final String replaceAll(String line, Pattern ptn, String des) {\r | |
30 | Matcher mtr = ptn.matcher(line);\r | |
31 | if (mtr.find()) {\r | |
32 | return mtr.replaceAll(des);\r | |
33 | }\r | |
34 | return line;\r | |
35 | }\r | |
36 | \r | |
37 | //-------------------------------------regex------------------------------------------//\r | |
38 | \r | |
39 | //-----------------------------------file&string---------------------------------------//\r | |
40 | \r | |
41 | public static final String file2string(String filename) throws Exception {\r | |
90503bad | 42 | BufferedReader rd = new BufferedReader(new FileReader(filename));\r |
43 | StringBuffer wholefile = new StringBuffer();\r | |
44 | String line;\r | |
45 | while ((line = rd.readLine()) != null) {\r | |
46 | wholefile.append(line + "\n");\r | |
47 | }\r | |
48 | return wholefile.toString();\r | |
49 | }\r | |
50 | \r | |
5ea254f6 | 51 | public static final void string2file(String content, String filename) throws Exception {\r |
a756211f | 52 | ensureDir(filename);\r |
53 | PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter(filename)));\r | |
54 | outfile.append(content);\r | |
55 | outfile.flush();\r | |
56 | outfile.close();\r | |
57 | }\r | |
58 | \r | |
5ea254f6 | 59 | //-----------------------------------file&string---------------------------------------//\r |
60 | \r | |
61 | //--------------------------------------dir--------------------------------------------//\r | |
62 | \r | |
63 | public static final void ensureDir(String objFileWhole) {\r | |
90503bad | 64 | File tempdir;\r |
8c4eeeb6 | 65 | Matcher mtrseparate = ptnseparate.matcher(objFileWhole);\r |
90503bad | 66 | if (mtrseparate.find()) {\r |
67 | tempdir = new File(mtrseparate.group(1));\r | |
68 | if (!tempdir.exists()) tempdir.mkdirs();\r | |
69 | }\r | |
fed802b1 | 70 | }\r |
71 | \r | |
5ea254f6 | 72 | public static final void deleteDir(String objFileWhole) {\r |
73 | String[] list = new File(objFileWhole).list();\r | |
74 | File temp;\r | |
fed802b1 | 75 | for (int i = 0 ; i < list.length ; i++) {\r |
5ea254f6 | 76 | temp = new File(objFileWhole + File.separator + list[i]);\r |
77 | if (temp.isDirectory()) {\r | |
78 | deleteDir(objFileWhole + File.separator + list[i]);\r | |
fed802b1 | 79 | } else {\r |
5ea254f6 | 80 | temp.delete();\r |
fed802b1 | 81 | }\r |
82 | }\r | |
5ea254f6 | 83 | new File(objFileWhole).delete();\r |
a756211f | 84 | }\r |
85 | \r | |
5ea254f6 | 86 | public static final String dirCopy_(String src) throws Exception {\r |
8c4eeeb6 | 87 | Matcher mtrseparate = Common.ptnseparate.matcher(src);\r |
88 | if (mtrseparate.find()) {\r | |
89 | dirCopy(src, mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2));\r | |
90 | }\r | |
91 | return mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2);\r | |
92 | }\r | |
93 | \r | |
5ea254f6 | 94 | public static final void dirCopy(String src, String des) throws Exception {\r |
8c4eeeb6 | 95 | String[] list = new File(src).list();\r |
96 | File test;\r | |
97 | \r | |
98 | for (int i = 0 ; i < list.length ; i++) {\r | |
99 | test = new File(src + File.separator + list[i]);\r | |
100 | if (test.isDirectory()) {\r | |
101 | dirCopy(src + File.separator + list[i], des + File.separator + list[i]);\r | |
102 | } else {\r | |
103 | ensureDir(des + File.separator + list[i]);\r | |
104 | string2file(file2string(src + File.separator + list[i]), des + File.separator + list[i]);\r | |
105 | }\r | |
106 | }\r | |
107 | }\r | |
5ea254f6 | 108 | \r |
109 | //--------------------------------------dir--------------------------------------------//\r | |
110 | \r | |
111 | //-------------------------------like python walk-----------------------------------------//\r | |
112 | \r | |
113 | public static final void toDoAll(String path, Method md, Object obj, Object[] args, int type) throws Exception {\r | |
114 | String[] list = new File(path).list();\r | |
115 | ArrayList<Object> _args = new ArrayList<Object>();\r | |
116 | \r | |
117 | _args.add(path);\r | |
118 | if (args != null) {\r | |
119 | for (int i = 0; i < args.length; i++) {\r | |
120 | _args.add(args[i]);\r | |
121 | }\r | |
122 | }\r | |
123 | \r | |
124 | if (type == DIR || type == BOTH) {\r | |
125 | md.invoke(obj, _args.toArray());\r | |
126 | }\r | |
127 | for (int i = 0 ; i < list.length ; i++) {\r | |
128 | if (new File(path + File.separator + list[i]).isDirectory()) {\r | |
129 | toDoAll(path + File.separator + list[i], md, obj, args, type);\r | |
130 | } else {\r | |
131 | if (type == FILE || type == BOTH) {\r | |
132 | _args.set(0, path + File.separator + list[i]);\r | |
133 | md.invoke(obj, _args.toArray());\r | |
134 | }\r | |
135 | }\r | |
136 | }\r | |
137 | }\r | |
fed802b1 | 138 | \r |
139 | public static void toDoAll(String path, ForDoAll fda) throws Exception { // filter of file type can be done in toDo\r | |
140 | String[] list = new File(path).list();\r | |
141 | File test;\r | |
142 | \r | |
143 | for (int i = 0 ; i < list.length ; i++) {\r | |
144 | test = new File(path + File.separator + list[i]);\r | |
145 | if (test.isDirectory()) {\r | |
146 | toDoAll(path + File.separator + list[i], fda);\r | |
147 | } else {\r | |
148 | fda.toDo(path + File.separator + list[i]);\r | |
149 | }\r | |
150 | }\r | |
151 | }\r | |
152 | \r | |
153 | public static interface ForDoAll {\r | |
154 | public void toDo(String filepath) throws Exception;\r | |
90503bad | 155 | }\r |
156 | }\r |