Example usage for jdk.nashorn.internal.runtime.options Options getFiles

List of usage examples for jdk.nashorn.internal.runtime.options Options getFiles

Introduction

In this page you can find the example usage for jdk.nashorn.internal.runtime.options Options getFiles.

Prototype

public List<String> getFiles() 

Source Link

Document

Return the JavaScript files passed to the program

Usage

From source file:z.zee.Z.java

License:Open Source License

/**
 * Make a new Nashorn Context to compile and/or run JavaScript files.
 *
 * @param in input stream for Z//from  w w  w .jav a2  s . c  o  m
 * @param out output stream for Z
 * @param err error stream for Z
 * @param args arguments to Z
 *
 * @return null if there are problems with option parsing.
 */
@SuppressWarnings("resource")
private static Context makeContext(final InputStream in, final OutputStream out, final OutputStream err,
        final String[] args) {
    final PrintStream pout = out instanceof PrintStream ? (PrintStream) out : new PrintStream(out);
    final PrintStream perr = err instanceof PrintStream ? (PrintStream) err : new PrintStream(err);
    final PrintWriter wout = new PrintWriter(pout, true);
    final PrintWriter werr = new PrintWriter(perr, true);

    // Set up error handler.
    final ErrorManager errors = new ErrorManager(werr);
    // Set up options.
    final Options options = new Options("nashorn", werr);

    // parse options
    if (args != null) {
        try {
            options.process(args);
        } catch (final IllegalArgumentException e) {
            werr.println(bundle.getString("shell.usage"));
            options.displayHelp(e);
            return null;
        }
    }

    // detect scripting mode by any source's first character being '#'
    scripting = options.getBoolean("scripting");
    if (!scripting) {
        for (final String fileName : options.getFiles()) {
            final File firstFile = new File(fileName);
            if (firstFile.isFile()) {
                try (final FileReader fr = new FileReader(firstFile)) {
                    final int firstChar = fr.read();
                    // starts with '#
                    if (firstChar == '#') {
                        options.set("scripting", true);
                        break;
                    }
                } catch (final IOException e) {
                    // ignore this. File IO errors will be reported later anyway
                }
            }
        }
    }

    return new Context(options, errors, wout, werr, Thread.currentThread().getContextClassLoader());
}