WIP: Convenient document creation utilities.
[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
6 public class DomUtil {
7     private static final DOMImplementation domimp;
8     
9     static {
10         DOMImplementationRegistry reg;
11         try {
12             reg = DOMImplementationRegistry.newInstance();
13         } catch(Exception e) {
14             throw(new Error(e));
15         }
16         DOMImplementation di = reg.getDOMImplementation("");
17         if(di == null)
18             throw(new RuntimeException("Could not get a DOM implemenation"));
19         domimp = di;
20     }
21     
22     public static Document document(String ns, String root, String doctype, String pubid, String sysid) {
23         if(doctype == null)
24             return(domimp.createDocument(ns, root, null));
25         else
26             return(domimp.createDocument(ns, root, domimp.createDocumentType(doctype, pubid, sysid)));
27     }
28     
29     public static Document document(String ns, String root) {
30         return(document(ns, root, null, null, null));
31     }
32     
33     public static Element insertel(Node p, String nm) {
34         Document doc;
35         if(p instanceof Document)
36             doc = (Document)p;
37         else
38             doc = p.getOwnerDocument();
39         Element el = doc.createElementNS(p.getNamespaceURI(), nm);
40         p.appendChild(el);
41         return(el);
42     }
43     
44     public static Text inserttext(Node p, String text) {
45         Document doc;
46         if(p instanceof Document)
47             doc = (Document)p;
48         else
49             doc = p.getOwnerDocument();
50         Text t = doc.createTextNode(text);
51         p.appendChild(t);
52         return(t);
53     }
54 }