| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 11 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 12 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 13 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 14 |
import net.sf.saxon.om.Item; |
| 15 |
import net.sf.saxon.om.SequenceIterator; |
| 16 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.type.AnyItemType; |
| 19 |
import net.sf.saxon.type.ItemType; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class ErrorExpression extends Expression { |
| 29 |
|
| 30 |
private XPathException exception; |
| 31 |
private String message; |
| 32 |
private String errorCode; |
| 33 |
private boolean isTypeError; |
| 34 |
private Expression original; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public ErrorExpression() { |
| 42 |
this("Unspecified error", "XXXX9999", false); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public ErrorExpression(String message, String errorCode, boolean isTypeError) { |
| 53 |
this.message = message; |
| 54 |
this.errorCode = errorCode; |
| 55 |
this.isTypeError = isTypeError; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public ErrorExpression(XPathException exception) { |
| 66 |
this.exception = exception; |
| 67 |
this.message = exception.getMessage(); |
| 68 |
this.errorCode = exception.getErrorCodeLocalPart(); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public XPathException getException() { |
| 78 |
return exception; |
| 79 |
} |
| 80 |
|
| 81 |
public boolean isTypeError() { |
| 82 |
return exception == null ? isTypeError : exception.isTypeError(); |
| 83 |
} |
| 84 |
|
| 85 |
public String getMessage() { |
| 86 |
return exception == null ? message : exception.getMessage(); |
| 87 |
} |
| 88 |
|
| 89 |
public String getErrorCodeLocalPart() { |
| 90 |
return exception == null ? errorCode : exception.getErrorCodeLocalPart(); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public void setOriginalExpression(Expression original) { |
| 100 |
this.original = original; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 109 |
return this; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 114 |
return this; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
@Override |
| 126 |
public int getImplementationMethod() { |
| 127 |
return EVALUATE_METHOD | ITERATE_METHOD; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public Item evaluateItem(XPathContext context) throws XPathException { |
| 136 |
if (exception != null) { |
| 137 |
|
| 138 |
XPathException err = new XPathException(exception); |
| 139 |
err.setLocation(exception.getLocator()); |
| 140 |
err.maybeSetLocation(getLocation()); |
| 141 |
err.setErrorCodeQName(exception.getErrorCodeQName()); |
| 142 |
err.setXPathContext(exception.getXPathContext()); |
| 143 |
err.maybeSetContext(context); |
| 144 |
err.setIsTypeError(exception.isTypeError()); |
| 145 |
err.setIsStaticError(exception.isStaticError()); |
| 146 |
err.setIsGlobalError(exception.isGlobalError()); |
| 147 |
throw err; |
| 148 |
} else { |
| 149 |
XPathException err = new XPathException(message); |
| 150 |
err.setLocation(getLocation()); |
| 151 |
err.setErrorCode(errorCode); |
| 152 |
err.setXPathContext(context); |
| 153 |
err.setIsTypeError(isTypeError); |
| 154 |
throw err; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 165 |
evaluateItem(context); |
| 166 |
return null; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public ItemType getItemType() { |
| 177 |
return AnyItemType.getInstance(); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public int computeCardinality() { |
| 185 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 186 |
|
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public Expression copy(RebindingMap rebindings) { |
| 192 |
if (exception != null) { |
| 193 |
ErrorExpression e2 = new ErrorExpression(exception); |
| 194 |
e2.setOriginalExpression(original); |
| 195 |
ExpressionTool.copyLocationInfo(this, e2); |
| 196 |
return e2; |
| 197 |
} else { |
| 198 |
ErrorExpression e2 = new ErrorExpression(message, errorCode, isTypeError); |
| 199 |
e2.setOriginalExpression(original); |
| 200 |
ExpressionTool.copyLocationInfo(this, e2); |
| 201 |
return e2; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
@Override |
| 213 |
public String getExpressionName() { |
| 214 |
return "errorExpr"; |
| 215 |
} |
| 216 |
|
| 217 |
@Override |
| 218 |
public String toString() { |
| 219 |
if (original != null) { |
| 220 |
return original.toString(); |
| 221 |
} else { |
| 222 |
return "error(\"" + message + "\")"; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
@Override |
| 227 |
public String toShortString() { |
| 228 |
if (original != null) { |
| 229 |
return original.toShortString(); |
| 230 |
} else { |
| 231 |
return "error(\"" + message + "\")"; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public void export(ExpressionPresenter destination) throws XPathException { |
| 242 |
destination.startElement("error", this); |
| 243 |
destination.emitAttribute("message", exception == null ? message : exception.getMessage()); |
| 244 |
destination.emitAttribute("code", exception == null ? errorCode : exception.getErrorCodeLocalPart()); |
| 245 |
destination.emitAttribute("isTypeErr", isTypeError?"0":"1"); |
| 246 |
destination.endElement(); |
| 247 |
} |
| 248 |
|
| 249 |
} |