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