//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Copyright (c) 2018 Saxonica Limited. // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// package net.sf.saxon.query; import net.sf.saxon.functions.UnparsedTextFunction; import net.sf.saxon.trans.Err; import net.sf.saxon.trans.XPathException; import net.sf.saxon.tree.util.FastStringBuffer; import net.sf.saxon.value.Whitespace; import javax.xml.transform.stream.StreamSource; import java.io.*; import java.util.function.IntPredicate; /** * This class contains static methods used to read a query as a byte stream, infer the encoding if * necessary, and return the text of the query as a string; also methods to import functions and variables * from one module into another, and check their consistency. */ public class QueryReader { /** * The class is never instantiated */ private QueryReader() { } /** * 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. *
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.
* @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 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 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; } } /** * Read a query from a Reader. The method checks that all characters are valid XML * characters. * * @param reader The Reader supplying the input * @param charChecker used to check characters are valid XML characters * @return the text of the query module, as a string * @throws XPathException if the file cannot be read or contains illegal characters */ private static String readQueryFromReader(Reader reader, IntPredicate charChecker) throws XPathException { try { CharSequence content = UnparsedTextFunction.readFile(charChecker, reader); return content.toString(); } catch (XPathException err) { err.setErrorCode("XPST0003"); err.setIsStaticError(true); throw err; } catch (IOException ioErr) { throw new XPathException("Failed to read supplied query file", ioErr); } } /** * Attempt to infer the encoding of a file by reading its byte order mark and if necessary * the encoding declaration in the query prolog * * @param start the bytes appearing at the start of the file * @param read the number of bytes supplied * @return the inferred encoding * @throws XPathException if, for example, an explicit encoding is specified but it is unsupported */ private static String inferEncoding(byte[] start, int read) throws XPathException { // Debugging code // StringBuffer sb = new StringBuffer(read*5); // for (int i=0; i