| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.parser.ExplicitLocation; |
| 12 |
import net.sf.saxon.lib.NamespaceConstant; |
| 13 |
import net.sf.saxon.om.*; |
| 14 |
import net.sf.saxon.pattern.NameTest; |
| 15 |
import net.sf.saxon.trans.XPathException; |
| 16 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 17 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 18 |
import net.sf.saxon.type.Type; |
| 19 |
import net.sf.saxon.value.QNameValue; |
| 20 |
import net.sf.saxon.om.ZeroOrOne; |
| 21 |
import net.sf.saxon.value.StringValue; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class Error extends SystemFunction implements Callable { |
| 28 |
|
| 29 |
@Override |
| 30 |
public int getSpecialProperties(Expression[] arguments) { |
| 31 |
return super.getSpecialProperties(arguments) & ~StaticProperty.NO_NODES_NEWLY_CREATED; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public boolean isVacuousExpression() { |
| 41 |
return true; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public Item error( |
| 88 |
XPathContext context, |
| 89 |
QNameValue errorCode, |
| 90 |
StringValue desc, |
| 91 |
SequenceIterator errObject) throws XPathException { |
| 92 |
QNameValue qname = null; |
| 93 |
if (getArity() > 0) { |
| 94 |
qname = errorCode; |
| 95 |
} |
| 96 |
if (qname == null) { |
| 97 |
qname = new QNameValue("err", NamespaceConstant.ERR, |
| 98 |
getArity() == 1 ? "FOTY0004" : "FOER0000", |
| 99 |
BuiltInAtomicType.QNAME, false); |
| 100 |
} |
| 101 |
String description; |
| 102 |
if (getArity() > 1) { |
| 103 |
description = desc == null ? "" : desc.getStringValue(); |
| 104 |
} else { |
| 105 |
description = "Error signalled by application call on error()"; |
| 106 |
} |
| 107 |
XPathException e = new XPathException(description); |
| 108 |
e.setErrorCodeQName(qname.getStructuredQName()); |
| 109 |
e.setXPathContext(context); |
| 110 |
if (getArity() > 2 && errObject != null) { |
| 111 |
Sequence<?> errorObject = errObject.materialize(); |
| 112 |
if (errorObject instanceof ZeroOrOne) { |
| 113 |
Item root = ((ZeroOrOne) errorObject).head(); |
| 114 |
if ((root instanceof NodeInfo) && ((NodeInfo) root).getNodeKind() == Type.DOCUMENT) { |
| 115 |
AxisIterator iter = ((NodeInfo) root).iterateAxis(AxisInfo.CHILD, |
| 116 |
new NameTest(Type.ELEMENT, "", "error", context.getConfiguration().getNamePool())); |
| 117 |
NodeInfo errorElement = iter.next(); |
| 118 |
if (errorElement != null) { |
| 119 |
String module = errorElement.getAttributeValue("", "module"); |
| 120 |
String lineVal = errorElement.getAttributeValue("", "line"); |
| 121 |
int line = lineVal == null ? -1 : Integer.parseInt(lineVal); |
| 122 |
String columnVal = errorElement.getAttributeValue("", "column"); |
| 123 |
int col = columnVal == null ? -1 : Integer.parseInt(columnVal); |
| 124 |
ExplicitLocation locator = new ExplicitLocation(module, line, col); |
| 125 |
e.setLocator(locator); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
e.setErrorObject(errorObject); |
| 130 |
} |
| 131 |
throw e; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 136 |
int len = arguments.length; |
| 137 |
|
| 138 |
switch (len) { |
| 139 |
case 0: |
| 140 |
return error(context, null, null, null); |
| 141 |
case 1: |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
QNameValue arg0 = (QNameValue)arguments[0].head(); |
| 147 |
if (arg0 == null) { |
| 148 |
arg0 = new QNameValue("err", NamespaceConstant.ERR, "FOER0000"); |
| 149 |
} |
| 150 |
return error(context, arg0, null, null); |
| 151 |
case 2: |
| 152 |
return error(context, (QNameValue) arguments[0].head(), (StringValue) arguments[1].head(), null); |
| 153 |
case 3: |
| 154 |
return error(context, (QNameValue) arguments[0].head(), (StringValue) arguments[1].head(), arguments[2].iterate()); |
| 155 |
default: |
| 156 |
return null; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|