| 1 7 package fr.jayasoft.ivy.namespace; 8 9 import java.util.ArrayList ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.regex.Matcher ; 13 import java.util.regex.Pattern ; 14 15 import fr.jayasoft.ivy.ModuleRevisionId; 16 import fr.jayasoft.ivy.util.Message; 17 18 public class MRIDTransformationRule implements NamespaceTransformer { 19 private static class MridRuleMatcher { 20 private static final String [] TYPES = new String [] {"o", "m", "b", "r"}; 21 private Matcher [] _matchers = new Matcher [4]; 22 23 public boolean match(MRIDRule src, ModuleRevisionId mrid) { 24 _matchers[0] = Pattern.compile(getPattern(src.getOrg())).matcher(mrid.getOrganisation()); 25 if (!_matchers[0].matches()) { 26 return false; 27 } 28 _matchers[1] = Pattern.compile(getPattern(src.getModule())).matcher(mrid.getName()); 29 if (!_matchers[1].matches()) { 30 return false; 31 } 32 if (mrid.getBranch() == null) { 33 _matchers[2] = null; 34 } else { 35 _matchers[2] = Pattern.compile(getPattern(src.getBranch())).matcher(mrid.getBranch()); 36 if (!_matchers[2].matches()) { 37 return false; 38 } 39 } 40 _matchers[3] = Pattern.compile(getPattern(src.getRev())).matcher(mrid.getRevision()); 41 if (!_matchers[3].matches()) { 42 return false; 43 } 44 45 return true; 46 } 47 public ModuleRevisionId apply(MRIDRule dest, ModuleRevisionId mrid) { 48 String org = applyRules(dest.getOrg(), "o"); 49 String mod = applyRules(dest.getModule(), "m"); 50 String branch = applyRules(dest.getBranch(), "b"); 51 String rev = applyRules(dest.getRev(), "r"); 52 53 return ModuleRevisionId.newInstance(org, mod, branch, rev, mrid.getExtraAttributes()); 54 } 55 private String applyRules(String str, String type) { 56 for (int i = 0; i < TYPES.length; i++) { 57 str = applyTypeRule(str, TYPES[i], type, _matchers[i]); 58 } 59 return str; 60 } 61 62 private String applyTypeRule(String rule, String type, String ruleType, Matcher m) { 63 if (m == null) { 64 return rule; 65 } 66 String res = rule == null ? "$"+ruleType+"0" : rule; 67 for (int i = 0; i < TYPES.length; i++) { 68 if (TYPES[i].equals(type)) { 69 res = res.replaceAll("([^\\\\])\\$"+type, "$1\\$"); 70 res = res.replaceAll("^\\$"+type, "\\$"); 71 } else { 72 res = res.replaceAll("([^\\\\])\\$"+TYPES[i], "$1\\\\\\$"+TYPES[i]); 73 res = res.replaceAll("^\\$"+TYPES[i], "\\\\\\$"+TYPES[i]); 74 } 75 } 76 77 StringBuffer sb = new StringBuffer (); 78 m.reset(); 79 m.find(); 80 m.appendReplacement(sb, res); 81 82 String str = sb.toString(); 83 if (rule == null && ("$"+ruleType+"0").equals(str)) { 85 return null; 86 } 87 88 return str; 89 } 90 91 private String getPattern(String p) { 92 return p == null ? ".*" : p; 93 } 94 } 95 private List _src = new ArrayList (); 96 private MRIDRule _dest; 97 98 public void addSrc(MRIDRule src) { 99 _src.add(src); 100 } 101 102 public void addDest(MRIDRule dest) { 103 if (_dest != null) { 104 throw new IllegalArgumentException ("only one dest is allowed per mapping"); 105 } 106 _dest = dest; 107 } 108 109 public ModuleRevisionId transform(ModuleRevisionId mrid) { 110 MridRuleMatcher matcher = new MridRuleMatcher(); 111 for (Iterator iter = _src.iterator(); iter.hasNext();) { 112 MRIDRule rule = (MRIDRule)iter.next(); 113 if (matcher.match(rule, mrid)) { 114 ModuleRevisionId destMrid = matcher.apply(_dest, mrid); 115 Message.debug("found matching namespace rule: "+rule+". Applied "+_dest+" on "+mrid+". Transformed to "+destMrid); 116 return destMrid; 117 } 118 } 119 return mrid; 120 } 121 122 public boolean isIdentity() { 123 return false; 124 } 125 126 } 127 | Popular Tags |