KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > sort > TextComparer


1 package net.sf.saxon.sort;
2 import net.sf.saxon.om.Item;
3 import net.sf.saxon.trans.XPathException;
4
5 import java.util.Comparator JavaDoc;
6
7 /**
8  * A Comparer used for comparing sort keys when data-type="text". The items to be
9  * compared are converted to strings, and the strings are then compared using an
10  * underlying collator
11  *
12  * @author Michael H. Kay
13  *
14  */

15
16 public class TextComparer implements Comparator JavaDoc, java.io.Serializable JavaDoc {
17
18     private Comparator JavaDoc collator;
19
20     public TextComparer(Comparator JavaDoc collator) {
21         this.collator = collator;
22     }
23
24     /**
25     * Compare two Items by converting them to strings and comparing the string values.
26     * @param a the first Item to be compared.
27     * @param b the second Item to be compared.
28     * @return <0 if a<b, 0 if a=b, >0 if a>b
29     * @throws ClassCastException if the objects are not Items, or are items that cannot be convered
30     * to strings (e.g. QNames)
31     */

32
33     public int compare(Object JavaDoc a, Object JavaDoc b) throws ClassCastException JavaDoc {
34
35         String JavaDoc s1, s2;
36
37         //try {
38
s1 = (a instanceof String JavaDoc ? (String JavaDoc)a : ((Item)a).getStringValue());
39         //} catch (XPathException err) {
40
// throw new ClassCastException("Cannot convert sort key from " + a.getClass() + " to a string");
41
//}
42

43         //try {
44
s2 = (b instanceof String JavaDoc ? (String JavaDoc)b : ((Item)b).getStringValue());
45         //} catch (XPathException err) {
46
// throw new ClassCastException("Cannot convert sort key from " + b.getClass() + " to a string");
47
//}
48

49         int x = collator.compare(s1, s2);
50         //System.err.println(((RuleBasedCollator)collator).getStrength() + " comparing " + s1 + " with " + s2 + " => " + x);
51
return x;
52
53     }
54
55 }
56
57 //
58
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
59
// you may not use this file except in compliance with the License. You may obtain a copy of the
60
// License at http://www.mozilla.org/MPL/
61
//
62
// Software distributed under the License is distributed on an "AS IS" basis,
63
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
64
// See the License for the specific language governing rights and limitations under the License.
65
//
66
// The Original Code is: all this file.
67
//
68
// The Initial Developer of this module is Michael H. Kay
69
//
70
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
71
//
72
// Contributor(s): none.
73
//
Popular Tags