Exemple #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();
  }
Exemple #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();
  }
Exemple #3
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;
  }
Exemple #4
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());
    }
  }
Exemple #5
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