Debian Codegraph @

net/sf/saxon/functions/CollatingFunctionFree.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.Configuration;
11 import net.sf.saxon.expr.Expression;
12 import net.sf.saxon.expr.Literal;
13 import net.sf.saxon.expr.XPathContext;
14 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
15 import net.sf.saxon.expr.parser.ExpressionVisitor;
16 import net.sf.saxon.lib.NamespaceConstant;
17 import net.sf.saxon.om.Item;
18 import net.sf.saxon.om.Sequence;
19 import net.sf.saxon.trans.XPathException;
20
21 import java.net.URI;
22 import java.net.URISyntaxException;
23
24 /**
25 * Generic class for all functions that take an optional collation argument,
26 * where the collation argument is supplied and has not yet been resolved. The
27 * class provides methods that either do early evaluation of the collation
28 * argument (converting the function to the corresponding instance of
29 * {@link net.sf.saxon.functions.CollatingFunctionFixed}, or failing that,
30 * the collation argument is evaluated at run-time.
31 */
32
33 public class CollatingFunctionFree extends SystemFunction {
34
35 /**
36 * Get the argument position (0-based) containing the collation name
37 *
38 * @return the position of the argument containing the collation URI
39 */
40
41 private int getCollationArgument() {
42 // the collation argument is generally the last, but we keep it flexible
43 return getArity() - 1;
44 }
45
46 /**
47 * Allow the function to create an optimized call based on the values of the actual arguments
48 *
49 * @param visitor the expression visitor
50 * @param contextInfo information about the context item
51 * @param arguments the supplied arguments to the function call
52 * @return either a function call on this function, or an expression that delivers
53 * the same result, or null indicating that no optimization has taken place
54 * @throws net.sf.saxon.trans.XPathException if an error is detected
55 */
56 @Override
57 public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException {
58 Expression c = arguments[arguments.length - 1];
59 if (c instanceof Literal) {
60 String coll = ((Literal) c).getValue().getStringValue();
61 try {
62 URI collUri = new URI(coll);
63 if (!collUri.isAbsolute()) {
64 collUri = ResolveURI.makeAbsolute(coll, getStaticBaseUriString());
65 coll = collUri.toASCIIString();
66 }
67 } catch (URISyntaxException e) {
68 visitor.getStaticContext().issueWarning(
69 "Cannot resolve relative collation URI " + coll, c.getLocation());
70 }
71 CollatingFunctionFixed fn = bindCollation(coll);
72 Expression[] newArgs = new Expression[arguments.length - 1];
73 System.arraycopy(arguments, 0, newArgs, 0, newArgs.length);
74 return fn.makeFunctionCall(newArgs);
75 }
76 return null;
77 }
78
79 /**
80 * Create an instance of (a subclass of) CollatingFunctionFixed representing the underlying
81 * function but with the collator already bound
82 * @param collationName the name of the collation to be used
83 * @return a function to implement this function with a fixed collation
84 * @throws XPathException if the collation is unknown
85 */
86
87 public CollatingFunctionFixed bindCollation(String collationName) throws XPathException {
88 Configuration config = getRetainedStaticContext().getConfiguration();
89 CollatingFunctionFixed fixed = (CollatingFunctionFixed)config.makeSystemFunction(
90 getFunctionName().getLocalPart(), getArity()-1);
91 fixed.setRetainedStaticContext(getRetainedStaticContext());
92 fixed.setCollationName(collationName);
93 return fixed;
94 }
95
96 /**
97 * Expand a collation URI, which may be a relative URI reference
98 *
99 * @param collationName the collation URI as provided
100 * @param expressionBaseURI the base URI against which the collation URI will be resolved if it is relative
101 * @return the resolved (expanded) absolute collation URI
102 * @throws net.sf.saxon.trans.XPathException if the collation URI cannot be resolved
103 */
104
105 public static String expandCollationURI(String collationName, URI expressionBaseURI) throws XPathException {
106 try {
107 URI collationURI = new URI(collationName);
108 if (!collationURI.isAbsolute()) {
109 if (expressionBaseURI == null) {
110 throw new XPathException("Cannot resolve relative collation URI '" + collationName +
111 "': unknown or invalid base URI", "FOCH0002");
112 }
113 collationURI = expressionBaseURI.resolve(collationURI);
114 collationName = collationURI.toString();
115 }
116 } catch (URISyntaxException e) {
117 throw new XPathException("Collation name '" + collationName + "' is not a valid URI", "FOCH0002");
118 }
119 return collationName;
120 }
121
122 /**
123 * Invoke the function. This is done in effect by currying the function: that is,
124 * creating a new function in which the collation argument is bound, and then invoking
125 * that new function.
126 *
127 * @param context the XPath dynamic evaluation context
128 * @param args the actual arguments to be supplied
129 * @return the result of invoking the function
130 * @throws net.sf.saxon.trans.XPathException if a dynamic error occurs within the function
131 */
132 public Sequence<?> call(XPathContext context, Sequence[] args) throws XPathException {
133 int c = getCollationArgument();
134 String collation = args[c].head().getStringValue();
135 collation = expandCollationURI(collation, getRetainedStaticContext().getStaticBaseUri());
136 CollatingFunctionFixed fixed = bindCollation(collation);
137 Sequence[] retainedArgs = new Sequence[args.length - 1];
138 System.arraycopy(args, 0, retainedArgs, 0, c);
139 if (c+1 < getArity()) {
140 System.arraycopy(args, c+1, retainedArgs, c, getArity() - c);
141 }
142 return fixed.call(context, retainedArgs);
143 }
144
145 public String getStreamerName() {
146 try {
147 return bindCollation(NamespaceConstant.CODEPOINT_COLLATION_URI).getStreamerName();
148 } catch (XPathException e) {
149 throw new AssertionError(e); // should not happen
150 }
151 }
152 }
153