4c5b417fca340c020399fcb557c1533e27b98771
[jsvc.git] / src / dolda / jsvc / next / Html.java
1 package dolda.jsvc.next;
2
3 import org.w3c.dom.*;
4 import org.w3c.dom.ls.*;
5 import javax.xml.validation.*;
6 import java.net.*;
7 import java.io.*;
8 import dolda.jsvc.*;
9 import dolda.jsvc.util.*;
10
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");
14
15     private Html(String pubid, String sysid) {
16         super(ns, "html", "html", pubid, sysid);
17     }
18
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"));
28         return(buf);
29     }
30     
31     public Element el(String name, Node contents, String... attrs) {
32         return(el(ns, name, contents, attrs));
33     }
34     
35     public Element csslink(String href, String name) {
36         Element el = el("link", null, "rel=stylesheet", "type=text/css");
37         if(name != null)
38             el.setAttribute("title", name);
39         el.setAttribute("href", href);
40         return(el);
41     }
42     
43     public void addcss(String href, String name) {
44         insert("head", csslink(href, name));
45     }
46     
47     public void validate() {
48         Validator val = schema.newValidator();
49         try {
50             val.validate(new javax.xml.transform.dom.DOMSource(doc));
51         } catch(org.xml.sax.SAXException e) {
52             throw(new RuntimeException(e));
53         } catch(java.io.IOException e) {
54             /* Should never happen. */
55             throw(new Error(e));
56         }
57     }
58     
59     private static boolean asxhtml(Request req) {
60         String ah = req.inheaders().get("Accept");
61         AcceptMap ctmap = AcceptMap.parse((ah == null)?"":ah);
62         AcceptMap.Entry ha = ctmap.accepts("text/html");
63         AcceptMap.Entry xa = ctmap.accepts("text/xhtml+xml");
64         if(xa == null)
65             xa = ctmap.accepts("application/xhtml+xml");
66         if((ha == null) && (xa == null))
67             return(false);
68         else if((ha != null) && (xa == null))
69             return(false);
70         else if((ha == null) && (xa != null))
71             return(true);
72         if(xa.q < ha.q)
73             return(false);
74         return(true);
75     }
76
77     public void output(Request req) throws IOException {
78         finalise();
79         validate();
80         XmlWriter w;
81         if(asxhtml(req)) {
82             req.outheaders().put("Content-Type", "application/xhtml+xml");
83             w = new XHtmlWriter(doc);
84         } else {
85             req.outheaders().put("Content-Type", "text/html; charset=utf-8");
86             w = new HtmlWriter(doc);
87         }
88         w.write(req.output());
89     }
90 }