| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class CollatingFunctionFree extends SystemFunction { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private int getCollationArgument() { |
| 42 |
|
| 43 |
return getArity() - 1; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 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 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 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 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 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 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 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); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|