1 package fr.jayasoft.ivy.version; 2 3 import java.util.Comparator ; 4 import java.util.regex.Matcher ; 5 import java.util.regex.Pattern ; 6 7 import fr.jayasoft.ivy.ArtifactInfo; 8 import fr.jayasoft.ivy.Ivy; 9 import fr.jayasoft.ivy.IvyAware; 10 import fr.jayasoft.ivy.LatestStrategy; 11 import fr.jayasoft.ivy.ModuleRevisionId; 12 13 35 public class VersionRangeMatcher extends AbstractVersionMatcher implements IvyAware { 36 private final static String OPEN_INC = "["; 38 private final static String OPEN_EXC = "]"; 39 private final static String CLOSE_INC = "]"; 40 private final static String CLOSE_EXC = "["; 41 private final static String LOWER_INFINITE = "("; 42 private final static String UPPER_INFINITE = ")"; 43 private final static String SEPARATOR = ","; 44 45 private final static String OPEN_INC_PATTERN = "\\"+OPEN_INC; 47 private final static String OPEN_EXC_PATTERN = "\\"+OPEN_EXC; 48 private final static String CLOSE_INC_PATTERN = "\\"+CLOSE_INC; 49 private final static String CLOSE_EXC_PATTERN = "\\"+CLOSE_EXC; 50 private final static String LI_PATTERN = "\\"+LOWER_INFINITE; 51 private final static String UI_PATTERN = "\\"+UPPER_INFINITE; 52 private final static String SEP_PATTERN = "\\"+SEPARATOR; 53 54 private final static String OPEN_PATTERN = "["+OPEN_INC_PATTERN+OPEN_EXC_PATTERN+"]"; 55 private final static String CLOSE_PATTERN = "["+CLOSE_INC_PATTERN+CLOSE_EXC_PATTERN+"]"; 56 private final static String ANY_NON_SPECIAL_PATTERN = "[^"+SEP_PATTERN+OPEN_INC_PATTERN+OPEN_EXC_PATTERN+CLOSE_INC_PATTERN+CLOSE_EXC_PATTERN+LI_PATTERN+UI_PATTERN+"]"; 57 58 private final static String FINITE_PATTERN = OPEN_PATTERN+"("+ANY_NON_SPECIAL_PATTERN+"+)"+SEP_PATTERN+"("+ANY_NON_SPECIAL_PATTERN+"+)"+CLOSE_PATTERN; 59 private final static String LOWER_INFINITE_PATTERN = LI_PATTERN+"\\,("+ANY_NON_SPECIAL_PATTERN+"+)"+CLOSE_PATTERN; 60 private final static String UPPER_INFINITE_PATTERN = OPEN_PATTERN+"("+ANY_NON_SPECIAL_PATTERN+"+)\\,"+UI_PATTERN; 61 62 private final static Pattern FINITE_RANGE = Pattern.compile(FINITE_PATTERN); 63 private final static Pattern LOWER_INFINITE_RANGE = Pattern.compile(LOWER_INFINITE_PATTERN); 64 private final static Pattern UPPER_INFINITE_RANGE = Pattern.compile(UPPER_INFINITE_PATTERN); 65 private final static Pattern ALL_RANGE = Pattern.compile(FINITE_PATTERN+"|"+LOWER_INFINITE_PATTERN+"|"+UPPER_INFINITE_PATTERN); 66 67 private final class MRIDArtifactInfo implements ArtifactInfo { 68 private ModuleRevisionId _mrid; 69 70 public MRIDArtifactInfo(ModuleRevisionId id) { 71 _mrid = id; 72 } 73 74 public long getLastModified() { 75 return 0; 76 } 77 78 public String getRevision() { 79 return _mrid.getRevision(); 80 } 81 } 82 83 private final Comparator COMPARATOR = new Comparator () { 84 public int compare(Object o1, Object o2) { 85 if (o1.equals(o2)) { 86 return 0; 87 } 88 ArtifactInfo art1 = new MRIDArtifactInfo((ModuleRevisionId)o1); 89 ArtifactInfo art2 = new MRIDArtifactInfo((ModuleRevisionId)o2); 90 ArtifactInfo art = getLatestStrategy().findLatest(new ArtifactInfo[] {art1,art2}, null); 91 return art == art1 ? -1 : 1; 92 } 93 }; 94 95 96 private LatestStrategy _latestStrategy; 97 98 private String _latestStrategyName = "default"; 99 100 public VersionRangeMatcher() { 101 super("version-range"); 102 } 103 104 public VersionRangeMatcher(String name) { 105 super(name); 106 } 107 108 public VersionRangeMatcher(String name, LatestStrategy strategy) { 109 super(name); 110 _latestStrategy = strategy; 111 } 112 113 public VersionRangeMatcher(String name, Ivy ivy) { 114 super(name); 115 setIvy(ivy); 116 } 117 118 public boolean isDynamic(ModuleRevisionId askedMrid) { 119 return ALL_RANGE.matcher(askedMrid.getRevision()).matches(); 120 } 121 122 public boolean accept(ModuleRevisionId askedMrid, ModuleRevisionId foundMrid) { 123 String revision = askedMrid.getRevision(); 124 Matcher m; 125 m = FINITE_RANGE.matcher(revision); 126 if (m.matches()) { 127 String lower = m.group(1); 128 String upper = m.group(2); 129 return isUpper(askedMrid, lower, foundMrid, revision.startsWith(OPEN_INC)) 130 && isLower(askedMrid, upper, foundMrid, revision.endsWith(CLOSE_INC)); 131 } 132 m = LOWER_INFINITE_RANGE.matcher(revision); 133 if (m.matches()) { 134 String upper = m.group(1); 135 return isLower(askedMrid, upper, foundMrid, revision.endsWith(CLOSE_INC)); 136 } 137 m = UPPER_INFINITE_RANGE.matcher(revision); 138 if (m.matches()) { 139 String lower = m.group(1); 140 return isUpper(askedMrid, lower, foundMrid, revision.startsWith(OPEN_INC)); 141 } 142 return false; 143 } 144 145 private boolean isLower(ModuleRevisionId askedMrid, String revision, ModuleRevisionId foundMrid, boolean inclusive) { 146 return COMPARATOR.compare(ModuleRevisionId.newInstance(askedMrid, revision), foundMrid) <= (inclusive ? 0 : -1); 147 } 148 149 private boolean isUpper(ModuleRevisionId askedMrid, String revision, ModuleRevisionId foundMrid, boolean inclusive) { 150 return COMPARATOR.compare(ModuleRevisionId.newInstance(askedMrid, revision), foundMrid) >= (inclusive ? 0 : 1); 151 } 152 153 public LatestStrategy getLatestStrategy() { 154 if (_latestStrategy == null) { 155 if (getIvy() == null) { 156 throw new IllegalStateException ("no ivy instance nor latest strategy configured in version range matcher "+this); 157 } 158 if (_latestStrategyName == null) { 159 throw new IllegalStateException ("null latest strategy defined in version range matcher "+this); 160 } 161 _latestStrategy = getIvy().getLatestStrategy(_latestStrategyName); 162 if (_latestStrategy == null) { 163 throw new IllegalStateException ("unknown latest strategy '"+_latestStrategyName+"' configured in version range matcher "+this); 164 } 165 } 166 return _latestStrategy; 167 } 168 169 public void setLatestStrategy(LatestStrategy latestStrategy) { 170 _latestStrategy = latestStrategy; 171 } 172 173 public void setLatest(String latestStrategyName) { 174 _latestStrategyName = latestStrategyName; 175 } 176 177 } 178 | Popular Tags |