| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.sort; |
| 9 |
|
| 10 |
import net.sf.saxon.lib.StringCollator; |
| 11 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 12 |
|
| 13 |
import java.math.BigInteger; |
| 14 |
import java.util.regex.Matcher; |
| 15 |
import java.util.regex.Pattern; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class AlphanumericCollator implements StringCollator, java.io.Serializable { |
| 27 |
|
| 28 |
private StringCollator baseCollator; |
| 29 |
private static Pattern pattern = Pattern.compile("\\d+"); |
| 30 |
public final static String PREFIX = "http://saxon.sf.net/collation/alphaNumeric?base="; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public AlphanumericCollator(StringCollator base) { |
| 39 |
baseCollator = base; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public String getCollationURI() { |
| 48 |
return PREFIX + baseCollator.getCollationURI(); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public int compareStrings(CharSequence cs1, CharSequence cs2) { |
| 58 |
String s1 = cs1.toString(); |
| 59 |
String s2 = cs2.toString(); |
| 60 |
int pos1 = 0; |
| 61 |
int pos2 = 0; |
| 62 |
Matcher m1 = pattern.matcher(s1); |
| 63 |
Matcher m2 = pattern.matcher(s2); |
| 64 |
while (true) { |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
boolean b1 = m1.find(pos1); |
| 69 |
boolean b2 = m2.find(pos2); |
| 70 |
int m1start = b1 ? m1.start() : s1.length(); |
| 71 |
int m2start = b2 ? m2.start() : s2.length(); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
int c = baseCollator.compareStrings(s1.substring(pos1, m1start), s2.substring(pos2, m2start)); |
| 76 |
if (c != 0) { |
| 77 |
return c; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
if (b1 && !b2) { |
| 83 |
return +1; |
| 84 |
} else if (b2 && !b1) { |
| 85 |
return -1; |
| 86 |
} else if (!b1) { |
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
BigInteger n1 = new BigInteger(s1.substring(m1start, m1.end())); |
| 93 |
BigInteger n2 = new BigInteger(s2.substring(m2start, m2.end())); |
| 94 |
c = n1.compareTo(n2); |
| 95 |
if (c != 0) { |
| 96 |
return c; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
pos1 = m1.end(); |
| 102 |
pos2 = m2.end(); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public boolean comparesEqual(CharSequence s1, CharSequence s2) { |
| 116 |
return compareStrings(s1, s2) == 0; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public AtomicMatchKey getCollationKey( CharSequence cs) { |
| 127 |
|
| 128 |
String s = cs.toString(); |
| 129 |
FastStringBuffer sb = new FastStringBuffer(s.length() * 2); |
| 130 |
int pos1 = 0; |
| 131 |
Matcher m1 = pattern.matcher(s); |
| 132 |
while (true) { |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
boolean b1 = m1.find(pos1); |
| 137 |
int m1start = b1 ? m1.start() : s.length(); |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
sb.append(baseCollator.getCollationKey(s.substring(pos1, m1start)).toString()); |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
if (!b1) { |
| 146 |
return new CodepointMatchKey(sb); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
int n1 = Integer.parseInt(s.substring(m1start, m1.end())); |
| 152 |
sb.append(n1 + ""); |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
pos1 = m1.end(); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
|