Debian Codegraph @

net/sf/saxon/expr/sort/RuleBasedSubstringMatcher.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.SubstringMatcher;
11 import net.sf.saxon.tree.util.FastStringBuffer;
12
13 import java.text.CollationElementIterator;
14 import java.text.RuleBasedCollator;
15
16 /**
17 * This class wraps a RuleBasedCollator to provide a SubstringMatcher. This
18 * users the facilities offered by the RuleBasedCollator to implement the XPath
19 * functions contains(), starts-with(), ends-with(), substring-before(), and
20 * substring-after().
21 */
22 public class RuleBasedSubstringMatcher extends SimpleCollation implements SubstringMatcher {
23
24 /**
25 * Create a RuleBasedSubstringMatcher
26 * @param uri
27 * @param collator the collation to be used
28 */
29
30 public RuleBasedSubstringMatcher(String uri, RuleBasedCollator collator) {
31 super(uri, collator);
32 }
33
34 private RuleBasedCollator getRuleBasedCollator() {
35 return (RuleBasedCollator) getComparator();
36 }
37
38 /**
39 * Test whether one string contains another, according to the rules
40 * of the XPath contains() function
41 *
42 * @param s1 the containing string
43 * @param s2 the contained string
44 * @return true iff s1 contains s2
45 */
46
47 public boolean contains(String s1, String s2) {
48 RuleBasedCollator collator = getRuleBasedCollator();
49 CollationElementIterator iter1 = collator.getCollationElementIterator(s1);
50 CollationElementIterator iter2 = collator.getCollationElementIterator(s2);
51 return collationContains(iter1, iter2, null, false);
52 }
53
54 /**
55 * Test whether one string ends with another, according to the rules
56 * of the XPath ends-with() function
57 *
58 * @param s1 the containing string
59 * @param s2 the contained string
60 * @return true iff s1 ends with s2
61 */
62
63 public boolean endsWith(String s1, String s2) {
64 RuleBasedCollator collator = getRuleBasedCollator();
65 CollationElementIterator iter1 = collator.getCollationElementIterator(s1);
66 CollationElementIterator iter2 = collator.getCollationElementIterator(s2);
67 return collationContains(iter1, iter2, null, true);
68 }
69
70 /**
71 * Test whether one string starts with another, according to the rules
72 * of the XPath starts-with() function
73 *
74 * @param s1 the containing string
75 * @param s2 the contained string
76 * @return true iff s1 starts with s2
77 */
78
79 public boolean startsWith(String s1, String s2) {
80 RuleBasedCollator collator = getRuleBasedCollator();
81 CollationElementIterator iter1 = collator.getCollationElementIterator(s1);
82 CollationElementIterator iter2 = collator.getCollationElementIterator(s2);
83 return collationStartsWith(iter1, iter2);
84 }
85
86 /**
87 * Return the part of a string after a given substring, according to the rules
88 * of the XPath substring-after() function
89 *
90 * @param s1 the containing string
91 * @param s2 the contained string
92 * @return the part of s1 that follows the first occurrence of s2
93 */
94
95 public String substringAfter(String s1, String s2) {
96 RuleBasedCollator collator = getRuleBasedCollator();
97 CollationElementIterator iter1 = collator.getCollationElementIterator(s1);
98 CollationElementIterator iter2 = collator.getCollationElementIterator(s2);
99 int[] ia = new int[2];
100 boolean ba = collationContains(iter1, iter2, ia, false);
101 if (ba) {
102 return s1.substring(ia[1]);
103 } else {
104 return "";
105 }
106 }
107
108 /**
109 * Return the part of a string before a given substring, according to the rules
110 * of the XPath substring-before() function
111 *
112 * @param s1 the containing string
113 * @param s2 the contained string
114 * @return the part of s1 that precedes the first occurrence of s2
115 */
116
117 public String substringBefore(String s1, String s2) {
118 RuleBasedCollator collator = getRuleBasedCollator();
119 CollationElementIterator iter1 = collator.getCollationElementIterator(s1);
120 CollationElementIterator iter2 = collator.getCollationElementIterator(s2);
121 int[] ib = new int[2];
122 boolean bb = collationContains(iter1, iter2, ib, false);
123 if (bb) {
124 return s1.substring(0, ib[0]);
125 } else {
126 return "";
127 }
128 }
129
130 /**
131 * Determine whether one string starts with another, under the terms of a given
132 * collating sequence.
133 *
134 * @param s0 iterator over the collation elements of the containing string
135 * @param s1 iterator over the collation elements of the contained string
136 * @return true if the first string starts with the second
137 */
138
139 private boolean collationStartsWith(CollationElementIterator s0,
140 CollationElementIterator s1) {
141 while (true) {
142 int e0, e1;
143 do {
144 e1 = s1.next();
145 } while (e1 == 0);
146 if (e1 == -1) {
147 return true;
148 }
149 do {
150 e0 = s0.next();
151 } while (e0 == 0);
152 if (e0 != e1) {
153 return false;
154 }
155 }
156 }
157
158 /**
159 * Determine whether one string contains another, under the terms of a given
160 * collating sequence. If matchAtEnd=true, the match must be at the end of the first
161 * string.
162 *
163 * @param s0 iterator over the collation elements of the containing string
164 * @param s1 iterator over the collation elements of the contained string
165 * @param offsets may be null, but if it is supplied, it must be an array of two
166 * integers which, if the function returns true, will contain the start position of the
167 * first matching substring, and the offset of the first character after the first
168 * matching substring. This is not available for matchAtEnd=true
169 * @param matchAtEnd true if the match is required to be at the end of the string
170 * @return true if the first string contains the second
171 */
172
173 private boolean collationContains(CollationElementIterator s0,
174 CollationElementIterator s1,
175 /*@Nullable*/ int[] offsets,
176 boolean matchAtEnd) {
177 int e0, e1;
178 do {
179 e1 = s1.next();
180 } while (e1 == 0);
181 if (e1 == -1) {
182 return true;
183 }
184 e0 = -1;
185 while (true) {
186 // scan the first string to find a matching character
187 while (e0 != e1) {
188 do {
189 e0 = s0.next();
190 } while (e0 == 0);
191 if (e0 == -1) {
192 // hit the end, no match
193 return false;
194 }
195 }
196 // matched first character, note the position of the possible match
197 int start = s0.getOffset();
198 if (collationStartsWith(s0, s1)) {
199 if (matchAtEnd) {
200 do {
201 e0 = s0.next();
202 } while (e0 == 0);
203 if (e0 == -1) {
204 // the match is at the end
205 return true;
206 }
207 // else ignore this match and keep looking
208 } else {
209 if (offsets != null) {
210 offsets[0] = start - 1;
211 offsets[1] = s0.getOffset();
212 }
213 return true;
214 }
215 }
216 // reset the position and try again
217 s0.setOffset(start);
218
219 // workaround for a difference between JDK 1.4.0 and JDK 1.4.1
220 if (s0.getOffset() != start) {
221 // JDK 1.4.0 takes this path
222 s0.next();
223 }
224 s1.reset();
225 e0 = -1;
226 do {
227 e1 = s1.next();
228 } while (e1 == 0);
229 // loop round to try again
230 }
231 }
232
233
234 /**
235 * Get a collation key for two Strings. The essential property of collation keys
236 * is that if two values are equal under the collation, then the collation keys are
237 * compare correctly under the equals() method.
238 * @param s
239 */
240
241 public AtomicMatchKey getCollationKey(CharSequence s) {
242 return new CollationMatchKey(getRuleBasedCollator().getCollationKey(s.toString()));
243 }
244
245
246 /**
247 * Test program to output the sequence of collation element iterators for a given input string
248 *
249 * @param args command line arguments (collationURI, test-string)
250 */
251 public static void main(String[] args) throws Exception {
252 String rules = " ='-'='*'< a < b < c < d < e < f < g < h < i < j < k < l < m < n < o < p < q < r < s < t < u < v < w < x < y < z";
253 RuleBasedCollator collator = new RuleBasedCollator(rules);
254
255 for (int i = 0; i < args.length; i++) {
256 System.err.println(args[i]);
257 FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.C256);
258 CollationElementIterator iter = collator.getCollationElementIterator(args[i]);
259 while (true) {
260 int e = iter.next();
261 if (e == -1) {
262 break;
263 }
264 sb.append(e + " ");
265 }
266 System.err.println(sb.toString());
267 }
268
269
270 }
271
272
273 }
274