Added some minor HTML utilities.
[jsvc.git] / src / dolda / jsvc / next / Html.java
index c90bb40..5984e96 100644 (file)
@@ -5,6 +5,8 @@ import org.w3c.dom.ls.*;
 import javax.xml.validation.*;
 import java.net.*;
 import java.io.*;
+import dolda.jsvc.*;
+import dolda.jsvc.util.*;
 
 public class Html extends DocBuffer {
     public static final String ns = "http://www.w3.org/1999/xhtml";
@@ -18,8 +20,8 @@ public class Html extends DocBuffer {
        Html buf = new Html("-//W3C//DTD XHTML 1.1//EN", "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd");
        Node html = buf.doc.getDocumentElement();
        Node head = DomUtil.insertel(html, "head");
-       head.appendChild(buf.makecursor("head"));
        Node tit = DomUtil.insertel(head, "title");
+       head.appendChild(buf.makecursor("head"));
        DomUtil.inserttext(tit, title);
        Node body = DomUtil.insertel(html, "body");
        body.appendChild(buf.makecursor("body"));
@@ -30,6 +32,19 @@ public class Html extends DocBuffer {
        return(el(ns, name, contents, attrs));
     }
     
+    public Element table(Object... headers) {
+       Element tbl = el("table", null);
+       tbl.appendChild(tr(true, headers));
+       return(tbl);
+    }
+    
+    public Element tr(boolean head, Object... cells) {
+       Element tr = el("tr", null);
+       for(Object cell : cells)
+           tr.appendChild(el(head?"th":"td", asnode(cell)));
+       return(tr);
+    }
+    
     public Element csslink(String href, String name) {
        Element el = el("link", null, "rel=stylesheet", "type=text/css");
        if(name != null)
@@ -43,7 +58,10 @@ public class Html extends DocBuffer {
     }
     
     public void validate() {
-       Validator val = schema.newValidator();
+       Validator val;
+       synchronized(schema) {
+           val = schema.newValidator();
+       }
        try {
            val.validate(new javax.xml.transform.dom.DOMSource(doc));
        } catch(org.xml.sax.SAXException e) {
@@ -53,4 +71,36 @@ public class Html extends DocBuffer {
            throw(new Error(e));
        }
     }
+    
+    private static boolean asxhtml(Request req) {
+       String ah = req.inheaders().get("Accept");
+       AcceptMap ctmap = AcceptMap.parse((ah == null)?"":ah);
+       AcceptMap.Entry ha = ctmap.accepts("text/html");
+       AcceptMap.Entry xa = ctmap.accepts("text/xhtml+xml");
+       if(xa == null)
+           xa = ctmap.accepts("application/xhtml+xml");
+       if((ha == null) && (xa == null))
+           return(false);
+       else if((ha != null) && (xa == null))
+           return(false);
+       else if((ha == null) && (xa != null))
+           return(true);
+       if(xa.q < ha.q)
+           return(false);
+       return(true);
+    }
+
+    public void output(Request req) throws IOException {
+       finalise();
+       validate();
+       XmlWriter w;
+       if(asxhtml(req)) {
+           req.outheaders().put("Content-Type", "application/xhtml+xml");
+           w = new XHtmlWriter(doc);
+       } else {
+           req.outheaders().put("Content-Type", "text/html; charset=utf-8");
+           w = new HtmlWriter(doc);
+       }
+       w.write(req.output());
+    }
 }