+++ /dev/null
-/** @file\r
- \r
- Copyright (c) 2006, Intel Corporation\r
- All rights reserved. This program and the accompanying materials\r
- are licensed and made available under the terms and conditions of the BSD License\r
- which accompanies this distribution. The full text of the license may be found at\r
- http://opensource.org/licenses/bsd-license.php\r
- \r
- THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
- \r
- **/\r
-package org.tianocore.migration;\r
-\r
-import java.io.*;\r
-import java.util.regex.*;\r
-import java.util.*;\r
-import java.lang.reflect.*;\r
-\r
-public final class Common {\r
- public static final int BOTH = 0;\r
- public static final int FILE = 1;\r
- public static final int DIR = 2;\r
- \r
- public static final String STRSEPARATER = "(.*)\\\\([^\\\\]*)";\r
- public static final Pattern PTNSEPARATER = Pattern.compile("(.*)\\\\([^\\\\]*)");\r
-\r
- //-------------------------------------regex------------------------------------------//\r
- \r
- public static final String replaceAll(String line, Pattern ptn, String des) {\r
- Matcher mtr = ptn.matcher(line);\r
-\r
- if (mtr.find()) {\r
- return mtr.replaceAll(des);\r
- }\r
- \r
- return line;\r
- }\r
-\r
- public static final boolean find (String line, String regex) {\r
- Pattern ptn = Pattern.compile(regex);\r
-\r
- return ptn.matcher (line).find (); \r
- }\r
- //-------------------------------------regex------------------------------------------//\r
- \r
- //-----------------------------------file&string---------------------------------------//\r
- \r
- public static final String file2string(String filename) throws Exception {\r
- BufferedReader rd = new BufferedReader(new FileReader(filename));\r
- StringBuffer wholefile = new StringBuffer();\r
- String line;\r
- while ((line = rd.readLine()) != null) {\r
- wholefile.append(line + "\n");\r
- }\r
- rd.close();\r
- return wholefile.toString();\r
- }\r
-\r
- public static final void string2file(String content, String filename) throws Exception {\r
- ensureDir(filename);\r
- PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter(filename)));\r
- outfile.append(content);\r
- outfile.flush();\r
- outfile.close();\r
- }\r
-\r
- public static final void fileCopy(String src, String des) throws Exception {\r
- string2file(file2string(src), des);\r
- }\r
- \r
- //-----------------------------------file&string---------------------------------------//\r
-\r
- //--------------------------------------dir--------------------------------------------//\r
- /*\r
- public static final HashSet<String> walkDir(String path, int mode) throws Exception {\r
- HashSet<String> pathlist = new HashSet<String>();\r
- Common.toDoAll(path, Common.class.getMethod("walkDir", String.class), null, null, mode);\r
- return pathlist;\r
- }\r
- */\r
- public static final void ensureDir(String objFileWhole) {\r
- File tempdir;\r
- Matcher mtrseparate = PTNSEPARATER.matcher(objFileWhole);\r
- if (mtrseparate.find()) {\r
- tempdir = new File(mtrseparate.group(1));\r
- if (!tempdir.exists()) tempdir.mkdirs();\r
- }\r
- }\r
- \r
- public static final void deleteDir(String objFileWhole) {\r
- String[] list = new File(objFileWhole).list();\r
- File temp;\r
- for (int i = 0 ; i < list.length ; i++) {\r
- temp = new File(objFileWhole + File.separator + list[i]);\r
- if (temp.isDirectory()) {\r
- deleteDir(objFileWhole + File.separator + list[i]);\r
- } else {\r
- temp.delete();\r
- }\r
- }\r
- new File(objFileWhole).delete();\r
- }\r
- \r
- public static final String dirCopy_(String src) throws Exception {\r
- Matcher mtrseparate = Common.PTNSEPARATER.matcher(src);\r
- if (mtrseparate.find()) {\r
- dirCopy(src, mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2));\r
- }\r
- return mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2);\r
- }\r
- \r
- public static final void dirCopy(String src, String des) throws Exception {\r
- String[] list = new File(src).list();\r
- File test;\r
-\r
- ensureDir(des);\r
- for (int i = 0 ; i < list.length ; i++) {\r
- test = new File(src + File.separator + list[i]);\r
- if (test.isDirectory()) {\r
- dirCopy(src + File.separator + list[i], des + File.separator + list[i]);\r
- } else {\r
- //ensureDir(des + File.separator + list[i]);\r
- string2file(file2string(src + File.separator + list[i]), des + File.separator + list[i]);\r
- }\r
- }\r
- }\r
- \r
- public static final void oneLevelDirCopy(String src, String des, String type) throws Exception {\r
- String[] list = new File(src).list();\r
- \r
- ensureDir(des);\r
- for (int i = 0; i < list.length; i++) {\r
- if (list[i].contains(type)) {\r
- string2file(file2string(src + File.separator + list[i]), des + File.separator + list[i]);\r
- }\r
- }\r
- }\r
-\r
- //--------------------------------------dir--------------------------------------------//\r
-\r
- //-------------------------------like python walk-----------------------------------------//\r
- \r
- public static final void toDoAll(String path, Method md, Object obj, Object[] args, int type) throws Exception {\r
- String[] list = new File(path).list();\r
- ArrayList<Object> _args = new ArrayList<Object>();\r
- \r
- _args.add(path);\r
- if (args != null) {\r
- for (int i = 0; i < args.length; i++) {\r
- _args.add(args[i]);\r
- }\r
- }\r
-\r
- if (type == DIR || type == BOTH) {\r
- md.invoke(obj, _args.toArray());\r
- }\r
- for (int i = 0 ; i < list.length ; i++) {\r
- if (new File(path + File.separator + list[i]).isDirectory()) {\r
- toDoAll(path + File.separator + list[i], md, obj, args, type);\r
- } else {\r
- if (type == FILE || type == BOTH) {\r
- _args.set(0, path + File.separator + list[i]);\r
- md.invoke(obj, _args.toArray());\r
- }\r
- }\r
- }\r
- }\r
-\r
- public static final void toDoAll(Set<String> set, ForDoAll fda) throws Exception {\r
- Iterator<String> di = set.iterator();\r
- while (di.hasNext()) {\r
- fda.run(di.next());\r
- }\r
- }\r
- \r
- public static final void toDoAll(String path, ForDoAll fda, int type) throws Exception { // filter of file type can be done in toDo\r
- String[] list = new File(path).list();\r
- File test;\r
-\r
- if (type == DIR || type == BOTH) {\r
- fda.run(path);\r
- }\r
- for (int i = 0 ; i < list.length ; i++) {\r
- test = new File(path + File.separator + list[i]);\r
- if (test.isDirectory()) {\r
- if (fda.filter(test)) {\r
- toDoAll(path + File.separator + list[i], fda, type);\r
- }\r
- } else {\r
- if (type == FILE || type == BOTH) {\r
- fda.run(path + File.separator + list[i]);\r
- }\r
- }\r
- }\r
- }\r
- \r
- public static interface ForDoAll {\r
- public void run(String filepath) throws Exception;\r
- \r
- public boolean filter(File dir);\r
- }\r
- \r
- public static abstract class Laplace {\r
- public void transform(String src, String des) throws Exception {\r
- Common.string2file(operation(Common.file2string(src)), des);\r
- }\r
- \r
- public abstract String operation(String wholeline);\r
- \r
- public abstract boolean recognize(String filename);\r
- \r
- public abstract String namechange(String oldname);\r
- }\r
- \r
- public static interface Element {\r
- \r
-// public int replace = 0;\r
-// public int type = 1;\r
- \r
- public String getReplace(String key);\r
- \r
-// public void getType(String key);\r
-// \r
-// public void setReplace(int num);\r
-// \r
-// public void setType(int num);\r
- \r
- }\r
-}\r