Greppublic class Grep extends Object A file searching class, similar to grep, which returns information about
lines matched in the specified files. Instances of this class are tied
to a specific regular expression pattern and may be applied repeatedly
to multiple files. Instances of Grep are thread safe, they may be shared. |
Fields Summary |
---|
private Pattern | pattern |
Constructors Summary |
---|
public Grep(Pattern pattern)Instantiate a Grep object for the given pre-compiled Pattern object.
this.pattern = pattern;
| public Grep(String regex, boolean ignoreCase)Instantiate a Grep object and compile the given regular expression
string.
this.pattern = Pattern.compile (regex,
(ignoreCase) ? Pattern.CASE_INSENSITIVE : 0);
| public Grep(String regex)Instantiate a Grep object with the given regular expression string,
with default options.
this (regex, false);
|
Methods Summary |
---|
public com.ronsoft.books.nio.regex.Grep$MatchedLine[] | grep(java.io.File file)Perform a grep on the given file.
List list = grepList (file);
MatchedLine matches [] = new MatchedLine [list.size()];
list.toArray (matches);
return (matches);
| public com.ronsoft.books.nio.regex.Grep$MatchedLine[] | grep(java.lang.String fileName)Perform a grep on the given file.
return (grep (new File (fileName)));
| public com.ronsoft.books.nio.regex.Grep$MatchedLine[] | grep(java.io.File[] files)Perform a grep on the given list of files. If a given file cannot
be read, it will be ignored as if empty.
List aggregate = new LinkedList();
for (int i = 0; i < files.length; i++) {
try {
List temp = grepList (files [i]);
aggregate.addAll (temp);
} catch (IOException e) {
// ignore I/O exceptions
}
}
MatchedLine matches [] = new MatchedLine [aggregate.size()];
aggregate.toArray (matches);
return (matches);
| private java.util.List | grepList(java.io.File file)Run the grepper on the given File.
if ( ! file.exists()) {
throw new IOException ("Does not exist: " + file);
}
if ( ! file.isFile()) {
throw new IOException ("Not a regular file: " + file);
}
if ( ! file.canRead()) {
throw new IOException ("Unreadable file: " + file);
}
LinkedList list = new LinkedList();
FileReader fr = new FileReader (file);
LineNumberReader lnr = new LineNumberReader (fr);
Matcher matcher = this.pattern.matcher ("");
String line;
while ((line = lnr.readLine()) != null) {
matcher.reset (line);
if (matcher.find()) {
list.add (new MatchedLine (file,
lnr.getLineNumber(), line,
matcher.start(), matcher.end()));
}
}
lnr.close();
return (list);
| public static void | main(java.lang.String[] argv)Test code to run grep operations. Accepts two command-line
options: -i or --ignore-case, compile the givn pattern so
that case of alpha characters is ignored. Or -1, which runs
the grep operation on each individual file, rather that passing
them all to one invocation. This is just to test the different
methods. The printed ouptut is slightly different when -1 is
specified.
// set defaults
boolean ignoreCase = false;
boolean onebyone = false;
List argList = new LinkedList(); // to gather args
// loop through the args, looking for switches and saving
// off the pattern an file names
for (int i = 0; i < argv.length; i++) {
if (argv [i].startsWith ("-")) {
if (argv [i].equals ("-i")
|| argv [i].equals ("--ignore-case"))
{
ignoreCase = true;
}
if (argv [i].equals ("-1")) {
onebyone = true;
}
continue;
}
// not a switch, add it to the list
argList.add (argv [i]);
}
// enough args to run?
if (argList.size() < 2) {
System.err.println ("usage: [options] pattern filename ...");
return;
}
// first arg on the list will be taken as the regex pattern
// pass the pattern to the new Grep object, along with the
// current value of the ignore case flag
Grep grepper = new Grep ((String) argList.remove (0),
ignoreCase);
// somewhat arbitrarily split into two ways of calling the
// grepper and printing out the results.
if (onebyone) {
Iterator it = argList.iterator();
// loop through the filenames and grep them
while (it.hasNext()) {
String fileName = (String) it.next();
// print the filename once before each grep
System.out.println (fileName + ":");
MatchedLine [] matches = null;
// catch exceptions
try {
matches = grepper.grep (fileName);
} catch (IOException e) {
System.err.println ("\t*** " + e);
continue;
}
// print out info about the matched lines
for (int i = 0; i < matches.length; i++) {
MatchedLine match = matches [i];
System.out.println (" "
+ match.getLineNumber()
+ " [" + match.start()
+ "-" + (match.end() - 1)
+ "]: "
+ match.getLineText());
}
}
} else {
// convert the filename list to an array of File
File [] files = new File [argList.size()];
for (int i = 0; i < files.length; i++) {
files [i] = new File ((String) argList.get (i));
}
// run the grepper, unreadable files are ignored
MatchedLine [] matches = grepper.grep (files);
// print out info about the matched lines
for (int i = 0; i < matches.length; i++) {
MatchedLine match = matches [i];
System.out.println (match.getFile().getName()
+ ", " + match.getLineNumber() + ": "
+ match.getLineText());
}
}
|
|