2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.ivy.plugins.matcher;
20
import java.util.regex.PatternSyntaxException;
22
import org.apache.oro.text.GlobCompiler;
23
import org.apache.oro.text.regex.MalformedPatternException;
24
import org.apache.oro.text.regex.Pattern;
25
import org.apache.oro.text.regex.Perl5Matcher;
28
* A pattern matcher matching input using unix-like glob matcher expressions. Meta characters are:
30
* <li> * - Matches zero or more characters</li>
31
* <li> ? - Matches exactly one character.</li>
33
* <p/> <b> Note that this matcher is available only with <a
34
* href="http://jakarta.apache.org/oro"Apache Jakarta Oro 2.0.8</a> in your classpath.</b>
37
* href="http://jakarta.apache.org/oro/api/org/apache/oro/text/GlobCompiler.html">GlobCompiler</a>
39
public/* @Immutable */final class GlobPatternMatcher extends AbstractPatternMatcher {
41
public static final GlobPatternMatcher INSTANCE = new GlobPatternMatcher();
44
* NOTE: GlobCompiler does ~100K compilation/s - If necessary look into using ThreadLocal for
45
* GlobCompiler/Perl5Matcher to cut on useless object creation - If expression are reused over
46
* and over a LRU cache could make sense
49
public GlobPatternMatcher() {
53
protected Matcher newMatcher(String expression) {
54
return new GlobMatcher(expression);
57
private static class GlobMatcher implements Matcher {
58
private Pattern pattern;
59
private String expression;
61
private Boolean exact;
63
public GlobMatcher(String expression) throws PatternSyntaxException {
64
this.expression = expression;
66
pattern = new GlobCompiler().compile(expression);
67
} catch (MalformedPatternException e) {
68
throw new PatternSyntaxException(e.getMessage(), expression, 0);
72
public boolean matches(String input) {
74
throw new NullPointerException();
76
return new Perl5Matcher().matches(input, pattern);
79
public boolean isExact() {
81
exact = calculateExact();
83
return exact.booleanValue();
86
private Boolean calculateExact() {
87
Boolean result = Boolean.TRUE;
89
char[] expressionChars = expression.toCharArray();
90
for (int i = 0; i < expressionChars.length; i++) {
91
char ch = expressionChars[i];
92
if (ch == '*' || ch == '?' || ch == '[' || ch == ']') {
93
result = Boolean.FALSE;