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