1 package net.sf.saxon.expr; 2 3 import net.sf.saxon.om.NamePool; 4 import net.sf.saxon.type.ItemType; 5 6 import javax.xml.transform.SourceLocator ; 7 import java.io.Serializable ; 8 9 15 public class RoleLocator implements Serializable { 16 17 private int kind; 18 private Object container; 19 private int operand; 20 private NamePool namePool; 21 private String errorCode = "XPTY0004"; private SourceLocator sourceLocator; 23 24 public static final int FUNCTION = 0; 25 public static final int BINARY_EXPR = 1; 26 public static final int TYPE_OP = 2; 27 public static final int VARIABLE = 3; 28 public static final int INSTRUCTION = 4; 29 public static final int FUNCTION_RESULT = 5; 30 public static final int ORDER_BY = 6; 31 public static final int TEMPLATE_RESULT = 7; 32 33 42 43 public RoleLocator(int kind, Object container, int operand, NamePool namePool) { 44 this.kind = kind; 45 this.container = container; 46 this.operand = operand; 47 this.namePool = namePool; 48 } 49 50 54 55 public void setErrorCode(String code) { 56 if (code != null) { 57 this.errorCode = code; 58 } 59 } 60 61 65 66 public String getErrorCode() { 67 return errorCode; 68 } 69 70 73 74 public void setSourceLocator(SourceLocator locator) { 75 if (locator instanceof ExpressionLocation) { 78 this.sourceLocator = locator; 79 } else { 80 this.sourceLocator = new ExpressionLocation(locator); 81 } 82 } 86 87 90 91 public SourceLocator getSourceLocator() { 92 return sourceLocator; 93 } 94 95 99 public String getMessage() { 100 String name; 101 if (container instanceof String ) { 102 name = (String )container; 103 } else { 104 if (namePool == null) { 105 name = "*unknown*"; 106 } else { 107 name = namePool.getDisplayName(((Integer )container).intValue()); 108 } 109 } 110 111 switch (kind) { 112 case FUNCTION: 113 return ordinal(operand+1) + " argument of " + name + "()"; 114 case BINARY_EXPR: 115 return ordinal(operand+1) + " operand of '" + name + '\''; 116 case TYPE_OP: 117 return "value in '" + name + "' expression"; 118 case VARIABLE: 119 return "value of variable $" + name; 120 case INSTRUCTION: 121 int slash = name.indexOf('/'); 122 String attributeName = ""; 123 if (slash >= 0) { 124 attributeName = name.substring(slash+1); 125 name = name.substring(0, slash); 126 } 127 return '@' + attributeName + " attribute of " + name; 128 case FUNCTION_RESULT: 129 return "result of function " + name + "()"; 130 case TEMPLATE_RESULT: 131 return "result of template " + name; 132 case ORDER_BY: 133 return ordinal(operand+1) + " sort key"; 134 default: 135 return ""; 136 } 137 } 138 139 142 143 public String composeErrorMessage(ItemType requiredItemType, ItemType suppliedItemType, NamePool pool) { 144 return "Required type of " + getMessage() + 145 " is " + requiredItemType.toString(pool) + 146 "; supplied value has type " + 147 suppliedItemType.toString(pool); 148 } 149 150 156 private static String ordinal(int n) { 157 switch(n) { 158 case 1: 159 return "first"; 160 case 2: 161 return "second"; 162 case 3: 163 return "third"; 164 default: 165 return n + "th"; 167 } 168 } 169 } 170 171 | Popular Tags |