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