Debian Codegraph @

net/sf/saxon/functions/CollatingFunctionFixed.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.functions;
9
10 import net.sf.saxon.expr.StaticContext;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.expr.parser.RetainedStaticContext;
13 import net.sf.saxon.expr.sort.*;
14 import net.sf.saxon.lib.NamespaceConstant;
15 import net.sf.saxon.lib.StringCollator;
16 import net.sf.saxon.lib.SubstringMatcher;
17 import net.sf.saxon.trace.ExpressionPresenter;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.type.AtomicType;
20 import net.sf.saxon.type.BuiltInAtomicType;
21 import net.sf.saxon.type.ErrorType;
22
23 import java.util.Properties;
24
25 /**
26 * Abstract superclass for functions that take an optional collation argument, in which the
27 * collation is not present as an explicit argument, either because it was defaulted in the
28 * original function call, or because it has been bound during static analysis.
29 */
30
31
32 public abstract class CollatingFunctionFixed extends SystemFunction implements StatefulSystemFunction {
33
34 // The absolute collation URI
35 private String collationName;
36
37 // The collation corresponding to this collation name
38 private StringCollator stringCollator = null;
39
40 // The AtomicComparer to be used (not used by all collating functions)
41 private AtomicComparer atomicComparer = null;
42
43 /**
44 * Ask whether this function needs a collation that can handle substring matching
45 *
46 * @return true in the case of functions such as contains() and starts-with() where
47 * substring matching is required. Returns false by default;
48 */
49
50 public boolean isSubstringMatchingFunction() {
51 return false;
52 }
53
54
55 /**
56 * Get the collation if known statically, as a StringCollator object
57 *
58 * @return a StringCollator. Return null if the collation is not known statically.
59 */
60
61 public StringCollator getStringCollator() {
62 return stringCollator;
63 }
64
65 @Override
66 public void setRetainedStaticContext(RetainedStaticContext retainedStaticContext) {
67 super.setRetainedStaticContext(retainedStaticContext);
68 if (collationName == null) {
69 collationName = retainedStaticContext.getDefaultCollationName();
70 try {
71 allocateCollator();
72 } catch (XPathException e) {
73 // ignore the failure, it will be reported later
74 }
75 }
76 }
77
78 public void setCollationName(String collationName) throws XPathException {
79 this.collationName = collationName;
80 allocateCollator();
81 }
82
83 private void allocateCollator() throws XPathException {
84 stringCollator = getRetainedStaticContext().getConfiguration().getCollation(collationName);
85 if (stringCollator == null) {
86 throw new XPathException("Unknown collation " + collationName, "FOCH0002");
87 }
88 if (isSubstringMatchingFunction()) {
89 if (stringCollator instanceof SimpleCollation) {
90 stringCollator = ((SimpleCollation) stringCollator).getSubstringMatcher();
91 }
92 if (!(stringCollator instanceof SubstringMatcher)) {
93 throw new XPathException("The collation requested for " + getFunctionName().getDisplayName() +
94 " does not support substring matching", "FOCH0004");
95 }
96 }
97 }
98
99 /**
100 * During static analysis, if types are known and the collation is known, pre-allocate a comparer
101 * for comparing atomic values. Called by some collating functions during type-checking. The comparer
102 * that is allocated treats NaN as not equal to NaN.
103 * @param type0 the type of the first comparand
104 * @param type1 the type of the second comparand
105 * @param env the static context
106 */
107
108 protected void preAllocateComparer(AtomicType type0, AtomicType type1, StaticContext env) {
109 StringCollator collation = getStringCollator();
110 if (type0 == ErrorType.getInstance() || type1 == ErrorType.getInstance()) {
111 // there will be no instances to compare, so we can use any comparer
112 atomicComparer = EqualityComparer.getInstance();
113 return;
114 }
115
116 atomicComparer = GenericAtomicComparer.makeAtomicComparer(
117 (BuiltInAtomicType) type0.getBuiltInBaseType(), (BuiltInAtomicType) type1.getBuiltInBaseType(),
118 stringCollator, env.makeEarlyEvaluationContext());
119
120 }
121
122
123 /**
124 * Get the pre-allocated atomic comparer, if available
125 *
126 * @return the preallocated atomic comparer, or null
127 */
128
129 public AtomicComparer getPreAllocatedAtomicComparer() {
130 return atomicComparer;
131 }
132
133 /**
134 * During evaluation, get the pre-allocated atomic comparer if available, or allocate a new one otherwise
135 *
136 * @param context the dynamic evaluation context
137 * @return the pre-allocated comparer if one is available; otherwise, a newly allocated one, using the specified
138 * StringCollator for comparing strings
139 */
140
141 public AtomicComparer getAtomicComparer(XPathContext context) {
142 if (atomicComparer != null) {
143 return atomicComparer.provideContext(context);
144 } else {
145 return new GenericAtomicComparer(getStringCollator(), context);
146 }
147 }
148
149 @Override
150 public void exportAttributes(ExpressionPresenter out) {
151 if (!collationName.equals(NamespaceConstant.CODEPOINT_COLLATION_URI)) {
152 out.emitAttribute("collation", collationName);
153 }
154 }
155
156 @Override
157 public void importAttributes(Properties attributes) throws XPathException {
158 String collationName = attributes.getProperty("collation");
159 if (collationName != null) {
160 setCollationName(collationName);
161 }
162 }
163
164 /**
165 * Make a copy of this SystemFunction. This is required only for system functions such as regex
166 * functions that maintain state on behalf of a particular caller.
167 *
168 * @return a copy of the system function able to contain its own copy of the state on behalf of
169 * the caller.
170 */
171 @Override
172 public CollatingFunctionFixed copy() {
173 SystemFunction copy = SystemFunction.makeFunction(getFunctionName().getLocalPart(), getRetainedStaticContext(), getArity());
174 if (copy instanceof CollatingFunctionFree) {
175 try {
176 copy = ((CollatingFunctionFree) copy).bindCollation(collationName);
177 } catch (XPathException e) {
178 throw new AssertionError(e);
179 }
180 }
181 if (copy instanceof CollatingFunctionFixed) {
182 ((CollatingFunctionFixed) copy).collationName = collationName;
183 ((CollatingFunctionFixed) copy).atomicComparer = atomicComparer;
184 ((CollatingFunctionFixed) copy).stringCollator = stringCollator;
185 return (CollatingFunctionFixed) copy;
186 }
187 throw new IllegalStateException();
188 }
189
190
191 }
192