Debian Codegraph @

net/sf/saxon/java/JavaCollationFactory.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * A JavaCollationFactory allows a Collation to be created given
27 * a set of properties that the collation should have. This class creates a collator using
28 * the facilities of the Java platform; there is a corresponding class that uses the .NET
29 * platform.
30 */
31
32 public abstract class JavaCollationFactory {
33
34 /**
35 * The class is a never instantiated
36 */
37 private JavaCollationFactory() {
38 }
39
40 /**
41 * Make a collator with given properties
42 *
43 * @param config the Configuration
44 * @param uri the Collation URI
45 * @param props the desired properties of the collation
46 * @return a collation with these properties
47 */
48
49 /*@Nullable*/
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 // If a specific collation class is requested, this overrides everything else
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 // If rules are specified, create a RuleBasedCollator
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 // Start with the lang attribute
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(); // use default locale
92 }
93 }
94 }
95
96 if (collator != null) {
97 // See if there is a strength attribute
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 // Look for the properties ignore-case, ignore-modifiers, ignore-width
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 // no-op
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 // no-op
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 // no-op
151 } else {
152 throw new XPathException("ignore-modifiers must be yes or no");
153 }
154 }
155
156 // The ignore-symbols property is ignored
157
158 // See if there is a decomposition attribute
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 // See if there is a case-order property
182 String caseOrder = props.getProperty("case-order");
183 if (caseOrder != null && !"#default".equals(caseOrder)) {
184 // force base collator to ignore case differences
185 if (collator != null) {
186 collator.setStrength(Collator.SECONDARY);
187 }
188 stringCollator = CaseFirstCollator.makeCaseOrderedCollator(uri, stringCollator, caseOrder);
189 }
190
191 // See if there is an alphanumeric property
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 * Get a locale given a language code in XML format
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