]>
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 Pattern ptnseparate
= Pattern
.compile("(.*)\\\\([^\\\\]*)");
27 //-------------------------------------regex------------------------------------------//
29 public static final String
replaceAll(String line
, Pattern ptn
, String des
) {
30 Matcher mtr
= ptn
.matcher(line
);
32 return mtr
.replaceAll(des
);
37 //-------------------------------------regex------------------------------------------//
39 //-----------------------------------file&string---------------------------------------//
41 public static final String
file2string(String filename
) throws Exception
{
42 BufferedReader rd
= new BufferedReader(new FileReader(filename
));
43 StringBuffer wholefile
= new StringBuffer();
45 while ((line
= rd
.readLine()) != null) {
46 wholefile
.append(line
+ "\n");
48 return wholefile
.toString();
51 public static final void string2file(String content
, String filename
) throws Exception
{
53 PrintWriter outfile
= new PrintWriter(new BufferedWriter(new FileWriter(filename
)));
54 outfile
.append(content
);
59 //-----------------------------------file&string---------------------------------------//
61 //--------------------------------------dir--------------------------------------------//
63 public static final void ensureDir(String objFileWhole
) {
65 Matcher mtrseparate
= ptnseparate
.matcher(objFileWhole
);
66 if (mtrseparate
.find()) {
67 tempdir
= new File(mtrseparate
.group(1));
68 if (!tempdir
.exists()) tempdir
.mkdirs();
72 public static final void deleteDir(String objFileWhole
) {
73 String
[] list
= new File(objFileWhole
).list();
75 for (int i
= 0 ; i
< list
.length
; i
++) {
76 temp
= new File(objFileWhole
+ File
.separator
+ list
[i
]);
77 if (temp
.isDirectory()) {
78 deleteDir(objFileWhole
+ File
.separator
+ list
[i
]);
83 new File(objFileWhole
).delete();
86 public static final String
dirCopy_(String src
) throws Exception
{
87 Matcher mtrseparate
= Common
.ptnseparate
.matcher(src
);
88 if (mtrseparate
.find()) {
89 dirCopy(src
, mtrseparate
.group(1) + File
.separator
+ "_" + mtrseparate
.group(2));
91 return mtrseparate
.group(1) + File
.separator
+ "_" + mtrseparate
.group(2);
94 public static final void dirCopy(String src
, String des
) throws Exception
{
95 String
[] list
= new File(src
).list();
98 for (int i
= 0 ; i
< list
.length
; i
++) {
99 test
= new File(src
+ File
.separator
+ list
[i
]);
100 if (test
.isDirectory()) {
101 dirCopy(src
+ File
.separator
+ list
[i
], des
+ File
.separator
+ list
[i
]);
103 ensureDir(des
+ File
.separator
+ list
[i
]);
104 string2file(file2string(src
+ File
.separator
+ list
[i
]), des
+ File
.separator
+ list
[i
]);
109 //--------------------------------------dir--------------------------------------------//
111 //-------------------------------like python walk-----------------------------------------//
113 public static final void toDoAll(String path
, Method md
, Object obj
, Object
[] args
, int type
) throws Exception
{
114 String
[] list
= new File(path
).list();
115 ArrayList
<Object
> _args
= new ArrayList
<Object
>();
119 for (int i
= 0; i
< args
.length
; i
++) {
124 if (type
== DIR
|| type
== BOTH
) {
125 md
.invoke(obj
, _args
.toArray());
127 for (int i
= 0 ; i
< list
.length
; i
++) {
128 if (new File(path
+ File
.separator
+ list
[i
]).isDirectory()) {
129 toDoAll(path
+ File
.separator
+ list
[i
], md
, obj
, args
, type
);
131 if (type
== FILE
|| type
== BOTH
) {
132 _args
.set(0, path
+ File
.separator
+ list
[i
]);
133 md
.invoke(obj
, _args
.toArray());
139 public static void toDoAll(String path
, ForDoAll fda
) throws Exception
{ // filter of file type can be done in toDo
140 String
[] list
= new File(path
).list();
143 for (int i
= 0 ; i
< list
.length
; i
++) {
144 test
= new File(path
+ File
.separator
+ list
[i
]);
145 if (test
.isDirectory()) {
146 toDoAll(path
+ File
.separator
+ list
[i
], fda
);
148 fda
.toDo(path
+ File
.separator
+ list
[i
]);
153 public static interface ForDoAll
{
154 public void toDo(String filepath
) throws Exception
;