| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.java; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.sort.AlphanumericCollator; |
| 12 |
import net.sf.saxon.expr.sort.CaseFirstCollator; |
| 13 |
import net.sf.saxon.expr.sort.CodepointCollator; |
| 14 |
import net.sf.saxon.expr.sort.SimpleCollation; |
| 15 |
import net.sf.saxon.lib.StringCollator; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
|
| 18 |
import java.text.Collator; |
| 19 |
import java.text.ParseException; |
| 20 |
import java.text.RuleBasedCollator; |
| 21 |
import java.util.Comparator; |
| 22 |
import java.util.Locale; |
| 23 |
import java.util.Properties; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public abstract class JavaCollationFactory { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
private JavaCollationFactory() { |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public static StringCollator makeCollation(Configuration config, String uri, Properties props) |
| 51 |
throws XPathException { |
| 52 |
|
| 53 |
Collator collator = null; |
| 54 |
StringCollator stringCollator = null; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
String classAtt = props.getProperty("class"); |
| 59 |
if (classAtt != null) { |
| 60 |
Object comparator = config.getInstance(classAtt, null); |
| 61 |
if (comparator instanceof Collator) { |
| 62 |
collator = (Collator) comparator; |
| 63 |
} else if (comparator instanceof StringCollator) { |
| 64 |
stringCollator = (StringCollator) comparator; |
| 65 |
} else if (comparator instanceof Comparator) { |
| 66 |
stringCollator = new SimpleCollation(uri, (Comparator) comparator); |
| 67 |
} else { |
| 68 |
throw new XPathException("Requested collation class " + classAtt + " is not a Comparator"); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
if (collator == null && stringCollator == null) { |
| 75 |
String rulesAtt = props.getProperty("rules"); |
| 76 |
if (rulesAtt != null) { |
| 77 |
try { |
| 78 |
collator = new RuleBasedCollator(rulesAtt); |
| 79 |
} catch (ParseException e) { |
| 80 |
throw new XPathException("Invalid collation rules: " + e.getMessage()); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
if (collator == null) { |
| 87 |
String langAtt = props.getProperty("lang"); |
| 88 |
if (langAtt != null) { |
| 89 |
collator = Collator.getInstance(getLocale(langAtt)); |
| 90 |
} else { |
| 91 |
collator = Collator.getInstance(); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if (collator != null) { |
| 97 |
|
| 98 |
String strengthAtt = props.getProperty("strength"); |
| 99 |
if (strengthAtt != null) { |
| 100 |
switch (strengthAtt) { |
| 101 |
case "primary": |
| 102 |
collator.setStrength(Collator.PRIMARY); |
| 103 |
break; |
| 104 |
case "secondary": |
| 105 |
collator.setStrength(Collator.SECONDARY); |
| 106 |
break; |
| 107 |
case "tertiary": |
| 108 |
collator.setStrength(Collator.TERTIARY); |
| 109 |
break; |
| 110 |
case "identical": |
| 111 |
collator.setStrength(Collator.IDENTICAL); |
| 112 |
break; |
| 113 |
default: |
| 114 |
throw new XPathException("strength must be primary, secondary, tertiary, or identical"); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
String ignore = props.getProperty("ignore-width"); |
| 121 |
if (ignore != null) { |
| 122 |
if (ignore.equals("yes") && strengthAtt == null) { |
| 123 |
collator.setStrength(Collator.TERTIARY); |
| 124 |
} else if (ignore.equals("no")) { |
| 125 |
|
| 126 |
} else { |
| 127 |
throw new XPathException("ignore-width must be yes or no"); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
ignore = props.getProperty("ignore-case"); |
| 132 |
if (ignore != null && strengthAtt == null) { |
| 133 |
switch (ignore) { |
| 134 |
case "yes": |
| 135 |
collator.setStrength(Collator.SECONDARY); |
| 136 |
break; |
| 137 |
case "no": |
| 138 |
|
| 139 |
break; |
| 140 |
default: |
| 141 |
throw new XPathException("ignore-case must be yes or no"); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
ignore = props.getProperty("ignore-modifiers"); |
| 146 |
if (ignore != null) { |
| 147 |
if (ignore.equals("yes") && strengthAtt == null) { |
| 148 |
collator.setStrength(Collator.PRIMARY); |
| 149 |
} else if (ignore.equals("no")) { |
| 150 |
|
| 151 |
} else { |
| 152 |
throw new XPathException("ignore-modifiers must be yes or no"); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
String decompositionAtt = props.getProperty("decomposition"); |
| 160 |
if (decompositionAtt != null) { |
| 161 |
switch (decompositionAtt) { |
| 162 |
case "none": |
| 163 |
collator.setDecomposition(Collator.NO_DECOMPOSITION); |
| 164 |
break; |
| 165 |
case "standard": |
| 166 |
collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION); |
| 167 |
break; |
| 168 |
case "full": |
| 169 |
collator.setDecomposition(Collator.FULL_DECOMPOSITION); |
| 170 |
break; |
| 171 |
default: |
| 172 |
throw new XPathException("decomposition must be none, standard, or full"); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
if (stringCollator == null) { |
| 178 |
stringCollator = new SimpleCollation(uri, collator); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
String caseOrder = props.getProperty("case-order"); |
| 183 |
if (caseOrder != null && !"#default".equals(caseOrder)) { |
| 184 |
|
| 185 |
if (collator != null) { |
| 186 |
collator.setStrength(Collator.SECONDARY); |
| 187 |
} |
| 188 |
stringCollator = CaseFirstCollator.makeCaseOrderedCollator(uri, stringCollator, caseOrder); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
String alphanumeric = props.getProperty("alphanumeric"); |
| 193 |
if (alphanumeric != null && !"no".equals(alphanumeric)) { |
| 194 |
switch (alphanumeric) { |
| 195 |
case "yes": |
| 196 |
stringCollator = new AlphanumericCollator(stringCollator); |
| 197 |
break; |
| 198 |
case "codepoint": |
| 199 |
stringCollator = new AlphanumericCollator(CodepointCollator.getInstance()); |
| 200 |
break; |
| 201 |
default: |
| 202 |
throw new XPathException("alphanumeric must be yes, no, or codepoint"); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
return stringCollator; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
private static Locale getLocale(String lang) { |
| 214 |
int hyphen = lang.indexOf("-"); |
| 215 |
String language, country; |
| 216 |
if (hyphen < 1) { |
| 217 |
language = lang; |
| 218 |
country = ""; |
| 219 |
} else { |
| 220 |
language = lang.substring(0, hyphen); |
| 221 |
country = lang.substring(hyphen + 1); |
| 222 |
} |
| 223 |
return new Locale(language, country); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
} |
| 228 |
|