1 package org.codehaus.plexus.util;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6
7
8
9
10
11
12 public class MatchPatterns
13 {
14 private final MatchPattern[] patterns;
15
16 private MatchPatterns( MatchPattern[] patterns )
17 {
18 this.patterns = patterns;
19 }
20
21
22
23
24
25
26
27
28
29
30 public boolean matches( String name, boolean isCaseSensitive )
31 {
32 String[] tokenized = MatchPattern.tokenizePathToString( name, File.separator );
33 return matches( name, tokenized, isCaseSensitive );
34 }
35
36 public boolean matches( String name, String[] tokenizedName, boolean isCaseSensitive )
37 {
38 char[][] tokenizedNameChar = new char[tokenizedName.length][];
39 for ( int i = 0; i < tokenizedName.length; i++ )
40 {
41 tokenizedNameChar[i] = tokenizedName[i].toCharArray();
42 }
43 return matches(name, tokenizedNameChar, isCaseSensitive);
44 }
45
46 public boolean matches(String name, char[][] tokenizedNameChar, boolean isCaseSensitive)
47 {
48 for ( MatchPattern pattern : patterns )
49 {
50 if ( pattern.matchPath( name, tokenizedNameChar, isCaseSensitive ) )
51 {
52 return true;
53 }
54 }
55 return false;
56 }
57
58 public boolean matchesPatternStart( String name, boolean isCaseSensitive )
59 {
60 for ( MatchPattern includesPattern : patterns )
61 {
62 if ( includesPattern.matchPatternStart( name, isCaseSensitive ) )
63 {
64 return true;
65 }
66 }
67 return false;
68 }
69
70 public static MatchPatterns from( String... sources )
71 {
72 final int length = sources.length;
73 MatchPattern[] result = new MatchPattern[length];
74 for ( int i = 0; i < length; i++ )
75 {
76 result[i] = MatchPattern.fromString( sources[i] );
77 }
78 return new MatchPatterns( result );
79 }
80
81 public static MatchPatterns from( Iterable<String> strings )
82 {
83 return new MatchPatterns( getMatchPatterns( strings ) );
84 }
85
86 private static MatchPattern[] getMatchPatterns( Iterable<String> items )
87 {
88 List<MatchPattern> result = new ArrayList<MatchPattern>();
89 for ( String string : items )
90 {
91 result.add( MatchPattern.fromString( string ) );
92 }
93 return result.toArray( new MatchPattern[0] );
94 }
95
96 }