The Visitor Pattern and a Java Grep Utility 

by W. David Pitt







Listing One

public void acceptVisitor( GrepVisitor aVisitor)

        Visitor = aVisitor;



Listing Two

/** Return true if current line contains pattern

 *  @return boolean

 */

public boolean match() {

        String aLine = subject.processor.currentLine;

         if (aLine.toUpperCase().indexOf( subject.pattern.toUpperCase() ) 

                                                        > 0){ return true; }

     return false;

}

/** Output file, linenumber and line to console. */

public void report() {

     String printString;

     printString = subject.currentFileName+"("+subject.lineNumber+") - 

                                         "+subject.processor.currentLine;

     System.out.println(printString);

     return;





Listing Three

/** Invoke Utility from command line

 *  @param args java.lang.String[]

 */

public static void main(String args[]) {

    if (args.length < 2) 

        { System.out.println("Usage: java Grep <file path>, <pattern>");

            return ;}   

    Grep aGrep = new Grep();

    aGrep.filePath = args[0];

    aGrep.pattern = args[1];

    // Create and accept a Visitor

    aGrep.acceptVisitor( new GrepSimplePattern() );

    aGrep.process();

    return;

}



1



