1 package dolda.jsvc.next;
4 import org.w3c.dom.ls.*;
5 import javax.xml.validation.*;
9 import dolda.jsvc.util.*;
11 public class Html extends DocBuffer {
12 public static final String ns = "http://www.w3.org/1999/xhtml";
13 private static final Schema schema = DomUtil.loadxsd("xhtml1-strict.xsd");
15 private Html(String pubid, String sysid) {
16 super(ns, "html", "html", pubid, sysid);
19 public static Html xhtml11(String title) {
20 Html buf = new Html("-//W3C//DTD XHTML 1.1//EN", "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd");
21 Node html = buf.doc.getDocumentElement();
22 Node head = DomUtil.insertel(html, "head");
23 Node tit = DomUtil.insertel(head, "title");
24 head.appendChild(buf.makecursor("head"));
25 DomUtil.inserttext(tit, title);
26 Node body = DomUtil.insertel(html, "body");
27 body.appendChild(buf.makecursor("body"));
31 public Element el(String name, Node contents, String... attrs) {
32 return(el(ns, name, contents, attrs));
35 public Element table(Object... headers) {
36 Element tbl = el("table", null);
37 tbl.appendChild(tr(true, headers));
41 public Element tr(boolean head, Object... cells) {
42 Element tr = el("tr", null);
43 for(Object cell : cells)
44 tr.appendChild(el(head?"th":"td", asnode(cell)));
48 public Element csslink(String href, String name) {
49 Element el = el("link", null, "rel=stylesheet", "type=text/css");
51 el.setAttribute("title", name);
52 el.setAttribute("href", href);
56 public void addcss(String href, String name) {
57 insert("head", csslink(href, name));
60 public void validate() {
62 synchronized(schema) {
63 val = schema.newValidator();
66 val.validate(new javax.xml.transform.dom.DOMSource(doc));
67 } catch(org.xml.sax.SAXException e) {
68 throw(new RuntimeException(e));
69 } catch(java.io.IOException e) {
70 /* Should never happen. */
75 private static boolean asxhtml(Request req) {
76 String ah = req.inheaders().get("Accept");
77 AcceptMap ctmap = AcceptMap.parse((ah == null)?"":ah);
78 AcceptMap.Entry ha = ctmap.accepts("text/html");
79 AcceptMap.Entry xa = ctmap.accepts("text/xhtml+xml");
81 xa = ctmap.accepts("application/xhtml+xml");
82 if((ha == null) && (xa == null))
84 else if((ha != null) && (xa == null))
86 else if((ha == null) && (xa != null))
93 public void output(Request req) throws IOException {
98 req.outheaders().put("Content-Type", "application/xhtml+xml");
99 w = new XHtmlWriter(doc);
101 req.outheaders().put("Content-Type", "text/html; charset=utf-8");
102 w = new HtmlWriter(doc);
104 w.write(req.output());