Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3 changes to make external usage easier #277

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ limitations under the License.
</parent>

<artifactId>plexus-utils</artifactId>
<version>3.5.1</version>
<version>3.5.2-SNAPSHOT</version>

<name>Plexus Common Utilities</name>
<description>A collection of various utility classes to ease working with strings, files, command lines, XML and
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/codehaus/plexus/util/MatchPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ private MatchPattern( String source, String separator )

}

/**
* Gets the source pattern for this matchpattern.
* @return the source string without Ant or Regex pattern markers.
*/
public String getSource() {
return regexPattern == null ? source : regexPattern;
}

public boolean matchPath( String str, boolean isCaseSensitive )
{
if ( regexPattern != null )
Expand Down Expand Up @@ -116,7 +124,7 @@ public boolean startsWith( String string )
return source.startsWith( string );
}

static String[] tokenizePathToString( String path, String separator )
public static String[] tokenizePathToString( String path, String separator )
{
List<String> ret = new ArrayList<String>();
StringTokenizer st = new StringTokenizer( path, separator );
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/codehaus/plexus/util/MatchPatterns.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ private MatchPatterns( MatchPattern[] patterns )
this.patterns = patterns;
}

/**
* Gets a list of enclosed MatchPattern sources.
* @return A list of enclosed MatchPattern sources.
*/
public List<String> getSources() {
List<String> sources = new ArrayList<>();
for (MatchPattern pattern : patterns) {
sources.add(pattern.getSource());
}
return sources;
}


/**
* <p>Checks these MatchPatterns against a specified string.</p>
*
Expand Down
27 changes: 23 additions & 4 deletions src/main/java/org/codehaus/plexus/util/SelectorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ public static boolean matchPatternStart( String pattern, String str, boolean isC
}
}

static boolean isAntPrefixedPattern( String pattern )
public static boolean isAntPrefixedPattern( String pattern )
{
return pattern.length() > ( ANT_HANDLER_PREFIX.length() + PATTERN_HANDLER_SUFFIX.length() + 1 )
return pattern.length() > ( ANT_HANDLER_PREFIX.length() + PATTERN_HANDLER_SUFFIX.length())
&& pattern.startsWith( ANT_HANDLER_PREFIX ) && pattern.endsWith( PATTERN_HANDLER_SUFFIX );
}

Expand Down Expand Up @@ -281,9 +281,9 @@ private static String toOSRelatedPath( String pattern, String separator )
return pattern;
}

static boolean isRegexPrefixedPattern( String pattern )
public static boolean isRegexPrefixedPattern( String pattern )
{
return pattern.length() > ( REGEX_HANDLER_PREFIX.length() + PATTERN_HANDLER_SUFFIX.length() + 1 )
return pattern.length() > ( REGEX_HANDLER_PREFIX.length() + PATTERN_HANDLER_SUFFIX.length())
&& pattern.startsWith( REGEX_HANDLER_PREFIX ) && pattern.endsWith( PATTERN_HANDLER_SUFFIX );
}

Expand Down Expand Up @@ -834,4 +834,23 @@ public static String removeWhitespace( String input )
}
return result.toString();
}

/**
* Extract the pattern without the Regex or Ant prefix. In the case of Ant style matches ensure
* that the path uses specified separator.
* @param pattern the pattern to extract from.
* @param separator the system file name separator in the pattern.
* @return The pattern without the Regex or Ant prefix.
*/
public static String extractPattern(final String pattern, final String separator) {
if (isRegexPrefixedPattern(pattern)) {
return pattern.substring(
REGEX_HANDLER_PREFIX.length(), pattern.length() - PATTERN_HANDLER_SUFFIX.length());
} else {
String localPattern = isAntPrefixedPattern(pattern)
? pattern.substring(ANT_HANDLER_PREFIX.length(), pattern.length() - PATTERN_HANDLER_SUFFIX.length())
: pattern;
return toOSRelatedPath(localPattern, separator);
}
}
}
32 changes: 32 additions & 0 deletions src/test/java/org/codehaus/plexus/util/MatchPatternTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* limitations under the License.
*/

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

