dfs         2004/02/17 22:44:21

  Added:       src/java/examples engineExample.java
               src/java/org/apache/oro/text GlobCompilerOptions.java
                        GlobEngine.java PatternMatchingEngineFactory.java
               src/java/org/apache/oro/text/awk AwkCompilerOptions.java
                        AwkEngine.java
               src/java/org/apache/oro/text/regex
                        PatternCompilerOptions.java
                        PatternMatchingEngine.java
                        Perl5CompilerOptions.java Perl5Engine.java
  Log:
  Added some classes for abstracting the use of multiple regular expression engines.  
The motivation for this were Ant and Commons Net comments on [EMAIL PROTECTED]
  
  Revision  Changes    Path
  1.1                  jakarta-oro/src/java/examples/engineExample.java
  
  Index: engineExample.java
  ===================================================================
  /* 
   * $Id: engineExample.java,v 1.1 2004/02/18 06:44:21 dfs Exp $
   *
   * Copyright 2000-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  
  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);
      }
    }
  
  }
  
  
  
  1.1                  
jakarta-oro/src/java/org/apache/oro/text/GlobCompilerOptions.java
  
  Index: GlobCompilerOptions.java
  ===================================================================
  /* 
   * $Id: GlobCompilerOptions.java,v 1.1 2004/02/18 06:44:21 dfs Exp $
   *
   * Copyright 2000-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  
  package org.apache.oro.text;
  
  import org.apache.oro.text.regex.*;
  
  /**
   * 
   * @version @version@
   * @since 2.1
   */
  public class GlobCompilerOptions implements PatternCompilerOptions {
  
    public int getMask(int option) throws UnsupportedOperationException {
      switch(option) {
      case DEFAULT          : return GlobCompiler.DEFAULT_MASK;
      case CASE_INSENSITIVE : return GlobCompiler.CASE_INSENSITIVE_MASK;
      }
  
      throw new UnsupportedOperationException();
    }
  
  }
  
  
  
  1.1                  jakarta-oro/src/java/org/apache/oro/text/GlobEngine.java
  
  Index: GlobEngine.java
  ===================================================================
  /* 
   * $Id: GlobEngine.java,v 1.1 2004/02/18 06:44:21 dfs Exp $
   *
   * Copyright 2000-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  
  package org.apache.oro.text;
  
  import org.apache.oro.text.regex.*;
  
  /**
   * 
   * @version @version@
   * @since 2.1
   */
  public class GlobEngine implements PatternMatchingEngine {
  
    private static final GlobCompilerOptions __OPTIONS =
      new GlobCompilerOptions();
  
    public PatternCompiler createCompiler() {
      return new GlobCompiler();
    }
  
    public PatternMatcher createMatcher() {
      return new Perl5Matcher();
    }
  
    public PatternCompilerOptions getOptions() {
      return __OPTIONS;
    }
  }
  
  
  
  1.1                  
jakarta-oro/src/java/org/apache/oro/text/PatternMatchingEngineFactory.java
  
  Index: PatternMatchingEngineFactory.java
  ===================================================================
  /* 
   * $Id: PatternMatchingEngineFactory.java,v 1.1 2004/02/18 06:44:21 dfs Exp $
   *
   * Copyright 2000-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  
  package org.apache.oro.text;
  
  import java.util.*;
  
  import org.apache.oro.text.regex.*;
  import org.apache.oro.text.awk.*;
  
  /**
   * 
   * @version @version@
   * @since 2.1
   */
  public class PatternMatchingEngineFactory {
  
    public static final String PERL5_KEY   = "org.apache.oro.text.regex";
    public static final String AWK_KEY     = "org.apache.oro.text.awk";
    public static final String GLOB_KEY    = "org.apache.oro.text";
    public static final String JAVA_KEY    = "java.util.regex";
    public static final String POSIX_KEY   = "org.apache.regexp";
    public static final String DEFAULT_KEY = PERL5_KEY;
  
    private HashMap __factories;
  
    public PatternMatchingEngineFactory() {
      __factories = new HashMap();
  
      put(PERL5_KEY, new Perl5Engine());
      put(AWK_KEY,   new AwkEngine());
      put(GLOB_KEY,  new GlobEngine());
    }
  
    public PatternMatchingEngine get(String key) {
      return (PatternMatchingEngine)__factories.get(key);
    }
  
    public PatternMatchingEngine put(String key,
                                      PatternMatchingEngine engine)
    {
      return(PatternMatchingEngine) __factories.put(key, engine);
    }
  
    /*
    public PatternMatchingEngine remove(String key) {
      return (PatternMatchingEngine)__factories.remove(key);
    }
    */
  
    public boolean containsKey(String key) {
      return __factories.containsKey(key);
    }
  }
  
  
  
  1.1                  
