]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/MigrationTools/org/tianocore/migration/Common.java
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
;
16 import java
.util
.regex
.*;
18 import java
.lang
.reflect
.*;
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;
25 public static final String strseparate
= "(.*)\\\\([^\\\\]*)";
26 public static final Pattern ptnseparate
= Pattern
.compile("(.*)\\\\([^\\\\]*)");
28 //-------------------------------------regex------------------------------------------//
30 public static final String
replaceAll(String line
, Pattern ptn
, String des
) {
31 Matcher mtr
= ptn
.matcher(line
);
34 return mtr
.replaceAll(des
);
40 //-------------------------------------regex------------------------------------------//
42 //-----------------------------------file&string---------------------------------------//
44 public static final String
file2string(String filename
) throws Exception
{
45 BufferedReader rd
= new BufferedReader(new FileReader(filename
));
46 StringBuffer wholefile
= new StringBuffer();
48 while ((line
= rd
.readLine()) != null) {
49 wholefile
.append(line
+ "\n");
51 return wholefile
.toString();
54 public static final void string2file(String content
, String filename
) throws Exception
{
56 PrintWriter outfile
= new PrintWriter(new BufferedWriter(new FileWriter(filename
)));
57 outfile
.append(content
);
62 //-----------------------------------file&string---------------------------------------//
64 //--------------------------------------dir--------------------------------------------//
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);
72 public static final void ensureDir(String objFileWhole
) {
74 Matcher mtrseparate
= ptnseparate
.matcher(objFileWhole
);
75 if (mtrseparate
.find()) {
76 tempdir
= new File(mtrseparate
.group(1));
77 if (!tempdir
.exists()) tempdir
.mkdirs();
81 public static final void deleteDir(String objFileWhole
) {
82 String
[] list
= new File(objFileWhole
).list();
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
]);
92 new File(objFileWhole
).delete();
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));
100 return mtrseparate
.group(1) + File
.separator
+ "_" + mtrseparate
.group(2);
103 public static final void dirCopy(String src
, String des
) throws Exception
{
104 String
[] list
= new File(src
).list();
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
]);
112 ensureDir(des
+ File
.separator
+ list
[i
]);
113 string2file(file2string(src
+ File
.separator
+ list
[i
]), des
+ File
.separator
+ list
[i
]);
118 //--------------------------------------dir--------------------------------------------//
120 //-------------------------------like python walk-----------------------------------------//
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
>();
128 for (int i
= 0; i
< args
.length
; i
++) {
133 if (type
== DIR
|| type
== BOTH
) {
134 md
.invoke(obj
, _args
.toArray());
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
);
140 if (type
== FILE
|| type
== BOTH
) {
141 _args
.set(0, path
+ File
.separator
+ list
[i
]);
142 md
.invoke(obj
, _args
.toArray());
148 public static final void toDoAll(Set
<String
> set
, ForDoAll fda
) throws Exception
{
149 Iterator
<String
> di
= set
.iterator();
150 while (di
.hasNext()) {
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();
159 if (type
== DIR
|| type
== BOTH
) {
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
);
169 if (type
== FILE
|| type
== BOTH
) {
170 fda
.run(path
+ File
.separator
+ list
[i
]);
176 public static interface ForDoAll
{
177 public void run(String filepath
) throws Exception
;
179 public boolean filter(File dir
);
182 public static abstract class Laplace
{
183 public void transform(String src
, String des
) throws Exception
{
184 Common
.string2file(operation(Common
.file2string(src
)), des
);
187 public abstract String
operation(String wholeline
);
189 public abstract boolean recognize(String filename
);
191 public abstract String
namechange(String oldname
);
194 public static interface Element
{
196 // public int replace = 0;
197 // public int type = 1;
199 public String
getReplace(String key
);
201 // public void getType(String key);
203 // public void setReplace(int num);
205 // public void setType(int num);