Separated the bsvc console for clarity.
[jsvc.git] / samples / bsh / src / dolda / bsvc / ShellPage.java
1 package dolda.bsvc;
2
3 import dolda.jsvc.*;
4 import dolda.jsvc.util.*;
5 import dolda.jsvc.next.*;
6 import org.w3c.dom.*;
7 import java.io.*;
8 import bsh.Interpreter;
9
10 public class ShellPage implements Responder {
11     private Console cons = new Console();
12     private Interpreter ip = new Interpreter(cons);
13     
14     public void respond(Request req) {
15         MultiMap<String, String> params = req.params();
16         String cmd = params.get("cmd");
17         
18         Html buf = Html.xhtml11("Shell");
19         buf.addcss("css", null);
20         buf.insert("body", buf.el("h1", buf.text("Shell")));
21         
22         if((req.method() == "POST") && (cmd != null)) {
23             String eo, ee;
24             synchronized(cons) {
25                 cons.reset();
26                 Object resp;
27                 try {
28                     ip.set("req", req);
29                     resp = ip.eval(cmd);
30                     buf.insert("body", buf.el("pre", buf.text((resp == null)?"(null)":(resp.toString()))));
31                 } catch(bsh.EvalError exc) {
32                     buf.insert("body", buf.el("h2", buf.text("Evaluation error")));
33                     buf.insert("body", buf.el("pre", buf.text(exc.toString())));
34                     if(exc instanceof bsh.TargetError) {
35                         bsh.TargetError te = (bsh.TargetError)exc;
36                         buf.insert("body", buf.el("h3", buf.text("Target error")));
37                         StringWriter sbuf = new StringWriter();
38                         te.getTarget().printStackTrace(new PrintWriter(sbuf));
39                         buf.insert("body", buf.el("pre", buf.text(sbuf.toString())));
40                     }
41                 }
42                 eo = new String(cons.obuf.toByteArray(), Misc.utf8);
43                 ee = new String(cons.ebuf.toByteArray(), Misc.utf8);
44             }
45             if(eo.length() > 0) {
46                 buf.insert("body", buf.el("h2", buf.text("Output")));
47                 buf.insert("body", buf.el("pre", buf.text(eo)));
48             }
49             if(ee.length() > 0) {
50                 buf.insert("body", buf.el("h2", buf.text("Errors")));
51                 buf.insert("body", buf.el("pre", buf.text(ee)));
52             }
53         }
54         
55         Element form;
56         buf.insert("body", buf.el("form", form = buf.el("p", null), "action=sh", "method=post"));
57         form.appendChild(buf.el("textarea", buf.text(cmd), "cols=80", "rows=5", "name=cmd"));
58         form.appendChild(buf.el("input", null, "type=submit", "value=Evaluate"));
59         form.appendChild(buf.el("input", null, "type=reset", "value=Reset"));
60         try {
61             buf.output(req);
62         } catch(IOException e) {
63             throw(new RuntimeException(e));
64         }
65     }
66 }