]>
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 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");
56 return wholefile
.toString();
59 public static final void string2file(String content
, String filename
) throws Exception
{
61 PrintWriter outfile
= new PrintWriter(new BufferedWriter(new FileWriter(filename
)));
62 outfile
.append(content
);
67 //-----------------------------------file&string---------------------------------------//
69 //--------------------------------------dir--------------------------------------------//
71 public static final HashSet<String> walkDir(String path, int mode) throws Exception {
72 HashSet<String> pathlist = new HashSet<String>();
73 Common.toDoAll(path, Common.class.getMethod("walkDir", String.class), null, null, mode);
77 public static final void ensureDir(String objFileWhole
) {
79 Matcher mtrseparate
= ptnseparate
.matcher(objFileWhole
);
80 if (mtrseparate
.find()) {
81 tempdir
= new File(mtrseparate
.group(1));
82 if (!tempdir
.exists()) tempdir
.mkdirs();
86 public static final void deleteDir(String objFileWhole
) {
87 String
[] list
= new File(objFileWhole
).list();
89 for (int i
= 0 ; i
< list
.length
; i
++) {
90 temp
= new File(objFileWhole
+ File
.separator
+ list
[i
]);
91 if (temp
.isDirectory()) {
92 deleteDir(objFileWhole
+ File
.separator
+ list
[i
]);
97 new File(objFileWhole
).delete();
100 public static final String
dirCopy_(String src
) throws Exception
{
101 Matcher mtrseparate
= Common
.ptnseparate
.matcher(src
);
102 if (mtrseparate
.find()) {
103 dirCopy(src
, mtrseparate
.group(1) + File
.separator
+ "_" + mtrseparate
.group(2));
105 return mtrseparate
.group(1) + File
.separator
+ "_" + mtrseparate
.group(2);
108 public static final void dirCopy(String src
, String des
) throws Exception
{
109 String
[] list
= new File(src
).list();
112 for (int i
= 0 ; i
< list
.length
; i
++) {
113 test
= new File(src
+ File
.separator
+ list
[i
]);
114 if (test
.isDirectory()) {
115 dirCopy(src
+ File
.separator
+ list
[i
], des
+ File
.separator
+ list
[i
]);
117 ensureDir(des
+ File
.separator
+ list
[i
]);
118 string2file(file2string(src
+ File
.separator
+ list
[i
]), des
+ File
.separator
+ list
[i
]);
123 //--------------------------------------dir--------------------------------------------//
125 //-------------------------------like python walk-----------------------------------------//
127 public static final void toDoAll(String path
, Method md
, Object obj
, Object
[] args
, int type
) throws Exception
{
128 String
[] list
= new File(path
).list();
129 ArrayList
<Object
> _args
= new ArrayList
<Object
>();
133 for (int i
= 0; i
< args
.length
; i
++) {
138 if (type
== DIR
|| type
== BOTH
) {
139 md
.invoke(obj
, _args
.toArray());
141 for (int i
= 0 ; i
< list
.length
; i
++) {
142 if (new File(path
+ File
.separator
+ list
[i
]).isDirectory()) {
143 toDoAll(path
+ File
.separator
+ list
[i
], md
, obj
, args
, type
);
145 if (type
== FILE
|| type
== BOTH
) {
146 _args
.set(0, path
+ File
.separator
+ list
[i
]);
147 md
.invoke(obj
, _args
.toArray());
153 public static final void toDoAll(Set
<String
> set
, ForDoAll fda
) throws Exception
{
154 Iterator
<String
> di
= set
.iterator();
155 while (di
.hasNext()) {
160 public static final void toDoAll(String path
, ForDoAll fda
, int type
) throws Exception
{ // filter of file type can be done in toDo
161 String
[] list
= new File(path
).list();
164 if (type
== DIR
|| type
== BOTH
) {
167 for (int i
= 0 ; i
< list
.length
; i
++) {
168 test
= new File(path
+ File
.separator
+ list
[i
]);
169 if (test
.isDirectory()) {
170 if (fda
.filter(test
)) {
171 toDoAll(path
+ File
.separator
+ list
[i
], fda
, type
);
174 if (type
== FILE
|| type
== BOTH
) {
175 fda
.run(path
+ File
.separator
+ list
[i
]);
181 public static interface ForDoAll
{
182 public void run(String filepath
) throws Exception
;
184 public boolean filter(File dir
);
187 public static abstract class Laplace
{
188 public void transform(String src
, String des
) throws Exception
{
189 Common
.string2file(operation(Common
.file2string(src
)), des
);
192 public abstract String
operation(String wholeline
);
194 public abstract boolean recognize(String filename
);
196 public abstract String
namechange(String oldname
);
199 public static interface Element
{
201 // public int replace = 0;
202 // public int type = 1;
204 public String
getReplace(String key
);
206 // public void getType(String key);
208 // public void setReplace(int num);
210 // public void setType(int num);