X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=samples%2Fbsh%2Fsrc%2Fdolda%2Fbsvc%2FShellPage.java;h=13be8144599a3dad99153e7e92b8f15c706bd333;hb=cb67d09c40c80d0389d7a0a796a4abf0007f61a6;hp=29d0d3d7d6bb429a160dee3664400e46c6caf760;hpb=eb81f42eef6ff400b79410db867f3c2bff687e11;p=jsvc.git diff --git a/samples/bsh/src/dolda/bsvc/ShellPage.java b/samples/bsh/src/dolda/bsvc/ShellPage.java index 29d0d3d..13be814 100644 --- a/samples/bsh/src/dolda/bsvc/ShellPage.java +++ b/samples/bsh/src/dolda/bsvc/ShellPage.java @@ -2,10 +2,15 @@ package dolda.bsvc; import dolda.jsvc.*; import dolda.jsvc.util.*; +import dolda.jsvc.next.*; +import org.w3c.dom.*; import java.io.*; import bsh.Interpreter; -public class ShellPage extends SimpleWriter { +public class ShellPage implements Responder { + private Console cons = new Console(); + private Interpreter ip = new Interpreter(cons); + private static class Console implements bsh.ConsoleInterface { ByteArrayOutputStream obuf = new ByteArrayOutputStream(); ByteArrayOutputStream ebuf = new ByteArrayOutputStream(); @@ -44,69 +49,63 @@ public class ShellPage extends SimpleWriter { public Reader getIn() { return(in); } + + public void reset() { + obuf.reset(); + ebuf.reset(); + } } - public void respond(Request req, PrintWriter out) { + public void respond(Request req) { MultiMap params = req.params(); String cmd = params.get("cmd"); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println("Shell"); - out.println(""); - out.println(""); - out.println(""); - out.println("

Shell

"); + Html buf = Html.xhtml11("Shell"); + buf.addcss("css", null); + buf.insert("body", buf.el("h1", buf.text("Shell"))); + if((req.method() == "POST") && (cmd != null)) { - Console cons = new Console(); - Interpreter ip = new Interpreter(cons); - Object resp; - try { - ip.set("req", req); - resp = ip.eval(cmd); - out.println("
");
-		out.println(Misc.htmlq((resp == null)?"(null)":(resp.toString())));
-		out.println("
"); - } catch(bsh.EvalError exc) { - out.println("

Evaluation error

"); - out.println("
");
-		out.print(exc.toString());
-		out.println("
"); - if(exc instanceof bsh.TargetError) { - bsh.TargetError te = (bsh.TargetError)exc; - out.println("

Target error

"); - out.println("
");
-		    te.getTarget().printStackTrace(out);
-		    out.println("
"); + String eo, ee; + synchronized(cons) { + cons.reset(); + Object resp; + try { + ip.set("req", req); + resp = ip.eval(cmd); + buf.insert("body", buf.el("pre", buf.text((resp == null)?"(null)":(resp.toString())))); + } catch(bsh.EvalError exc) { + buf.insert("body", buf.el("h2", buf.text("Evaluation error"))); + buf.insert("body", buf.el("pre", buf.text(exc.toString()))); + if(exc instanceof bsh.TargetError) { + bsh.TargetError te = (bsh.TargetError)exc; + buf.insert("body", buf.el("h3", buf.text("Target error"))); + StringWriter sbuf = new StringWriter(); + te.getTarget().printStackTrace(new PrintWriter(sbuf)); + buf.insert("body", buf.el("pre", buf.text(sbuf.toString()))); + } } + eo = new String(cons.obuf.toByteArray(), Misc.utf8); + ee = new String(cons.ebuf.toByteArray(), Misc.utf8); } - String eo = new String(cons.obuf.toByteArray(), Misc.utf8); - String ee = new String(cons.ebuf.toByteArray(), Misc.utf8); - cons = null; if(eo.length() > 0) { - out.println("

Output

"); - out.println("
");
-		out.println(Misc.htmlq(eo));
-		out.println("
"); + buf.insert("body", buf.el("h2", buf.text("Output"))); + buf.insert("body", buf.el("pre", buf.text(eo))); } if(ee.length() > 0) { - out.println("

Errors

"); - out.println("
");
-		out.println(Misc.htmlq(ee));
-		out.println("
"); + buf.insert("body", buf.el("h2", buf.text("Errors"))); + buf.insert("body", buf.el("pre", buf.text(ee))); } } - out.println("
"); - out.println(""); - out.println(""); - out.println(""); - out.println("
"); - out.println(""); - out.println(""); + + Element form; + buf.insert("body", buf.el("form", form = buf.el("p", null), "action=sh", "method=post")); + form.appendChild(buf.el("textarea", buf.text(cmd), "cols=80", "rows=5", "name=cmd")); + form.appendChild(buf.el("input", null, "type=submit", "value=Evaluate")); + form.appendChild(buf.el("input", null, "type=reset", "value=Reset")); + try { + buf.output(req); + } catch(IOException e) { + throw(new RuntimeException(e)); + } } }