Author: echatellier Date: 2009-10-02 12:01:18 +0200 (Fri, 02 Oct 2009) New Revision: 1685 Modified: trunk/changelog.txt trunk/src/main/java/org/nuiton/util/FileUtil.java trunk/src/test/java/org/nuiton/util/FileUtilTest.java Log: Add sed method (unix like) on FileUtil. Modified: trunk/changelog.txt =================================================================== --- trunk/changelog.txt 2009-09-26 02:30:43 UTC (rev 1684) +++ trunk/changelog.txt 2009-10-02 10:01:18 UTC (rev 1685) @@ -1,3 +1,9 @@ +1.1.2 xxx 200910xx + + * [FEATURE] Add sed and grep method on FileUtil + +-- + 1.1.1 chemit 20090903 * [FEATURE] #39 add a filterVersions method in VersionUtil Modified: trunk/src/main/java/org/nuiton/util/FileUtil.java =================================================================== --- trunk/src/main/java/org/nuiton/util/FileUtil.java 2009-09-26 02:30:43 UTC (rev 1684) +++ trunk/src/main/java/org/nuiton/util/FileUtil.java 2009-10-02 10:01:18 UTC (rev 1685) @@ -43,6 +43,7 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.io.PrintStream; import java.nio.CharBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; @@ -958,9 +959,9 @@ Matcher lm = linePattern.matcher(cb); // Line matcher Matcher pm = null; // Pattern matcher - int lines = 0; + //int lines = 0; while (lm.find()) { - lines++; + //lines++; CharSequence cs = lm.group(); // The current line if (pm == null) { pm = pattern.matcher(cs); @@ -982,7 +983,7 @@ return linesList; } - + /** * Java implementation for the unix grep command. * @@ -1056,4 +1057,73 @@ return results; } + /** + * Sed implementation for a single file. + * + * Oginal source code from http://kickjava.com/src/org/apache/lenya/util/SED.java.htm. + * + * @param searchRegex Prefix which shall be replaced + * @param replace Prefix which is going to replace the original + * @param file File which sed shall be applied + * @param encoding charset encoding + * @throws IOException + */ + public static void sed(String searchRegex, String replace, File file, String encoding) throws IOException { + + Pattern pattern = Pattern.compile(searchRegex); + + // Open the file and then get a channel from the stream + FileInputStream fis = new FileInputStream(file); + FileChannel fc = fis.getChannel(); + + // Get the file's size and then map it into memory + int sz = (int)fc.size(); + MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); + + // Decode the file into a char buffer + // Charset and decoder for encoding + Charset charset = Charset.forName(encoding); + CharsetDecoder decoder = charset.newDecoder(); + CharBuffer cb = decoder.decode(bb); + + Matcher matcher = pattern.matcher(cb); + String outString = matcher.replaceAll(replace); + + FileOutputStream fos = new FileOutputStream(file.getAbsolutePath()); + PrintStream ps = new PrintStream(fos); + ps.print(outString); + ps.close(); + fos.close(); + } + + /** + * Java implementation for the unix sed command. + * + * @param searchRegex regex to search into file + * @param replace string to replace matching patterns + * @param rootDirectory directory to search into + * @param fileRegex regex for file to find in {@code rootDirectory} + * @param encoding encoding to use + * @throws IOException + */ + public static void sed(String searchRegex, String replace, File rootDirectory, String fileRegex, String encoding) throws IOException { + List<File> files = FileUtil.find(rootDirectory, fileRegex, true); + for (File file : files) { + sed(searchRegex, replace, file, encoding); + } + } + + /** + * Java implementation for the unix sed command. + * + * @param searchRegex regex to search into file + * @param replace string to replace matching patterns + * @param fileRegex regex for file to find in current dir + * @param encoding encoding to use + * @throws IOException + */ + public static void sed(String searchRegex, String replace, String fileRegex, String encoding) throws IOException { + sed(searchRegex, replace, new File("."), fileRegex, encoding); + } + } // FileUtil Modified: trunk/src/test/java/org/nuiton/util/FileUtilTest.java =================================================================== --- trunk/src/test/java/org/nuiton/util/FileUtilTest.java 2009-09-26 02:30:43 UTC (rev 1684) +++ trunk/src/test/java/org/nuiton/util/FileUtilTest.java 2009-10-02 10:01:18 UTC (rev 1685) @@ -141,5 +141,66 @@ Assert.assertTrue(results.size() > 100); } + + /** + * Test sed method on a single file. + * @throws IOException + */ + @Test + public void testSedSingleFile() throws IOException { + + // try to not make sed in real src dir ;) + File testDirectory = FileUtil.createTempDirectory("sed", "test"); + FileUtil.copyRecursively(new File("src").getAbsoluteFile(), testDirectory); + File testUtilFile = FileUtil.find(testDirectory, ".*FileUtil\\.java", true).get(0); + List<CharSequence> lines = FileUtil.grep("grep\\(String .*\\)", testUtilFile , "UTF-8"); + Assert.assertEquals(4, lines.size()); + + lines = FileUtil.grep("sedfoo", testUtilFile , "UTF-8"); + Assert.assertNull(lines); + + // real method to test here : sed + FileUtil.sed("grep", "sedfoo", testUtilFile , "UTF-8"); + + lines = FileUtil.grep("grep\\(String .*\\)", testUtilFile , "UTF-8"); + Assert.assertNull(lines); + + lines = FileUtil.grep("sedfoo", testUtilFile , "UTF-8"); + Assert.assertEquals(9, lines.size()); + + // clean + FileUtil.deleteRecursively(testDirectory); + } + + /** + * Test sed on a multiple files. + * + * Try to replace all "CodeLutin" by "nuiton" in all files. Can fail if some + * src files are deleted. + * + * @throws IOException + */ + @Test + public void testSedMultiple() throws IOException { + + // try to not make sed in real src dir ;) + File testDirectory = FileUtil.createTempDirectory("sed", "test"); + FileUtil.copyRecursively(new File("src").getAbsoluteFile(), testDirectory); + + Map<File, List<CharSequence>> results = FileUtil.grep("CodeLutin", testDirectory , ".*\\.java", "UTF-8"); + Assert.assertTrue(results.size() > 100); + + FileUtil.sed("CodeLutin", "Nuiton", testDirectory, ".*\\.java" , "UTF-8"); + + results = FileUtil.grep("CodeLutin", testDirectory , ".*\\.java", "UTF-8"); + Assert.assertTrue(results.isEmpty()); + + results = FileUtil.grep("Nuiton", testDirectory , ".*\\.java", "UTF-8"); + Assert.assertTrue(results.size() > 100); + + // clean + FileUtil.deleteRecursively(testDirectory); + } + } // FileUtilTest