Debian Codegraph @

net/sf/saxon/lib/StandardCollationURIResolver.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.lib;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.Version;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.value.AnyURIValue;
14
15 import java.net.URI;
16 import java.net.URISyntaxException;
17 import java.util.Properties;
18 import java.util.StringTokenizer;
19
20
21 /**
22 * StandardCollationURIResolver allows a Collation to be created given
23 * a URI starting with "http://saxon.sf.net/collation" followed by a set of query parameters.
24 */
25
26 public class StandardCollationURIResolver implements CollationURIResolver {
27
28 private static final StandardCollationURIResolver theInstance = new StandardCollationURIResolver();
29
30 /**
31 * The class is normally used as a singleton, but the constructor is public to allow the class to be named
32 * as a value of the configuration property COLLATION_URI_RESOLVER
33 */
34 public StandardCollationURIResolver() {
35 }
36
37 /**
38 * Return the singleton instance of this class
39 *
40 * @return the singleton instance
41 */
42
43 public static StandardCollationURIResolver getInstance() {
44 return theInstance;
45 }
46
47
48 /**
49 * Create a collator from a parameterized URI
50 *
51 * @return null if the collation URI is not recognized. If the collation URI is recognized but contains
52 * errors, the method returns null after sending a warning to the ErrorListener.
53 */
54
55 /*@Nullable*/
56 public StringCollator resolve(String uri, Configuration config) throws XPathException {
57 if (uri.equals("http://saxon.sf.net/collation")) {
58 return Version.platform.makeCollation(config, new Properties(), uri);
59 } else if (uri.startsWith("http://saxon.sf.net/collation?")) {
60 URI uuri;
61 try {
62 uuri = new URI(uri);
63 } catch (URISyntaxException err) {
64 throw new XPathException(err);
65 }
66 Properties props = new Properties();
67 String query = uuri.getRawQuery();
68 StringTokenizer queryTokenizer = new StringTokenizer(query, ";&");
69 while (queryTokenizer.hasMoreElements()) {
70 String param = queryTokenizer.nextToken();
71 int eq = param.indexOf('=');
72 if (eq > 0 && eq < param.length() - 1) {
73 String kw = param.substring(0, eq);
74 String val = AnyURIValue.decode(param.substring(eq + 1));
75 props.setProperty(kw, val);
76 }
77 }
78 return Version.platform.makeCollation(config, props, uri);
79 } else if (uri.startsWith("http://www.w3.org/2013/collation/UCA")) {
80 StringCollator uca = Version.platform.makeUcaCollator(uri, config);
81 if (uca != null) {
82 return uca;
83 }
84 if (uri.contains("fallback=no")) {
85 return null;
86 } else {
87 URI uuri;
88 try {
89 uuri = new URI(uri);
90 } catch (URISyntaxException err) {
91 throw new XPathException(err);
92 }
93 Properties props = new Properties();
94 String query = AnyURIValue.decode(uuri.getRawQuery());
95 for (String param : query.split(";")) {
96 String tokens[] = param.split("=");
97 if (tokens.length == 2) {
98 String kw = tokens[0];
99 String val = tokens[1];
100 if (kw.equals("fallback")) {
101 if (val.equals("no")) {
102 return null;
103 } else if (!val.equals("yes")) {
104 // effect is implementation-defined, but it seems best to reject it
105 return null;
106 }
107 }
108 switch (kw) {
109 case "strength":
110 switch (val) {
111 case "1":
112 val = "primary";
113 break;
114 case "2":
115 val = "secondary";
116 break;
117 case "3":
118 val = "tertiary";
119 break;
120 case "quaternary":
121 case "4":
122 case "5":
123 val = "identical";
124 break;
125 }
126 break;
127 case "caseFirst":
128 kw = "case-order";
129 val += "-first"; // Should check correct?
130
131 break;
132 case "numeric":
133 kw = "alphanumeric";
134 break;
135 }
136 props.setProperty(kw, val);
137 }
138 }
139 return Version.platform.makeCollation(config, props, uri);
140 }
141 } else {
142 return null;
143 }
144
145
146 }
147
148 }
149