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

« back to all changes in this revision

Viewing changes to src/java/org/apache/ivy/plugins/version/Match.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.version;
 
19
 
 
20
import java.util.ArrayList;
 
21
import java.util.HashMap;
 
22
import java.util.List;
 
23
import java.util.Map;
 
24
import java.util.StringTokenizer;
 
25
 
 
26
import org.apache.ivy.core.IvyContext;
 
27
import org.apache.ivy.core.IvyPatternHelper;
 
28
import org.apache.ivy.core.module.id.ModuleRevisionId;
 
29
import org.apache.ivy.plugins.matcher.Matcher;
 
30
import org.apache.ivy.plugins.matcher.PatternMatcher;
 
31
 
 
32
/**
 
33
 * 
 
34
 */
 
35
public class Match {
 
36
    private String revision;
 
37
 
 
38
    private String pattern;
 
39
 
 
40
    private String args;
 
41
 
 
42
    private String matcher;
 
43
 
 
44
    public String getArgs() {
 
45
        return args;
 
46
    }
 
47
 
 
48
    public void setArgs(String args) {
 
49
        this.args = args;
 
50
    }
 
51
 
 
52
    public String getMatcher() {
 
53
        return matcher;
 
54
    }
 
55
 
 
56
    public void setMatcher(String matcher) {
 
57
        this.matcher = matcher;
 
58
    }
 
59
 
 
60
    public String getPattern() {
 
61
        return pattern;
 
62
    }
 
63
 
 
64
    public void setPattern(String pattern) {
 
65
        this.pattern = pattern;
 
66
    }
 
67
 
 
68
    public String getRevision() {
 
69
        return revision;
 
70
    }
 
71
 
 
72
    public void setRevision(String revision) {
 
73
        this.revision = revision;
 
74
    }
 
75
 
 
76
    public Matcher getPatternMatcher(ModuleRevisionId askedMrid) {
 
77
        String revision = askedMrid.getRevision();
 
78
 
 
79
        String[] args = split(getArgs());
 
80
        String[] argValues = getRevisionArgs(revision);
 
81
 
 
82
        if (args.length != argValues.length) {
 
83
            return new NoMatchMatcher();
 
84
        }
 
85
 
 
86
        Map variables = new HashMap();
 
87
        for (int i = 0; i < args.length; i++) {
 
88
            variables.put(args[i], argValues[i]);
 
89
        }
 
90
 
 
91
        String pattern = getPattern();
 
92
        pattern = IvyPatternHelper.substituteVariables(pattern, variables);
 
93
 
 
94
        PatternMatcher pMatcher = IvyContext.getContext().getSettings().getMatcher(matcher);
 
95
        return pMatcher.getMatcher(pattern);
 
96
    }
 
97
 
 
98
    private String[] getRevisionArgs(String revision) {
 
99
        int bracketStartIndex = revision.indexOf('(');
 
100
        if (bracketStartIndex == -1) {
 
101
            return new String[0];
 
102
        }
 
103
 
 
104
        int bracketEndIndex = revision.indexOf(')');
 
105
        if (bracketEndIndex <= (bracketStartIndex + 1)) {
 
106
            return new String[0];
 
107
        }
 
108
 
 
109
        String args = revision.substring(bracketStartIndex + 1, bracketEndIndex);
 
110
        return split(args);
 
111
    }
 
112
 
 
113
    private static String[] split(String string) {
 
114
        if (string == null) {
 
115
            return new String[0];
 
116
        }
 
117
 
 
118
        StringTokenizer tokenizer = new StringTokenizer(string, ", ");
 
119
        List tokens = new ArrayList();
 
120
        while (tokenizer.hasMoreTokens()) {
 
121
            tokens.add(tokenizer.nextToken());
 
122
        }
 
123
 
 
124
        return (String[]) tokens.toArray(new String[tokens.size()]);
 
125
    }
 
126
 
 
127
    private static class NoMatchMatcher implements Matcher {
 
128
        public boolean isExact() {
 
129
            return false;
 
130
        }
 
131
 
 
132
        public boolean matches(String str) {
 
133
            return false;
 
134
        }
 
135
    }
 
136
}