| 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 Contains extends CollatingFunctionFixed { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
@Override |
| 39 |
public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, final Expression... arguments) throws XPathException { |
| 40 |
if (getStringCollator() == CodepointCollator.getInstance()) { |
| 41 |
|
| 42 |
return new SystemFunctionCall.Optimized(this, arguments) { |
| 43 |
@Override |
| 44 |
public boolean effectiveBooleanValue(XPathContext context) throws XPathException { |
| 45 |
String s0 = getArg(0).evaluateAsString(context).toString(); |
| 46 |
CharSequence s1 = getArg(1).evaluateAsString(context); |
| 47 |
return s0.contains(s1); |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) { |
| 52 |
return this; |
| 53 |
} |
| 54 |
}; |
| 55 |
} else { |
| 56 |
return super.makeOptimizedFunctionCall(visitor, contextInfo, arguments); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
@Override |
| 62 |
public boolean isSubstringMatchingFunction() { |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
private static boolean contains(StringValue arg0, StringValue arg1, SubstringMatcher collator) { |
| 67 |
if (arg1 == null || arg1.isZeroLength() || collator.comparesEqual(arg1.getPrimitiveStringValue(), "")) { |
| 68 |
return true; |
| 69 |
} |
| 70 |
if (arg0 == null || arg0.isZeroLength()) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
String s0 = arg0.getStringValue(); |
| 74 |
String s1 = arg1.getStringValue(); |
| 75 |
return collator.contains(s0, s1); |
| 76 |
} |
| 77 |
|
| 78 |
public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 79 |
StringValue s0 = (StringValue) arguments[0].head(); |
| 80 |
StringValue s1 = (StringValue) arguments[1].head(); |
| 81 |
return BooleanValue.get(contains(s0, s1, (SubstringMatcher)getStringCollator())); |
| 82 |
} |
| 83 |
|
| 84 |
public String getCompilerName() { |
| 85 |
return "ContainsCompiler"; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
} |
| 90 |
|