donaldp 2002/10/30 01:10:18
Modified: loader build.xml ant.properties.sample
Added: loader/src/test/org/apache/excalibur/loader/util/test
PathMatcherTestCase.java
loader/src/java/org/apache/excalibur/loader/util
PathMatcher.java
Log:
Move the PathMatcher into the loader package.
Revision Changes Path
1.1
jakarta-avalon-excalibur/loader/src/test/org/apache/excalibur/loader/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.excalibur.loader.util.test;
import junit.framework.TestCase;
import org.apache.excalibur.loader.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 09:10: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 ) );
}
public void testMatch2()
{
final String[] includes = new String[]{"**/*.jar"};
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 + " not matches", !matcher.match( PATH3 ) );
}
public void testMatch3()
{
final String[] includes = new String[]{"**/*.jar"};
final String[] excludes = new String[]{"**/bar*"};
final PathMatcher matcher = new PathMatcher( includes, excludes );
assertTrue( PATH1 + " matches", matcher.match( PATH1 ) );
assertTrue( PATH2 + " not matches", !matcher.match( PATH2 ) );
assertTrue( PATH3 + " not matches", !matcher.match( PATH3 ) );
}
}
1.1
jakarta-avalon-excalibur/loader/src/java/org/apache/excalibur/loader/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.excalibur.loader.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 09:10:18 $
*/
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.6 +5 -0 jakarta-avalon-excalibur/loader/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/loader/build.xml,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- build.xml 28 Oct 2002 04:16:07 -0000 1.5
+++ build.xml 30 Oct 2002 09:10:18 -0000 1.6
@@ -17,6 +17,7 @@
<pathelement location="${excalibur-extension.jar}"/>
<pathelement location="${checkstyle.jar}"/>
<pathelement location="${xml-apis.jar}"/>
+ <pathelement location="${jakarta-oro.jar}"/>
<pathelement path="${java.class.path}"/>
</path>
@@ -40,6 +41,10 @@
<ant antfile="${depchecker.prefix}/depchecker.xml" target="checkFramework"/>
<ant antfile="${depchecker.prefix}/depchecker.xml" target="checkI18N"/>
<ant antfile="${depchecker.prefix}/depchecker.xml" target="checkExtension"/>
+ <ant antfile="${depchecker.prefix}/depchecker.xml"
target="checkRequiredFile">
+ <property name="proj.jar.name" value="jakarta-oro.jar"/>
+ <property name="path" value="${jakarta-oro.jar}"/>
+ </ant>
</target>
<target name="dependencies-test" depends="dist-jar, dependencies"
1.2 +6 -3 jakarta-avalon-excalibur/loader/ant.properties.sample
Index: ant.properties.sample
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/loader/ant.properties.sample,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ant.properties.sample 1 Sep 2002 05:54:59 -0000 1.1
+++ ant.properties.sample 30 Oct 2002 09:10:18 -0000 1.2
@@ -26,14 +26,17 @@
# REQUIRED LIBRARIES
# --------------------------------------------------
-
+# ----- Jakarta ORO 2.0.6 or later -----
+jakarta-oro.home=${base.path}/jakarta-oro-2.0.6
+jakarta-oro.lib=${jakarta-oro.home}/
+jakarta-oro.jar=${jakarta-oro.home}/jakarta-oro-2.0.6.jar
# --------------------------------------------------
# OPTIONAL LIBRARIES
# --------------------------------------------------
# ----- JUnit Unit Test Suite, version 3.7 or later. -----
-# Not needed if junit.jar is in $ANT_HOME/lib
+# Not needed if junit.jar is in $ANT_HOME/lib
junit.home=${base.path}/junit3.7
junit.lib=${junit.home}
junit.jar=${junit.lib}/junit.jar
--
To unsubscribe, e-mail: <mailto:avalon-cvs-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@;jakarta.apache.org>