| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.sort; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.XPathContext; |
| 11 |
import net.sf.saxon.lib.StringCollator; |
| 12 |
import net.sf.saxon.om.StandardNames; |
| 13 |
import net.sf.saxon.trans.NoDynamicContextException; |
| 14 |
import net.sf.saxon.trans.XPathException; |
| 15 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 16 |
import net.sf.saxon.type.Type; |
| 17 |
import net.sf.saxon.value.AtomicValue; |
| 18 |
import net.sf.saxon.value.CalendarValue; |
| 19 |
import net.sf.saxon.value.StringValue; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public class GenericAtomicComparer implements AtomicComparer { |
| 31 |
|
| 32 |
private StringCollator collator; |
| 33 |
private transient XPathContext context; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public GenericAtomicComparer( StringCollator collator, XPathContext conversionContext) { |
| 43 |
this.collator = collator; |
| 44 |
if (collator == null) { |
| 45 |
this.collator = CodepointCollator.getInstance(); |
| 46 |
} |
| 47 |
context = conversionContext; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public static AtomicComparer makeAtomicComparer( |
| 62 |
BuiltInAtomicType type0, BuiltInAtomicType type1, StringCollator collator, XPathContext context) { |
| 63 |
int fp0 = type0.getFingerprint(); |
| 64 |
int fp1 = type1.getFingerprint(); |
| 65 |
if (fp0 == fp1) { |
| 66 |
switch (fp0) { |
| 67 |
case StandardNames.XS_DATE_TIME: |
| 68 |
case StandardNames.XS_DATE: |
| 69 |
case StandardNames.XS_TIME: |
| 70 |
case StandardNames.XS_G_DAY: |
| 71 |
case StandardNames.XS_G_MONTH: |
| 72 |
case StandardNames.XS_G_YEAR: |
| 73 |
case StandardNames.XS_G_MONTH_DAY: |
| 74 |
case StandardNames.XS_G_YEAR_MONTH: |
| 75 |
return new CalendarValueComparer(context); |
| 76 |
|
| 77 |
case StandardNames.XS_BOOLEAN: |
| 78 |
case StandardNames.XS_DAY_TIME_DURATION: |
| 79 |
case StandardNames.XS_YEAR_MONTH_DURATION: |
| 80 |
return ComparableAtomicValueComparer.getInstance(); |
| 81 |
|
| 82 |
case StandardNames.XS_BASE64_BINARY: |
| 83 |
case StandardNames.XS_HEX_BINARY: |
| 84 |
|
| 85 |
return ComparableAtomicValueComparer.getInstance(); |
| 86 |
|
| 87 |
case StandardNames.XS_QNAME: |
| 88 |
case StandardNames.XS_NOTATION: |
| 89 |
return EqualityComparer.getInstance(); |
| 90 |
|
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (type0.isPrimitiveNumeric() && type1.isPrimitiveNumeric()) { |
| 95 |
return ComparableAtomicValueComparer.getInstance(); |
| 96 |
} |
| 97 |
|
| 98 |
if ((fp0 == StandardNames.XS_STRING || |
| 99 |
fp0 == StandardNames.XS_UNTYPED_ATOMIC || |
| 100 |
fp0 == StandardNames.XS_ANY_URI) && |
| 101 |
(fp1 == StandardNames.XS_STRING || |
| 102 |
fp1 == StandardNames.XS_UNTYPED_ATOMIC || |
| 103 |
fp1 == StandardNames.XS_ANY_URI)) { |
| 104 |
if (collator instanceof CodepointCollator) { |
| 105 |
return CodepointCollatingComparer.getInstance(); |
| 106 |
} else { |
| 107 |
return new CollatingAtomicComparer(collator); |
| 108 |
} |
| 109 |
} |
| 110 |
return new GenericAtomicComparer(collator, context); |
| 111 |
} |
| 112 |
|
| 113 |
public StringCollator getCollator() { |
| 114 |
return collator; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public GenericAtomicComparer provideContext(XPathContext context) { |
| 126 |
return new GenericAtomicComparer(collator, context); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public StringCollator getStringCollator() { |
| 136 |
return collator; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public int compareAtomicValues(AtomicValue a, AtomicValue b) throws NoDynamicContextException { |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
if (a == null) { |
| 163 |
return b == null ? 0 : -1; |
| 164 |
} else if (b == null) { |
| 165 |
return +1; |
| 166 |
} |
| 167 |
|
| 168 |
if (a instanceof StringValue && b instanceof StringValue) { |
| 169 |
if (collator instanceof CodepointCollator) { |
| 170 |
return CodepointCollator.compareCS(a.getStringValueCS(), b.getStringValueCS()); |
| 171 |
} else { |
| 172 |
return collator.compareStrings(a.getStringValue(), b.getStringValue()); |
| 173 |
} |
| 174 |
} else { |
| 175 |
int implicitTimezone = context.getImplicitTimezone(); |
| 176 |
Comparable ac = (Comparable) a.getXPathComparable(true, collator, implicitTimezone); |
| 177 |
Comparable bc = (Comparable) b.getXPathComparable(true, collator, implicitTimezone); |
| 178 |
if (ac == null || bc == null) { |
| 179 |
XPathException e = new XPathException("Objects are not comparable (" + |
| 180 |
Type.displayTypeName(a) + ", " + Type.displayTypeName(b) + ')', "XPTY0004"); |
| 181 |
throw new ComparisonException(e); |
| 182 |
} else { |
| 183 |
return ac.compareTo(bc); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public boolean comparesEqual(AtomicValue a, AtomicValue b) throws NoDynamicContextException { |
| 202 |
|
| 203 |
if (a instanceof StringValue && b instanceof StringValue) { |
| 204 |
return collator.comparesEqual(a.getStringValue(), b.getStringValue()); |
| 205 |
} else if (a instanceof CalendarValue && b instanceof CalendarValue) { |
| 206 |
return ((CalendarValue) a).compareTo((CalendarValue) b, context.getImplicitTimezone()) == 0; |
| 207 |
} else { |
| 208 |
int implicitTimezone = context.getImplicitTimezone(); |
| 209 |
Object ac = a.getXPathComparable(false, collator, implicitTimezone); |
| 210 |
Object bc = b.getXPathComparable(false, collator, implicitTimezone); |
| 211 |
return ac.equals(bc); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
public XPathContext getContext() { |
| 216 |
return context; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public String save() { |
| 226 |
return "GAC|" + collator.getCollationURI(); |
| 227 |
} |
| 228 |
|
| 229 |
@Override |
| 230 |
public int hashCode() { |
| 231 |
return collator.hashCode(); |
| 232 |
} |
| 233 |
|
| 234 |
@Override |
| 235 |
public boolean equals(Object obj) { |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
return obj instanceof GenericAtomicComparer && collator.equals(((GenericAtomicComparer)obj).collator); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|