KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > namespace > MRIDTransformationRule


1 /*
2  * This file is subject to the licence found in LICENCE.TXT in the root directory of the project.
3  * Copyright Jayasoft 2005 - All rights reserved
4  *
5  * #SNAPSHOT#
6  */

7 package fr.jayasoft.ivy.namespace;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.regex.Matcher JavaDoc;
13 import java.util.regex.Pattern JavaDoc;
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 JavaDoc[] TYPES = new String JavaDoc[] {"o", "m", "b", "r"};
21         private Matcher JavaDoc[] _matchers = new Matcher JavaDoc[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 JavaDoc org = applyRules(dest.getOrg(), "o");
49             String JavaDoc mod = applyRules(dest.getModule(), "m");
50             String JavaDoc branch = applyRules(dest.getBranch(), "b");
51             String JavaDoc rev = applyRules(dest.getRev(), "r");
52             
53             return ModuleRevisionId.newInstance(org, mod, branch, rev, mrid.getExtraAttributes());
54         }
55         private String JavaDoc applyRules(String JavaDoc str, String JavaDoc 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 JavaDoc applyTypeRule(String JavaDoc rule, String JavaDoc type, String JavaDoc ruleType, Matcher JavaDoc m) {
63             if (m == null) {
64                 return rule;
65             }
66             String JavaDoc 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 JavaDoc sb = new StringBuffer JavaDoc();
78             m.reset();
79             m.find();
80             m.appendReplacement(sb, res);
81
82             String JavaDoc str = sb.toString();
83             // null rule not replaced, let it be null
84
if (rule == null && ("$"+ruleType+"0").equals(str)) {
85                 return null;
86             }
87             
88             return str;
89         }
90         
91         private String JavaDoc getPattern(String JavaDoc p) {
92             return p == null ? ".*" : p;
93         }
94     }
95     private List JavaDoc _src = new ArrayList JavaDoc();
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 JavaDoc("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 JavaDoc 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