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