Added some minor HTML utilities.
authorFredrik Tolf <fredrik@dolda2000.com>
Mon, 6 Sep 2010 01:53:43 +0000 (03:53 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Mon, 6 Sep 2010 01:53:43 +0000 (03:53 +0200)
src/dolda/jsvc/next/DocBuffer.java
src/dolda/jsvc/next/Html.java

index 2690309..d885a0b 100644 (file)
@@ -76,6 +76,18 @@ public class DocBuffer {
        return(doc.createTextNode(text));
     }
     
+    public Node asnode(Object o) {
+       if(o instanceof Node) {
+           Node n = (Node)o;
+           if(n.getOwnerDocument() != doc)
+               return(doc.importNode(n, true));
+           return(n);
+       }
+       if(o instanceof String)
+           return(text((String)o));
+       throw(new RuntimeException("Cannot convert a " + o.getClass().getName() + " to a DOM node"));
+    }
+    
     public void finalise() {
        Node n = doc;
        while(true) {
index 978cad2..5984e96 100644 (file)
@@ -32,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)