dol: initial dol commit
[jump.git] / dol / src / dol / util / Sed.java
1 /* $Id: Sed.java 1 2010-02-24 13:03:05Z haidw $ */\r
2 package dol.util;\r
3 \r
4 import java.io.BufferedReader;\r
5 import java.io.BufferedWriter;\r
6 import java.io.File;\r
7 import java.io.FileInputStream;\r
8 import java.io.FileWriter;\r
9 import java.io.IOException;\r
10 import java.io.InputStreamReader;\r
11 \r
12 /**\r
13  * Class to find and replace strings in a files of a directory.\r
14  * The intention is to get a behavior similar to UNIX's sed (stream\r
15  * editor).\r
16  */\r
17 public class Sed {\r
18 \r
19     /**\r
20      * In the given file, search for occurences of the given search\r
21      * pattern and replace it by the given replacement.\r
22      * searchPattern, replacementPattern will be used in a call of\r
23      * String.replaceAll(searchPattern, replacementPattern).\r
24      *\r
25      * @param filename file to search\r
26      * @param searchPattern regular expression to search for\r
27      * @param replacementPattern replacement\r
28      */\r
29     protected void readReplace(String filename, String searchPattern,\r
30             String replacementPattern) throws IOException {\r
31         String line;\r
32         StringBuffer buffer = new StringBuffer();\r
33         FileInputStream fileInputStream = new FileInputStream(filename);\r
34         BufferedReader reader = new BufferedReader(\r
35                 new InputStreamReader(fileInputStream));\r
36         while((line = reader.readLine()) != null) {\r
37             String newline = line.replaceAll(searchPattern, replacementPattern);\r
38             /*\r
39             if (!newline.equals(line)) {\r
40                 System.out.println("Found pattern in " + filename\r
41                         + ". New line: " + newline);\r
42             }\r
43             */\r
44             buffer.append(newline + "\n");\r
45         }\r
46         reader.close();\r
47         BufferedWriter out = new BufferedWriter(new FileWriter(filename));\r
48         out.write(buffer.toString());\r
49         out.close();\r
50     }\r
51 \r
52     /**\r
53      * Iterate through the given directory tree. For each file found in\r
54      * the file, the method\r
55      * {@link #processFile(java.lang.String)} is called.\r
56      *\r
57      * @param path the path of the directory to be browsed\r
58      * @param currentDir the subdirectory currently explored\r
59      */\r
60     protected void browseDirectoryTree(String path)\r
61             throws IOException {\r
62         File file = new File(path);\r
63 \r
64         if (!file.exists()) return;\r
65         if (!file.isDirectory()) return;\r
66 \r
67         //loop through files in directory\r
68         String[] files = file.list();\r
69 \r
70         for (int k = 0; k < files.length; k++) {\r
71             File newfile = new File(file.getPath(), files[k]);\r
72             if (newfile.isFile())\r
73                 readReplace(path + System.getProperty("file.separator")\r
74                         + files[k], _searchPattern, _replacementPattern);\r
75             else if (newfile.isDirectory()) {\r
76                 browseDirectoryTree(file.getPath()\r
77                         + System.getProperty("file.separator")\r
78                         + files[k]);\r
79             }\r
80         }\r
81     }\r
82 \r
83     /**\r
84      * In the given file or in the files located in the given directory,\r
85      * search for occurences of the given search pattern and replace it\r
86      * by the given replacement. searchPattern, replacementPattern will\r
87      * be used in a call of\r
88      * String.replaceAll(searchPattern, replacementPattern).\r
89      *\r
90      * @param path file or path to search\r
91      * @param searchPattern regular expression to search for\r
92      * @param replacementPattern replacement\r
93      */\r
94     public void sed(String path, String searchPattern,\r
95             String replacementPattern) throws IOException {\r
96         File file = new File(path);\r
97         _searchPattern = searchPattern;\r
98         _replacementPattern = replacementPattern;\r
99         if (!file.exists()) return;\r
100         if (file.isFile()) {\r
101             readReplace(path, searchPattern, replacementPattern);\r
102         }\r
103         else if (file.isDirectory()) {\r
104             browseDirectoryTree(path);\r
105         }\r
106     }\r
107 \r
108     /**\r
109      * Test the implementation.\r
110      *\r
111      * @param args args[0] file or directory to search\r
112      *             args[1] regular expression to search for\r
113      *             args[2] replacement\r
114      */\r
115     public static void main(String args[]) {\r
116         if (args.length == 3) {\r
117             try {\r
118                 new Sed().sed(args[0], args[1], args[2]);\r
119             }\r
120             catch (IOException e) {\r
121                 System.out.println("Sed: An error occured:");\r
122                 System.out.println(e.getMessage());\r
123             }\r
124         }\r
125         else {\r
126             System.out.println("usage: java Sed directory|file "\r
127                     + "searchPattern replacementPattern");\r
128         }\r
129     }\r
130 \r
131     String _searchPattern = "";\r
132     String _replacementPattern = "";\r
133 }\r