]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/MigrationTools/org/tianocore/migration/Common.java
3a6f179df83861402a613c4690a82481b687ed43
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
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.
13 package org
.tianocore
.migration
;
15 import java
.io
.BufferedReader
;
16 import java
.io
.BufferedWriter
;
18 import java
.io
.FileReader
;
19 import java
.io
.FileWriter
;
20 import java
.io
.PrintWriter
;
21 import java
.lang
.reflect
.Method
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Iterator
;
25 import java
.util
.regex
.Matcher
;
26 import java
.util
.regex
.Pattern
;
28 public final class Common
{
29 public static final int BOTH
= 0;
31 public static final int FILE
= 1;
33 public static final int DIR
= 2;
35 public static final String STRSEPARATER
= "(.*)\\\\([^\\\\]*)";
37 public static final Pattern PTNSEPARATER
= Pattern
38 .compile("(.*)\\\\([^\\\\]*)");
40 // -------------------------------------regex------------------------------------------//
42 public static final String
replaceAll(String line
, Pattern ptn
, String des
) {
43 Matcher mtr
= ptn
.matcher(line
);
46 return mtr
.replaceAll(des
);
52 public static final boolean find(String line
, String regex
) {
53 Pattern ptn
= Pattern
.compile(regex
);
55 return ptn
.matcher(line
).find();
58 // -------------------------------------regex------------------------------------------//
60 // -----------------------------------file&string---------------------------------------//
62 public static final String
file2string(String filename
) throws Exception
{
63 BufferedReader rd
= new BufferedReader(new FileReader(filename
));
64 StringBuffer wholefile
= new StringBuffer();
66 while ((line
= rd
.readLine()) != null) {
67 wholefile
.append(line
+ "\n");
70 return wholefile
.toString();
73 public static final void string2file(String content
, String filename
)
76 PrintWriter outfile
= new PrintWriter(new BufferedWriter(
77 new FileWriter(filename
)));
78 outfile
.append(content
);
83 public static final void fileCopy(String src
, String des
) throws Exception
{
84 string2file(file2string(src
), des
);
87 // -----------------------------------file&string---------------------------------------//
89 // --------------------------------------dir--------------------------------------------//
91 * public static final HashSet<String> walkDir(String path, int mode)
92 * throws Exception { HashSet<String> pathlist = new HashSet<String>();
93 * Common.toDoAll(path, Common.class.getMethod("walkDir", String.class),
94 * null, null, mode); return pathlist; }
96 public static final void ensureDir(String objFileWhole
) {
98 Matcher mtrseparate
= PTNSEPARATER
.matcher(objFileWhole
);
99 if (mtrseparate
.find()) {
100 tempdir
= new File(mtrseparate
.group(1));
101 if (!tempdir
.exists())
106 public static final void deleteDir(String objFileWhole
) {
107 String
[] list
= new File(objFileWhole
).list();
109 for (int i
= 0; i
< list
.length
; i
++) {
110 temp
= new File(objFileWhole
+ File
.separator
+ list
[i
]);
111 if (temp
.isDirectory()) {
112 deleteDir(objFileWhole
+ File
.separator
+ list
[i
]);
117 new File(objFileWhole
).delete();
120 public static final String
dirCopy_(String src
) throws Exception
{
121 Matcher mtrseparate
= Common
.PTNSEPARATER
.matcher(src
);
122 if (mtrseparate
.find()) {
123 dirCopy(src
, mtrseparate
.group(1) + File
.separator
+ "_"
124 + mtrseparate
.group(2));
126 return mtrseparate
.group(1) + File
.separator
+ "_"
127 + mtrseparate
.group(2);
130 public static final void dirCopy(String src
, String des
) throws Exception
{
131 String
[] list
= new File(src
).list();
135 for (int i
= 0; i
< list
.length
; i
++) {
136 test
= new File(src
+ File
.separator
+ list
[i
]);
137 if (test
.isDirectory()) {
138 dirCopy(src
+ File
.separator
+ list
[i
], des
+ File
.separator
141 // ensureDir(des + File.separator + list[i]);
142 string2file(file2string(src
+ File
.separator
+ list
[i
]), des
143 + File
.separator
+ list
[i
]);
148 public static final void oneLevelDirCopy(String src
, String des
, String type
)
150 String
[] list
= new File(src
).list();
153 for (int i
= 0; i
< list
.length
; i
++) {
154 if (list
[i
].contains(type
)) {
155 string2file(file2string(src
+ File
.separator
+ list
[i
]), des
156 + File
.separator
+ list
[i
]);
161 // --------------------------------------dir--------------------------------------------//
163 // -------------------------------like python
164 // walk-----------------------------------------//
166 public static final void toDoAll(String path
, Method md
, Object obj
,
167 Object
[] args
, int type
) throws Exception
{
168 String
[] list
= new File(path
).list();
169 ArrayList
<Object
> _args
= new ArrayList
<Object
>();
173 for (int i
= 0; i
< args
.length
; i
++) {
178 if (type
== DIR
|| type
== BOTH
) {
179 md
.invoke(obj
, _args
.toArray());
181 for (int i
= 0; i
< list
.length
; i
++) {
182 if (new File(path
+ File
.separator
+ list
[i
]).isDirectory()) {
183 toDoAll(path
+ File
.separator
+ list
[i
], md
, obj
, args
, type
);
185 if (type
== FILE
|| type
== BOTH
) {
186 _args
.set(0, path
+ File
.separator
+ list
[i
]);
187 md
.invoke(obj
, _args
.toArray());
193 public static final void toDoAll(Set
<String
> set
, ForDoAll fda
)
195 Iterator
<String
> di
= set
.iterator();
196 while (di
.hasNext()) {
201 public static final void toDoAll(String path
, ForDoAll fda
, int type
)
202 throws Exception
{ // filter of file type can be done in toDo
203 String
[] list
= new File(path
).list();
206 if (type
== DIR
|| type
== BOTH
) {
209 for (int i
= 0; i
< list
.length
; i
++) {
210 test
= new File(path
+ File
.separator
+ list
[i
]);
211 if (test
.isDirectory()) {
212 if (fda
.filter(test
)) {
213 toDoAll(path
+ File
.separator
+ list
[i
], fda
, type
);
216 if (type
== FILE
|| type
== BOTH
) {
217 fda
.run(path
+ File
.separator
+ list
[i
]);
223 public static interface ForDoAll
{
224 public void run(String filepath
) throws Exception
;
226 public boolean filter(File dir
);
229 public static abstract class Laplace
{
230 public void transform(String src
, String des
) throws Exception
{
231 Common
.string2file(operation(Common
.file2string(src
)), des
);
234 public abstract String
operation(String wholeline
);
236 public abstract boolean recognize(String filename
);
238 public abstract String
namechange(String oldname
);
241 public static interface Element
{
243 // public int replace = 0;
244 // public int type = 1;
246 public String
getReplace(String key
);
248 // public void getType(String key);
250 // public void setReplace(int num);
252 // public void setType(int num);