/** * Creates a source from literal text, but which acts as a file and can be retrieved by name * (unlike other literal sources); intended for testing. * * @param chars textual source code * @param pseudoFileName string to use for indexing/lookup * @return a newly created, source representation, canonical with respect to its name */ public static Source asPseudoFile(CharSequence chars, String pseudoFileName) { final Source source = new LiteralSource(pseudoFileName, chars.toString()); filePathToSource.put(pseudoFileName, new WeakReference<>(source)); notifyNewSource(source).tagAs(Tags.FROM_LITERAL); return source; }
/** * Creates a non-canonical source from literal text. If an already created literal source must * be retrievable by name, use {@link #asPseudoFile(CharSequence, String)}. * * @param chars textual source code * @param description a note about the origin, for error messages and debugging * @return a newly created, non-indexed source representation */ public static Source fromText(CharSequence chars, String description) { assert chars != null; final LiteralSource source = new LiteralSource(description, chars.toString()); notifyNewSource(source).tagAs(Tags.FROM_LITERAL); return source; }
/** * Gets the canonical representation of a source file, whose contents have already been read and * need not be read again. It is confirmed that the file resolves to a file name, so it can be * indexed by canonical path. It is not confirmed that the text supplied agrees with the file's * contents or even whether the file is readable. * * @param chars textual source code already read from the file * @param fileName * @return canonical representation of the file's contents. * @throws IOException if the file cannot be found */ public static Source fromFileName(CharSequence chars, String fileName) throws IOException { final WeakReference<Source> nameRef = filePathToSource.get(fileName); Source source = nameRef == null ? null : nameRef.get(); if (source == null) { final File file = new File(fileName); // We are going to trust that the fileName is readable. final String path = file.getCanonicalPath(); final WeakReference<Source> pathRef = filePathToSource.get(path); source = pathRef == null ? null : pathRef.get(); if (source == null) { source = new FileSource(file, fileName, path, chars); filePathToSource.put(path, new WeakReference<>(source)); } } notifyNewSource(source).tagAs(Tags.FROM_FILE); return source; }
/** * Creates a source whose contents will be read immediately from a URL and cached. * * @param url * @param description identifies the origin, possibly useful for debugging * @return a newly created, non-indexed source representation * @throws IOException if reading fails */ public static Source fromURL(URL url, String description) throws IOException { final URLSource source = URLSource.get(url, description); notifyNewSource(source).tagAs(Tags.FROM_URL); return source; }
source.reset(); notifyNewSource(source).tagAs(Tags.FROM_FILE); return source;
/** * Creates a source from raw bytes. This can be used if the encoding of strings in your language * is not compatible with Java strings, or if your parser returns byte indices instead of * character indices. The returned source is then indexed by byte, not by character. Offsets are * relative to byteIndex. * * @param bytes the raw bytes of the source * @param byteIndex where the string starts in the byte array * @param length the length of the string in the byte array * @param description a note about the origin, possibly useful for debugging * @param decoder how to decode the bytes into Java strings * @return a newly created, non-indexed source representation */ public static Source fromBytes(byte[] bytes, int byteIndex, int length, String description, BytesDecoder decoder) { final BytesSource source = new BytesSource(description, bytes, byteIndex, length, decoder); notifyNewSource(source).tagAs(Tags.FROM_BYTES); return source; }
/** * Creates a source whose contents will be read immediately and cached. * * @param reader * @param description a note about the origin, possibly useful for debugging * @return a newly created, non-indexed source representation * @throws IOException if reading fails */ public static Source fromReader(Reader reader, String description) throws IOException { final LiteralSource source = new LiteralSource(description, read(reader)); notifyNewSource(source).tagAs(Tags.FROM_READER); return source; }