donaldp     2002/10/29 23:39:18

  Added:       src/java/org/apache/avalon/phoenix/components/util
                        PathMatcher.java
               src/test/org/apache/avalon/phoenix/components/util/test
                        PathMatcherTestCase.java
               lib/container jakarta-oro-2.0.6.jar
  Log:
  Add in some basic code to do include/excludes ala Ant style.
  
  Revision  Changes    Path
  1.1                  
jakarta-avalon-phoenix/src/java/org/apache/avalon/phoenix/components/util/PathMatcher.java
  
  Index: PathMatcher.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.phoenix.components.util;
  
  import org.apache.oro.text.regex.Pattern;
  import org.apache.oro.text.regex.Perl5Compiler;
  import org.apache.oro.text.regex.MalformedPatternException;
  import org.apache.oro.text.regex.Perl5Matcher;
  
  /**
   * Utility class for scanning a filesystem and matching a
   * particular set of include and exclude patterns ala ant.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/10/30 07:39:17 $
   */
  public class PathMatcher
  {
      /**
       * The set of patterns that indicate paths to include.
       */
      private final Pattern[] m_includes;
  
      /**
       * The set of patterns that indicate paths to exclude.
       * Takes precedence over includes.
       */
      private final Pattern[] m_excludes;
  
      /**
       * The object that is capable of performing the matching.
       */
      private final Perl5Matcher m_matcher = new Perl5Matcher();
  
      /**
       * Construct a matcher from ant style includes/excludes.
       *
       * @param includes the set of includes
       * @param excludes the set of excludes
       */
      public PathMatcher( final String[] includes,
                          final String[] excludes )
      {
          if( null == includes )
          {
              throw new NullPointerException( "includes" );
          }
          if( null == excludes )
          {
              throw new NullPointerException( "excludes" );
          }
  
          m_includes = toPatterns( includes );
          m_excludes = toPatterns( excludes );
      }
  
      /**
       * Test if a virtual path matches any of the ant style
       * patterns specified in this objects constructor.
       *
       * @param vPath the virtual path string
       * @return true if matches, false otherwise
       */
      public boolean match( final String vPath )
      {
          if( isExcluded( vPath ) )
          {
              return false;
          }
          else if( isIncluded( vPath ) )
          {
              return true;
          }
          else
          {
              return false;
          }
      }
  
      /**
       * Determine if specified path is excludes by patterns.
       *
       * @param vPath the virtual path
       * @return true if excluded
       */
      private boolean isExcluded( final String vPath )
      {
          return testMatch( m_excludes, vPath );
      }
  
      /**
       * Determine if specified path is includes by patterns.
       *
       * @param vPath the virtual path
       * @return true if included
       */
      private boolean isIncluded( final String vPath )
      {
          return testMatch( m_includes, vPath );
      }
  
      /**
       * Determine whether the virtual path matches
       * specified patterns.
       *
       * @param patterns the patterns
       * @param vPath the virtual path
       * @return true if matches, false otherwise
       */
      private boolean testMatch( final Pattern[] patterns,
                                 final String vPath )
      {
          for( int i = 0; i < patterns.length; i++ )
          {
              if( m_matcher.matches( vPath, patterns[ i ] ) )
              {
                  return true;
              }
          }
          return false;
      }
  
      /**
       * Convert a set of ant patterns into ORO Pattern objects.
       *
       * @param strs the ant style patterns
       * @return the ORO Pattern objects
       */
      private Pattern[] toPatterns( final String[] strs )
      {
          final Perl5Compiler compiler = new Perl5Compiler();
          final Pattern[] patterns = new Pattern[ strs.length ];
          for( int i = 0; i < patterns.length; i++ )
          {
              final String perlPatternStr = toPerlPatternStr( strs[ i ] );
              try
              {
                  patterns[ i ] = compiler.compile( perlPatternStr );
              }
              catch( final MalformedPatternException mpe )
              {
                  throw new IllegalArgumentException( mpe.toString() );
              }
          }
          return patterns;
      }
  
      /**
       * Convert an ant style fileset pattern to a Perl pattern.
       *
       * @param str the ant style pattern
       * @return the perl pattern
       */
      private String toPerlPatternStr( String str )
      {
          final StringBuffer sb = new StringBuffer();
          final int size = str.length();
  
          for( int i = 0; i < size; i++ )
          {
              final char ch = str.charAt( i );
              if( '.' == ch || '/' == ch || '\\' == ch )
              {
                  sb.append( '\\' );
                  sb.append( ch );
              }
              else if( '*' == ch )
              {
                  if( ( i + 1 ) < size &&
                      '*' == str.charAt( i + 1 ) )
                  {
                      sb.append( "([^/]*/)*" );
                  }
                  else
                  {
                      sb.append( "[^/]*" );
                  }
              }
              else
              {
                  sb.append( ch );
              }
          }
  
          return sb.toString();
      }
  }
  
  
  
  1.1                  
jakarta-avalon-phoenix/src/test/org/apache/avalon/phoenix/components/util/test/PathMatcherTestCase.java
  
  Index: PathMatcherTestCase.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.phoenix.components.util.test;
  
  import junit.framework.TestCase;
  import org.apache.avalon.phoenix.components.util.PathMatcher;
  
  /**
   *  An basic test case for the PathMatcher.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/10/30 07:39:18 $
   */
  public class PathMatcherTestCase
      extends TestCase
  {
      private static final String PATH1 = "SAR-INF/lib/foo.jar";
      private static final String PATH2 = "SAR-INF/lib/bar.jar";
      private static final String PATH3 = "SAR-INF/classes";
  
      public PathMatcherTestCase( final String name )
      {
          super( name );
      }
  
      public void testMatch1()
      {
          final String[] includes = new String[]{"**/*"};
          final String[] excludes = new String[]{};
          final PathMatcher matcher = new PathMatcher( includes, excludes );
  
          assertTrue( PATH1 + " matches", matcher.match( PATH1 ) );
          assertTrue( PATH2 + " matches", matcher.match( PATH2 ) );
          assertTrue( PATH3 + " matches", matcher.match( PATH3 ) );
      }
  }
  
  
  
  1.1                  jakarta-avalon-phoenix/lib/container/jakarta-oro-2.0.6.jar
  
        <<Binary file>>
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@;jakarta.apache.org>

Reply via email to