KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > sort > Compare_en


1 package com.icl.saxon.sort;
2
3 // Copyright © International Computers Limited 1998
4
// See conditions of use
5

6 /**
7  * A Comparer used with lang="en". Note this only does anything intelligent with characters
8  * in ISO 8859/1, which are mapped to their unaccented equivalents
9  *
10  * @author Michael H. Kay (mhkay@iclway.co.uk)
11  *
12  */

13
14 public class Compare_en extends TextComparer {
15
16     // Following string maps Latin-1 characters in the range C0-FF to equivalent unaccented letter
17

18     private static String JavaDoc supp =
19     "AAAAAAACEEEEIIII[NOOOOO*OUUUUY]Saaaaaaaceeeeiiii{nooooo*ouuuuy}y";
20
21     int caseOrder = UPPERCASE_FIRST;
22
23     /**
24     * Compare two string objects, in three phases:
25     * (a) ignoring accents and case
26     * (b) if still equal, ignoring case
27     * (c) if still equal, taking case into account
28     * @return <0 if a<b, 0 if a=b, >0 if a>b
29     */

30
31     public int compare(Object JavaDoc a, Object JavaDoc b) {
32
33         char[] a1 = ((String JavaDoc)a).toCharArray();
34         char[] b1 = ((String JavaDoc)b).toCharArray();
35         int alen = a1.length;
36         int blen = b1.length;
37
38         // strip off the accents
39
for (int k=0; k<alen; k++) {
40             int code = (int)a1[k];
41             if (code>=192 && code<=255) {
42                 a1[k] = supp.charAt(code-192);
43             }
44         }
45         for (int k=0; k<blen; k++) {
46             int code = (int)b1[k];
47             if (code>=192 && code<=255) {
48                 b1[k] = supp.charAt(code-192);
49             }
50         }
51         int i = 0;
52         int j = 0;
53         
54         // do an accent-blind comparison
55
while (true) {
56             if (i==alen && j==blen) break;
57             if (i==alen) return -1;
58             if (j==blen) return +1;
59             int diff = Character.toLowerCase(a1[i]) -
60                              Character.toLowerCase(b1[j]);
61             i++;
62             j++;
63             if (diff!=0) return diff;
64         }
65
66         // still equal - try again with accents, but still case-blind
67

68         a1 = ((String JavaDoc)a).toCharArray();
69         b1 = ((String JavaDoc)b).toCharArray();
70         i = 0;
71         j = 0;
72         while (true) {
73             if (i==alen && j==blen) break;
74             if (i==alen) return -1;
75             if (j==blen) return +1;
76             int diff = Character.toLowerCase(a1[i]) -
77                              Character.toLowerCase(b1[j]);
78             i++;
79             j++;
80             if (diff!=0) return diff;
81         }
82
83         // still equal: take case into account
84

85         i = 0;
86         j = 0;
87         while (true) {
88             if (i==alen) return 0;
89             int diff = a1[i++] - b1[j++];
90             if (diff!=0) {
91                 if (caseOrder==LOWERCASE_FIRST) {
92                     return (Character.isLowerCase(a1[i-1]) ? -1 : +1);
93                 } else {
94                     return (Character.isUpperCase(a1[i-1]) ? -1 : +1);
95                 }
96             }
97         }
98             
99     }
100
101     public Comparer setCaseOrder(int caseOrder) {
102         this.caseOrder = caseOrder;
103         return this;
104     }
105         
106 }
107
Popular Tags