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.namespace;
019:
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.regex.Matcher;
024: import java.util.regex.Pattern;
025:
026: import org.apache.ivy.core.module.id.ModuleRevisionId;
027: import org.apache.ivy.util.Message;
028:
029: public class MRIDTransformationRule implements NamespaceTransformer {
030: private static class MridRuleMatcher {
031: private static final String[] TYPES = new String[] { "o", "m",
032: "b", "r" };
033:
034: private Matcher[] matchers = new Matcher[TYPES.length];
035:
036: public boolean match(MRIDRule src, ModuleRevisionId mrid) {
037: //CheckStyle:MagicNumber| OFF
038: matchers[0] = Pattern.compile(getPattern(src.getOrg()))
039: .matcher(mrid.getOrganisation());
040: if (!matchers[0].matches()) {
041: return false;
042: }
043: matchers[1] = Pattern.compile(getPattern(src.getModule()))
044: .matcher(mrid.getName());
045: if (!matchers[1].matches()) {
046: return false;
047: }
048: if (mrid.getBranch() == null) {
049: matchers[2] = null;
050: } else {
051: matchers[2] = Pattern.compile(
052: getPattern(src.getBranch())).matcher(
053: mrid.getBranch());
054: if (!matchers[2].matches()) {
055: return false;
056: }
057: }
058: matchers[3] = Pattern.compile(getPattern(src.getRev()))
059: .matcher(mrid.getRevision());
060: if (!matchers[3].matches()) {
061: return false;
062: }
063:
064: return true;
065: //CheckStyle:MagicNumber| ON
066: }
067:
068: public ModuleRevisionId apply(MRIDRule dest,
069: ModuleRevisionId mrid) {
070: String org = applyRules(dest.getOrg(), "o");
071: String mod = applyRules(dest.getModule(), "m");
072: String branch = applyRules(dest.getBranch(), "b");
073: String rev = applyRules(dest.getRev(), "r");
074:
075: return ModuleRevisionId.newInstance(org, mod, branch, rev,
076: mrid.getExtraAttributes());
077: }
078:
079: private String applyRules(String str, String type) {
080: for (int i = 0; i < TYPES.length; i++) {
081: str = applyTypeRule(str, TYPES[i], type, matchers[i]);
082: }
083: return str;
084: }
085:
086: private String applyTypeRule(String rule, String type,
087: String ruleType, Matcher m) {
088: if (m == null) {
089: return rule;
090: }
091: String res = rule == null ? "$" + ruleType + "0" : rule;
092: for (int i = 0; i < TYPES.length; i++) {
093: if (TYPES[i].equals(type)) {
094: res = res
095: .replaceAll("([^\\\\])\\$" + type, "$1\\$");
096: res = res.replaceAll("^\\$" + type, "\\$");
097: } else {
098: res = res.replaceAll("([^\\\\])\\$" + TYPES[i],
099: "$1\\\\\\$" + TYPES[i]);
100: res = res.replaceAll("^\\$" + TYPES[i], "\\\\\\$"
101: + TYPES[i]);
102: }
103: }
104:
105: StringBuffer sb = new StringBuffer();
106: m.reset();
107: m.find();
108: m.appendReplacement(sb, res);
109:
110: String str = sb.toString();
111: // null rule not replaced, let it be null
112: if (rule == null && ("$" + ruleType + "0").equals(str)) {
113: return null;
114: }
115:
116: return str;
117: }
118:
119: private String getPattern(String p) {
120: return p == null ? ".*" : p;
121: }
122: }
123:
124: private List src = new ArrayList();
125:
126: private MRIDRule dest;
127:
128: public void addSrc(MRIDRule src) {
129: this .src.add(src);
130: }
131:
132: public void addDest(MRIDRule dest) {
133: if (this .dest != null) {
134: throw new IllegalArgumentException(
135: "only one dest is allowed per mapping");
136: }
137: this .dest = dest;
138: }
139:
140: public ModuleRevisionId transform(ModuleRevisionId mrid) {
141: MridRuleMatcher matcher = new MridRuleMatcher();
142: for (Iterator iter = src.iterator(); iter.hasNext();) {
143: MRIDRule rule = (MRIDRule) iter.next();
144: if (matcher.match(rule, mrid)) {
145: ModuleRevisionId destMrid = matcher.apply(dest, mrid);
146: Message.debug("found matching namespace rule: " + rule
147: + ". Applied " + dest + " on " + mrid
148: + ". Transformed to " + destMrid);
149: return destMrid;
150: }
151: }
152: return mrid;
153: }
154:
155: public boolean isIdentity() {
156: return false;
157: }
158:
159: }
|