| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.trans; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.expr.instruct.Instruction; |
| 12 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 13 |
import net.sf.saxon.om.*; |
| 14 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 15 |
import net.sf.saxon.type.Type; |
| 16 |
import net.sf.saxon.value.Whitespace; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class Err { |
| 23 |
|
| 24 |
public static final int ELEMENT = 1; |
| 25 |
public static final int ATTRIBUTE = 2; |
| 26 |
public static final int FUNCTION = 3; |
| 27 |
public static final int VALUE = 4; |
| 28 |
public static final int VARIABLE = 5; |
| 29 |
public static final int GENERAL = 6; |
| 30 |
public static final int URI = 7; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public static String wrap(CharSequence cs) { |
| 39 |
return wrap(cs, GENERAL); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public static String wrap( CharSequence cs, int valueType) { |
| 50 |
if (cs == null) { |
| 51 |
return "(NULL)"; |
| 52 |
} |
| 53 |
FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.C64); |
| 54 |
int len = cs.length(); |
| 55 |
for (int i = 0; i < len; i++) { |
| 56 |
char c = cs.charAt(i); |
| 57 |
switch (c) { |
| 58 |
case '\n': |
| 59 |
sb.append("\\n"); |
| 60 |
break; |
| 61 |
case '\t': |
| 62 |
sb.append("\\t"); |
| 63 |
break; |
| 64 |
case '\r': |
| 65 |
sb.append("\\r"); |
| 66 |
break; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
default: |
| 71 |
if (c < 32) { |
| 72 |
sb.append("\\x"); |
| 73 |
sb.append(Integer.toHexString(c)); |
| 74 |
} else { |
| 75 |
sb.append(c); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
String s; |
| 80 |
if (valueType == ELEMENT || valueType == ATTRIBUTE) { |
| 81 |
s = sb.toString(); |
| 82 |
if (s.startsWith("{")) { |
| 83 |
s = "Q" + s; |
| 84 |
} |
| 85 |
if (s.startsWith("Q{")) { |
| 86 |
try { |
| 87 |
StructuredQName qn = StructuredQName.fromEQName(sb.toString()); |
| 88 |
String uri = abbreviateURI(qn.getURI()); |
| 89 |
s = "Q{" + uri + "}" + qn.getLocalPart(); |
| 90 |
} catch (Exception e) { |
| 91 |
s = sb.toString(); |
| 92 |
} |
| 93 |
} |
| 94 |
} else if (valueType == URI) { |
| 95 |
s = abbreviateURI(sb.toString()); |
| 96 |
} else { |
| 97 |
s = len > 30 ? sb.toString().substring(0, 30) + "..." : sb.toString(); |
| 98 |
} |
| 99 |
switch (valueType) { |
| 100 |
case ELEMENT: |
| 101 |
return "<" + s + ">"; |
| 102 |
case ATTRIBUTE: |
| 103 |
return "@" + s; |
| 104 |
case FUNCTION: |
| 105 |
return s + "()"; |
| 106 |
case VARIABLE: |
| 107 |
return "$" + s; |
| 108 |
case VALUE: |
| 109 |
return "\"" + s + "\""; |
| 110 |
default: |
| 111 |
return "{" + s + "}"; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public static CharSequence depict(Item item) { |
| 120 |
if (item instanceof NodeInfo) { |
| 121 |
NodeInfo node = (NodeInfo)item; |
| 122 |
switch (node.getNodeKind()) { |
| 123 |
case Type.DOCUMENT: |
| 124 |
return "doc(" + abbreviateURI(node.getSystemId()) + ')'; |
| 125 |
case Type.ELEMENT: |
| 126 |
return '<' + node.getDisplayName() + '>'; |
| 127 |
case Type.ATTRIBUTE: |
| 128 |
return '@' + node.getDisplayName() + "=\"" + node.getStringValueCS() + '"'; |
| 129 |
case Type.TEXT: |
| 130 |
return "text{" + truncate30(node.getStringValueCS()) + "}"; |
| 131 |
case Type.COMMENT: |
| 132 |
return "<!--...-->"; |
| 133 |
case Type.PROCESSING_INSTRUCTION: |
| 134 |
return "<?" + node.getLocalPart() + "...?>"; |
| 135 |
case Type.NAMESPACE: |
| 136 |
return "xmlns:" + node.getLocalPart() + "=" + abbreviateURI(node.getStringValue()); |
| 137 |
default: |
| 138 |
return ""; |
| 139 |
} |
| 140 |
} else { |
| 141 |
return item.toShortString(); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
public static CharSequence depictSequence(Sequence<?> seq) { |
| 146 |
if (seq == null) { |
| 147 |
return "(*null*)"; |
| 148 |
} |
| 149 |
try { |
| 150 |
GroundedValue<?> val = seq.materialize(); |
| 151 |
if (val.getLength() == 0) { |
| 152 |
return "()"; |
| 153 |
} else if (val.getLength() == 1) { |
| 154 |
return depict(seq.head()); |
| 155 |
} else { |
| 156 |
return depictSequenceStart(val.iterate(), 3, val.getLength()); |
| 157 |
} |
| 158 |
} catch (Exception e) { |
| 159 |
return "(*unreadable*)"; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
public static String depictSequenceStart(SequenceIterator seq, int max, int actual) { |
| 164 |
try { |
| 165 |
FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.C64); |
| 166 |
int count = 0; |
| 167 |
sb.append(" ("); |
| 168 |
Item next; |
| 169 |
while ((next = seq.next()) != null) { |
| 170 |
if (count++ > 0) { |
| 171 |
sb.append(", "); |
| 172 |
} |
| 173 |
if (count > max) { |
| 174 |
sb.append("... [" + actual + "])"); |
| 175 |
return sb.toString(); |
| 176 |
} |
| 177 |
|
| 178 |
sb.append(Err.depict(next)); |
| 179 |
} |
| 180 |
sb.append(") "); |
| 181 |
return sb.toString(); |
| 182 |
} catch (XPathException e) { |
| 183 |
return ""; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
public static CharSequence truncate30(CharSequence cs) { |
| 188 |
if (cs.length() <= 30) { |
| 189 |
return Whitespace.collapseWhitespace(cs); |
| 190 |
} else { |
| 191 |
return Whitespace.collapseWhitespace(cs.subSequence(0, 30)) + "..."; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public static String abbreviateURI(String uri) { |
| 203 |
if (uri==null) { |
| 204 |
return ""; |
| 205 |
} |
| 206 |
int lastSlash = (uri.endsWith("/") ? uri.substring(0, uri.length()-1) : uri).lastIndexOf('/'); |
| 207 |
if (lastSlash < 0) { |
| 208 |
if (uri.length() > 15) { |
| 209 |
uri = "..." + uri.substring(uri.length() - 15); |
| 210 |
} |
| 211 |
return uri; |
| 212 |
} else { |
| 213 |
return "..." + uri.substring(lastSlash); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
public static String wrap(Expression exp) { |
| 218 |
if (ExpressionTool.expressionSize(exp) < 10 && !(exp instanceof Instruction)) { |
| 219 |
return "{" + exp + "}"; |
| 220 |
} else { |
| 221 |
return exp.getExpressionName(); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
} |