| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.om.NameChecker; |
| 11 |
import net.sf.saxon.trans.XPathException; |
| 12 |
|
| 13 |
import java.util.ArrayList; |
| 14 |
import java.util.List; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class PackageVersion implements Comparable<PackageVersion> { |
| 23 |
public List<Integer> parts; |
| 24 |
public String suffix; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public static PackageVersion ZERO = new PackageVersion(new int[]{0}); |
| 30 |
public static PackageVersion ONE = new PackageVersion(new int[]{1}); |
| 31 |
public static PackageVersion MAX_VALUE = new PackageVersion(new int[]{Integer.MAX_VALUE}); |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public PackageVersion(int[] values) { |
| 40 |
parts = new ArrayList<Integer>(values.length); |
| 41 |
for (int value : values) { |
| 42 |
parts.add(value); |
| 43 |
} |
| 44 |
trimTrailingZeroes(); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private void trimTrailingZeroes() { |
| 51 |
for (int i = parts.size() - 1; i > 0; i--) { |
| 52 |
if (parts.get(i) != 0) { |
| 53 |
return; |
| 54 |
} else { |
| 55 |
parts.remove(i); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public PackageVersion(String s) throws XPathException { |
| 68 |
parts = new ArrayList<Integer>(); |
| 69 |
String original = s; |
| 70 |
if (s.contains("-")) { |
| 71 |
int i = s.indexOf('-'); |
| 72 |
suffix = s.substring(i + 1); |
| 73 |
if (!NameChecker.isValidNCName(suffix)) { |
| 74 |
throw new XPathException("Illegal NCName as package-version NamePart: " + original, "XTSE0020"); |
| 75 |
} |
| 76 |
s = s.substring(0, i); |
| 77 |
} |
| 78 |
if (s.equals("")) { |
| 79 |
throw new XPathException("No numeric component of package-version: " + original, "XTSE0020"); |
| 80 |
} |
| 81 |
if (s.startsWith(".")) { |
| 82 |
throw new XPathException("The package-version cannot start with '.'", "XTSE0020"); |
| 83 |
} |
| 84 |
if (s.endsWith(".")) { |
| 85 |
throw new XPathException("The package-version cannot end with '.'", "XTSE0020"); |
| 86 |
} |
| 87 |
for (String p : s.trim().split("\\.")) { |
| 88 |
try { |
| 89 |
parts.add(Integer.valueOf(p)); |
| 90 |
} catch (NumberFormatException e) { |
| 91 |
throw new XPathException("Error in package-version: " + e.getMessage(), "XTSE0020"); |
| 92 |
} |
| 93 |
} |
| 94 |
trimTrailingZeroes(); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public boolean equals(Object o) { |
| 99 |
if (o instanceof PackageVersion) { |
| 100 |
PackageVersion p = (PackageVersion) o; |
| 101 |
if (parts.equals(p.parts)) { |
| 102 |
if (suffix != null) { |
| 103 |
return suffix.equals(p.suffix); |
| 104 |
} else { |
| 105 |
return p.suffix == null; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public boolean equalsIgnoringSuffix(PackageVersion other) { |
| 123 |
return parts.equals(other.parts); |
| 124 |
} |
| 125 |
|
| 126 |
public int compareTo(PackageVersion o) { |
| 127 |
PackageVersion pv = (PackageVersion) o; |
| 128 |
List<Integer> p = pv.parts; |
| 129 |
int extent = parts.size() - p.size(); |
| 130 |
int len = Math.min(parts.size(), p.size()); |
| 131 |
for (int i = 0; i < len; i++) { |
| 132 |
int comp = parts.get(i).compareTo(p.get(i)); |
| 133 |
if (comp != 0) { |
| 134 |
return comp; |
| 135 |
} |
| 136 |
} |
| 137 |
if (extent == 0) { |
| 138 |
if (suffix != null) { |
| 139 |
if (pv.suffix == null) { |
| 140 |
return -1; |
| 141 |
} else { |
| 142 |
return suffix.compareTo(pv.suffix); |
| 143 |
} |
| 144 |
} else if (pv.suffix != null) { |
| 145 |
return +1; |
| 146 |
} |
| 147 |
} |
| 148 |
return extent; |
| 149 |
} |
| 150 |
|
| 151 |
public String toString() { |
| 152 |
StringBuilder result = new StringBuilder(); |
| 153 |
for (Integer i : parts) { |
| 154 |
result.append(".").append(i); |
| 155 |
} |
| 156 |
if (!parts.isEmpty()) { |
| 157 |
result = new StringBuilder(result.substring(1)); |
| 158 |
} |
| 159 |
if (suffix != null) { |
| 160 |
result.append("-").append(suffix); |
| 161 |
} |
| 162 |
return result.toString(); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public boolean isPrefix(PackageVersion v) { |
| 173 |
if (v.parts.size() >= parts.size()) { |
| 174 |
for (int i = 0; i < parts.size(); i++) { |
| 175 |
if (!parts.get(i).equals(v.parts.get(i))) { |
| 176 |
return false; |
| 177 |
} |
| 178 |
} |
| 179 |
return true; |
| 180 |
} |
| 181 |
return false; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|