ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Daniel F. Savarese" <...@savarese.org>
Subject Re: Incorrect documentation of FTP task [net]
Date Wed, 18 Feb 2004 07:05:02 GMT

In message <200402172104.11737.scohen@javactivity.org>, Steve Cohen writes:
>You've built an interface to encapsulate the choice in regex implementations 
>with automatic checking.  Sweet!  Perhaps we could port it to commons-net - 
...
>Perhaps this checker mechanism could be ported to commons (a la 
>commons-logging).  Then it could be available to other clients, in 
>particular, to commons clients.  But of course then we'd be requiring yet 
...
>I think we ought to think seriously about what we are doing here before we 
>rush into coding anything like this.

*sigh*  No one ever brings these things up on the oro or regexp mailing
lists.  This is one of those really easy things to do with oro, which
most folks don't realize implements three different regular expression
engines.  I know I promised to do the vpp-based conditional compilation
to provide a J2ME/JDK1.1 compatible version of oro, but I just haven't
found the time.  This however is easy stuff.  I just added two new
interfaces to org.apache.oro.text.regex: PatternMatchingEngine and
PatternCompilerOptions.  I implemented these interfaces for the
Perl5, Awk, and Glob engines.  I then wrote a PatternMatchingEngineFactory
class to generate engines.  You can put the J2SE 1.4 detection code
in there and add an org.apache.oro.text.java package that wraps
java.util.regex.  All you have to do is implement PatternMatcher,
PatternCompiler, and Pattern for J2SE 1.4.  If someone will step up
to do this, I'm sure the PMC will grant commit access immediately.
I'd actually rather if all of jakarta had commit access to oro for
this very kind of situation.  When you don't have commit access, there
is sometimes a tendency to reinvent the wheel.

I've appended a modified version of the grep example that shows how
you might pick an engine based on a set of preferences.  At any rate,
I very much thing oro is the place for generic regular expression
engine support since it was designed with that in mind from the
start (although the pattern compilation options could have been
abstracted better at its genesis).  I invite anyone and everyone to
do a checkout of the latest jakarta-oro files in CVS and start
improving my 30 minute hack.

daniel


package examples;

import java.io.*;
import java.util.*;

import org.apache.oro.text.*;
import org.apache.oro.text.regex.*;

/**
 * This is a no-frills implementation of grep that demos the use of
 * PatternMatchingEngineFactory to choose different
 * regular expression engines.  It performs case insensitive matching
 * to demonstrate the use of the PatternCompilerOptions interface.
 *
 * @version @version@
 */
public final class engineExample {
  static int _file = 0;

  static final String[] _preferences = {
    PatternMatchingEngineFactory.JAVA_KEY,
    PatternMatchingEngineFactory.PERL5_KEY, 
    PatternMatchingEngineFactory.POSIX_KEY,
    PatternMatchingEngineFactory.AWK_KEY, 
    PatternMatchingEngineFactory.GLOB_KEY
  };

  // args[] is declared final so that Inner Class may reference it.
  public static final void main(final String[] args) {
    PatternMatchingEngineFactory factory;
    PatternMatchingEngine engine = null;
    PatternCompiler compiler;
    PatternCompilerOptions options;
    PatternMatcher matcher;
    MatchActionProcessor processor;
    int mask;

    if(args.length < 2) {
      System.err.println("Usage: grep <pattern> <filename> ...");
      System.exit(1);
    }

    factory = new PatternMatchingEngineFactory();

    // Demonstration of choosing engine based on preferences.
    for(int i = 0; i < _preferences.length; ++i) {
      if(factory.containsKey(_preferences[i])) {
        engine = factory.get(_preferences[i]);
        break;
      }
    }

    if(engine == null)
      engine = factory.get(PatternMatchingEngineFactory.DEFAULT_KEY);

    compiler = engine.createCompiler();
    matcher  = engine.createMatcher();
    options  = engine.getOptions();
    mask     = options.getMask(PatternCompilerOptions.CASE_INSENSITIVE);
    processor = new MatchActionProcessor(compiler, matcher);

    try {
      if(args.length > 2) {
	// Print filename before line if more than one file is specified.
	// Rely on file to point to current file being processed.
	processor.addAction(args[0], mask, new MatchAction() {
	  public void processMatch(MatchActionInfo info) {
	    info.output.println(args[_file] + ":" + info.line);
	  }
        });
      } else {
	// We rely on the default action of printing matched 
	// lines to the given OutputStream
	processor.addAction(args[0], mask);
      }

      for(_file = 1; _file < args.length; _file++)
	processor.processMatches(new FileInputStream(args[_file]), System.out);

    } catch(MalformedPatternException e) {
      System.err.println("Bad pattern.");
      e.printStackTrace();
      System.exit(1);
    } catch(IOException e) {
      System.err.println("Error opening or reading " + args[_file]);
      e.printStackTrace();
      System.exit(1);
    }
  }

}







---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message