Recipe 4.12 Program: Full Grep


Now that we've seen how the regular expressions package works, it's time to write Grep2, a full-blown version of the line-matching program with option parsing. Table 4-3 lists some typical command-line options that a Unix implementation of grep might include.

Table 4-3. Grep command-line options

Option

Meaning

-c

Count only: don't print lines, just count them

-C

Context; print some lines above and below each line that matches (not implemented in this version; left as an exercise for the reader)

-f pattern

Take pattern from file named after -f instead of from command line

-h

Suppress printing filename ahead of lines

-i

Ignore case

-l

List filenames only: don't print lines, just the names they're found in

-n

Print line numbers before matching lines

-s

Suppress printing certain error messages

-v

Invert: print only lines that do NOT match the pattern


We discussed the GetOpt class in Recipe 2.6. Here we use it to control the operation of an application program. As usual, since main( ) runs in a static context but our application main line does not, we could wind up passing a lot of information into the constructor. Because we have so many options, and it would be inconvenient to keep expanding the options list as we add new functionality to the program, we use a kind of Collection called a BitSet to pass all the true/false arguments: true to print line numbers, false to print filenames, etc. (Collections are covered in Chapter 7.) A BitSet is much like a Vector (see Recipe 7.3) but is specialized to store only Boolean values and is ideal for handling command-line arguments.

The program basically just reads lines, matches the pattern in them, and, if a match is found (or not found, with -v), prints the line (and optionally some other stuff, too). Having said all that, the code is shown in Example 4-9.

Example 4-9. Grep2.java
import com.darwinsys.util.*; import java.io.*; import java.util.*; /** A command-line grep-like program. Accepts some options and takes a pattern  * and a list of text files.  */ public class Grep2 {     /** The pattern we're looking for */     protected Matcher pattern;     /** The Reader for the current file */     protected BufferedReader d;     /** Are we to only count lines, instead of printing? */     protected boolean countOnly = false;     /** Are we to ignore case? */     protected boolean ignoreCase = false;     /** Are we to suppress printing of filenames? */     protected boolean dontPrintFileName = false;     /** Are we to only list names of files that match? */     protected boolean listOnly = false;     /** are we to print line numbers? */     protected boolean numbered = false;     /** Are we to be silent about errors? */     protected boolean silent = false;     /** are we to print only lines that DONT match? */     protected boolean inVert = false;     /** Construct a Grep object for each pattern, and run it      * on all input files listed in argv.      */     public static void main(String[] argv) throws RESyntaxException {         if (argv.length < 1) {             System.err.println("Usage: Grep2 pattern [filename...]");             System.exit(1);         }         String pattern = null;         GetOpt go = new GetOpt("cf:hilnsv");         BitSet args = new BitSet( );         char c;         while ((c = go.getopt(argv)) != 0) {             switch(c) {                 case 'c':                     args.set('C');                     break;                 case 'f':                     try {                         BufferedReader b = new BufferedReader                         pattern = b.readLine( );                         b.close( );                     } catch (IOException e) {                         System.err.println("Can't read pattern file " +                          System.exit(1);                     }                     break;                 case 'h':                     args.set('H');                     break;                 case 'i':                     args.set('I');                     break;                 case 'l':                     args.set('L');                     break;                 case 'n':                     args.set('N');                     break;                 case 's':                     args.set('S');                     break;                 case 'v':                     args.set('V');                     break;             }         }         int ix = go.getOptInd( );         if (pattern == null)             pattern = argv[ix-1];         Grep2 pg = new Grep2(pattern, args);         if (argv.length == ix)             pg.process(new InputStreamReader(System.in), "(standard input)");         else             for (int i=ix; i<argv.length; i++) {                 try {                     pg.process(new FileReader(argv[i]), argv[i]);                 } catch(Exception e) {                     System.err.println(e);                 }             }     }     /** Construct a Grep2 object.         // compile the regular expression         if (args.get('C'))             countOnly = true;         if (args.get('H'))             dontPrintFileName = true;         if (args.get('I'))             ignoreCase = true;         if (args.get('L'))             listOnly = true;         if (args.get('N'))             numbered = true;         if (args.get('S'))             silent = true;         if (args.get('V'))             inVert = true;         int caseMode = ignoreCase ? Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE : 0;         pattern = Pattern.compile(patt, caseMode);     }              /** Do the work of scanning one file      * @param    ifile    Reader    Reader object already open      * @param    fileName String    Name of the input file      */     public void process(Reader ifile, String fileName) {         String line;         int matches = 0;         try {             d = new BufferedReader(ifile);                          while ((line = d.readLine( )) != null) {                 if (pattern.match(line)) {                     if (countOnly)                         matches++;                     else {                     if (!dontPrintFileName)                         System.out.print(fileName + ": ");                     System.out.println(line);                     }                 } else if (inVert) {                     System.out.println(line);                 }             }             if (countOnly)                 System.out.println(matches + " matches in " + fileName);             d.close( );         } catch (IOException e) { System.err.println(e); }     } }



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net