--- /dev/null
+/* $Id: Sed.java 1 2010-02-24 13:03:05Z haidw $ */\r
+package dol.util;\r
+\r
+import java.io.BufferedReader;\r
+import java.io.BufferedWriter;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileWriter;\r
+import java.io.IOException;\r
+import java.io.InputStreamReader;\r
+\r
+/**\r
+ * Class to find and replace strings in a files of a directory.\r
+ * The intention is to get a behavior similar to UNIX's sed (stream\r
+ * editor).\r
+ */\r
+public class Sed {\r
+\r
+ /**\r
+ * In the given file, search for occurences of the given search\r
+ * pattern and replace it by the given replacement.\r
+ * searchPattern, replacementPattern will be used in a call of\r
+ * String.replaceAll(searchPattern, replacementPattern).\r
+ *\r
+ * @param filename file to search\r
+ * @param searchPattern regular expression to search for\r
+ * @param replacementPattern replacement\r
+ */\r
+ protected void readReplace(String filename, String searchPattern,\r
+ String replacementPattern) throws IOException {\r
+ String line;\r
+ StringBuffer buffer = new StringBuffer();\r
+ FileInputStream fileInputStream = new FileInputStream(filename);\r
+ BufferedReader reader = new BufferedReader(\r
+ new InputStreamReader(fileInputStream));\r
+ while((line = reader.readLine()) != null) {\r
+ String newline = line.replaceAll(searchPattern, replacementPattern);\r
+ /*\r
+ if (!newline.equals(line)) {\r
+ System.out.println("Found pattern in " + filename\r
+ + ". New line: " + newline);\r
+ }\r
+ */\r
+ buffer.append(newline + "\n");\r
+ }\r
+ reader.close();\r
+ BufferedWriter out = new BufferedWriter(new FileWriter(filename));\r
+ out.write(buffer.toString());\r
+ out.close();\r
+ }\r
+\r
+ /**\r
+ * Iterate through the given directory tree. For each file found in\r
+ * the file, the method\r
+ * {@link #processFile(java.lang.String)} is called.\r
+ *\r
+ * @param path the path of the directory to be browsed\r
+ * @param currentDir the subdirectory currently explored\r
+ */\r
+ protected void browseDirectoryTree(String path)\r
+ throws IOException {\r
+ File file = new File(path);\r
+\r
+ if (!file.exists()) return;\r
+ if (!file.isDirectory()) return;\r
+\r
+ //loop through files in directory\r
+ String[] files = file.list();\r
+\r
+ for (int k = 0; k < files.length; k++) {\r
+ File newfile = new File(file.getPath(), files[k]);\r
+ if (newfile.isFile())\r
+ readReplace(path + System.getProperty("file.separator")\r
+ + files[k], _searchPattern, _replacementPattern);\r
+ else if (newfile.isDirectory()) {\r
+ browseDirectoryTree(file.getPath()\r
+ + System.getProperty("file.separator")\r
+ + files[k]);\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * In the given file or in the files located in the given directory,\r
+ * search for occurences of the given search pattern and replace it\r
+ * by the given replacement. searchPattern, replacementPattern will\r
+ * be used in a call of\r
+ * String.replaceAll(searchPattern, replacementPattern).\r
+ *\r
+ * @param path file or path to search\r
+ * @param searchPattern regular expression to search for\r
+ * @param replacementPattern replacement\r
+ */\r
+ public void sed(String path, String searchPattern,\r
+ String replacementPattern) throws IOException {\r
+ File file = new File(path);\r
+ _searchPattern = searchPattern;\r
+ _replacementPattern = replacementPattern;\r
+ if (!file.exists()) return;\r
+ if (file.isFile()) {\r
+ readReplace(path, searchPattern, replacementPattern);\r
+ }\r
+ else if (file.isDirectory()) {\r
+ browseDirectoryTree(path);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Test the implementation.\r
+ *\r
+ * @param args args[0] file or directory to search\r
+ * args[1] regular expression to search for\r
+ * args[2] replacement\r
+ */\r
+ public static void main(String args[]) {\r
+ if (args.length == 3) {\r
+ try {\r
+ new Sed().sed(args[0], args[1], args[2]);\r
+ }\r
+ catch (IOException e) {\r
+ System.out.println("Sed: An error occured:");\r
+ System.out.println(e.getMessage());\r
+ }\r
+ }\r
+ else {\r
+ System.out.println("usage: java Sed directory|file "\r
+ + "searchPattern replacementPattern");\r
+ }\r
+ }\r
+\r
+ String _searchPattern = "";\r
+ String _replacementPattern = "";\r
+}\r