Debian Codegraph @

net/sf/saxon/style/PackageVersionRanges.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.trans.XPathException;
11 import net.sf.saxon.tree.util.FastStringBuffer;
12
13 import java.util.ArrayList;
14
15 /**
16 * A class to handle a set of package version ranges
17 * <p>This implements the semantics given in
18 * <a href="http://www.w3.org/TR/xslt-30/#package-versions">http://www.w3.org/TR/xslt-30/#package-versions</a>,
19 * where a comma-separated sequence of package-version ranges are declared as one of:</p>
20 * <ul>
21 * <li>'*' - any version</li>
22 * <li><code>package-version</code> - an exact match</li>
23 * <li><code>package-version'.*'</code> - any package whose version starts with the given prefix</li>
24 * <li><code>package-version'+'</code> - any package whose version orders equal or later than the given version</li>
25 * <li><code>'to 'package-version</code> - any package whose version orders equal or earlier than the given version</li>
26 * <li><code>package-version' to 'package-version</code> - any package whose version orders equal to or between the given versions</li>
27 * </ul>
28 */
29 public class PackageVersionRanges {
30
31 ArrayList<PackageVersionRange> ranges;
32
33 private class PackageVersionRange {
34 String display;
35 PackageVersion low;
36 PackageVersion high;
37 boolean all = false;
38 boolean prefix = false;
39
40 PackageVersionRange(String s) throws XPathException {
41 display = s;
42 if ("*".equals(s)) {
43 all = true;
44 } else if (s.endsWith("+")) {
45 low = new PackageVersion(s.replace("+", ""));
46 high = PackageVersion.MAX_VALUE;
47 } else if (s.endsWith(".*")) {
48 prefix = true;
49 low = new PackageVersion(s.replace(".*", ""));
50 } else if (s.matches(".*\\s*to\\s+.*")) {
51 String range[] = s.split("\\s*to\\s+");
52 if (range.length > 2) {
53 throw new XPathException("Invalid version range:" + s, "XTSE0020");
54 }
55 low = range[0].equals("") ? PackageVersion.ZERO : new PackageVersion(range[0]);
56 high = new PackageVersion(range[1]);
57 } else {
58 low = new PackageVersion(s);
59 high = low;
60 }
61 }
62
63 boolean contains(PackageVersion v) {
64 if (all) {
65 return true;
66 } else if (prefix) {
67 return low.isPrefix(v);
68 } else {
69 return low.compareTo(v) <= 0 && v.compareTo(high) <= 0;
70 }
71 }
72
73 @Override
74 public String toString() {
75 return display;
76 }
77 }
78
79 /**
80 * Generate a set of package version ranges
81 *
82 * @param s Input string describing the ranges in the grammar described in
83 * <a href="http://www.w3.org/TR/xslt-30/#package-versions">http://www.w3.org/TR/xslt-30/#package-versions</a>
84 * @throws XPathException
85 */
86 public PackageVersionRanges(String s) throws XPathException {
87 ranges = new ArrayList<PackageVersionRange>();
88 for (String p : s.trim().split("\\s*,\\s*")) {
89 ranges.add(new PackageVersionRange(p));
90 }
91 }
92
93 /**
94 * Test whether a given package version lies within any of the ranges described in this PackageVersionRanges
95 * @param version The version to be checked
96 * @return true if the version is contained in any of the ranges, false otherwise
97 */
98 public boolean contains(PackageVersion version) {
99 for (PackageVersionRange r : ranges) {
100 if (r.contains(version)) {
101 return true;
102 }
103 }
104 return false;
105 }
106
107 @Override
108 public String toString() {
109 if (ranges.size() == 1) {
110 return ranges.get(0).display;
111 } else {
112 FastStringBuffer buffer = new FastStringBuffer(256);
113 for (PackageVersionRange r : ranges) {
114 buffer.append(r.display);
115 buffer.append(",");
116 }
117 buffer.setLength(buffer.length()-1);
118 return buffer.toString();
119 }
120 }
121 }
122