Debian Codegraph @

net/sf/saxon/style/PackageVersion.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * A class to represent an XSLT package version such as 1.12.5 or 3.0-alpha
18 * <p>This implements the semantics given in
19 * <a href="http://www.w3.org/TR/xslt-30/#package-versions">http://www.w3.org/TR/xslt-30/#package-versions</a>,
20 * where a series of dot-separated integers may be followed optionally by '-'NCName</p>
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 * Return a package version defined by a fixed sequence of int values, which implies no suffix
36 *
37 * @param values the sequence of integer components
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 * Remove any trailing zero components from the package version
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 * Generate a package version from a string description
62 *
63 * @param s The input string describing the package version according to the grammar given at:
64 * <a href="http://www.w3.org/TR/xslt-30/#package-versions">http://www.w3.org/TR/xslt-30/#package-versions</a>
65 * @throws XPathException
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 * Compare two version numbers for equality, ignoring the suffix part
114 * of the version number. For example 2.1-alpha compares equal
115 * to 2.1-beta
116 *
117 * @param other the other name/version pair
118 * @return true if the values are equal in all respects other than the alphanumeric
119 * suffix of the version number.
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 * Tests whether this package version is a prefix (i.e. shares all its components in order)
167 * of another package version, and thus this version.* should match it.
168 *
169 * @param v The version to be checked that it is equal, or 'extends' this version
170 * @return true if this is a prefix, false if not.
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 // Copyright (c) 2018 Saxonica Limited.