示例#1
0
  String formatScriptResultHTML(
      String script, Object result, Exception error, StringBuffer scriptOutput) throws IOException {
    SimpleTemplate tmplt;

    if (error != null) {
      tmplt = new SimpleTemplate(getClass().getResource("error.template"));

      String errString;

      if (error instanceof bsh.EvalError) {
        int lineNo = ((EvalError) error).getErrorLineNumber();
        String msg = error.getMessage();
        int contextLines = 4;
        errString = escape(msg);
        if (lineNo > -1) errString += "<hr>" + showScriptContextHTML(script, lineNo, contextLines);
      } else errString = escape(error.toString());

      tmplt.replace("error", errString);
    } else {
      tmplt = new SimpleTemplate(getClass().getResource("result.template"));
      tmplt.replace("value", escape(String.valueOf(result)));
      tmplt.replace("output", escape(scriptOutput.toString()));
    }

    return tmplt.toString();
  }
示例#2
0
  void sendHTML(
      HttpServletRequest request,
      HttpServletResponse response,
      String script,
      Exception scriptError,
      Object scriptResult,
      StringBuffer scriptOutput,
      boolean capture)
      throws IOException {
    // Format the output using a simple templating utility
    SimpleTemplate st = new SimpleTemplate(BshServlet.class.getResource("page.template"));
    st.replace("version", getBshVersion());

    // String requestURI = HttpUtils.getRequestURL( request ).toString()
    // I was told this should work
    String requestURI = request.getRequestURI();

    st.replace("servletURL", requestURI);
    if (script != null) st.replace("script", script);
    else st.replace("script", exampleScript);
    if (capture) st.replace("captureOutErr", "CHECKED");
    else st.replace("captureOutErr", "");
    if (script != null)
      st.replace(
          "scriptResult", formatScriptResultHTML(script, scriptResult, scriptError, scriptOutput));

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    st.write(out);
    out.flush();
  }