]>
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 STRSEPARATER
= "(.*)\\\\([^\\\\]*)";
26 public static final Pattern PTNSEPARATER
= 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 public static final boolean find (String line
, String regex
) {
41 Pattern ptn
= Pattern
.compile(regex
);
43 return ptn
.matcher (line
).find ();
45 //-------------------------------------regex------------------------------------------//
47 //-----------------------------------file&string---------------------------------------//
49 public static final String
file2string(String filename
) throws Exception
{
50 BufferedReader rd
= new BufferedReader(new FileReader(filename
));
51 StringBuffer wholefile
= new StringBuffer();
53 while ((line
= rd
.readLine()) != null) {
54 wholefile
.append(line
+ "\n");
57 return wholefile
.toString();
60 public static final void string2file(String content
, String filename
) throws Exception
{
62 PrintWriter outfile
= new PrintWriter(new BufferedWriter(new FileWriter(filename
)));
63 outfile
.append(content
);
68 public static final void fileCopy(String src
, String des
) throws Exception
{
69 string2file(file2string(src
), des
);
72 //-----------------------------------file&string---------------------------------------//
74 //--------------------------------------dir--------------------------------------------//
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);
82 public static final void ensureDir(String objFileWhole
) {
84 Matcher mtrseparate
= PTNSEPARATER
.matcher(objFileWhole
);
85 if (mtrseparate
.find()) {
86 tempdir
= new File(mtrseparate
.group(1));
87 if (!tempdir
.exists()) tempdir
.mkdirs();
91 public static final void deleteDir(String objFileWhole
) {
92 String
[] list
= new File(objFileWhole
).list();
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
]);
102 new File(objFileWhole
).delete();
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));
110 return mtrseparate
.group(1) + File
.separator
+ "_" + mtrseparate
.group(2);
113 public static final void dirCopy(String src
, String des
) throws Exception
{
114 String
[] list
= new File(src
).list();
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
]);
123 //ensureDir(des + File.separator + list[i]);
124 string2file(file2string(src
+ File
.separator
+ list
[i
]), des
+ File
.separator
+ list
[i
]);
129 public static final void oneLevelDirCopy(String src
, String des
, String type
) throws Exception
{
130 String
[] list
= new File(src
).list();
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
]);
140 //--------------------------------------dir--------------------------------------------//
142 //-------------------------------like python walk-----------------------------------------//
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
>();
150 for (int i
= 0; i
< args
.length
; i
++) {
155 if (type
== DIR
|| type
== BOTH
) {
156 md
.invoke(obj
, _args
.toArray());
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
);
162 if (type
== FILE
|| type
== BOTH
) {
163 _args
.set(0, path
+ File
.separator
+ list
[i
]);
164 md
.invoke(obj
, _args
.toArray());
170 public static final void toDoAll(Set
<String
> set
, ForDoAll fda
) throws Exception
{
171 Iterator
<String
> di
= set
.iterator();
172 while (di
.hasNext()) {
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();
181 if (type
== DIR
|| type
== BOTH
) {
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
);
191 if (type
== FILE
|| type
== BOTH
) {
192 fda
.run(path
+ File
.separator
+ list
[i
]);
198 public static interface ForDoAll
{
199 public void run(String filepath
) throws Exception
;
201 public boolean filter(File dir
);
204 public static abstract class Laplace
{
205 public void transform(String src
, String des
) throws Exception
{
206 Common
.string2file(operation(Common
.file2string(src
)), des
);
209 public abstract String
operation(String wholeline
);
211 public abstract boolean recognize(String filename
);
213 public abstract String
namechange(String oldname
);
216 public static interface Element
{
218 // public int replace = 0;
219 // public int type = 1;
221 public String
getReplace(String key
);
223 // public void getType(String key);
225 // public void setReplace(int num);
227 // public void setType(int num);