| 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.parser.ContextItemStaticInfo; |
| 14 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 15 |
import net.sf.saxon.om.Sequence; |
| 16 |
import net.sf.saxon.regex.RegularExpression; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.value.StringValue; |
| 19 |
|
| 20 |
import java.util.ArrayList; |
| 21 |
import java.util.List; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public abstract class RegexFunction extends SystemFunction implements StatefulSystemFunction { |
| 28 |
|
| 29 |
private RegularExpression staticRegex; |
| 30 |
|
| 31 |
public RegularExpression getStaticRegex() { |
| 32 |
return staticRegex; |
| 33 |
} |
| 34 |
|
| 35 |
private void tryToBindRegularExpression(Expression[] arguments) { |
| 36 |
|
| 37 |
|
| 38 |
if (arguments[1] instanceof Literal && arguments[arguments.length - 1] instanceof Literal) { |
| 39 |
try { |
| 40 |
Configuration config = getRetainedStaticContext().getConfiguration(); |
| 41 |
String re = ((Literal) arguments[1]).getValue().getStringValue(); |
| 42 |
String flags = ((Literal) arguments[arguments.length - 1]).getValue().getStringValue(); |
| 43 |
String hostLang = "XP30"; |
| 44 |
if (config.getXsdVersion() == Configuration.XSD11) { |
| 45 |
hostLang += "/XSD11"; |
| 46 |
} |
| 47 |
List<String> warnings = new ArrayList<>(1); |
| 48 |
staticRegex = config.compileRegularExpression(re, flags, hostLang, warnings); |
| 49 |
if (!allowRegexMatchingEmptyString() && staticRegex.matches("")) { |
| 50 |
staticRegex = null; |
| 51 |
|
| 52 |
} |
| 53 |
} catch (XPathException err) { |
| 54 |
|
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
@Override |
| 67 |
public RegexFunction copy() { |
| 68 |
RegexFunction copy = (RegexFunction) SystemFunction.makeFunction(getFunctionName().getLocalPart(), getRetainedStaticContext(), getArity()); |
| 69 |
copy.staticRegex = staticRegex; |
| 70 |
return copy; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
protected abstract boolean allowRegexMatchingEmptyString(); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
@Override |
| 85 |
public Expression makeFunctionCall(Expression... arguments) { |
| 86 |
tryToBindRegularExpression(arguments); |
| 87 |
return super.makeFunctionCall(arguments); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
@Override |
| 102 |
public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException { |
| 103 |
tryToBindRegularExpression(arguments); |
| 104 |
return super.makeOptimizedFunctionCall(visitor, contextInfo, arguments); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
protected RegularExpression getRegularExpression(Sequence[] args) throws XPathException { |
| 118 |
if (staticRegex != null) { |
| 119 |
return staticRegex; |
| 120 |
} |
| 121 |
Configuration config = getRetainedStaticContext().getConfiguration(); |
| 122 |
StringValue regexArg = (StringValue)args[1].head(); |
| 123 |
String re = regexArg.getStringValue(); |
| 124 |
String flags = args[args.length - 1].head().getStringValue(); |
| 125 |
String hostLang = "XP30"; |
| 126 |
if (config.getXsdVersion() == Configuration.XSD11) { |
| 127 |
hostLang += "/XSD11"; |
| 128 |
} |
| 129 |
List<String> warnings = new ArrayList<>(1); |
| 130 |
RegularExpression regex = config.compileRegularExpression(re, flags, hostLang, warnings); |
| 131 |
|
| 132 |
if (!allowRegexMatchingEmptyString() && regex.matches("")) { |
| 133 |
throw new XPathException("The regular expression must not be one that matches a zero-length string", "FORX0003"); |
| 134 |
} |
| 135 |
return regex; |
| 136 |
} |
| 137 |
|
| 138 |
} |
| 139 |
|