| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.query; |
| 9 |
|
| 10 |
import net.sf.saxon.functions.UnparsedTextFunction; |
| 11 |
import net.sf.saxon.trans.Err; |
| 12 |
import net.sf.saxon.trans.XPathException; |
| 13 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 14 |
import net.sf.saxon.value.Whitespace; |
| 15 |
|
| 16 |
import javax.xml.transform.stream.StreamSource; |
| 17 |
import java.io.*; |
| 18 |
import java.util.function.IntPredicate; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class QueryReader { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
private QueryReader() { |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public static String readSourceQuery( StreamSource ss, IntPredicate charChecker) throws XPathException { |
| 48 |
CharSequence queryText; |
| 49 |
if (ss.getInputStream() != null) { |
| 50 |
InputStream is = ss.getInputStream(); |
| 51 |
if (!is.markSupported()) { |
| 52 |
is = new BufferedInputStream(is); |
| 53 |
} |
| 54 |
String encoding = readEncoding(is); |
| 55 |
queryText = readInputStream(is, encoding, charChecker); |
| 56 |
} else if (ss.getReader() != null) { |
| 57 |
queryText = readQueryFromReader(ss.getReader(), charChecker); |
| 58 |
} else { |
| 59 |
throw new XPathException("Module URI Resolver must supply either an InputStream or a Reader"); |
| 60 |
} |
| 61 |
return queryText.toString(); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static String readEncoding( InputStream is) throws XPathException { |
| 74 |
try { |
| 75 |
if (!is.markSupported()) { |
| 76 |
throw new IllegalArgumentException("InputStream must have markSupported() = true"); |
| 77 |
} |
| 78 |
is.mark(100); |
| 79 |
byte[] start = new byte[100]; |
| 80 |
int read = is.read(start, 0, 100); |
| 81 |
if (read == -1) { |
| 82 |
throw new XPathException("Query source file is empty"); |
| 83 |
} |
| 84 |
is.reset(); |
| 85 |
return inferEncoding(start, read); |
| 86 |
} catch (IOException e) { |
| 87 |
throw new XPathException("Failed to read query source file", e); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public static String readInputStream(InputStream is, String encoding, IntPredicate nameChecker) throws XPathException { |
| 102 |
if (encoding == null) { |
| 103 |
if (!is.markSupported()) { |
| 104 |
is = new BufferedInputStream(is); |
| 105 |
} |
| 106 |
encoding = readEncoding(is); |
| 107 |
} |
| 108 |
try { |
| 109 |
Reader reader = new BufferedReader(new InputStreamReader(is, encoding)); |
| 110 |
return readQueryFromReader(reader, nameChecker); |
| 111 |
} catch (UnsupportedEncodingException encErr) { |
| 112 |
XPathException err = new XPathException("Unknown encoding " + Err.wrap(encoding), encErr); |
| 113 |
err.setErrorCode("XQST0087"); |
| 114 |
throw err; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
private static String readQueryFromReader(Reader reader, IntPredicate charChecker) throws XPathException { |
| 129 |
try { |
| 130 |
CharSequence content = UnparsedTextFunction.readFile(charChecker, reader); |
| 131 |
return content.toString(); |
| 132 |
} catch (XPathException err) { |
| 133 |
err.setErrorCode("XPST0003"); |
| 134 |
err.setIsStaticError(true); |
| 135 |
throw err; |
| 136 |
} catch (IOException ioErr) { |
| 137 |
throw new XPathException("Failed to read supplied query file", ioErr); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
private static String inferEncoding(byte[] start, int read) throws XPathException { |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
if (read >= 2) { |
| 159 |
if (ch(start[0]) == 0xFE && ch(start[1]) == 0xFF) { |
| 160 |
return "UTF-16"; |
| 161 |
} else if (ch(start[0]) == 0xFF && ch(start[1]) == 0xFE) { |
| 162 |
return "UTF-16LE"; |
| 163 |
} |
| 164 |
} |
| 165 |
if (read >= 3) { |
| 166 |
if (ch(start[0]) == 0xEF && ch(start[1]) == 0xBB && ch(start[2]) == 0xBF) { |
| 167 |
return "UTF-8"; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
if (read >= 8 && start[0] == 0 && start[2] == 0 && start[4] == 0 && start[6] == 0) { |
| 173 |
return "UTF-16"; |
| 174 |
} |
| 175 |
if (read >= 8 && start[1] == 0 && start[3] == 0 && start[5] == 0 && start[7] == 0) { |
| 176 |
return "UTF-16LE"; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
int i = 0; |
| 184 |
String tok = readToken(start, i, read); |
| 185 |
if (Whitespace.trim(tok).equals("xquery")) { |
| 186 |
i += tok.length(); |
| 187 |
} else { |
| 188 |
return "UTF-8"; |
| 189 |
} |
| 190 |
tok = readToken(start, i, read); |
| 191 |
if (Whitespace.trim(tok).equals("encoding")) { |
| 192 |
|
| 193 |
i += tok.length(); |
| 194 |
} else { |
| 195 |
if (Whitespace.trim(tok).equals("version")) { |
| 196 |
i += tok.length(); |
| 197 |
} else { |
| 198 |
return "UTF-8"; |
| 199 |
} |
| 200 |
tok = readToken(start, i, read); |
| 201 |
i += tok.length(); |
| 202 |
tok = readToken(start, i, read); |
| 203 |
if (Whitespace.trim(tok).equals("encoding")) { |
| 204 |
i += tok.length(); |
| 205 |
} else { |
| 206 |
return "UTF-8"; |
| 207 |
} |
| 208 |
} |
| 209 |
tok = Whitespace.trim(readToken(start, i, read)); |
| 210 |
if (tok.startsWith("\"") && tok.endsWith("\"") && tok.length() > 2) { |
| 211 |
return tok.substring(1, tok.length() - 1); |
| 212 |
} else if (tok.startsWith("'") && tok.endsWith("'") && tok.length() > 2) { |
| 213 |
return tok.substring(1, tok.length() - 1); |
| 214 |
} else { |
| 215 |
throw new XPathException("Unrecognized encoding " + Err.wrap(tok) + " in query prolog"); |
| 216 |
} |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
private static String readToken(byte[] in, int i, int len) { |
| 232 |
int p = i; |
| 233 |
while (p < len && " \n\r\t".indexOf(ch(in[p])) >= 0) { |
| 234 |
p++; |
| 235 |
} |
| 236 |
if (ch(in[p]) == '"') { |
| 237 |
p++; |
| 238 |
while (p < len && ch(in[p]) != '"') { |
| 239 |
p++; |
| 240 |
} |
| 241 |
} else if (ch(in[p]) == '\'') { |
| 242 |
p++; |
| 243 |
while (p < len && ch(in[p]) != '\'') { |
| 244 |
p++; |
| 245 |
} |
| 246 |
} else { |
| 247 |
while (p < len && " \n\r\t".indexOf(ch(in[p])) < 0) { |
| 248 |
p++; |
| 249 |
} |
| 250 |
} |
| 251 |
if (p >= len) { |
| 252 |
return new String(in, i, len - i); |
| 253 |
} |
| 254 |
FastStringBuffer sb = new FastStringBuffer(p - i + 1); |
| 255 |
for (int c = i; c <= p; c++) { |
| 256 |
sb.append((char) ch(in[c])); |
| 257 |
} |
| 258 |
return sb.toString(); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
private static int ch(byte b) { |
| 269 |
return (int) b & 0xff; |
| 270 |
} |
| 271 |
|
| 272 |
} |
| 273 |
|