| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions.registry; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.expr.Literal; |
| 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.parser.XPathParser; |
| 16 |
import net.sf.saxon.functions.ApplyFn; |
| 17 |
import net.sf.saxon.functions.Doc_2; |
| 18 |
import net.sf.saxon.functions.SystemFunction; |
| 19 |
import net.sf.saxon.lib.NamespaceConstant; |
| 20 |
import net.sf.saxon.ma.arrays.ArrayItemType; |
| 21 |
import net.sf.saxon.ma.map.MapCreate; |
| 22 |
import net.sf.saxon.ma.map.MapType; |
| 23 |
import net.sf.saxon.ma.map.MapUntypedContains; |
| 24 |
import net.sf.saxon.om.NamespaceBinding; |
| 25 |
import net.sf.saxon.om.NodeInfo; |
| 26 |
import net.sf.saxon.om.Sequence; |
| 27 |
import net.sf.saxon.om.StructuredQName; |
| 28 |
import net.sf.saxon.pattern.NodeKindTest; |
| 29 |
import net.sf.saxon.trans.XPathException; |
| 30 |
import net.sf.saxon.tree.tiny.TinyElementImpl; |
| 31 |
import net.sf.saxon.type.*; |
| 32 |
import net.sf.saxon.value.*; |
| 33 |
|
| 34 |
import javax.xml.transform.SourceLocator; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class VendorFunctionSetHE extends BuiltInFunctionSet { |
| 42 |
|
| 43 |
private static VendorFunctionSetHE THE_INSTANCE = new VendorFunctionSetHE(); |
| 44 |
|
| 45 |
public static VendorFunctionSetHE getInstance() { |
| 46 |
return THE_INSTANCE; |
| 47 |
} |
| 48 |
|
| 49 |
private VendorFunctionSetHE() { |
| 50 |
init(); |
| 51 |
} |
| 52 |
|
| 53 |
private void init() { |
| 54 |
|
| 55 |
|
| 56 |
register("is-whole-number", 1, IsWholeNumberFn.class, BuiltInAtomicType.BOOLEAN, ONE, 0, 0) |
| 57 |
.arg(0, NumericType.getInstance(), OPT, EMPTY); |
| 58 |
|
| 59 |
|
| 60 |
register("dynamic-error-info", 1, DynamicErrorInfoFn.class, AnyItemType.getInstance(), STAR, 0, FOCUS | LATE | SIDE) |
| 61 |
.arg(0, BuiltInAtomicType.STRING, ONE, null); |
| 62 |
|
| 63 |
|
| 64 |
register("apply", 2, ApplyFn.class, AnyItemType.getInstance(), STAR, 0, LATE) |
| 65 |
.arg(0, AnyFunctionType.getInstance(), ONE, null) |
| 66 |
.arg(1, ArrayItemType.ANY_ARRAY_TYPE, ONE, null); |
| 67 |
|
| 68 |
|
| 69 |
register("create-map", 1, MapCreate.class, MapType.ANY_MAP_TYPE, ONE, 0, 0) |
| 70 |
.arg(0, MapType.ANY_MAP_TYPE, STAR, null); |
| 71 |
|
| 72 |
|
| 73 |
register("doc", 2, Doc_2.class, NodeKindTest.DOCUMENT, ONE, 0, LATE) |
| 74 |
.arg(0, BuiltInAtomicType.STRING, ONE, null) |
| 75 |
.arg(1, MapType.ANY_MAP_TYPE, ONE, EMPTY) |
| 76 |
.optionDetails(Doc_2.makeOptionsParameter()); |
| 77 |
|
| 78 |
|
| 79 |
register("has-local-namespaces", 1, HasLocalNamespaces.class, BuiltInAtomicType.BOOLEAN, ONE, 0, 0) |
| 80 |
.arg(0, NodeKindTest.ELEMENT, ONE, null); |
| 81 |
|
| 82 |
|
| 83 |
register("has-uniform-namespaces", 1, HasUniformNamespaces.class, BuiltInAtomicType.BOOLEAN, ONE, 0, 0) |
| 84 |
.arg(0, NodeKindTest.ELEMENT, ONE, null); |
| 85 |
|
| 86 |
|
| 87 |
register("map-untyped-contains", 2, MapUntypedContains.class, BuiltInAtomicType.BOOLEAN, ONE, 0, 0) |
| 88 |
.arg(0, MapType.ANY_MAP_TYPE, STAR, null) |
| 89 |
.arg(1, BuiltInAtomicType.ANY_ATOMIC, ONE, null); |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public String getNamespace() { |
| 95 |
return NamespaceConstant.SAXON; |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public String getConventionalPrefix() { |
| 100 |
return "saxon"; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public static class IsWholeNumberFn extends SystemFunction { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
@Override |
| 120 |
public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException { |
| 121 |
if (arguments[0].getItemType().getPrimitiveItemType() == BuiltInAtomicType.INTEGER) { |
| 122 |
return Literal.makeLiteral(BooleanValue.TRUE); |
| 123 |
} |
| 124 |
return null; |
| 125 |
} |
| 126 |
|
| 127 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 128 |
NumericValue val = (NumericValue) arguments[0].head(); |
| 129 |
return BooleanValue.get(val != null && val.isWholeNumber()); |
| 130 |
} |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public static class HasLocalNamespaces extends SystemFunction { |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 150 |
NodeInfo val = (NodeInfo) arguments[0].head(); |
| 151 |
NodeInfo parent = val.getParent(); |
| 152 |
return BooleanValue.get( |
| 153 |
parent == null || parent.getNodeKind() == Type.DOCUMENT || |
| 154 |
val.getDeclaredNamespaces(NamespaceBinding.EMPTY_ARRAY).length > 0); |
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public static class HasUniformNamespaces extends SystemFunction { |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 176 |
NodeInfo val = (NodeInfo) arguments[0].head(); |
| 177 |
if (val instanceof TinyElementImpl) { |
| 178 |
return BooleanValue.get(((TinyElementImpl)val).hasUniformNamespaces()); |
| 179 |
} else { |
| 180 |
return BooleanValue.FALSE; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public static class DynamicErrorInfoFn extends SystemFunction { |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
@Override |
| 201 |
public int getSpecialProperties(Expression[] arguments) { |
| 202 |
return 0; |
| 203 |
} |
| 204 |
|
| 205 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 206 |
String var = arguments[0].head().getStringValue(); |
| 207 |
@SuppressWarnings({"ThrowableResultOfMethodCallIgnored"}) |
| 208 |
XPathException error = context.getCurrentException(); |
| 209 |
if (error == null) { |
| 210 |
return EmptySequence.getInstance(); |
| 211 |
} |
| 212 |
SourceLocator locator = error.getLocator(); |
| 213 |
switch (var) { |
| 214 |
case "code": |
| 215 |
StructuredQName errorCodeQName = error.getErrorCodeQName(); |
| 216 |
if (errorCodeQName == null) { |
| 217 |
errorCodeQName = new StructuredQName("saxon", NamespaceConstant.SAXON, "XXXX9999"); |
| 218 |
} |
| 219 |
return new QNameValue(errorCodeQName, BuiltInAtomicType.QNAME); |
| 220 |
case "description": |
| 221 |
String s = error.getMessage(); |
| 222 |
if (error.getCause() != null) { |
| 223 |
s += "(" + error.getCause().getMessage() + ")"; |
| 224 |
} |
| 225 |
return new StringValue(s); |
| 226 |
case "value": |
| 227 |
Sequence<?> value = error.getErrorObject(); |
| 228 |
if (value == null) { |
| 229 |
return EmptySequence.getInstance(); |
| 230 |
} else { |
| 231 |
return value; |
| 232 |
} |
| 233 |
case "module": |
| 234 |
String module = locator == null ? null : locator.getSystemId(); |
| 235 |
if (module == null) { |
| 236 |
return EmptySequence.getInstance(); |
| 237 |
} else { |
| 238 |
return new StringValue(module); |
| 239 |
} |
| 240 |
case "line-number": |
| 241 |
int line = locator == null ? -1 : locator.getLineNumber(); |
| 242 |
if (line == -1) { |
| 243 |
return EmptySequence.getInstance(); |
| 244 |
} else { |
| 245 |
return new Int64Value(line); |
| 246 |
} |
| 247 |
case "column-number": |
| 248 |
|
| 249 |
int column = -1; |
| 250 |
if (locator == null) { |
| 251 |
return EmptySequence.getInstance(); |
| 252 |
} else if (locator instanceof XPathParser.NestedLocation) { |
| 253 |
column = ((XPathParser.NestedLocation)locator).getContainingLocation().getColumnNumber(); |
| 254 |
} else { |
| 255 |
column = locator.getColumnNumber(); |
| 256 |
} |
| 257 |
if (column == -1) { |
| 258 |
return EmptySequence.getInstance(); |
| 259 |
} else { |
| 260 |
return new Int64Value(column); |
| 261 |
} |
| 262 |
default: |
| 263 |
return EmptySequence.getInstance(); |
| 264 |
} |
| 265 |
|
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
|