r1683 - in trunk/src: main/java/org/nuiton/util test/java/org/nuiton/util
Author: echatellier Date: 2009-09-22 11:32:56 +0200 (Tue, 22 Sep 2009) New Revision: 1683 Modified: trunk/src/main/java/org/nuiton/util/FileUtil.java trunk/src/test/java/org/nuiton/util/FileUtilTest.java Log: Add java grep implementation Modified: trunk/src/main/java/org/nuiton/util/FileUtil.java =================================================================== --- trunk/src/main/java/org/nuiton/util/FileUtil.java 2009-09-03 18:26:13 UTC (rev 1682) +++ trunk/src/main/java/org/nuiton/util/FileUtil.java 2009-09-22 09:32:56 UTC (rev 1683) @@ -29,9 +29,6 @@ package org.nuiton.util; -import org.apache.commons.logging.LogFactory; - -import javax.swing.JFileChooser; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; @@ -46,12 +43,24 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.nio.CharBuffer; +import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.swing.JFileChooser; + +import org.apache.commons.logging.LogFactory; + public class FileUtil { // FileUtil /** to use log facility, just put in your code: log.info(\"...\"); */ @@ -910,7 +919,7 @@ } /** - * @param file le fichier à tester. + * @param file le fichier à tester. * @param includePatterns les patterns pour accepeter le fichier depuis son nom * @return <code>true</code> si le fichier est accepté, <code>false> autrement. */ @@ -925,6 +934,126 @@ } return result; } + + /** + * Use the linePattern to break the given CharBuffer into lines, applying + * the input pattern to each line to see if we have a match + * + * Code taken from : + * + * http://java.sun.com/javase/6/docs/technotes/guides/io/example/Grep.java + * + * @param regex regex to search into file + * @param cb nio buffer + * @return matching lines (or {code null} if no matching lines) + * @throws IOException + */ + protected static List<CharSequence> grep(String regex, CharBuffer cb) { + + List<CharSequence> linesList = null; + + Pattern pattern = Pattern.compile(regex); + Pattern linePattern = Pattern.compile(".*\r?\n"); + + Matcher lm = linePattern.matcher(cb); // Line matcher + Matcher pm = null; // Pattern matcher + int lines = 0; + while (lm.find()) { + lines++; + CharSequence cs = lm.group(); // The current line + if (pm == null) { + pm = pattern.matcher(cs); + } + else { + pm.reset(cs); + } + if (pm.find()) { + // init + if (linesList == null) { + linesList = new ArrayList<CharSequence>(); + } + linesList.add(cs); + } + if (lm.end() == cb.limit()) { + break; + } + } + + return linesList; + } + + /** + * Java implementation for the unix grep command. + * + * Code taken from : + * + * http://java.sun.com/javase/6/docs/technotes/guides/io/example/Grep.java + * + * @param searchRegex regex to search into file + * @param f file to search into + * @param encoding encoding to use + * @return matching lines (or {code null} if no matching lines) + * @throws IOException + */ + public static List<CharSequence> grep(String searchRegex, File f, String encoding) throws IOException { + + // Open the file and then get a channel from the stream + FileInputStream fis = new FileInputStream(f); + 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 charset = Charset.forName(encoding); + CharsetDecoder decoder = charset.newDecoder(); + CharBuffer cb = decoder.decode(bb); + + // Perform the search + List<CharSequence> lines = grep(searchRegex, cb); + + // Close the channel and the stream + fc.close(); + + return lines; + } + + /** + * Java implementation for the unix grep command. + * + * @param searchRegex regex to search into file + * @param rootDirectory directory to seacrh into + * @param fileRegex regex for file to find in {@code rootDirectory} + * @param encoding encoding to use + * @return all matching lines for each files + * @throws IOException + */ + public static Map<File, List<CharSequence>> grep(String searchRegex, File rootDirectory, String fileRegex, String encoding) throws IOException { + Map<File, List<CharSequence>> results = new HashMap<File, List<CharSequence>>(); + List<File> files = FileUtil.find(rootDirectory, fileRegex, true); + for (File file : files) { + List<CharSequence> lines = grep(searchRegex, file, encoding); + if (lines != null) { + results.put(file, lines); + } + } + return results; + } + + /** + * Search for files matching regex in current directory. + * + * @param searchRegex regex to search into file + * @param fileRegex regex for file to find in current dir + * @param encoding encoding to use + * @return all matching lines for each files + * @throws IOException + */ + public static Map<File, List<CharSequence>> grep(String searchRegex, String fileRegex, String encoding) throws IOException { + Map<File, List<CharSequence>> results = grep(searchRegex, new File("."), fileRegex, encoding); + return results; + } + } // FileUtil - Modified: trunk/src/test/java/org/nuiton/util/FileUtilTest.java =================================================================== --- trunk/src/test/java/org/nuiton/util/FileUtilTest.java 2009-09-03 18:26:13 UTC (rev 1682) +++ trunk/src/test/java/org/nuiton/util/FileUtilTest.java 2009-09-22 09:32:56 UTC (rev 1683) @@ -18,7 +18,9 @@ package org.nuiton.util; import java.io.File; +import java.io.IOException; import java.util.List; +import java.util.Map; import org.junit.Assert; import org.junit.Test; @@ -38,7 +40,7 @@ @Test public void testFind() throws Exception { - List result = FileUtil.find(new File("."), ".*FileUtil.*", true); + List<File> result = FileUtil.find(new File("."), ".*FileUtil.*", true); Assert.assertTrue(result.size() != 0); } @@ -95,6 +97,49 @@ Assert.assertEquals(file.length(), dest.length()); Assert.assertEquals(content, FileUtil.readAsString(dest)); } + + /** + * Test grep on a single file. + * + * Search for grep() method count. + * + * @throws IOException + */ + @Test + public void testGrepSingleFile() throws IOException { + // search for + File testUtilFile = FileUtil.find(new File("."), ".*FileUtil\\.java", true).get(0); + + List<CharSequence> lines = FileUtil.grep("grep\\(String .*\\)", testUtilFile , "UTF-8"); + + Assert.assertNotNull(lines); + Assert.assertEquals(4, lines.size()); + + // assert result are ordered + Assert.assertTrue(lines.get(0).toString().indexOf("CharBuffer") > 0); + Assert.assertTrue(lines.get(1).toString().indexOf("File f,") > 0); + Assert.assertTrue(lines.get(2).toString().indexOf("rootDirectory") > 0); + Assert.assertTrue(lines.get(3).toString().indexOf("String searchRegex, String fileRegex, String encoding") > 0); + } + + /** + * Test grep on a multiple files. + * + * Try to find all java file containing "CodeLutin". Can fail if some + * src files are deleted. + * + * @throws IOException + */ + @Test + public void testGrepMultiple() throws IOException { + + File rootDir = new File("src"); + + Map<File, List<CharSequence>> results = FileUtil.grep("CodeLutin", rootDir , ".*\\.java", "UTF-8"); + + Assert.assertTrue(results.size() > 100); + + } + } // FileUtilTest -
participants (1)
-
echatellier@users.nuiton.org