01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
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.
16: *
17: */
18: package org.apache.ivy.plugins.matcher;
19:
20: import java.util.regex.PatternSyntaxException;
21:
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;
26:
27: /**
28: * A pattern matcher matching input using unix-like glob matcher expressions. Meta characters are:
29: * <ul>
30: * <li> * - Matches zero or more characters</li>
31: * <li> ? - Matches exactly one character.</li>
32: * </ul>
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>
35: *
36: * @see <a
37: * href="http://jakarta.apache.org/oro/api/org/apache/oro/text/GlobCompiler.html">GlobCompiler</a>
38: */
39: public/* @Immutable */final class GlobPatternMatcher extends
40: AbstractPatternMatcher {
41:
42: public static final GlobPatternMatcher INSTANCE = new GlobPatternMatcher();
43:
44: /*
45: * NOTE: GlobCompiler does ~100K compilation/s - If necessary look into using ThreadLocal for
46: * GlobCompiler/Perl5Matcher to cut on useless object creation - If expression are reused over
47: * and over a LRU cache could make sense
48: */
49:
50: public GlobPatternMatcher() {
51: super (GLOB);
52: }
53:
54: protected Matcher newMatcher(String expression) {
55: return new GlobMatcher(expression);
56: }
57:
58: private static class GlobMatcher implements Matcher {
59: private Pattern pattern;
60:
61: public GlobMatcher(String expression)
62: throws PatternSyntaxException {
63: try {
64: pattern = new GlobCompiler().compile(expression);
65: } catch (MalformedPatternException e) {
66: throw new PatternSyntaxException(e.getMessage(),
67: expression, 0);
68: }
69: }
70:
71: public boolean matches(String input) {
72: if (input == null) {
73: throw new NullPointerException();
74: }
75: return new Perl5Matcher().matches(input, pattern);
76: }
77:
78: public boolean isExact() {
79: return false;
80: }
81: }
82:
83: }
|