| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.StaticContext; |
| 11 |
import net.sf.saxon.expr.XPathContext; |
| 12 |
import net.sf.saxon.expr.parser.RetainedStaticContext; |
| 13 |
import net.sf.saxon.expr.sort.*; |
| 14 |
import net.sf.saxon.lib.NamespaceConstant; |
| 15 |
import net.sf.saxon.lib.StringCollator; |
| 16 |
import net.sf.saxon.lib.SubstringMatcher; |
| 17 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
import net.sf.saxon.type.AtomicType; |
| 20 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 21 |
import net.sf.saxon.type.ErrorType; |
| 22 |
|
| 23 |
import java.util.Properties; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public abstract class CollatingFunctionFixed extends SystemFunction implements StatefulSystemFunction { |
| 33 |
|
| 34 |
|
| 35 |
private String collationName; |
| 36 |
|
| 37 |
|
| 38 |
private StringCollator stringCollator = null; |
| 39 |
|
| 40 |
|
| 41 |
private AtomicComparer atomicComparer = null; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public boolean isSubstringMatchingFunction() { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public StringCollator getStringCollator() { |
| 62 |
return stringCollator; |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void setRetainedStaticContext(RetainedStaticContext retainedStaticContext) { |
| 67 |
super.setRetainedStaticContext(retainedStaticContext); |
| 68 |
if (collationName == null) { |
| 69 |
collationName = retainedStaticContext.getDefaultCollationName(); |
| 70 |
try { |
| 71 |
allocateCollator(); |
| 72 |
} catch (XPathException e) { |
| 73 |
|
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
public void setCollationName(String collationName) throws XPathException { |
| 79 |
this.collationName = collationName; |
| 80 |
allocateCollator(); |
| 81 |
} |
| 82 |
|
| 83 |
private void allocateCollator() throws XPathException { |
| 84 |
stringCollator = getRetainedStaticContext().getConfiguration().getCollation(collationName); |
| 85 |
if (stringCollator == null) { |
| 86 |
throw new XPathException("Unknown collation " + collationName, "FOCH0002"); |
| 87 |
} |
| 88 |
if (isSubstringMatchingFunction()) { |
| 89 |
if (stringCollator instanceof SimpleCollation) { |
| 90 |
stringCollator = ((SimpleCollation) stringCollator).getSubstringMatcher(); |
| 91 |
} |
| 92 |
if (!(stringCollator instanceof SubstringMatcher)) { |
| 93 |
throw new XPathException("The collation requested for " + getFunctionName().getDisplayName() + |
| 94 |
" does not support substring matching", "FOCH0004"); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
protected void preAllocateComparer(AtomicType type0, AtomicType type1, StaticContext env) { |
| 109 |
StringCollator collation = getStringCollator(); |
| 110 |
if (type0 == ErrorType.getInstance() || type1 == ErrorType.getInstance()) { |
| 111 |
|
| 112 |
atomicComparer = EqualityComparer.getInstance(); |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
atomicComparer = GenericAtomicComparer.makeAtomicComparer( |
| 117 |
(BuiltInAtomicType) type0.getBuiltInBaseType(), (BuiltInAtomicType) type1.getBuiltInBaseType(), |
| 118 |
stringCollator, env.makeEarlyEvaluationContext()); |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public AtomicComparer getPreAllocatedAtomicComparer() { |
| 130 |
return atomicComparer; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public AtomicComparer getAtomicComparer(XPathContext context) { |
| 142 |
if (atomicComparer != null) { |
| 143 |
return atomicComparer.provideContext(context); |
| 144 |
} else { |
| 145 |
return new GenericAtomicComparer(getStringCollator(), context); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void exportAttributes(ExpressionPresenter out) { |
| 151 |
if (!collationName.equals(NamespaceConstant.CODEPOINT_COLLATION_URI)) { |
| 152 |
out.emitAttribute("collation", collationName); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public void importAttributes(Properties attributes) throws XPathException { |
| 158 |
String collationName = attributes.getProperty("collation"); |
| 159 |
if (collationName != null) { |
| 160 |
setCollationName(collationName); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
@Override |
| 172 |
public CollatingFunctionFixed copy() { |
| 173 |
SystemFunction copy = SystemFunction.makeFunction(getFunctionName().getLocalPart(), getRetainedStaticContext(), getArity()); |
| 174 |
if (copy instanceof CollatingFunctionFree) { |
| 175 |
try { |
| 176 |
copy = ((CollatingFunctionFree) copy).bindCollation(collationName); |
| 177 |
} catch (XPathException e) { |
| 178 |
throw new AssertionError(e); |
| 179 |
} |
| 180 |
} |
| 181 |
if (copy instanceof CollatingFunctionFixed) { |
| 182 |
((CollatingFunctionFixed) copy).collationName = collationName; |
| 183 |
((CollatingFunctionFixed) copy).atomicComparer = atomicComparer; |
| 184 |
((CollatingFunctionFixed) copy).stringCollator = stringCollator; |
| 185 |
return (CollatingFunctionFixed) copy; |
| 186 |
} |
| 187 |
throw new IllegalStateException(); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
} |
| 192 |
|