]>
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
);
33 return mtr
.replaceAll(des
);
38 //-------------------------------------regex------------------------------------------//
40 //-----------------------------------file&string---------------------------------------//
42 public static final String
file2string(String filename
) throws Exception
{
43 BufferedReader rd
= new BufferedReader(new FileReader(filename
));
44 StringBuffer wholefile
= new StringBuffer();
46 while ((line
= rd
.readLine()) != null) {
47 wholefile
.append(line
+ "\n");
49 return wholefile
.toString();
52 public static final void string2file(String content
, String filename
) throws Exception
{
54 PrintWriter outfile
= new PrintWriter(new BufferedWriter(new FileWriter(filename
)));
55 outfile
.append(content
);
60 //-----------------------------------file&string---------------------------------------//
62 //--------------------------------------dir--------------------------------------------//
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);
70 public static final void ensureDir(String objFileWhole
) {
72 Matcher mtrseparate
= ptnseparate
.matcher(objFileWhole
);
73 if (mtrseparate
.find()) {
74 tempdir
= new File(mtrseparate
.group(1));
75 if (!tempdir
.exists()) tempdir
.mkdirs();
79 public static final void deleteDir(String objFileWhole
) {
80 String
[] list
= new File(objFileWhole
).list();
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
]);
90 new File(objFileWhole
).delete();
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));
98 return mtrseparate
.group(1) + File
.separator
+ "_" + mtrseparate
.group(2);
101 public static final void dirCopy(String src
, String des
) throws Exception
{
102 String
[] list
= new File(src
).list();
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
]);
110 ensureDir(des
+ File
.separator
+ list
[i
]);
111 string2file(file2string(src
+ File
.separator
+ list
[i
]), des
+ File
.separator
+ list
[i
]);
116 //--------------------------------------dir--------------------------------------------//
118 //-------------------------------like python walk-----------------------------------------//
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
>();
126 for (int i
= 0; i
< args
.length
; i
++) {
131 if (type
== DIR
|| type
== BOTH
) {
132 md
.invoke(obj
, _args
.toArray());
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
);
138 if (type
== FILE
|| type
== BOTH
) {
139 _args
.set(0, path
+ File
.separator
+ list
[i
]);
140 md
.invoke(obj
, _args
.toArray());
146 public static final void toDoAll(Set
<String
> set
, ForDoAll fda
) throws Exception
{
147 Iterator
<String
> di
= set
.iterator();
148 while (di
.hasNext()) {
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();
157 if (type
== DIR
|| type
== BOTH
) {
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
);
167 if (type
== FILE
|| type
== BOTH
) {
168 fda
.run(path
+ File
.separator
+ list
[i
]);
174 public static interface ForDoAll
{
175 public void run(String filepath
) throws Exception
;
177 public boolean filter(File dir
);
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
);
185 public abstract String
operation(String wholeline
);