1 9 10 package org.ozoneDB.xml.dom4j.o3impl; 11 12 import org.dom4j.*; 13 import org.dom4j.io.XMLWriter; 14 15 import java.io.IOException ; 16 import java.io.StringWriter ; 17 import java.io.Writer ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 28 public abstract class AbstractDocument extends AbstractBranch implements Document { 29 30 public AbstractDocument() { 31 } 32 33 public short getNodeType() { 34 return DOCUMENT_NODE; 35 } 36 37 public String getPath(Element context) { 38 return "/"; 39 } 40 41 public String getUniquePath(Element context) { 42 return "/"; 43 } 44 45 public Document getDocument() { 46 return this; 47 } 48 49 public String getStringValue() { 50 Element root = getRootElement(); 51 return (root != null) ? root.getStringValue() : ""; 52 } 53 54 public String asXML() { 55 try { 56 StringWriter out = new StringWriter (); 57 XMLWriter writer = new XMLWriter(out, outputFormat); 58 writer.write(this); 59 return out.toString(); 60 } catch (IOException e) { 61 throw new RuntimeException ("Wierd IOException while generating textual representation: " + e.getMessage()); 62 } 63 } 64 65 public void write(Writer out) throws IOException { 66 XMLWriter writer = new XMLWriter(out, outputFormat); 67 writer.write(this); 68 } 69 70 75 public void accept(Visitor visitor) { 76 visitor.visit(this); 77 78 DocumentType docType = getDocType(); 79 if (docType != null) { 80 visitor.visit(docType); 81 } 82 83 List content = content(); 85 if (content != null) { 86 for (Iterator iter = content.iterator(); iter.hasNext();) { 87 Object object = iter.next(); 88 if (object instanceof String ) { 89 Text text = getNodeFactory().createText((String ) object); 90 visitor.visit(text); 91 } else { 92 Node node = (Node) object; 93 node.accept(visitor); 94 } 95 } 96 } 97 } 98 99 public String toString() { 100 return super.toString() + " [Document: name " + getName() + "]"; 101 } 102 103 public void normalize() { 104 Element element = getRootElement(); 105 if (element != null) { 106 element.normalize(); 107 } 108 } 109 110 public Document addComment(String comment) { 111 Comment node = getNodeFactory().createComment(comment); 112 add(node); 113 return this; 114 } 115 116 public Document addProcessingInstruction(String target, String data) { 117 ProcessingInstruction node = getNodeFactory().createProcessingInstruction(target, data); 118 add(node); 119 return this; 120 } 121 122 public Document addProcessingInstruction(String target, Map data) { 123 ProcessingInstruction node = getNodeFactory().createProcessingInstruction(target, data); 124 add(node); 125 return this; 126 } 127 128 public Element addElement(String name) { 129 checkAddElementAllowed(); 130 Element node = super.addElement(name); 131 rootElementAdded(node); 132 return node; 133 } 134 135 public Element addElement(String qualifiedName, String namespaceURI) { 136 checkAddElementAllowed(); 137 Element node = super.addElement(qualifiedName, namespaceURI); 138 rootElementAdded(node); 139 return node; 140 } 141 142 public Element addElement(QName qName) { 143 checkAddElementAllowed(); 144 Element node = super.addElement(qName); 145 rootElementAdded(node); 146 return node; 147 } 148 149 public void setRootElement(Element rootElement) { 150 clearContent(); 151 if (rootElement != null) { 152 super.add(rootElement); 153 rootElementAdded(rootElement); 154 } 155 } 156 157 public void add(Element element) { 158 checkAddElementAllowed(); 159 super.add(element); 160 rootElementAdded(element); 161 } 162 163 public boolean remove(Element element) { 164 boolean answer = super.remove(element); 165 Element root = getRootElement(); 166 if (root != null && answer) { 167 setRootElement(null); 168 } 169 element.setDocument(null); 170 return answer; 171 } 172 173 public Node asXPathResult(Element parent) { 174 return this; 175 } 176 177 178 protected void childAdded(Node node) { 179 if (node != null) { 180 node.setDocument(this); 181 } 182 } 183 184 protected void childRemoved(Node node) { 185 if (node != null) { 186 node.setDocument(null); 187 } 188 } 189 190 protected void checkAddElementAllowed() { 191 Element root = getRootElement(); 192 if (root != null) { 193 throw new IllegalAddException( 194 this, 195 root, 196 "Cannot add another element to this Document as it already has " 197 + " a root element of: " + root.getQualifiedName() 198 ); 199 } 200 } 201 202 203 protected abstract void rootElementAdded(Element rootElement); 204 205 } 206 207 208 252 | Popular Tags |