~ubuntu-branches/ubuntu/trusty/ivy/trusty-proposed

« back to all changes in this revision

Viewing changes to src/java/org/apache/ivy/plugins/matcher/GlobPatternMatcher.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2009-03-06 22:04:56 UTC
  • Revision ID: james.westby@ubuntu.com-20090306220456-5v37luqiuqda8ewp
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
8
 *
 
9
 *      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 AbstractPatternMatcher {
 
40
 
 
41
    public static final GlobPatternMatcher INSTANCE = new GlobPatternMatcher();
 
42
 
 
43
    /*
 
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
 
47
     */
 
48
 
 
49
    public GlobPatternMatcher() {
 
50
        super(GLOB);
 
51
    }
 
52
 
 
53
    protected Matcher newMatcher(String expression) {
 
54
        return new GlobMatcher(expression);
 
55
    }
 
56
 
 
57
    private static class GlobMatcher implements Matcher {
 
58
        private Pattern pattern;
 
59
        private String expression;
 
60
 
 
61
        private Boolean exact;
 
62
        
 
63
        public GlobMatcher(String expression) throws PatternSyntaxException {
 
64
            this.expression = expression;
 
65
            try {
 
66
                pattern = new GlobCompiler().compile(expression);
 
67
            } catch (MalformedPatternException e) {
 
68
                throw new PatternSyntaxException(e.getMessage(), expression, 0);
 
69
            }
 
70
        }
 
71
 
 
72
        public boolean matches(String input) {
 
73
            if (input == null) {
 
74
                throw new NullPointerException();
 
75
            }
 
76
            return new Perl5Matcher().matches(input, pattern);
 
77
        }
 
78
 
 
79
        public boolean isExact() {
 
80
            if (exact == null) {
 
81
                exact = calculateExact();
 
82
            }
 
83
            return exact.booleanValue();
 
84
        }
 
85
        
 
86
        private Boolean calculateExact() {
 
87
            Boolean result = Boolean.TRUE;
 
88
            
 
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;
 
94
                    break;
 
95
                }
 
96
            }
 
97
            
 
98
            return result;
 
99
        }
 
100
    }
 
101
 
 
102
}