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