if (ch(start[0]) == 0xFE && ch(start[1]) == 0xFF) { return "UTF-16"; } else if (ch(start[0]) == 0xFF && ch(start[1]) == 0xFE) { return "UTF-16LE"; if (ch(start[0]) == 0xEF && ch(start[1]) == 0xBB && ch(start[2]) == 0xBF) { return "UTF-8"; String tok = readToken(start, i, read); if (Whitespace.trim(tok).equals("xquery")) { i += tok.length(); return "UTF-8"; tok = readToken(start, i, read); if (Whitespace.trim(tok).equals("encoding")) { return "UTF-8"; tok = readToken(start, i, read); i += tok.length(); tok = readToken(start, i, read); if (Whitespace.trim(tok).equals("encoding")) { i += tok.length(); tok = Whitespace.trim(readToken(start, i, read)); if (tok.startsWith("\"") && tok.endsWith("\"") && tok.length() > 2) { return tok.substring(1, tok.length() - 1);
/** * Read an input stream non-destructively to determine the encoding from the Query Prolog * * @param is the input stream: this must satisfy the precondition is.markSupported() = true. * @return the encoding to be used: defaults to UTF-8 if no encoding was specified explicitly * in the query prolog * @throws XPathException if the input stream cannot be read */ public static String readEncoding(/*@NotNull*/ InputStream is) throws XPathException { try { if (!is.markSupported()) { throw new IllegalArgumentException("InputStream must have markSupported() = true"); } is.mark(100); byte[] start = new byte[100]; int read = is.read(start, 0, 100); if (read == -1) { throw new XPathException("Query source file is empty"); } is.reset(); return inferEncoding(start, read); } catch (IOException e) { throw new XPathException("Failed to read query source file", e); } }
/** * Read a query module given a StreamSource * * @param ss the supplied StreamSource. This must contain a non-null systemID which defines the base * URI of the query module, and either an InputStream or a Reader containing the query text. In the * case of an InputStream the method attempts to infer the encoding; in the case of a Reader, this has * already been done, and the encoding specified within the query itself is ignored. * <p>The method reads from the InputStream or Reader contained in the StreamSource up to the end * of file unless a fatal error occurs. It does not close the InputStream or Reader; this is the caller's * responsibility.</p> * @param charChecker this checks XML characters against either the XML 1.0 or XML 1.1 rules * @return the text of the query */ public static String readSourceQuery(/*@NotNull*/ StreamSource ss, IntPredicate charChecker) throws XPathException { CharSequence queryText; if (ss.getInputStream() != null) { InputStream is = ss.getInputStream(); if (!is.markSupported()) { is = new BufferedInputStream(is); } String encoding = readEncoding(is); queryText = readInputStream(is, encoding, charChecker); } else if (ss.getReader() != null) { queryText = readQueryFromReader(ss.getReader(), charChecker); } else { throw new XPathException("Module URI Resolver must supply either an InputStream or a Reader"); } return queryText.toString(); }
/** * Read a query from an InputStream. The method checks that all characters are valid XML * characters, and also performs normalization of line endings. * @param is the input stream * @param encoding the encoding, or null if the encoding is unknown * @param nameChecker the nameChecker to be used for checking characters * @return the content of the InputStream as a string */ public static String readInputStream(InputStream is, String encoding, NameChecker nameChecker) throws XPathException { if (encoding == null) { if (!is.markSupported()) { is = new BufferedInputStream(is); } encoding = readEncoding(is); } try { Reader reader = new BufferedReader(new InputStreamReader(is, encoding)); return readQueryFromReader(reader, nameChecker); } catch (UnsupportedEncodingException encErr) { throw new XPathException("Unknown encoding " + Err.wrap(encoding), encErr); } }
quit("Module URI Resolver must return a single StreamSource", 2); String queryText = QueryReader.readSourceQuery((StreamSource)sources[0], config.getNameChecker()); exp = staticEnv.compileQuery(queryText); } else {
/** * Prepare an XQuery query for subsequent evaluation. The Query is supplied * in the form of a InputStream, with an optional encoding. If the encoding is not specified, * the query parser attempts to obtain the encoding by inspecting the input stream: it looks specifically * for a byte order mark, and for the encoding option in the version declaration of an XQuery prolog. * The encoding defaults to UTF-8. * The base URI of the query is taken from the static context, * and defaults to the current working directory. * * @param source An InputStream giving access to the text of the XQuery query to be compiled, as a stream * of octets * @param encoding The encoding used to translate characters to octets in the query source. The parameter * may be null: in this case the query parser attempts to infer the encoding by inspecting the source, * and if that fails, it assumes UTF-8 encoding * @return an XPathExpression object representing the prepared expression. * @throws net.sf.saxon.trans.XPathException * if the syntax of the expression is wrong, or if it references namespaces, * variables, or functions that have not been declared, or any other static error is reported. * @since 8.5 */ /*@Nullable*/ public synchronized XQueryExpression compileQuery(/*@NotNull*/ InputStream source, /*@Nullable*/ String encoding) throws XPathException { String query = QueryReader.readInputStream(source, encoding, config.getValidCharacterChecker()); return compileQuery(query); }
while (p < len && " \n\r\t".indexOf(ch(in[p])) >= 0) { p++; if (ch(in[p]) == '"') { p++; while (p < len && ch(in[p]) != '"') { p++; } else if (ch(in[p]) == '\'') { p++; while (p < len && ch(in[p]) != '\'') { p++; while (p < len && " \n\r\t".indexOf(ch(in[p])) < 0) { p++; sb.append((char) ch(in[c]));
/** * Read a query from an InputStream. The method checks that all characters are valid XML * characters, and also performs normalization of line endings. * @param is the input stream * @param encoding the encoding, or null if the encoding is unknown * @param nameChecker the nameChecker to be used for checking characters * @return the content of the InputStream as a string */ public static String readInputStream(InputStream is, String encoding, NameChecker nameChecker) throws XPathException { if (encoding == null) { if (!is.markSupported()) { is = new BufferedInputStream(is); } encoding = readEncoding(is); } try { Reader reader = new BufferedReader(new InputStreamReader(is, encoding)); return readQueryFromReader(reader, nameChecker); } catch (UnsupportedEncodingException encErr) { throw new XPathException("Unknown encoding " + Err.wrap(encoding), encErr); } }
quit("Module URI Resolver must return a single StreamSource", 2); String queryText = QueryReader.readSourceQuery((StreamSource)sources[0], config.getNameChecker()); exp = staticEnv.compileQuery(queryText); break;
/** * Prepare an XQuery query for subsequent evaluation. The Query is supplied * in the form of a InputStream, with an optional encoding. If the encoding is not specified, * the query parser attempts to obtain the encoding by inspecting the input stream: it looks specifically * for a byte order mark, and for the encoding option in the version declaration of an XQuery prolog. * The encoding defaults to UTF-8. * The base URI of the query is taken from the static context, * and defaults to the current working directory. * * @param source An InputStream giving access to the text of the XQuery query to be compiled, as a stream * of octets * @param encoding The encoding used to translate characters to octets in the query source. The parameter * may be null: in this case the query parser attempts to infer the encoding by inspecting the source, * and if that fails, it assumes UTF-8 encoding * @return an XPathExpression object representing the prepared expression. * @throws net.sf.saxon.trans.XPathException * if the syntax of the expression is wrong, or if it references namespaces, * variables, or functions that have not been declared, or any other static error is reported. * @since 8.5 */ /*@Nullable*/ public synchronized XQueryExpression compileQuery(/*@NotNull*/ InputStream source, /*@Nullable*/ String encoding) throws XPathException { String query = QueryReader.readInputStream(source, encoding, config.getValidCharacterChecker()); return compileQuery(query); }
/** * Read a query module given a StreamSource * @param ss the supplied StreamSource. This must contain a non-null systemID which defines the base * URI of the query module, and either an InputStream or a Reader containing the query text. In the * case of an InputStream the method attempts to infer the encoding; in the case of a Reader, this has * already been done, and the encoding specified within the query itself is ignored. * <p>The method reads from the InputStream or Reader contained in the StreamSource up to the end * of file unless a fatal error occurs. It does not close the InputStream or Reader; this is the caller's * responsibility.</p> * @param nameChecker this checks XML names against either the XML 1.0 or XML 1.1 rules * @return the text of the query */ public static String readSourceQuery(StreamSource ss, NameChecker nameChecker) throws XPathException { CharSequence queryText; if (ss.getInputStream() != null) { InputStream is = ss.getInputStream(); if (!is.markSupported()) { is = new BufferedInputStream(is); } String encoding = readEncoding(is); queryText = readInputStream(is, encoding, nameChecker); } else if (ss.getReader() != null) { queryText = readQueryFromReader(ss.getReader(), nameChecker); } else { throw new XPathException("Module URI Resolver must supply either an InputSource or a Reader"); } return queryText.toString(); }
/** * Read a query from an InputStream. The method checks that all characters are valid XML * characters, and also performs normalization of line endings. * * @param is the input stream * @param encoding the encoding, or null if the encoding is unknown * @param nameChecker the predicate to be used for checking characters * @return the content of the InputStream as a string */ public static String readInputStream(InputStream is, String encoding, IntPredicate nameChecker) throws XPathException { if (encoding == null) { if (!is.markSupported()) { is = new BufferedInputStream(is); } encoding = readEncoding(is); } try { Reader reader = new BufferedReader(new InputStreamReader(is, encoding)); return readQueryFromReader(reader, nameChecker); } catch (UnsupportedEncodingException encErr) { XPathException err = new XPathException("Unknown encoding " + Err.wrap(encoding), encErr); err.setErrorCode("XQST0087"); throw err; } }
if (ch(start[0]) == 0xFE && ch(start[1]) == 0xFF) { return "UTF-16"; } else if (ch(start[0]) == 0xFF && ch(start[1]) == 0xFE) { return "UTF-16LE"; if (ch(start[0]) == 0xEF && ch(start[1]) == 0xBB && ch(start[2]) == 0xBF) { return "UTF-8"; String tok = readToken(start, i, read); if (Whitespace.trim(tok).equals("xquery")) { i += tok.length(); tok = readToken(start, i, read); if (Whitespace.trim(tok).equals("version")) { i += tok.length(); tok = readToken(start, i, read); if (tok == null) { return "UTF-8"; tok = readToken(start, i, read); if (Whitespace.trim(tok).equals("encoding")) { i += tok.length(); tok = Whitespace.trim(readToken(start, i, read)); if (tok.startsWith("\"") && tok.endsWith("\"") && tok.length()>2) { return tok.substring(1, tok.length()-1);
String queryText = QueryReader.readSourceQuery((StreamSource) sources[0], config.getValidCharacterChecker()); exp = compiler.compile(queryText); } catch (XPathException e) {
/** * Prepare an XQuery query for subsequent evaluation. The Query is supplied * in the form of a InputStream, with an optional encoding. If the encoding is not specified, * the query parser attempts to obtain the encoding by inspecting the input stream: it looks specifically * for a byte order mark, and for the encoding option in the version declaration of an XQuery prolog. * The encoding defaults to UTF-8. * The base URI of the query is taken from the static context, * and defaults to the current working directory. * * @param source An InputStream giving access to the text of the XQuery query to be compiled, as a stream * of octets * @param encoding The encoding used to translate characters to octets in the query source. The parameter * may be null: in this case the query parser attempts to infer the encoding by inspecting the source, * and if that fails, it assumes UTF-8 encoding * @return an XPathExpression object representing the prepared expression. * @throws net.sf.saxon.trans.XPathException * if the syntax of the expression is wrong, or if it references namespaces, * variables, or functions that have not been declared, or any other static error is reported. * @throws java.io.IOException if a failure occurs reading the supplied input. * @since 8.5 */ public XQueryExpression compileQuery(InputStream source, String encoding) throws XPathException, IOException { String query = QueryReader.readInputStream(source, encoding, config.getNameChecker()); return compileQuery(query); }
/** * Read an input stream non-destructively to determine the encoding from the Query Prolog * @param is the input stream: this must satisfy the precondition is.markSupported() = true. * @return the encoding to be used: defaults to UTF-8 if no encoding was specified explicitly * in the query prolog * @throws XPathException if the input stream cannot be read */ public static String readEncoding(InputStream is) throws XPathException { try { if (!is.markSupported()) { throw new IllegalArgumentException("InputStream must have markSupported() = true"); } is.mark(100); byte[] start = new byte[100]; int read = is.read(start, 0, 100); if (read == -1) { throw new XPathException("Query source file is empty"); } is.reset(); return inferEncoding(start, read); } catch (IOException e) { throw new XPathException("Failed to read query source file", e); } }
/** * Read a query module given a StreamSource * @param ss the supplied StreamSource. This must contain a non-null systemID which defines the base * URI of the query module, and either an InputStream or a Reader containing the query text. In the * case of an InputStream the method attempts to infer the encoding; in the case of a Reader, this has * already been done, and the encoding specified within the query itself is ignored. * <p>The method reads from the InputStream or Reader contained in the StreamSource up to the end * of file unless a fatal error occurs. It does not close the InputStream or Reader; this is the caller's * responsibility.</p> * @param nameChecker this checks XML names against either the XML 1.0 or XML 1.1 rules * @return the text of the query */ public static String readSourceQuery(StreamSource ss, NameChecker nameChecker) throws XPathException { CharSequence queryText; if (ss.getInputStream() != null) { InputStream is = ss.getInputStream(); if (!is.markSupported()) { is = new BufferedInputStream(is); } String encoding = readEncoding(is); queryText = readInputStream(is, encoding, nameChecker); } else if (ss.getReader() != null) { queryText = readQueryFromReader(ss.getReader(), nameChecker); } else { throw new XPathException("Module URI Resolver must supply either an InputSource or a Reader"); } return queryText.toString(); }
/** * Read a query from an InputStream. The method checks that all characters are valid XML * characters, and also performs normalization of line endings. * * @param is the input stream * @param encoding the encoding, or null if the encoding is unknown * @param nameChecker the predicate to be used for checking characters * @return the content of the InputStream as a string */ public static String readInputStream(InputStream is, String encoding, IntPredicate nameChecker) throws XPathException { if (encoding == null) { if (!is.markSupported()) { is = new BufferedInputStream(is); } encoding = readEncoding(is); } try { Reader reader = new BufferedReader(new InputStreamReader(is, encoding)); return readQueryFromReader(reader, nameChecker); } catch (UnsupportedEncodingException encErr) { XPathException err = new XPathException("Unknown encoding " + Err.wrap(encoding), encErr); err.setErrorCode("XQST0087"); throw err; } }