1 package net.sf.saxon.sort; 2 import net.sf.saxon.Configuration; 3 import net.sf.saxon.trans.XPathException; 4 5 import java.net.URI ; 6 import java.net.URISyntaxException ; 7 import java.text.Collator ; 8 import java.util.Comparator ; 9 import java.util.Locale ; 10 import java.util.StringTokenizer ; 11 12 16 17 public class StandardCollationURIResolver implements CollationURIResolver { 18 19 private static final StandardCollationURIResolver theInstance = new StandardCollationURIResolver(); 20 21 24 private StandardCollationURIResolver() { 25 } 26 27 30 31 public static final StandardCollationURIResolver getInstance() { 32 return theInstance; 33 } 34 35 43 44 private static Collator makeUsingProperties( 45 String langAtt, 46 String strengthAtt, 47 String decompositionAtt ) 48 { 49 50 Collator collator; 51 53 if (langAtt!=null) { 54 collator = Collator.getInstance(getLocale(langAtt)); 55 } else { 56 collator = Collator.getInstance(); } 58 59 if (strengthAtt!=null && collator instanceof Collator ) { 60 if (strengthAtt.equals("primary")) { 61 collator.setStrength(Collator.PRIMARY); 62 } else if (strengthAtt.equals("secondary")) { 63 collator.setStrength(Collator.SECONDARY); 64 } else if (strengthAtt.equals("tertiary")) { 65 collator.setStrength(Collator.TERTIARY); 66 } else if (strengthAtt.equals("identical")) { 67 collator.setStrength(Collator.IDENTICAL); 68 } else { 69 } 71 } 72 73 if (decompositionAtt!=null && collator instanceof Collator ) { 74 if (decompositionAtt.equals("none")) { 75 collator.setDecomposition(Collator.NO_DECOMPOSITION); 76 } else if (decompositionAtt.equals("standard")) { 77 collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION); 78 } else if (decompositionAtt.equals("full")) { 79 collator.setDecomposition(Collator.FULL_DECOMPOSITION); 80 } else { 81 } 83 } 84 return collator; 85 } 86 87 90 91 private static Locale getLocale(String lang) { 92 int hyphen = lang.indexOf("-"); 93 String language, country; 94 if (hyphen < 1) { 95 language = lang; 96 country = ""; 97 } else { 98 language = lang.substring(1, hyphen); 99 country = lang.substring(hyphen+1); 100 } 101 return new Locale (language, country); 102 } 103 104 108 109 public Comparator resolve(String uri, String base, Configuration config) { 110 if (uri.equals("http://saxon.sf.net/collation")) { 111 return makeUsingProperties(null, null, null); 112 } else if (uri.startsWith("http://saxon.sf.net/collation?")) { 113 URI uuri; 114 try { 115 uuri = new URI (uri); 116 } catch (URISyntaxException err) { 117 return null; 118 } 119 String query = uuri.getQuery(); 120 String lang = null; 121 String strength = null; 122 String decomposition = null; 123 String classname = null; 124 StringTokenizer queryTokenizer = new StringTokenizer (query, ";&"); 125 while (queryTokenizer.hasMoreElements()) { 126 String param = queryTokenizer.nextToken(); 127 int eq = param.indexOf('='); 128 if (eq > 0 && eq < param.length()-1) { 129 String kw = param.substring(0, eq); 130 String val = param.substring(eq + 1); 131 if (kw.equals("lang")) { 132 lang = val; 133 } else if (kw.equals("strength")) { 134 strength = val; 135 } else if (kw.equals("decomposition")) { 136 decomposition = val; 137 } else if (kw.equals("class")) { 138 classname = val; 139 } 140 } 141 } 142 if (classname != null) { 143 try { 144 return config.makeCollator(classname); 145 } catch (XPathException err) { 146 return null; 147 } 148 } 149 return makeUsingProperties(lang, strength, decomposition); 150 } else { 151 return null; 152 } 153 } 154 155 156 } 157 158 | Popular Tags |