| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 12 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 13 |
import net.sf.saxon.lib.NamespaceConstant; |
| 14 |
import net.sf.saxon.om.NameChecker; |
| 15 |
import net.sf.saxon.om.QNameParser; |
| 16 |
import net.sf.saxon.om.Sequence; |
| 17 |
import net.sf.saxon.om.StructuredQName; |
| 18 |
import net.sf.saxon.style.StylesheetPackage; |
| 19 |
import net.sf.saxon.trans.SymbolicName; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.value.BooleanValue; |
| 22 |
import net.sf.saxon.value.NumericValue; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class FunctionAvailable extends SystemFunction { |
| 29 |
|
| 30 |
@Override |
| 31 |
public Expression makeFunctionCall(Expression... arguments) { |
| 32 |
PackageData pack = getRetainedStaticContext().getPackageData(); |
| 33 |
if (pack instanceof StylesheetPackage) { |
| 34 |
((StylesheetPackage) pack).setRetainUnusedFunctions(); |
| 35 |
} |
| 36 |
return super.makeFunctionCall(arguments); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
@Override |
| 50 |
public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException { |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
if (arguments[0] instanceof Literal && (arguments.length == 1 || arguments[1] instanceof Literal)) { |
| 57 |
String lexicalQName = ((Literal) arguments[0]).getValue().getStringValue(); |
| 58 |
StaticContext env = visitor.getStaticContext(); |
| 59 |
boolean b = false; |
| 60 |
|
| 61 |
QNameParser qp = new QNameParser(getRetainedStaticContext()); |
| 62 |
qp.setAcceptEQName(true); |
| 63 |
qp.setDefaultNamespace(env.getDefaultFunctionNamespace()); |
| 64 |
qp.setErrorOnBadSyntax("XTDE1400"); |
| 65 |
qp.setErrorOnUnresolvedPrefix("XTDE1400"); |
| 66 |
|
| 67 |
StructuredQName functionName = qp.parse(lexicalQName); |
| 68 |
|
| 69 |
int minArity = 0; |
| 70 |
int maxArity = 20; |
| 71 |
if (getArity() == 2) { |
| 72 |
minArity = (int) ((NumericValue) arguments[1].evaluateItem(env.makeEarlyEvaluationContext())).longValue(); |
| 73 |
maxArity = minArity; |
| 74 |
} |
| 75 |
|
| 76 |
for (int i = minArity; i <= maxArity; i++) { |
| 77 |
SymbolicName.F sn = new SymbolicName.F(functionName, i); |
| 78 |
if (env.getFunctionLibrary().isAvailable(sn)) { |
| 79 |
b = true; |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return Literal.makeLiteral(BooleanValue.get(b)); |
| 85 |
} else { |
| 86 |
return null; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
private boolean isFunctionAvailable(String lexicalName, String edition, int arity, XPathContext context) throws XPathException { |
| 91 |
if (arity == -1) { |
| 92 |
for (int i = 0; i < 20; i++) { |
| 93 |
if (isFunctionAvailable(lexicalName, edition, i, context)) { |
| 94 |
return true; |
| 95 |
} |
| 96 |
} |
| 97 |
return false; |
| 98 |
} |
| 99 |
StructuredQName qName; |
| 100 |
try { |
| 101 |
if (NameChecker.isValidNCName(lexicalName)) { |
| 102 |
|
| 103 |
String uri = NamespaceConstant.FN; |
| 104 |
qName = new StructuredQName("", uri, lexicalName); |
| 105 |
} else { |
| 106 |
qName = StructuredQName.fromLexicalQName(lexicalName, |
| 107 |
false, true, |
| 108 |
getRetainedStaticContext()); |
| 109 |
} |
| 110 |
} catch (XPathException e) { |
| 111 |
e.setErrorCode("XTDE1400"); |
| 112 |
e.setXPathContext(context); |
| 113 |
throw e; |
| 114 |
} |
| 115 |
|
| 116 |
final FunctionLibrary lib = context.getController().getExecutable().getFunctionLibrary(); |
| 117 |
SymbolicName.F sn = new SymbolicName.F(qName, arity); |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
return lib.isAvailable(sn); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 142 |
String lexicalQName = arguments[0].head().getStringValue(); |
| 143 |
int arity = -1; |
| 144 |
if (arguments.length == 2) { |
| 145 |
arity = (int) ((NumericValue) arguments[1].head()).longValue(); |
| 146 |
} |
| 147 |
return BooleanValue.get( |
| 148 |
isFunctionAvailable(lexicalQName, getRetainedStaticContext().getPackageData().getTargetEdition(), arity, context)); |
| 149 |
} |
| 150 |
} |
| 151 |
|