jakarta-oro/src/java/org/apache/oro/text/awk/AwkCompilerOptions.java
  
  Index: AwkCompilerOptions.java
  ===================================================================
  /* 
   * $Id: AwkCompilerOptions.java,v 1.1 2004/02/18 06:44:21 dfs Exp $
   *
   * Copyright 2000-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  
  package org.apache.oro.text.awk;
  
  import org.apache.oro.text.regex.*;
  
  /**
   * 
   * @version @version@
   * @since 2.1
   */
  public class AwkCompilerOptions implements PatternCompilerOptions {
  
    public int getMask(int option) throws UnsupportedOperationException {
      switch(option) {
      case DEFAULT          : return AwkCompiler.DEFAULT_MASK;
      case CASE_INSENSITIVE : return AwkCompiler.CASE_INSENSITIVE_MASK;
      case MULTILINE        : return AwkCompiler.MULTILINE_MASK;
      }
  
      throw new UnsupportedOperationException();
    }
  
  }
  
  
  
  1.1                  jakarta-oro/src/java/org/apache/oro/text/awk/AwkEngine.java
  
  Index: AwkEngine.java
  ===================================================================
  /* 
   * $Id: AwkEngine.java,v 1.1 2004/02/18 06:44:21 dfs Exp $
   *
   * Copyright 2000-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  
  package org.apache.oro.text.awk;
  
  import org.apache.oro.text.regex.*;
  
  /**
   * 
   * @version @version@
   * @since 2.1
   */
  public class AwkEngine implements PatternMatchingEngine {
  
    private static final AwkCompilerOptions __OPTIONS =
      new AwkCompilerOptions();
  
    public PatternCompiler createCompiler() {
      return new AwkCompiler();
    }
  
    public PatternMatcher createMatcher() {
      return new AwkMatcher();
    }
  
    public PatternCompilerOptions getOptions() {
      return __OPTIONS;
    }
  
  }
  
  
  
  1.1                  
jakarta-oro/src/java/org/apache/oro/text/regex/PatternCompilerOptions.java
  
  Index: PatternCompilerOptions.java
  ===================================================================
  /* 
   * $Id: PatternCompilerOptions.java,v 1.1 2004/02/18 06:44:21 dfs Exp $
   *
   * Copyright 2000-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  
  package org.apache.oro.text.regex;
  
  
  /**
   * 
   * @version @version@
   * @since 2.1
   */
  public interface PatternCompilerOptions {
  
    public int DEFAULT          = 0;
    public int CASE_INSENSITIVE = 1;
    public int MULTILINE        = 2;
  
    public int getMask(int option)
      throws UnsupportedOperationException;
  
  }
  
  
  
  1.1                  
jakarta-oro/src/java/org/apache/oro/text/regex/PatternMatchingEngine.java
  
  Index: PatternMatchingEngine.java
  ===================================================================
  /* 
   * $Id: PatternMatchingEngine.java,v 1.1 2004/02/18 06:44:21 dfs Exp $
   *
   * Copyright 2000-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  
  package org.apache.oro.text.regex;
  
  
  /**
   * 
   * @version @version@
   * @since 2.1
   */
  public interface PatternMatchingEngine {
  
    public PatternCompiler createCompiler();
  
    public PatternMatcher createMatcher();
  
    public PatternCompilerOptions getOptions();
  
  }
  
  
  
  1.1                  
jakarta-oro/src/java/org/apache/oro/text/regex/Perl5CompilerOptions.java
  
  Index: Perl5CompilerOptions.java
  ===================================================================
  /* 
   * $Id: Perl5CompilerOptions.java,v 1.1 2004/02/18 06:44:21 dfs Exp $
   *
   * Copyright 2000-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  
  package org.apache.oro.text.regex;
  
  
  /**
   * 
   * @version @version@
   * @since 2.1
   */
  public class Perl5CompilerOptions implements PatternCompilerOptions {
  
    public int getMask(int option) throws UnsupportedOperationException {
      switch(option) {
      case DEFAULT          : return Perl5Compiler.DEFAULT_MASK;
      case CASE_INSENSITIVE : return Perl5Compiler.CASE_INSENSITIVE_MASK;
      case MULTILINE        : return Perl5Compiler.MULTILINE_MASK;
      }
  
      throw new UnsupportedOperationException();
    }
  
  }
  
  
  
  1.1                  jakarta-oro/src/java/org/apache/oro/text/regex/Perl5Engine.java
  
  Index: Perl5Engine.java
  ===================================================================
  /* 
   * $Id: Perl5Engine.java,v 1.1 2004/02/18 06:44:21 dfs Exp $
   *
   * Copyright 2000-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  
  package org.apache.oro.text.regex;
  
  
  /**
   * 
   * @version @version@
   * @since 2.1
   */
  public class Perl5Engine implements PatternMatchingEngine {
  
    private static final Perl5CompilerOptions __OPTIONS =
      new Perl5CompilerOptions();
  
    public PatternCompiler createCompiler() {
      return new Perl5Compiler();
    }
  
    public PatternMatcher createMatcher() {
      return new Perl5Matcher();
    }
  
    public PatternCompilerOptions getOptions() {
      return __OPTIONS;
    }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to