| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.parser; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.expr.Literal; |
| 12 |
import net.sf.saxon.expr.StaticProperty; |
| 13 |
import net.sf.saxon.om.AxisInfo; |
| 14 |
import net.sf.saxon.om.Item; |
| 15 |
import net.sf.saxon.trans.Err; |
| 16 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 17 |
import net.sf.saxon.type.ItemType; |
| 18 |
import net.sf.saxon.type.TypeHierarchy; |
| 19 |
import net.sf.saxon.type.UType; |
| 20 |
import net.sf.saxon.value.SequenceType; |
| 21 |
|
| 22 |
import java.util.Optional; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public class RoleDiagnostic { |
| 31 |
|
| 32 |
private int kind; |
| 33 |
private String operation; |
| 34 |
private int operand; |
| 35 |
private String errorCode = "XPTY0004"; |
| 36 |
|
| 37 |
public static final int FUNCTION = 0; |
| 38 |
public static final int BINARY_EXPR = 1; |
| 39 |
public static final int TYPE_OP = 2; |
| 40 |
public static final int VARIABLE = 3; |
| 41 |
public static final int INSTRUCTION = 4; |
| 42 |
public static final int FUNCTION_RESULT = 5; |
| 43 |
public static final int ORDER_BY = 6; |
| 44 |
public static final int TEMPLATE_RESULT = 7; |
| 45 |
public static final int PARAM = 8; |
| 46 |
public static final int UNARY_EXPR = 9; |
| 47 |
public static final int UPDATING_EXPR = 10; |
| 48 |
|
| 49 |
public static final int EVALUATE_RESULT = 12; |
| 50 |
public static final int CONTEXT_ITEM = 13; |
| 51 |
public static final int AXIS_STEP = 14; |
| 52 |
public static final int OPTION = 15; |
| 53 |
public static final int CHARACTER_MAP_EXPANSION = 16; |
| 54 |
|
| 55 |
|
| 56 |
public static final int MATCH_PATTERN = 19; |
| 57 |
public static final int MISC = 20; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public RoleDiagnostic(int kind, String operation, int operand) { |
| 75 |
this.kind = kind; |
| 76 |
this.operation = operation; |
| 77 |
this.operand = operand; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public void setErrorCode( String code) { |
| 87 |
if (code != null) { |
| 88 |
this.errorCode = code; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public String getErrorCode() { |
| 99 |
return errorCode; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public boolean isTypeError() { |
| 109 |
return !errorCode.startsWith("FORG") && !errorCode.equals("XPDY0050"); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public String getMessage() { |
| 118 |
String name = operation; |
| 119 |
|
| 120 |
switch (kind) { |
| 121 |
case FUNCTION: |
| 122 |
if (name.equals("saxon:call") || name.equals("saxon:apply")) { |
| 123 |
if (operand == 0) { |
| 124 |
return "target of the dynamic function call"; |
| 125 |
} else { |
| 126 |
return ordinal(operand) + " argument of the dynamic function call"; |
| 127 |
} |
| 128 |
} else { |
| 129 |
return ordinal(operand + 1) + " argument of " + |
| 130 |
(name.isEmpty() ? "the anonymous function" : name + "()"); |
| 131 |
} |
| 132 |
case BINARY_EXPR: |
| 133 |
return ordinal(operand + 1) + " operand of '" + name + '\''; |
| 134 |
case UNARY_EXPR: |
| 135 |
return "operand of '-'"; |
| 136 |
case TYPE_OP: |
| 137 |
return "value in '" + name + "' expression"; |
| 138 |
case VARIABLE: |
| 139 |
if (name.equals("saxon:context-item")) { |
| 140 |
return "context item"; |
| 141 |
} else { |
| 142 |
return "value of variable $" + name; |
| 143 |
} |
| 144 |
case INSTRUCTION: |
| 145 |
int slash = name.indexOf('/'); |
| 146 |
String attributeName = ""; |
| 147 |
if (slash >= 0) { |
| 148 |
attributeName = name.substring(slash + 1); |
| 149 |
name = name.substring(0, slash); |
| 150 |
} |
| 151 |
return "@" + attributeName + " attribute of " + (name.equals("LRE") ? "a literal result element" : name); |
| 152 |
case FUNCTION_RESULT: |
| 153 |
if (name.isEmpty()) { |
| 154 |
return "result of the anonymous function"; |
| 155 |
} else { |
| 156 |
return "result of a call to " + name; |
| 157 |
} |
| 158 |
case TEMPLATE_RESULT: |
| 159 |
return "result of template " + name; |
| 160 |
case ORDER_BY: |
| 161 |
return ordinal(operand + 1) + " sort key"; |
| 162 |
case PARAM: |
| 163 |
return "value of parameter $" + name; |
| 164 |
case UPDATING_EXPR: |
| 165 |
return "value of the " + ordinal(operand + 1) + " operand of " + name + " expression"; |
| 166 |
case EVALUATE_RESULT: |
| 167 |
return "result of the expression {" + name + "} evaluated by xsl:evaluate"; |
| 168 |
case CONTEXT_ITEM: |
| 169 |
return "context item"; |
| 170 |
case AXIS_STEP: |
| 171 |
return "context item for the " + AxisInfo.axisName[operand] + " axis"; |
| 172 |
case OPTION: |
| 173 |
return "value of the " + name + " option"; |
| 174 |
case CHARACTER_MAP_EXPANSION: |
| 175 |
return "substitute value for character '" + name + "' in the character map"; |
| 176 |
case MATCH_PATTERN: |
| 177 |
return "match pattern"; |
| 178 |
case MISC: |
| 179 |
return operation; |
| 180 |
default: |
| 181 |
return ""; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public String composeRequiredMessage(ItemType requiredItemType) { |
| 193 |
return "The required item type of the " + getMessage() + |
| 194 |
" is " + requiredItemType; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public String composeErrorMessage(ItemType requiredItemType, ItemType suppliedItemType) { |
| 206 |
return composeRequiredMessage(requiredItemType) + |
| 207 |
"; supplied value has item type " + |
| 208 |
suppliedItemType; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public String composeErrorMessage(ItemType requiredItemType, Expression supplied, TypeHierarchy th) { |
| 221 |
if (supplied instanceof Literal) { |
| 222 |
String s = composeRequiredMessage(requiredItemType); |
| 223 |
Optional<String> more = SequenceType.makeSequenceType(requiredItemType, StaticProperty.ALLOWS_ZERO_OR_MORE) |
| 224 |
.explainMismatch(((Literal)supplied).getValue(), th); |
| 225 |
if (more.isPresent()) { |
| 226 |
s = s + ". " + more.get(); |
| 227 |
} |
| 228 |
return s; |
| 229 |
} |
| 230 |
return composeRequiredMessage(requiredItemType) + |
| 231 |
"; supplied expression (" + supplied.toShortString() + ") has item type " + |
| 232 |
supplied.getItemType(); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public String composeErrorMessage(ItemType requiredItemType, Item item, TypeHierarchy th) { |
| 246 |
|
| 247 |
FastStringBuffer message = new FastStringBuffer(256); |
| 248 |
message.append(composeRequiredMessage(requiredItemType)); |
| 249 |
message.append("; the supplied value "); |
| 250 |
message.append(Err.depict(item)); |
| 251 |
|
| 252 |
if (requiredItemType.getGenre() != item.getGenre()) { |
| 253 |
message.append(" is "); |
| 254 |
message.append(item.getGenre().getDescription()); |
| 255 |
} else { |
| 256 |
message.append(" does not match. "); |
| 257 |
if (th != null) { |
| 258 |
Optional<String> more = requiredItemType.explainMismatch(item, th); |
| 259 |
more.ifPresent(message::append); |
| 260 |
} |
| 261 |
} |
| 262 |
return message.toString(); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public String composeErrorMessage(ItemType requiredItemType, UType suppliedItemType) { |
| 275 |
return composeRequiredMessage(requiredItemType) + |
| 276 |
"; supplied value has item type " + |
| 277 |
suppliedItemType; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public String save() { |
| 286 |
FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C256); |
| 287 |
fsb.append(kind + "|"); |
| 288 |
fsb.append(operand + "|"); |
| 289 |
fsb.append(errorCode.equals("XPTY0004") ? "" : errorCode); |
| 290 |
fsb.append("|"); |
| 291 |
fsb.append(operation); |
| 292 |
return fsb.toString(); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public static RoleDiagnostic reconstruct(String in) { |
| 301 |
int v = in.indexOf('|'); |
| 302 |
int kind = Integer.parseInt(in.substring(0, v)); |
| 303 |
int w = in.indexOf('|', v+1); |
| 304 |
int operand = Integer.parseInt(in.substring(v+1, w)); |
| 305 |
int x = in.indexOf('|', w+1); |
| 306 |
String errorCode = in.substring(w+1, x); |
| 307 |
String operation = in.substring(x+1); |
| 308 |
RoleDiagnostic cd = new RoleDiagnostic(kind, operation, operand); |
| 309 |
if (!errorCode.isEmpty()) { |
| 310 |
cd.setErrorCode(errorCode); |
| 311 |
} |
| 312 |
return cd; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public static String ordinal(int n) { |
| 323 |
switch (n) { |
| 324 |
case 1: |
| 325 |
return "first"; |
| 326 |
case 2: |
| 327 |
return "second"; |
| 328 |
case 3: |
| 329 |
return "third"; |
| 330 |
default: |
| 331 |
if (n >= 21) { |
| 332 |
switch (n % 10) { |
| 333 |
case 1: |
| 334 |
return n + "st"; |
| 335 |
case 2: |
| 336 |
return n + "nd"; |
| 337 |
case 3: |
| 338 |
return n + "rd"; |
| 339 |
} |
| 340 |
} |
| 341 |
return n + "th"; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
} |
| 347 |
|