001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.plugins.version;
019:
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.StringTokenizer;
025:
026: import org.apache.ivy.core.IvyContext;
027: import org.apache.ivy.core.IvyPatternHelper;
028: import org.apache.ivy.core.module.id.ModuleRevisionId;
029: import org.apache.ivy.plugins.matcher.Matcher;
030: import org.apache.ivy.plugins.matcher.PatternMatcher;
031:
032: /**
033: *
034: */
035: public class Match {
036: private String revision;
037:
038: private String pattern;
039:
040: private String args;
041:
042: private String matcher;
043:
044: public String getArgs() {
045: return args;
046: }
047:
048: public void setArgs(String args) {
049: this .args = args;
050: }
051:
052: public String getMatcher() {
053: return matcher;
054: }
055:
056: public void setMatcher(String matcher) {
057: this .matcher = matcher;
058: }
059:
060: public String getPattern() {
061: return pattern;
062: }
063:
064: public void setPattern(String pattern) {
065: this .pattern = pattern;
066: }
067:
068: public String getRevision() {
069: return revision;
070: }
071:
072: public void setRevision(String revision) {
073: this .revision = revision;
074: }
075:
076: public Matcher getPatternMatcher(ModuleRevisionId askedMrid) {
077: String revision = askedMrid.getRevision();
078:
079: String[] args = split(getArgs());
080: String[] argValues = getRevisionArgs(revision);
081:
082: if (args.length != argValues.length) {
083: return new NoMatchMatcher();
084: }
085:
086: Map variables = new HashMap();
087: for (int i = 0; i < args.length; i++) {
088: variables.put(args[i], argValues[i]);
089: }
090:
091: String pattern = getPattern();
092: pattern = IvyPatternHelper.substituteVariables(pattern,
093: variables);
094:
095: PatternMatcher pMatcher = IvyContext.getContext().getSettings()
096: .getMatcher(matcher);
097: return pMatcher.getMatcher(pattern);
098: }
099:
100: private String[] getRevisionArgs(String revision) {
101: int bracketStartIndex = revision.indexOf('(');
102: if (bracketStartIndex == -1) {
103: return new String[0];
104: }
105:
106: int bracketEndIndex = revision.indexOf(')');
107: if (bracketEndIndex <= (bracketStartIndex + 1)) {
108: return new String[0];
109: }
110:
111: String args = revision.substring(bracketStartIndex + 1,
112: bracketEndIndex);
113: return split(args);
114: }
115:
116: private static String[] split(String string) {
117: if (string == null) {
118: return new String[0];
119: }
120:
121: StringTokenizer tokenizer = new StringTokenizer(string, ", ");
122: List tokens = new ArrayList();
123: while (tokenizer.hasMoreTokens()) {
124: tokens.add(tokenizer.nextToken());
125: }
126:
127: return (String[]) tokens.toArray(new String[tokens.size()]);
128: }
129:
130: private static class NoMatchMatcher implements Matcher {
131: public boolean isExact() {
132: return false;
133: }
134:
135: public boolean matches(String str) {
136: return false;
137: }
138: }
139: }
|