Debian Codegraph @

net/sf/saxon/expr/sort/CaseFirstCollator.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.expr.sort;
9
10 import net.sf.saxon.lib.StringCollator;
11 import net.sf.saxon.trans.XPathException;
12
13
14 /**
15 * A StringCollator that sorts lowercase before uppercase, or vice versa.
16 * <p>Case is irrelevant, unless the strings are equal ignoring
17 * case, in which case lowercase comes first.</p>
18 */
19
20 public class CaseFirstCollator implements StringCollator {
21
22 private StringCollator baseCollator;
23 private boolean upperFirst;
24 private String uri;
25
26 /**
27 * Create a CaseFirstCollator
28 * @param base the base collator, which determines how characters are sorted irrespective of case
29 * @param upperFirst true if uppercase precedes lowercase, false otherwise
30 * @param collationURI the URI of the collation
31 */
32
33 public CaseFirstCollator(StringCollator base, boolean upperFirst, String collationURI) {
34 this.baseCollator = base;
35 this.upperFirst = upperFirst;
36 this.uri = collationURI;
37 }
38
39 public static StringCollator makeCaseOrderedCollator(String uri, StringCollator stringCollator, String caseOrder) throws XPathException {
40 switch (caseOrder) {
41 case "lower-first":
42 stringCollator = new CaseFirstCollator(stringCollator, false, uri);
43 break;
44 case "upper-first":
45 stringCollator = new CaseFirstCollator(stringCollator, true, uri);
46 break;
47 default:
48 throw new XPathException("case-order must be lower-first, upper-first, or #default");
49 }
50 return stringCollator;
51 }
52
53 /**
54 * Get the collation URI. It must be possible to use this collation URI to reconstitute the collation
55 *
56 * @return a collation URI that can be used to reconstruct the collation when an XSLT package is reloaded.
57 */
58 public String getCollationURI() {
59 return uri;
60 }
61
62 /**
63 * Compare two string objects: case is irrelevant, unless the strings are equal ignoring
64 * case, in which case lowercase comes first.
65 *
66 * @return &lt;0 if a&lt;b, 0 if a=b, &gt;0 if a&gt;b
67 * @throws ClassCastException if the objects are of the wrong type for this Comparer
68 */
69
70 public int compareStrings(CharSequence a, CharSequence b) {
71 int diff = baseCollator.compareStrings(a, b);
72 if (diff != 0) {
73 return diff;
74 }
75
76 // This is doing a character-by-character comparison, which isn't really right.
77 // There might be a sequence of letters constituting a single collation unit.
78
79 int i = 0;
80 int j = 0;
81 while (true) {
82 // Skip characters that are equal in the two strings
83 while (i < a.length() && j < b.length() && a.charAt(i) == b.charAt(j)) {
84 i++;
85 j++;
86 }
87 // Skip non-letters in the first string
88 while (i < a.length() && !Character.isLetter(a.charAt(i))) {
89 i++;
90 }
91 // Skip non-letters in the second string
92 while (j < b.length() && !Character.isLetter(b.charAt(j))) {
93 j++;
94 }
95 // If we've got to the end of either string, treat the strings as equal
96 if (i >= a.length()) {
97 return 0;
98 }
99 if (j >= b.length()) {
100 return 0;
101 }
102 // If one of the characters is upper/lower case and the other isn't, the issue is decided
103 boolean aFirst = upperFirst ? Character.isUpperCase(a.charAt(i++)) : Character.isLowerCase(a.charAt(i++));
104 boolean bFirst = upperFirst ? Character.isUpperCase(b.charAt(j++)) : Character.isLowerCase(b.charAt(j++));
105 if (aFirst && !bFirst) {
106 return -1;
107 }
108 if (bFirst && !aFirst) {
109 return +1;
110 }
111 }
112 }
113
114 /**
115 * Compare two strings for equality. This may be more efficient than using compareStrings and
116 * testing whether the result is zero, but it must give the same result
117 *
118 * @param s1 the first string
119 * @param s2 the second string
120 * @return true if and only if the strings are considered equal,
121 */
122
123 public boolean comparesEqual(CharSequence s1, /*@NotNull*/ CharSequence s2) {
124 return compareStrings(s1, s2) == 0;
125 }
126
127 /**
128 * Get a collation key for two Strings. The essential property of collation keys
129 * is that if two values are equal under the collation, then the collation keys are
130 * compare correctly under the equals() method.
131 * @param s
132 */
133
134 public AtomicMatchKey getCollationKey(CharSequence s) {
135 return baseCollator.getCollationKey(s);
136 }
137
138 }
139