KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > conflict > RegexpConflictManager


1 /*
2  * This file is subject to the license found in LICENCE.TXT in the root directory of the project.
3  *
4  * #SNAPSHOT#
5  */

6 package fr.jayasoft.ivy.conflict;
7
8 import java.util.Collection JavaDoc;
9 import java.util.Collections JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.regex.Matcher JavaDoc;
12 import java.util.regex.Pattern JavaDoc;
13
14 import fr.jayasoft.ivy.IvyNode;
15 import fr.jayasoft.ivy.util.Message;
16
17 /**
18  * A ConflictManager that can be used to resolve conflicts based on regular
19  * expressions of the revision of the module. The conflict manager is added like
20  * this:
21  *
22  * <pre>
23  * &lt;!-- Match all revisions, but ignore the last dot(.) and the character after it.
24  * Used to match api changes in out milestones. --&gt;
25  * &lt;conflict-managers&gt;
26  * &lt;regexp-cm name=&quot;regexp&quot; regexp=&quot;(.*)\..$&quot; ignoreNonMatching=&quot;true&quot;/&gt;
27  * &lt;/conflict-managers&gt;
28  * </pre>
29  *
30  * The regular expression must contain a capturing group. The group will be used
31  * to resolve the conflicts by an String.equals() test. If ignoreNonMatching is
32  * false non matching modules will result in an exception. If it is true they
33  * will be compaired by their full revision.
34  *
35  * @author Anders janmyr
36  *
37  */

38 public class RegexpConflictManager extends AbstractConflictManager {
39     private Pattern JavaDoc pattern = Pattern.compile("(.*)");
40
41     private boolean mIgnoreNonMatching;
42
43     public RegexpConflictManager() {
44     }
45
46     public void setRegexp(String JavaDoc regexp) {
47         pattern = Pattern.compile(regexp);
48         Matcher JavaDoc matcher = pattern.matcher("abcdef");
49         if (matcher.groupCount() != 1) {
50             String JavaDoc message = "Pattern does not contain ONE (capturing group): '"
51                     + pattern + "'";
52             Message.error(message);
53             throw new IllegalArgumentException JavaDoc(message);
54         }
55     }
56
57     public void setIgnoreNonMatching(boolean ignoreNonMatching) {
58         mIgnoreNonMatching = ignoreNonMatching;
59     }
60
61     public Collection JavaDoc resolveConflicts(IvyNode parent, Collection JavaDoc conflicts) {
62         IvyNode lastNode = null;
63         for (Iterator JavaDoc iter = conflicts.iterator(); iter.hasNext();) {
64             IvyNode node = (IvyNode) iter.next();
65
66             if (lastNode != null && !matchEquals(node, lastNode)) {
67                 String JavaDoc msg = lastNode + ":" + getMatch(lastNode)
68                         + " (needed by " + lastNode.getParent()
69                         + ") conflicts with " + node + ":" + getMatch(node)
70                         + " (needed by " + node.getParent() + ")";
71                 Message.error(msg);
72                 Message.sumupProblems();
73                 throw new StrictConflictException(msg);
74             }
75             if (lastNode == null || nodeIsGreater(node, lastNode)) {
76                 lastNode = node;
77             }
78         }
79
80         return Collections.singleton(lastNode);
81     }
82
83     private boolean nodeIsGreater(IvyNode node, IvyNode lastNode) {
84         return getMatch(node).compareTo(getMatch(lastNode)) > 0;
85     }
86
87     private boolean matchEquals(IvyNode lastNode, IvyNode node) {
88         return getMatch(lastNode).equals(getMatch(node));
89     }
90
91     private String JavaDoc getMatch(IvyNode node) {
92         String JavaDoc revision = node.getId().getRevision();
93         Matcher JavaDoc matcher = pattern.matcher(revision);
94         if (matcher.matches()) {
95             String JavaDoc match = matcher.group(1);
96             if (match != null) {
97                 return match;
98             }
99             warnOrThrow("First group of pattern: '" + pattern
100                     + "' does not match: " + revision + " " + node);
101         } else {
102             warnOrThrow("Pattern: '" + pattern + "' does not match: "
103                     + revision + " " + node);
104         }
105         return revision;
106     }
107
108     private void warnOrThrow(String JavaDoc message) {
109         if (mIgnoreNonMatching) {
110             Message.warn(message);
111         } else {
112             throw new StrictConflictException(message);
113         }
114     }
115 }
116
Popular Tags