1 package dolda.jsvc.next;
6 public class DocBuffer {
7 public final Document doc;
8 private final Map<String, Node> cursors = new HashMap<String, Node>();
9 public static final String ns = "jsvc:next:buffer";
11 public DocBuffer(String ns, String root, String doctype, String pubid, String sysid) {
12 doc = DomUtil.document(ns, root, doctype, pubid, sysid);
15 public DocBuffer(String ns, String root) {
16 this(ns, root, null, null, null);
19 private Node findcursor(Node c, String name) {
20 if(c instanceof Element) {
21 Element el = (Element)c;
22 String ns = el.getNamespaceURI();
23 if((ns != null) && ns.equals(DocBuffer.ns) && el.getTagName().equals("cursor") && el.getAttributeNS(null, "name").equals(name))
26 for(Node n = c.getFirstChild(); n != null; n = n.getNextSibling()) {
27 Node r = findcursor(n, name);
34 private Node cursor(String name) {
36 if((n = cursors.get(name)) != null) {
39 if((n = findcursor(doc, name)) == null)
45 public void insert(String cursor, Node n) {
46 Node c = cursor(cursor);
48 throw(new RuntimeException("No such cursor: `" + cursor + "'"));
49 c.getParentNode().insertBefore(doc.importNode(n, true), c);
52 public Element makecursor(String name) {
53 Element el = doc.createElementNS(ns, "cursor");
54 Attr a = doc.createAttributeNS(null, "name");
56 el.setAttributeNodeNS(a);
60 public Element el(String ns, String nm, Node contents, String... attrs) {
61 Element el = doc.createElementNS(ns, nm);
63 el.appendChild(contents);
64 for(String attr : attrs) {
65 int p = attr.indexOf('=');
66 el.setAttribute(attr.substring(0, p), attr.substring(p + 1));
71 public Text text(String text) {
72 return(doc.createTextNode(text));
75 public void finalise() {
79 if(n.getFirstChild() != null) {
80 nx = n.getFirstChild();
81 } else if(n.getNextSibling() != null) {
82 nx = n.getNextSibling();
84 for(nx = n.getParentNode(); nx != null; nx = nx.getParentNode()) {
85 if(nx.getNextSibling() != null) {
86 nx = nx.getNextSibling();
91 String ns = n.getNamespaceURI();
92 if((ns != null) && ns.equals(DocBuffer.ns))
93 n.getParentNode().removeChild(n);