示例#1
0
  /*
  	Show context number lines of string before and after target line.
  	Add HTML formatting to bold the target line.
  */
  String showScriptContextHTML(String s, int lineNo, int context) {
    StringBuffer sb = new StringBuffer();
    BufferedReader br = new BufferedReader(new StringReader(s));

    int beginLine = Math.max(1, lineNo - context);
    int endLine = lineNo + context;
    for (int i = 1; i <= lineNo + context + 1; i++) {
      if (i < beginLine) {
        try {
          br.readLine();
        } catch (IOException e) {
          throw new RuntimeException(e.toString());
        }
        continue;
      }
      if (i > endLine) break;

      String line;
      try {
        line = br.readLine();
      } catch (IOException e) {
        throw new RuntimeException(e.toString());
      }

      if (line == null) break;
      if (i == lineNo) sb.append("<font color=\"red\">" + i + ": " + line + "</font><br/>");
      else sb.append(i + ": " + line + "<br/>");
    }

    return sb.toString();
  }
示例#2
0
  /** Convert special characters to entities for XML output */
  public static String escape(String value) {
    String search = "&<>";
    String[] replace = {"&amp;", "&lt;", "&gt;"};

    StringBuffer buf = new StringBuffer();

    for (int i = 0; i < value.length(); i++) {
      char c = value.charAt(i);
      int pos = search.indexOf(c);
      if (pos < 0) buf.append(c);
      else buf.append(replace[pos]);
    }

    return buf.toString();
  }
示例#3
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();
  }
示例#4
0
 void sendRaw(
     HttpServletRequest request,
     HttpServletResponse response,
     Exception scriptError,
     Object scriptResult,
     StringBuffer scriptOutput)
     throws IOException {
   response.setContentType("text/plain");
   PrintWriter out = response.getWriter();
   if (scriptError != null) out.println("Script Error:\n" + scriptError);
   else out.println(scriptOutput.toString());
   out.flush();
 }
示例#5
0
  private static String getBody(HttpServletRequest req) {
    try {
      // Try reading the post body using characters.
      // This might throw an exception if something on the
      // server side already called getInputStream().
      // In that case we'll pull as bytes.
      Reader reader = null;
      try {
        reader = new BufferedReader(req.getReader());
      } catch (IOException e) {
        reader = new BufferedReader(new InputStreamReader(req.getInputStream(), "UTF-8"));
      }

      StringBuffer sbuf = new StringBuffer();
      char[] cbuf = new char[4096];
      int count = 0;
      while ((count = reader.read(cbuf)) != -1) {
        sbuf.append(cbuf, 0, count);
      }
      return sbuf.toString();
    } catch (IOException e2) {
      throw new ServerProblemException("IOException in reading POST body: " + e2.getMessage());
    }
  }
示例#6
0
  Object evalScript(
      String script,
      StringBuffer scriptOutput,
      boolean captureOutErr,
      HttpServletRequest request,
      HttpServletResponse response)
      throws EvalError {
    // Create a PrintStream to capture output
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream pout = new PrintStream(baos);

    // Create an interpreter instance with a null inputstream,
    // the capture out/err stream, non-interactive
    Interpreter bsh = new Interpreter(null, pout, pout, false);

    // set up interpreter
    bsh.set("bsh.httpServletRequest", request);
    bsh.set("bsh.httpServletResponse", response);

    // Eval the text, gathering the return value or any error.
    Object result = null;
    String error = null;
    PrintStream sout = System.out;
    PrintStream serr = System.err;
    if (captureOutErr) {
      System.setOut(pout);
      System.setErr(pout);
    }
    try {
      // Eval the user text
      result = bsh.eval(script);
    } finally {
      if (captureOutErr) {
        System.setOut(sout);
        System.setErr(serr);
      }
    }
    pout.flush();
    scriptOutput.append(baos.toString());
    return result;
  }
示例#7
0
 /**
  * String Representation incl. Result
  *
  * @return Scipt
  */
 @Override
 public String toString() {
   StringBuffer sb = new StringBuffer(m_variable);
   sb.append(" { ").append(m_script).append(" } = ").append(getResult(true));
   return sb.toString();
 } //  toString