| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.expr.SystemFunctionCall; |
| 12 |
import net.sf.saxon.expr.XPathContext; |
| 13 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 14 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 15 |
import net.sf.saxon.expr.sort.CodepointCollator; |
| 16 |
import net.sf.saxon.lib.SubstringMatcher; |
| 17 |
import net.sf.saxon.om.Sequence; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
import net.sf.saxon.value.BooleanValue; |
| 20 |
import net.sf.saxon.value.StringValue; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class EndsWith extends CollatingFunctionFixed { |
| 26 |
|
| 27 |
@Override |
| 28 |
public boolean isSubstringMatchingFunction() { |
| 29 |
return true; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
@Override |
| 44 |
public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, final Expression... arguments) throws XPathException { |
| 45 |
if (getStringCollator() == CodepointCollator.getInstance()) { |
| 46 |
|
| 47 |
return new SystemFunctionCall.Optimized(this, arguments) { |
| 48 |
@Override |
| 49 |
public boolean effectiveBooleanValue(XPathContext context) throws XPathException { |
| 50 |
String s0 = getArg(0).evaluateAsString(context).toString(); |
| 51 |
String s1 = getArg(1).evaluateAsString(context).toString(); |
| 52 |
return s0.endsWith(s1); |
| 53 |
} |
| 54 |
}; |
| 55 |
} else { |
| 56 |
return super.makeOptimizedFunctionCall(visitor, contextInfo, arguments); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
public static boolean endsWith(StringValue arg0, StringValue arg1, SubstringMatcher collator) { |
| 62 |
if (arg1 == null || arg1.isZeroLength() || collator.comparesEqual(arg1.getPrimitiveStringValue(), "")) { |
| 63 |
return true; |
| 64 |
} |
| 65 |
if (arg0 == null || arg0.isZeroLength()) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
String s0 = arg0.getStringValue(); |
| 70 |
String s1 = arg1.getStringValue(); |
| 71 |
|
| 72 |
return collator.endsWith(s0, s1); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 85 |
StringValue s0 = (StringValue) arguments[0].head(); |
| 86 |
StringValue s1 = (StringValue) arguments[1].head(); |
| 87 |
return BooleanValue.get(endsWith(s0, s1, (SubstringMatcher)getStringCollator())); |
| 88 |
} |
| 89 |
|
| 90 |
public String getCompilerName() { |
| 91 |
return "StartsWithCompiler"; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
} |
| 96 |
|