Improved the DocBuffer and DOM writers to where they are kind of usable.
[jsvc.git] / samples / bsh / src / dolda / bsvc / ShellPage.java
index e7412ad..13be814 100644 (file)
@@ -2,51 +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 {
-    private RConsole cons = new RConsole();
+public class ShellPage implements Responder {
+    private Console cons = new Console();
     private Interpreter ip = new Interpreter(cons);
     
-    private static class RConsole implements bsh.ConsoleInterface {
-       public Console back;
-       Reader in = new StringReader("");
-       
-       public void error(Object msg) {
-           if(back != null)
-               back.error(msg);
-       }
-       
-       public void print(Object msg) {
-           if(back != null)
-               back.print(msg);
-       }
-       
-       public void println(Object msg) {
-           if(back != null)
-               back.println(msg);
-       }
-       
-       public PrintStream getOut() {
-           if(back == null)
-               return(null);
-           return(back.getOut());
-       }
-       
-       public PrintStream getErr() {
-           if(back == null)
-               return(null);
-           return(back.getErr());
-       }
-       
-       public Reader getIn() {
-           if(back == null)
-               return(null);
-           return(in);
-       }
-    }
-    
     private static class Console implements bsh.ConsoleInterface {
        ByteArrayOutputStream obuf = new ByteArrayOutputStream();
        ByteArrayOutputStream ebuf = new ByteArrayOutputStream();
@@ -85,75 +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<String, String> params = req.params();
        String cmd = params.get("cmd");
        
-       out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
-       out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
-       out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\">");
-       out.println("<head>");
-       out.println("<title>Shell</title>");
-       out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"css\" />");
-       out.println("</head>");
-       out.println("<body>");
-       out.println("<h1>Shell</h1>");
+       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();
-           synchronized(ip) {
-               this.cons.back = cons;
+           String eo, ee;
+           synchronized(cons) {
+               cons.reset();
+               Object resp;
                try {
-                   Object resp;
-                   try {
-                       ip.set("req", req);
-                       resp = ip.eval(cmd);
-                       out.println("<pre>");
-                       out.println(Misc.htmlq((resp == null)?"(null)":(resp.toString())));
-                       out.println("</pre>");
-                   } catch(bsh.EvalError exc) {
-                       out.println("<h2>Evaluation error</h2>");
-                       out.println("<pre>");
-                       out.print(exc.toString());
-                       out.println("</pre>");
-                       if(exc instanceof bsh.TargetError) {
-                           bsh.TargetError te = (bsh.TargetError)exc;
-                           out.println("<h3>Target error</h3>");
-                           out.println("<pre>");
-                           te.getTarget().printStackTrace(out);
-                           out.println("</pre>");
-                       }
+                   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())));
                    }
-               } finally {
-                   this.cons.back = null;
                }
+               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("<h2>Output</h2>");
-               out.println("<pre>");
-               out.println(Misc.htmlq(eo));
-               out.println("</pre>");
+               buf.insert("body", buf.el("h2", buf.text("Output")));
+               buf.insert("body", buf.el("pre", buf.text(eo)));
            }
            if(ee.length() > 0) {
-               out.println("<h2>Errors</h2>");
-               out.println("<pre>");
-               out.println(Misc.htmlq(ee));
-               out.println("</pre>");
+               buf.insert("body", buf.el("h2", buf.text("Errors")));
+               buf.insert("body", buf.el("pre", buf.text(ee)));
            }
        }
-       out.println("<form action=\"sh\" method=\"post\">");
-       out.println("<textarea cols=\"80\" rows=\"5\" name=\"cmd\">");
-       if(cmd != null)
-           out.print(cmd);
-       out.println("</textarea>");
-       out.println("<input type=\"submit\" value=\"Evaluate\" />");
-       out.println("<input type=\"reset\" value=\"Reset\" />");
-       out.println("</form>");
-       out.println("</body>");
-       out.println("</html>");
+       
+       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));
+       }
     }
 }