]>
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 void ensureDir(String objFileWhole
) {
66 Matcher mtrseparate
= ptnseparate
.matcher(objFileWhole
);
67 if (mtrseparate
.find()) {
68 tempdir
= new File(mtrseparate
.group(1));
69 if (!tempdir
.exists()) tempdir
.mkdirs();
73 public static final void deleteDir(String objFileWhole
) {
74 String
[] list
= new File(objFileWhole
).list();
76 for (int i
= 0 ; i
< list
.length
; i
++) {
77 temp
= new File(objFileWhole
+ File
.separator
+ list
[i
]);
78 if (temp
.isDirectory()) {
79 deleteDir(objFileWhole
+ File
.separator
+ list
[i
]);
84 new File(objFileWhole
).delete();
87 public static final String
dirCopy_(String src
) throws Exception
{
88 Matcher mtrseparate
= Common
.ptnseparate
.matcher(src
);
89 if (mtrseparate
.find()) {
90 dirCopy(src
, mtrseparate
.group(1) + File
.separator
+ "_" + mtrseparate
.group(2));
92 return mtrseparate
.group(1) + File
.separator
+ "_" + mtrseparate
.group(2);
95 public static final void dirCopy(String src
, String des
) throws Exception
{
96 String
[] list
= new File(src
).list();
99 for (int i
= 0 ; i
< list
.length
; i
++) {
100 test
= new File(src
+ File
.separator
+ list
[i
]);
101 if (test
.isDirectory()) {
102 dirCopy(src
+ File
.separator
+ list
[i
], des
+ File
.separator
+ list
[i
]);
104 ensureDir(des
+ File
.separator
+ list
[i
]);
105 string2file(file2string(src
+ File
.separator
+ list
[i
]), des
+ File
.separator
+ list
[i
]);
110 //--------------------------------------dir--------------------------------------------//
112 //-------------------------------like python walk-----------------------------------------//
114 public static final void toDoAll(String path
, Method md
, Object obj
, Object
[] args
, int type
) throws Exception
{
115 String
[] list
= new File(path
).list();
116 ArrayList
<Object
> _args
= new ArrayList
<Object
>();
120 for (int i
= 0; i
< args
.length
; i
++) {
125 if (type
== DIR
|| type
== BOTH
) {
126 md
.invoke(obj
, _args
.toArray());
128 for (int i
= 0 ; i
< list
.length
; i
++) {
129 if (new File(path
+ File
.separator
+ list
[i
]).isDirectory()) {
130 toDoAll(path
+ File
.separator
+ list
[i
], md
, obj
, args
, type
);
132 if (type
== FILE
|| type
== BOTH
) {
133 _args
.set(0, path
+ File
.separator
+ list
[i
]);
134 md
.invoke(obj
, _args
.toArray());
140 public static void toDoAll(String path
, ForDoAll fda
) throws Exception
{ // filter of file type can be done in toDo
141 String
[] list
= new File(path
).list();
144 for (int i
= 0 ; i
< list
.length
; i
++) {
145 test
= new File(path
+ File
.separator
+ list
[i
]);
146 if (test
.isDirectory()) {
147 toDoAll(path
+ File
.separator
+ list
[i
], fda
);
149 fda
.toDo(path
+ File
.separator
+ list
[i
]);
154 public static interface ForDoAll
{
155 public void toDo(String filepath
) throws Exception
;