]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/MigrationTools/org/tianocore/migration/Common.java
Many Many Modifies
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / Common.java
1 /** @file
2
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
8
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.
11
12 **/
13 package org.tianocore.migration;
14
15 import java.io.*;
16 import java.util.regex.*;
17 import java.util.*;
18 import java.lang.reflect.*;
19
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;
24
25 public static final Pattern ptnseparate = Pattern.compile("(.*)\\\\([^\\\\]*)");
26
27 //-------------------------------------regex------------------------------------------//
28
29 public static final String replaceAll(String line, Pattern ptn, String des) {
30 Matcher mtr = ptn.matcher(line);
31 if (mtr.find()) {
32 return mtr.replaceAll(des);
33 }
34 return line;
35 }
36
37 //-------------------------------------regex------------------------------------------//
38
39 //-----------------------------------file&string---------------------------------------//
40
41 public static final String file2string(String filename) throws Exception {
42 BufferedReader rd = new BufferedReader(new FileReader(filename));
43 StringBuffer wholefile = new StringBuffer();
44 String line;
45 while ((line = rd.readLine()) != null) {
46 wholefile.append(line + "\n");
47 }
48 return wholefile.toString();
49 }
50
51 public static final void string2file(String content, String filename) throws Exception {
52 ensureDir(filename);
53 PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
54 outfile.append(content);
55 outfile.flush();
56 outfile.close();
57 }
58
59 //-----------------------------------file&string---------------------------------------//
60
61 //--------------------------------------dir--------------------------------------------//
62
63 public static final void ensureDir(String objFileWhole) {
64 File tempdir;
65 Matcher mtrseparate = ptnseparate.matcher(objFileWhole);
66 if (mtrseparate.find()) {
67 tempdir = new File(mtrseparate.group(1));
68 if (!tempdir.exists()) tempdir.mkdirs();
69 }
70 }
71
72 public static final void deleteDir(String objFileWhole) {
73 String[] list = new File(objFileWhole).list();
74 File temp;
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]);
79 } else {
80 temp.delete();
81 }
82 }
83 new File(objFileWhole).delete();
84 }
85
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));
90 }
91 return mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2);
92 }
93
94 public static final void dirCopy(String src, String des) throws Exception {
95 String[] list = new File(src).list();
96 File test;
97
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]);
102 } else {
103 ensureDir(des + File.separator + list[i]);
104 string2file(file2string(src + File.separator + list[i]), des + File.separator + list[i]);
105 }
106 }
107 }
108
109 //--------------------------------------dir--------------------------------------------//
110
111 //-------------------------------like python walk-----------------------------------------//
112
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>();
116
117 _args.add(path);
118 if (args != null) {
119 for (int i = 0; i < args.length; i++) {
120 _args.add(args[i]);
121 }
122 }
123
124 if (type == DIR || type == BOTH) {
125 md.invoke(obj, _args.toArray());
126 }
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);
130 } else {
131 if (type == FILE || type == BOTH) {
132 _args.set(0, path + File.separator + list[i]);
133 md.invoke(obj, _args.toArray());
134 }
135 }
136 }
137 }
138
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();
141 File test;
142
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);
147 } else {
148 fda.toDo(path + File.separator + list[i]);
149 }
150 }
151 }
152
153 public static interface ForDoAll {
154 public void toDo(String filepath) throws Exception;
155 }
156 }