Expand All @@ -30,6 +32,20 @@
*/
public class MatchPatternTest
{
/**
* <p>testGetSource</p>
*/
@Test
public void testGetSource()
{
MatchPattern mp = MatchPattern.fromString( "ABC*" );
assertEquals("ABC*", mp.getSource());
mp = MatchPattern.fromString( "%ant[some/ABC*]" );
assertEquals("some/ABC*", mp.getSource());
mp = MatchPattern.fromString( "%regex[[ABC].*]" );
assertEquals("[ABC].*", mp.getSource());
}

/**
* <p>testMatchPath.</p>
*
Expand Down Expand Up @@ -63,4 +79,20 @@ public void testMatchPatternStart()
assertFalse( mp.matchPatternStart( "XXXX", false ) );
}

/**
* <p>testTokenizePathToString.</p>
*/
@Test
public void testTokenizePathToString()
{
String[] expected = {"hello", "world"};
String[] actual = MatchPattern.tokenizePathToString("hello/world", "/");
assertArrayEquals(expected, actual);

actual = MatchPattern.tokenizePathToString("/hello/world", "/");
assertArrayEquals(expected, actual);

actual = MatchPattern.tokenizePathToString("/hello/world/", "/");
assertArrayEquals(expected, actual);
}
}
16 changes: 16 additions & 0 deletions src/test/java/org/codehaus/plexus/util/MatchPatternsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
* limitations under the License.
*/

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;

/**
* <p>MatchPatternsTest class.</p>
*
Expand All @@ -30,6 +34,18 @@
*/
public class MatchPatternsTest
{
/**
* <p>testGetSource</p>
*/
@Test
public void testGetSources()
{
List<String> expected = Arrays.asList("ABC**", "some/ABC*", "[ABC].*");
MatchPatterns from = MatchPatterns.from( "ABC**", "%ant[some/ABC*]", "%regex[[ABC].*]" );
List<String> actual = from.getSources();
assertEquals(expected, actual);
}

/**
* <p>testMatches.</p>
*
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/org/codehaus/plexus/util/SelectorUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

Expand All @@ -31,6 +32,41 @@
*/
public class SelectorUtilsTest
{
/**
* <p>testExtractPattern.</p>
*/
@Test
public void testExtractPattern() {
assertEquals("[A-Z].*", SelectorUtils.extractPattern("%regex[[A-Z].*]", "/"));
assertEquals("ABC*", SelectorUtils.extractPattern("%ant[ABC*]", "/"));
assertEquals("some/ABC*", SelectorUtils.extractPattern("%ant[some/ABC*]", "/"));
assertEquals("some\\ABC*", SelectorUtils.extractPattern("%ant[some\\ABC*]", "\\"));
assertEquals("some/ABC*", SelectorUtils.extractPattern("%ant[some\\ABC*]", "/"));
assertEquals("some\\ABC*", SelectorUtils.extractPattern("%ant[some/ABC*]", "\\"));
}

/**
* <p>testIsAntPrefixedPattern.</p>
*/
@Test
public void testIsAntPrefixedPattern() {
assertTrue(SelectorUtils.isAntPrefixedPattern("%ant[A]")); // single char not allowed
assertTrue(SelectorUtils.isAntPrefixedPattern("%ant[AB]"));
assertFalse(SelectorUtils.isAntPrefixedPattern("%ant[]"));
assertFalse(SelectorUtils.isAntPrefixedPattern("*"));
}

/**
* <p>testIsRegexPrefixedPattern.</p>
*/
@Test
public void testIsRegexPrefixedPattern() {
assertTrue(SelectorUtils.isRegexPrefixedPattern("%regex[A]")); // single char not allowed
assertTrue(SelectorUtils.isRegexPrefixedPattern("%regex[.*]"));
assertFalse(SelectorUtils.isRegexPrefixedPattern("%regex[]"));
assertFalse(SelectorUtils.isRegexPrefixedPattern("*"));
}

/**
* <p>testMatchPath_DefaultFileSeparator.</p>
*/
Expand Down
Loading