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

📄 cmsshell.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                System.out.println(je.getMessage());
            }
        }else System.out.print("\n");
    }

    /**
     * the ecmascript interpreter
     *
     * @fis the file input stream for commands
     */
    public void ecmacommands(FileInputStream fis)throws Exception {

      boolean lineMode=true;
      boolean continueReading=true;

      String in="";
      String input = null;

      // the prompt: "user@projectname>"
      ecmaPrompt="cms.getRequestContext().currentUser().getName()+\"@\"+cms.getRequestContext().currentProject().getName()+\">\"";

      JSGlobalObject jSGO = null;
      jSGO = JSUtil.makeEvaluator();

      JSObject jsCMS = jSGO.makeObjectWrapper(m_cms);
      jSGO.setMember("cms", jsCMS);

      // create a bufferedReader to read the data from
      BufferedReader ins = new BufferedReader(new InputStreamReader(fis));

      // print the ecmascript welcome-text
      printEcmaHelpText();

      Class[] args = {String.class};

      // new command: echoNoLF (no linefeed)
      jSGO.setMember("echoNoLF", new JSFunctionAdapter(){
        public Object doCall(JSObject thisObject, Object args[]) throws JSException
        {
            if(args.length==0)System.out.println(" ");
            else System.out.print(args[0]);
            return null;
        }
      });

      // new command: echo
      jSGO.setMember("echo", new JSFunctionAdapter(){
        public Object doCall(JSObject thisObject, Object args[]) throws JSException
        {
            if(args.length==0)System.out.print(" ");
            else {
                //if m_echo=true all commands are echoed
                if(args[0].equals("on"))m_echo=true;
                if(args[0].equals("off"))m_echo=false;
                System.out.println(args[0]);
            }
            return null;
        }
      });

      // new command: input
      // the string-parameter is optional. if given it's used as prompt for the input
      jSGO.setMember("input", new JSFunctionAdapter(){
        public Object doCall(JSObject thisObject, Object args[]) throws JSException
        {
            String inputPrompt="? ";
            if(args.length!=0)inputPrompt=args[0].toString();
            return ecmaInput(inputPrompt);
        }
      });

      // new command: setprompt
      // if there is no string-parameter, there is no prompt
      jSGO.setMember("setPrompt", new JSFunctionAdapter(){
        public Object doCall(JSObject thisObject, Object args[]) throws JSException
        {
            if(args.length!=0)ecmaPrompt=args[0].toString();
                else ecmaPrompt=null;
            System.out.println("");
            return null;
        }
      });

      // new command: help
      jSGO.setMember("help", new JSFunctionAdapter(){
        public Object doCall(JSObject thisObject, Object args[]) throws JSException
        {
            Method meth[] = m_cms.getClass().getMethods();

            if(args.length==0){
                for(int z = 0;z < meth.length;z++) {
                    cmsHelp(meth[z],"cms");
                }
                System.out.println("echo(java.lang.String);");
                System.out.println("exit();");
                System.out.println("input(String); returns input (int or string)");
                System.out.println("input(); returns input (int or string)");

            } else {
                //because for example: user could search for: "cms.readUser", but
                //the search only processes m_cms methods, so the "cms." must be deleted.
                if(args[0].toString().startsWith("cms")){
                    if(args[0].toString().charAt(3)=='.'){
                        String ar=args[0].toString().substring(4);
                        args[0]=ar;
                    }
                }
                for(int z = 0;z < meth.length;z++)
                    if(meth[z].getName().equals(args[0]))cmsHelp(meth[z],"cms");
                    if(args[0].equals("echo"))System.out.println("echo(java.lang.String);");
                    if(args[0].equals("exit"))System.out.println("exit();");
                    if(args[0].equals("input"))System.out.println("input(String); returns input (int or string");
                    if(args[0].equals("input"))System.out.println("input(); returns input (int or string");
            }
            return null;
        }
      });

      // new command: exit
      jSGO.setMember("exit", new JSFunctionAdapter(){
        public Object doCall(JSObject thisObject, Object args[]) throws JSException
        {
            return "exit";
        }
      });

      String eol = System.getProperty("line.separator", "\n");
      ESValue theValue = ESUndefined.theUndefined;

      Evaluator evaluator = new Evaluator();

      // load fesi extension to access java from javascript
      try {
          evaluator.addMandatoryExtension("FESI.Extensions.JavaAccess");
      } catch (EcmaScriptException e) {
          System.out.println("Cannot initialize JavaAccess - exiting: " + eol + e);
          e.printStackTrace();
      }

      while (continueReading) {               // main input loop
          in="";
          while(in.equals("")){

              if(lineMode)printEcmaPrompt(jSGO,ecmaPrompt);
                  else if(ecmaPrompt!=null)System.out.print("More> ");    // linemode = false if the line is incomplete
              try{
                  in=ins.readLine();          // read a line from input
              } catch (IOException ef){
                  System.out.println("IOException!!!");
              }

              if(in==null){                   // reached end of file or control-c was pressed
                  continueReading=false;
                  break;
              }
          }

          if(lineMode) input = in;
              else input += in;

          if(continueReading)try {
                  theValue = evaluator.evaluate(input);
              } catch (EcmaScriptException e) {
                  if (e.isIncomplete()) {
                      lineMode=false;         // if the entered line is not complete
                  }else{
                      if (input == null) break;
                      try {
                          if(m_echo)System.out.println(input);

                          Object result = jSGO.eval(input);     // interpret!

                          if (result!=null) {
                              if (result.toString().equals("exit"))break;
                              System.out.println(result.toString());
                          }
                          lineMode=true;
                      }catch (JSException je) {
                          System.out.println(je.getMessage());
                          lineMode=true;
                          input="";
                      }
                  }// else
              } // catch
        } // while
    }

    /**
     * Prints a exception with the stacktrace.
     *
     * @param exc The exception to print.
     */
    protected static void printException(Exception exc) {
        if(CmsShell.m_shortException) {
            String exceptionText;

            if(exc instanceof CmsException) {
                exceptionText = ((CmsException)exc).getTypeText();    // this is a cms-exception: print a very short exeption-text
            } else {
                exceptionText = exc.getMessage();     // only return the exception message
            }
            if((exceptionText == null) || (exceptionText.length() == 0)) {
                // the exception-text was empty, return the class-name of the exeption
                exceptionText = exc.getClass().getName();
            }
            System.out.println(exceptionText);
        }
        else {
            exc.printStackTrace();
        }
    }

    /**
     * Prints the full name and signature of a method.<br>
     * Called by help-methods.
     * Creation date: (09/28/00)
     * @author Jan Krag
     * @param param java.lang.reflect.Method
     */
    protected static void printMethod(Method method) {
        System.out.print("  " + method.getName() + " (");
        Class[] params = method.getParameterTypes();
        for(int i = 0;i < params.length;i++) {
            String par = params[i].getName();
            par = par.substring(par.lastIndexOf(".") + 1);
            if(i == 0) {
                System.out.print(par);
            }
            else {
                System.out.print(", " + par);
            }
        }
        System.out.println(")");
    }

    /**
     * Prints the current prompt.
     * Creation date: (10/03/00 %r)
     * @author: Jan Krag
     */
    private void printPrompt() {
        System.out.print("{" + m_cms.getRequestContext().currentUser().getName() + "@"
                + m_cms.getRequestContext().currentProject().getName() + "}");

        // print out memory-informations, if needed
        if(m_logMemory) {
            long total = Runtime.getRuntime().totalMemory() / 1024;
            long free = Runtime.getRuntime().freeMemory() / 1024;
            System.out.print(("[" + total + "/" + free + "/" + (total - free) + "]"));
        }
        System.out.print("> ");
    }
}

⌨️ 快捷键说明

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