⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 groovymain.java

📁 大名鼎鼎的java动态脚本语言。已经通过了sun的认证
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        main.isScriptFile = !line.hasOption('e');        main.debug = line.hasOption('d');        main.conf.setDebug(main.debug);        main.processFiles = line.hasOption('p') || line.hasOption('n');        main.autoOutput = line.hasOption('p');        main.editFiles = line.hasOption('i');        if (main.editFiles) {            main.backupExtension = line.getOptionValue('i');        }        if (main.isScriptFile) {            if (args.isEmpty())                throw new ParseException("neither -e or filename provided");            main.script = (String) args.remove(0);            if (main.script.endsWith(".java"))                throw new ParseException("error: cannot compile file with .java extension: " + main.script);        } else {            main.script = line.getOptionValue('e');        }        main.processSockets = line.hasOption('l');        if (main.processSockets) {            String p = line.getOptionValue('l', "1960"); // default port to listen to            main.port = new Integer(p).intValue();        }        main.args = args;        return main.run();    }    /**     * Run the script.     */    private boolean run() {        try {            if (processSockets) {                processSockets();            } else if (processFiles) {                processFiles();            } else {                processOnce();            }            return true;        } catch (CompilationFailedException e) {            System.err.println(e);            return false;        } catch (Throwable e) {            if (e instanceof InvokerInvocationException) {                InvokerInvocationException iie = (InvokerInvocationException) e;                e = iie.getCause();            }            System.err.println("Caught: " + e);            if (debug) {                e.printStackTrace();            } else {                StackTraceElement[] stackTrace = e.getStackTrace();                for (int i = 0; i < stackTrace.length; i++) {                    StackTraceElement element = stackTrace[i];                    String fileName = element.getFileName();                    if (fileName!=null && !fileName.endsWith(".java")) {                        System.err.println("\tat " + element);                    }                }            }            return false;        }    }    /**     * Process Sockets.     */    private void processSockets() throws CompilationFailedException, IOException {        GroovyShell groovy = new GroovyShell(conf);        //check the script is currently valid before starting a server against the script        if (isScriptFile) {            groovy.parse(new FileInputStream(huntForTheScriptFile(script)));        } else {            groovy.parse(script);        }        new GroovySocketServer(groovy, isScriptFile, script, autoOutput, port);    }    /**     * Hunt for the script file, doesn't bother if it is named precisely.     *     * Tries in this order:     * - actual supplied name     * - name.groovy     * - name.gvy     * - name.gy     * - name.gsh     */    public File huntForTheScriptFile(String scriptFileName) {        File scriptFile = new File(scriptFileName);        String[] standardExtensions = {".groovy",".gvy",".gy",".gsh"};        int i = 0;        while (i < standardExtensions.length && !scriptFile.exists()) {            scriptFile = new File(scriptFileName + standardExtensions[i]);            i++;        }        // if we still haven't found the file, point back to the originally specified filename        if (!scriptFile.exists()) {            scriptFile = new File(scriptFileName);        }        return scriptFile;    }    /**     * Process the input files.     */    private void processFiles() throws CompilationFailedException, IOException {        GroovyShell groovy = new GroovyShell(conf);        Script s = null;        if (isScriptFile) {            s = groovy.parse(huntForTheScriptFile(script));        } else {            s = groovy.parse(script, "main");        }        if (args.isEmpty()) {            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));            PrintWriter writer = new PrintWriter(System.out);            try {                processReader(s, reader, writer);            } finally {                reader.close();                writer.close();            }        } else {            Iterator i = args.iterator();            while (i.hasNext()) {                String filename = (String) i.next();                File file = huntForTheScriptFile(filename);                processFile(s, file);            }        }    }    /**     * Process a single input file.     *     * @param s    the script to execute.     * @param file the input file.     */    private void processFile(Script s, File file) throws IOException {        if (!file.exists())            throw new FileNotFoundException(file.getName());        if (!editFiles) {            BufferedReader reader = new BufferedReader(new FileReader(file));            try {                PrintWriter writer = new PrintWriter(System.out);                processReader(s, reader, writer);                writer.flush();            } finally {                reader.close();            }        } else {            File backup = null;            if (backupExtension == null) {                backup = File.createTempFile("groovy_", ".tmp");                backup.deleteOnExit();            } else {                backup = new File(file.getPath() + backupExtension);            }            backup.delete();            if (!file.renameTo(backup))                throw new IOException("unable to rename " + file + " to " + backup);            BufferedReader reader = new BufferedReader(new FileReader(backup));            try {                PrintWriter writer = new PrintWriter(new FileWriter(file));                try {                    processReader(s, reader, writer);                } finally {                    writer.close();                }            } finally {                reader.close();            }        }    }    /**     * Process a script against a single input file.     *     * @param s      script to execute.     * @param reader input file.     * @param pw     output sink.     */    private void processReader(Script s, BufferedReader reader, PrintWriter pw) throws IOException {        String line = null;        s.setProperty("out", pw);        while ((line = reader.readLine()) != null) {            s.setProperty("line", line);            Object o = s.run();            if (autoOutput) {                pw.println(o);            }        }    }        private static ClassLoader getLoader(ClassLoader cl) {        if (cl!=null) return cl;        cl = Thread.currentThread().getContextClassLoader();        if (cl!=null) return cl;        cl = GroovyMain.class.getClassLoader();        if (cl!=null) return cl;        return null;    }        /**     * Process the standard, single script with args.     */    private void processOnce() throws CompilationFailedException, IOException {        GroovyShell groovy = new GroovyShell(conf);        if (isScriptFile)            groovy.run(huntForTheScriptFile(script), args);        else            groovy.run(script, "script_from_command_line", args);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -