1 6 package fr.jayasoft.ivy.conflict; 7 8 import java.util.Collection ; 9 import java.util.Collections ; 10 import java.util.Iterator ; 11 import java.util.regex.Matcher ; 12 import java.util.regex.Pattern ; 13 14 import fr.jayasoft.ivy.IvyNode; 15 import fr.jayasoft.ivy.util.Message; 16 17 38 public class RegexpConflictManager extends AbstractConflictManager { 39 private Pattern pattern = Pattern.compile("(.*)"); 40 41 private boolean mIgnoreNonMatching; 42 43 public RegexpConflictManager() { 44 } 45 46 public void setRegexp(String regexp) { 47 pattern = Pattern.compile(regexp); 48 Matcher matcher = pattern.matcher("abcdef"); 49 if (matcher.groupCount() != 1) { 50 String message = "Pattern does not contain ONE (capturing group): '" 51 + pattern + "'"; 52 Message.error(message); 53 throw new IllegalArgumentException (message); 54 } 55 } 56 57 public void setIgnoreNonMatching(boolean ignoreNonMatching) { 58 mIgnoreNonMatching = ignoreNonMatching; 59 } 60 61 public Collection resolveConflicts(IvyNode parent, Collection conflicts) { 62 IvyNode lastNode = null; 63 for (Iterator iter = conflicts.iterator(); iter.hasNext();) { 64 IvyNode node = (IvyNode) iter.next(); 65 66 if (lastNode != null && !matchEquals(node, lastNode)) { 67 String 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 getMatch(IvyNode node) { 92 String revision = node.getId().getRevision(); 93 Matcher matcher = pattern.matcher(revision); 94 if (matcher.matches()) { 95 String 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 message) { 109 if (mIgnoreNonMatching) { 110 Message.warn(message); 111 } else { 112 throw new StrictConflictException(message); 113 } 114 } 115 } 116 | Popular Tags |