Added XHTML validation support.
[jsvc.git] / src / dolda / jsvc / next / DomUtil.java
1 package dolda.jsvc.next;
2
3 import org.w3c.dom.*;
4 import org.w3c.dom.bootstrap.*;
5 import org.w3c.dom.ls.*;
6 import javax.xml.validation.*;
7 import java.io.*;
8
9 public class DomUtil {
10     private static final DOMImplementation domimp;
11     private static final SchemaFactory xsdfac;
12     
13     static {
14         xsdfac = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
15         xsdfac.setResourceResolver(new LSResourceResolver() {
16                 public LSInput resolveResource(String type, String ns, String pubid, String sysid, String base) {
17                     if(sysid.indexOf('/') >= 0) {
18                         InputStream in = getcatalog(sysid.substring(sysid.lastIndexOf('/') + 1));
19                         if(in != null) {
20                             LSInput ret = new LSInputAdapter(pubid, sysid, base);
21                             ret.setByteStream(in);
22                             ret.setEncoding("us-ascii");
23                             return(ret);
24                         }
25                     }
26                     throw(new RuntimeException(String.format("Will not load external resources (for %s); please fix catalog.", sysid)));
27                 }
28             });
29     }
30     
31     static {
32         DOMImplementationRegistry reg;
33         try {
34             reg = DOMImplementationRegistry.newInstance();
35         } catch(Exception e) {
36             throw(new Error(e));
37         }
38         DOMImplementation di = reg.getDOMImplementation("");
39         if(di == null)
40             throw(new RuntimeException("Could not get a DOM implemenation"));
41         domimp = di;
42     }
43     
44     public static Document document(String ns, String root, String doctype, String pubid, String sysid) {
45         if(doctype == null)
46             return(domimp.createDocument(ns, root, null));
47         else
48             return(domimp.createDocument(ns, root, domimp.createDocumentType(doctype, pubid, sysid)));
49     }
50     
51     public static Document document(String ns, String root) {
52         return(document(ns, root, null, null, null));
53     }
54     
55     public static Element insertel(Node p, String nm) {
56         Document doc;
57         if(p instanceof Document)
58             doc = (Document)p;
59         else
60             doc = p.getOwnerDocument();
61         Element el = doc.createElementNS(p.getNamespaceURI(), nm);
62         p.appendChild(el);
63         return(el);
64     }
65     
66     public static Text inserttext(Node p, String text) {
67         Document doc;
68         if(p instanceof Document)
69             doc = (Document)p;
70         else
71             doc = p.getOwnerDocument();
72         Text t = doc.createTextNode(text);
73         p.appendChild(t);
74         return(t);
75     }
76     
77     public static class LSInputAdapter implements LSInput {
78         private String pubid, sysid, baseuri, encoding = null, data = null;
79         private boolean cert = false;
80         private InputStream bs = null;
81         private Reader cs = null;
82         
83         public LSInputAdapter(String pubid, String sysid, String baseuri) {
84             this.pubid = pubid;
85             this.sysid = sysid;
86             this.baseuri = baseuri;
87         }
88         
89         public String getBaseURI() {return(baseuri);}
90         public String getPublicId() {return(pubid);}
91         public String getSystemId() {return(sysid);}
92         public void setBaseURI(String baseuri) {this.baseuri = baseuri;}
93         public void setPublicId(String pubid) {this.pubid = pubid;}
94         public void setSystemId(String sysid) {this.sysid = sysid;}
95
96         public InputStream getByteStream() {return(bs);}
97         public boolean getCertifiedText() {return(cert);}
98         public Reader getCharacterStream() {return(cs);}
99         public String getEncoding() {return(encoding);}
100         public String getStringData() {return(data);}
101         public void setByteStream(InputStream bs) {this.bs = bs;}
102         public void setCertifiedText(boolean cert) {this.cert = cert;}
103         public void setCharacterStream(Reader cs) {this.cs = cs;}
104         public void setEncoding(String encoding) {this.encoding = encoding;}
105         public void setStringData(String data) {this.data = data;}
106     }
107     
108     private static InputStream getcatalog(String name) {
109         if(name.indexOf('/') >= 0)
110             throw(new RuntimeException("Illegal catalog resource name `" + name + "'"));
111         return(DomUtil.class.getResourceAsStream("catalog/" + name));
112     }
113
114     public static Schema loadxsd(String name) {
115         InputStream in = getcatalog(name);
116         if(in == null)
117             throw(new RuntimeException("Could not find schema `" + name + "'"));
118         try {
119             return(xsdfac.newSchema(new javax.xml.transform.stream.StreamSource(in)));
120         } catch(org.xml.sax.SAXException e) {
121             throw(new RuntimeException(e));
122         }
123     }
124 }