| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.XPathContext; |
| 11 |
import net.sf.saxon.om.Item; |
| 12 |
import net.sf.saxon.om.ZeroOrOne; |
| 13 |
import net.sf.saxon.serialize.charcode.UTF8CharacterSet; |
| 14 |
import net.sf.saxon.trans.Err; |
| 15 |
import net.sf.saxon.trans.XPathException; |
| 16 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 17 |
import net.sf.saxon.value.AtomicValue; |
| 18 |
import net.sf.saxon.value.StringValue; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class EncodeForUri extends ScalarSystemFunction { |
| 25 |
|
| 26 |
@Override |
| 27 |
public AtomicValue evaluate(Item arg, XPathContext context) throws XPathException { |
| 28 |
final CharSequence s = arg.getStringValueCS(); |
| 29 |
return StringValue.makeStringValue(escape(s, "-_.~")); |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
public ZeroOrOne<? extends AtomicValue> resultWhenEmpty() { |
| 34 |
return ZERO_LENGTH_STRING; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public static CharSequence escape(CharSequence s, String allowedPunctuation) { |
| 50 |
FastStringBuffer sb = new FastStringBuffer(s.length()); |
| 51 |
for (int i = 0; i < s.length(); i++) { |
| 52 |
char c = s.charAt(i); |
| 53 |
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) { |
| 54 |
sb.append(c); |
| 55 |
} else if (c <= 0x20 || c >= 0x7f) { |
| 56 |
escapeChar(c, (i + 1) < s.length() ? s.charAt(i + 1) : ' ', sb); |
| 57 |
} else if (allowedPunctuation.indexOf(c) >= 0) { |
| 58 |
sb.append(c); |
| 59 |
} else { |
| 60 |
escapeChar(c, ' ', sb); |
| 61 |
} |
| 62 |
|
| 63 |
} |
| 64 |
return sb; |
| 65 |
} |
| 66 |
|
| 67 |
private static final String hex = "0123456789ABCDEF"; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static void escapeChar(char c, char c2, FastStringBuffer sb) { |
| 79 |
byte[] array = new byte[4]; |
| 80 |
int used = UTF8CharacterSet.getUTF8Encoding(c, c2, array); |
| 81 |
for (int b = 0; b < used; b++) { |
| 82 |
int v = (int) array[b] & 0xff; |
| 83 |
sb.append('%'); |
| 84 |
sb.append(hex.charAt(v / 16)); |
| 85 |
sb.append(hex.charAt(v % 16)); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public static void checkPercentEncoding(String uri) throws XPathException { |
| 99 |
final String hexDigits = "0123456789abcdefABCDEF"; |
| 100 |
for (int i = 0; i < uri.length(); ) { |
| 101 |
char c = uri.charAt(i); |
| 102 |
@SuppressWarnings("MismatchedReadAndWriteOfArray") byte[] bytes; |
| 103 |
|
| 104 |
int expectedOctets; |
| 105 |
if (c == '%') { |
| 106 |
if (i + 2 >= uri.length()) { |
| 107 |
throw new XPathException("% sign in URI must be followed by two hex digits" + |
| 108 |
Err.wrap(uri)); |
| 109 |
} |
| 110 |
int h1 = hexDigits.indexOf(uri.charAt(i + 1)); |
| 111 |
if (h1 > 15) { |
| 112 |
h1 -= 6; |
| 113 |
} |
| 114 |
|
| 115 |
int h2 = hexDigits.indexOf(uri.charAt(i + 2)); |
| 116 |
if (h2 > 15) { |
| 117 |
h2 -= 6; |
| 118 |
} |
| 119 |
if (h1 >= 0 && h2 >= 0) { |
| 120 |
int b = h1 << 4 | h2; |
| 121 |
expectedOctets = UTF8RepresentationLength[h1]; |
| 122 |
if (expectedOctets == -1) { |
| 123 |
throw new XPathException("First %-encoded octet in URI is not valid as the start of a UTF-8 " + |
| 124 |
"character: first two bits must not be '10'" + |
| 125 |
Err.wrap(uri)); |
| 126 |
} |
| 127 |
bytes = new byte[expectedOctets]; |
| 128 |
bytes[0] = (byte) b; |
| 129 |
i += 3; |
| 130 |
for (int q = 1; q < expectedOctets; q++) { |
| 131 |
if (i + 2 > uri.length() || uri.charAt(i) != '%') { |
| 132 |
throw new XPathException("Incomplete %-encoded UTF-8 octet sequence in URI " + |
| 133 |
Err.wrap(uri)); |
| 134 |
} |
| 135 |
h1 = hexDigits.indexOf(uri.charAt(i + 1)); |
| 136 |
if (h1 > 15) { |
| 137 |
h1 -= 6; |
| 138 |
} |
| 139 |
|
| 140 |
h2 = hexDigits.indexOf(uri.charAt(i + 2)); |
| 141 |
if (h2 > 15) { |
| 142 |
h2 -= 6; |
| 143 |
} |
| 144 |
if (h1 < 0 || h2 < 0) { |
| 145 |
throw new XPathException("Invalid %-encoded UTF-8 octet sequence in URI" + |
| 146 |
Err.wrap(uri)); |
| 147 |
} |
| 148 |
if (UTF8RepresentationLength[h1] != -1) { |
| 149 |
throw new XPathException("In a URI, a %-encoded UTF-8 octet after the first " + |
| 150 |
"must have '10' as the first two bits" + |
| 151 |
Err.wrap(uri)); |
| 152 |
} |
| 153 |
b = h1 << 4 | h2; |
| 154 |
bytes[q] = (byte) b; |
| 155 |
i += 3; |
| 156 |
} |
| 157 |
} else { |
| 158 |
throw new XPathException("% sign in URI must be followed by two hex digits" + |
| 159 |
Err.wrap(uri)); |
| 160 |
} |
| 161 |
} else { |
| 162 |
i++; |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
private static int[] UTF8RepresentationLength = {1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 2, 2, 3, 4}; |
| 171 |
} |
| 172 |
|