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