cmdrunner.java

来自「数据仓库展示程序」· Java 代码 · 共 1,855 行 · 第 1/5 页

JAVA
1,855
字号
     * interpreted. If interactive then prompts are printed.
     *
     * @param in Input stream
     * @param interactive Whether the session is interactive
     * @throws IOException if stream can not be accessed
     */
    protected void commandLoop(InputStream in, boolean interactive)
        throws IOException {

        StringBuffer buf = new StringBuffer(2048);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        boolean inMDXCmd = false;
        String resultString = null;

        for(;;) {
            if (resultString != null) {
                printResults(resultString);
                resultString = null;
            } else if (interactive && (error != null)) {
                printResults(error);
            }
            if (interactive) {
                if (inMDXCmd)
                    System.out.print(COMMAND_PROMPT_MID);
                else
                    System.out.print(COMMAND_PROMPT_START);
            }
            if (!inMDXCmd) {
                buf.setLength(0);
                //buf = new StringBuffer(2048);
            }
            String line = readLine(br, inMDXCmd);
            if (line != null) {
                line = line.trim();
            }
            debug("line="+line);

            if (! inMDXCmd) {
                // If not in the middle of reading an mdx query and
                // we reach end of file on the stream, then we are over.
                if (line == null) {
                    return;
                }
            }

            // If not reading an mdx query, then check if the line is a
            // user command.
            if (! inMDXCmd) {
                String cmd = line;
                if (cmd.startsWith("help")) {
                    resultString = executeHelp(cmd);
                } else if (cmd.startsWith("set")) {
                    resultString = executeSet(cmd);
                } else if (cmd.startsWith("log")) {
                    resultString = executeLog(cmd);
                } else if (cmd.startsWith("file")) {
                    resultString = executeFile(cmd);
                } else if (cmd.startsWith("list")) {
                    resultString = executeList(cmd);
                } else if (cmd.startsWith("func")) {
                    resultString = executeFunc(cmd);
                } else if (cmd.startsWith("param")) {
                    resultString = executeParam(cmd);
                } else if (cmd.startsWith("cube")) {
                    resultString = executeCube(cmd);
                } else if (cmd.startsWith("error")) {
                    resultString = executeError(cmd);
                } else if (cmd.startsWith("echo")) {
                    resultString = executeEcho(cmd);
                } else if (cmd.startsWith("expr")) {
                    resultString = executeExpr(cmd);
                } else if (cmd.equals("=")) {
                    resultString = reexecuteMDXCmd();
                } else if (cmd.startsWith("exit")) {
                    break;
                }
                if (resultString != null) {
                    inMDXCmd = false;
                    continue;
                }
            }

            // Are we ready to execute an mdx query.
            if ((line == null) ||
                    ((line.length() == 1) &&
                    ((line.charAt(0) == EXECUTE_CHAR) ||
                        (line.charAt(0) == CANCEL_CHAR)) )) {

                // If EXECUTE_CHAR, then execute, otherwise its the
                // CANCEL_CHAR and simply empty buffer.
                if ((line == null) || (line.charAt(0) == EXECUTE_CHAR)) {
                    String mdxCmd = buf.toString().trim();
                    debug("mdxCmd=\""+mdxCmd+"\"");

                    resultString = executeMDXCmd(mdxCmd);
                }

                inMDXCmd = false;

            } else if (line.length() > 0) {
                // OK, just add the line to the mdx query we are building.
                inMDXCmd = true;
                buf.append(line);
                if (line.endsWith(SEMI_COLON_STRING)) {
                    String mdxCmd = buf.toString().trim();
                    debug("mdxCmd=\""+mdxCmd+"\"");
                    resultString = executeMDXCmd(mdxCmd);
                    inMDXCmd = false;
                } else {
                    // add carriage return so that query keeps formatting
                    buf.append(nl);
                }
            }
        }
    }

    protected void printResults(String resultString) {
        if (resultString != null) {
            resultString = resultString.trim();
            if (resultString.length() > 0) {
                System.out.println(resultString);
            }
            if (timeQueries && (queryTime != -1)) {
                System.out.println("time[" +queryTime+ "ms]");
                queryTime = -1;
            }
        }
    }

    /**
     * Gather up a line ending in '\n' or EOF.
     * Returns null if at EOF.
     * Strip out comments. If a comment character appears within a
     * string then its not a comment. Strings are defined with "\"" or
     * "'" characters. Also, a string can span more than one line (a
     * nice little complication). So, if we read a string, then we consume
     * the whole string as part of the "line" returned,
     * including EOL characters.
     * If an escape character is seen '\\', then it and the next character
     * is added to the line regardless of what the next character is.
     *
     * @param reader
     * @return
     * @throws IOException
     */
    protected static String readLine(Reader reader, boolean inMDXCmd)
                    throws IOException {
        StringBuffer buf = null;

        int i = reader.read();
        for (;;) {
            if (i == -1) {
                // At EOF, return what we've read so far.
                return (buf == null) ? null : buf.toString();
            }
            char c = (char) i;

            if (buf == null) {
                buf = new StringBuffer(128);
            }

            if (c == ESCAPE_CHAR) {
                buf.append(c);

                i = reader.read();
                if (i == -1) {
                    // At EOF, return what we've read so far.
                    return buf.toString();
                }
                buf.append((char)i);

                i = reader.read();
                continue;
            }

            // At EOL, return what we've read so far.
            if ((c == '\n') || (c == '\r')) {
                return buf.toString();
            }
            // comment handling
            if (! inMDXCmd) {
                if (c == COMMENT_CHAR) {
                    // Not in string, read to EOL or EOF
                    i = reader.read();
                    for (;;) {
                        if (i == -1) {
                            return buf.toString();
                        }
                        c = (char) i;
                        if ((c == '\n') || (c == '\r')) {
                            return buf.toString();
                        }
                        i = reader.read();
                    }
                    // In string, do nothing special with comment character
                }
            }

            if ((c == STRING_CHAR_1) || (c == STRING_CHAR_2)) {
                // Start of a string, read all of it even if it spans
                // more than one line adding each line's <cr> to the
                // buffer.

                char str_char = c;
                buf.append(c);

                i = reader.read();

                STRING_LOOP:
                for (;;) {
                    if (i == -1) {
                        return buf.toString();
                    }
                    c = (char) i;

                    if (c == ESCAPE_CHAR) {
                        buf.append(c);

                        i = reader.read();
                        if (i == -1) {
                            // At EOF, return what we've read so far.
                            return buf.toString();
                        }
                        buf.append((char)i);

                        i = reader.read();
                        continue STRING_LOOP;
                    }

                    buf.append(c);

                    if (c == str_char) {
                        break STRING_LOOP;
                    }

                    i = reader.read();
                }


            } else {
                buf.append(c);
            }

            i = reader.read();
        }
    }

    /////////////////////////////////////////////////////////////////////////
    // user commands and help messages
    /////////////////////////////////////////////////////////////////////////
    private static final String INDENT = "  ";

    private static final int UNKNOWN_CMD        = 0x0000;
    private static final int HELP_CMD           = 0x0001;
    private static final int SET_CMD            = 0x0002;
    private static final int LOG_CMD            = 0x0004;
    private static final int FILE_CMD           = 0x0008;
    private static final int LIST_CMD           = 0x0010;
    private static final int MDX_CMD            = 0x0020;
    private static final int FUNC_CMD           = 0x0040;
    private static final int PARAM_CMD          = 0x0080;
    private static final int CUBE_CMD           = 0x0100;
    private static final int ERROR_CMD          = 0x0200;
    private static final int ECHO_CMD           = 0x0400;
    private static final int EXPR_CMD           = 0x0800;
    private static final int EXIT_CMD           = 0x1000;

    private static final int ALL_CMD  = HELP_CMD  |
                                        SET_CMD   |
                                        LOG_CMD   |
                                        FILE_CMD  |
                                        LIST_CMD  |
                                        MDX_CMD   |
                                        FUNC_CMD  |
                                        PARAM_CMD |
                                        CUBE_CMD  |
                                        ERROR_CMD |
                                        ECHO_CMD  |
                                        EXPR_CMD  |
                                        EXIT_CMD;

    private static final char ESCAPE_CHAR         = '\\';
    private static final char EXECUTE_CHAR        = '=';
    private static final char CANCEL_CHAR         = '~';
    private static final char COMMENT_CHAR        = '#';
    private static final char STRING_CHAR_1       = '"';
    private static final char STRING_CHAR_2       = '\'';

    private static final String SEMI_COLON_STRING = ";";

    //////////////////////////////////////////////////////////////////////////
    // help
    //////////////////////////////////////////////////////////////////////////
    protected static String executeHelp(String mdxCmd) {
        StringBuffer buf = new StringBuffer(200);

        String[] tokens = mdxCmd.split("\\s+");

        int cmd = UNKNOWN_CMD;

        if (tokens.length == 1) {
            buf.append("Commands:");
            cmd = ALL_CMD;

        } else if (tokens.length == 2) {
            String cmdName = tokens[1];

            if (cmdName.equals("help")) {
                cmd = HELP_CMD;
            } else if (cmdName.equals("set")) {
                cmd = SET_CMD;
            } else if (cmdName.equals("log")) {
                cmd = LOG_CMD;
            } else if (cmdName.equals("file")) {
                cmd = FILE_CMD;
            } else if (cmdName.equals("list")) {
                cmd = LIST_CMD;
            } else if (cmdName.equals("func")) {
                cmd = FUNC_CMD;
            } else if (cmdName.equals("param")) {
                cmd = PARAM_CMD;
            } else if (cmdName.equals("cube")) {
                cmd = CUBE_CMD;
            } else if (cmdName.equals("error")) {
                cmd = ERROR_CMD;
            } else if (cmdName.equals("echo")) {
                cmd = ECHO_CMD;
            } else if (cmdName.equals("exit")) {
                cmd = EXIT_CMD;
            } else {
                cmd = UNKNOWN_CMD;
            }
        }

        if (cmd == UNKNOWN_CMD) {
            buf.append("Unknown help command: ");
            buf.append(mdxCmd);
            buf.append(nl);
            buf.append("Type \"help\" for list of commands");
        }

        if ((cmd & HELP_CMD) != 0) {
            // help
            buf.append(nl);
            appendIndent(buf, 1);
            buf.append("help");
            buf.append(nl);
            appendIndent(buf, 2);
            buf.append("Prints this text");
        }

        if ((cmd & SET_CMD) != 0) {
            // set
            buf.append(nl);
            appendSet(buf);
        }

        if ((cmd & LOG_CMD) != 0) {
            // set
            buf.append(nl);
            appendLog(buf);
        }

        if ((cmd & FILE_CMD) != 0) {
            // file
            buf.append(nl);
            appendFile(buf);

        }
        if ((cmd & LIST_CMD) != 0) {
            // list

⌨️ 快捷键说明

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