* @throws XPathException if the file cannot be read or contains illegal characters
*/
private static String readQueryFromReader(Reader reader, NameChecker nameChecker) throws XPathException {
try {
FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.LARGE);
char[] buffer = new char[2048];
boolean first = true;
int actual;
int line = 1; // track line/column position for reporting bad characters
int column = 1;
while (true) {
actual = reader.read(buffer, 0, 2048);
if (actual < 0) {
break;
}
for (int c=0; c<actual;) {
int ch32 = buffer[c++];
if (ch32 == '\n') {
line++;
column = 0;
}
column++;
if (UTF16CharacterSet.isHighSurrogate(ch32)) {
char low = buffer[c++];
ch32 = UTF16CharacterSet.combinePair((char)ch32, low);
}
if (!nameChecker.isValidChar(ch32)) {
XPathException err = new XPathException("The query file contains a character illegal in XML " +
nameChecker.getXMLVersion() +
" (line=" + line +
" column=" + column +
" value=x" + Integer.toHexString(ch32) + ')');
err.setErrorCode("XPST0003");
err.setIsStaticError(true);
throw err;
}
}
if (first) {
first = false;
if (buffer[0]=='\ufeff') {
sb.append(buffer, 1, actual-1);
} else {
sb.append(buffer, 0, actual);
}
} else {
sb.append(buffer, 0, actual);
}
}
return sb.condense().toString();
} catch (IOException ioErr) {
throw new XPathException("Failed to read input file", ioErr);
}
}