1 package gnu.xquery.util; 2 import java.io.*; 3 4 public class NamedCollator 5 6 extends java.text.Collator 7 8 implements Externalizable 9 { 10 11 java.text.Collator collator; 12 13 14 String name; 15 16 public static final String UNICODE_CODEPOINT_COLLATION 17 = "http://www.w3.org/2005/xpath-functions/collation/codepoint"; 18 19 public static NamedCollator make (String name) 20 { 21 NamedCollator coll = new NamedCollator(); 22 coll.name = name; 23 coll.resolve(); 24 return coll; 25 } 26 27 public String getName () 28 { 29 return name; 30 } 31 32 public static NamedCollator find (String name) 33 { 34 return make(name); 35 } 36 37 public static final NamedCollator codepointCollation = new NamedCollator(); 38 static { codepointCollation.name = UNICODE_CODEPOINT_COLLATION; } 39 40 public void resolve () 41 { 42 if (name != null && ! name.equals(UNICODE_CODEPOINT_COLLATION)) 43 { 44 throw new RuntimeException ("unknown collation: "+name); 46 } 47 } 48 49 public int compare (String str1, String str2) 50 { 51 52 if (collator != null) 53 return collator.compare(str1, str2); 54 55 return str1.compareTo(str2); 56 } 57 58 59 public java.text.CollationKey getCollationKey (String source) 60 { 61 return collator.getCollationKey(source); 62 } 63 64 65 public int hashCode () 66 { 67 68 if (collator != null) 69 return collator.hashCode(); 70 71 return 0; 72 } 73 74 public void writeExternal(ObjectOutput out) throws IOException 75 { 76 out.writeUTF(name); 77 } 78 79 public void readExternal(ObjectInput in) 80 throws IOException, ClassNotFoundException 81 { 82 name = in.readUTF(); 83 resolve(); 84 } 85 } 86 | Popular Tags |