| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class RuleBasedSubstringMatcher extends SimpleCollation implements SubstringMatcher { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 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 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 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 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 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 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 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 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 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 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 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 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 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 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
private boolean collationContains(CollationElementIterator s0, |
| 174 |
CollationElementIterator s1, |
| 175 |
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 |
|
| 187 |
while (e0 != e1) { |
| 188 |
do { |
| 189 |
e0 = s0.next(); |
| 190 |
} while (e0 == 0); |
| 191 |
if (e0 == -1) { |
| 192 |
|
| 193 |
return false; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 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 |
|
| 205 |
return true; |
| 206 |
} |
| 207 |
|
| 208 |
} else { |
| 209 |
if (offsets != null) { |
| 210 |
offsets[0] = start - 1; |
| 211 |
offsets[1] = s0.getOffset(); |
| 212 |
} |
| 213 |
return true; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
s0.setOffset(start); |
| 218 |
|
| 219 |
|
| 220 |
if (s0.getOffset() != start) { |
| 221 |
|
| 222 |
s0.next(); |
| 223 |
} |
| 224 |
s1.reset(); |
| 225 |
e0 = -1; |
| 226 |
do { |
| 227 |
e1 = s1.next(); |
| 228 |
} while (e1 == 0); |
| 229 |
|
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public AtomicMatchKey getCollationKey(CharSequence s) { |
| 242 |
return new CollationMatchKey(getRuleBasedCollator().getCollationKey(s.toString())); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 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 |
|