01: package net.sf.saxon.sort;
02:
03: import net.sf.saxon.om.Item;
04: import net.sf.saxon.trans.XPathException;
05:
06: import java.util.Comparator;
07:
08: /**
09: * A Comparer used for comparing sort keys when data-type="text". The items to be
10: * compared are converted to strings, and the strings are then compared using an
11: * underlying collator
12: *
13: * @author Michael H. Kay
14: *
15: */
16:
17: public class TextComparer implements Comparator, java.io.Serializable {
18:
19: private Comparator collator;
20:
21: public TextComparer(Comparator collator) {
22: this .collator = collator;
23: }
24:
25: /**
26: * Compare two Items by converting them to strings and comparing the string values.
27: * @param a the first Item to be compared.
28: * @param b the second Item to be compared.
29: * @return <0 if a<b, 0 if a=b, >0 if a>b
30: * @throws ClassCastException if the objects are not Items, or are items that cannot be convered
31: * to strings (e.g. QNames)
32: */
33:
34: public int compare(Object a, Object b) throws ClassCastException {
35:
36: String s1, s2;
37:
38: //try {
39: s1 = (a instanceof String ? (String) a : ((Item) a)
40: .getStringValue());
41: //} catch (XPathException err) {
42: // throw new ClassCastException("Cannot convert sort key from " + a.getClass() + " to a string");
43: //}
44:
45: //try {
46: s2 = (b instanceof String ? (String) b : ((Item) b)
47: .getStringValue());
48: //} catch (XPathException err) {
49: // throw new ClassCastException("Cannot convert sort key from " + b.getClass() + " to a string");
50: //}
51:
52: int x = collator.compare(s1, s2);
53: //System.err.println(((RuleBasedCollator)collator).getStrength() + " comparing " + s1 + " with " + s2 + " => " + x);
54: return x;
55:
56: }
57:
58: }
59:
60: //
61: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
62: // you may not use this file except in compliance with the License. You may obtain a copy of the
63: // License at http://www.mozilla.org/MPL/
64: //
65: // Software distributed under the License is distributed on an "AS IS" basis,
66: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
67: // See the License for the specific language governing rights and limitations under the License.
68: //
69: // The Original Code is: all this file.
70: //
71: // The Initial Developer of this module is Michael H. Kay
72: //
73: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
74: //
75: // Contributor(s): none.
76: //
|