| 1 |  |  | 
| 2 |  |  | 
| 3 |  |  | 
| 4 |  |  | 
| 5 |  |  | 
| 6 |  |  | 
| 7 |  |  | 
| 8 |  | package org.dom4j.tree; | 
| 9 |  |  | 
| 10 |  | import java.io.IOException; | 
| 11 |  | import java.io.StringWriter; | 
| 12 |  | import java.io.Writer; | 
| 13 |  | import java.util.Iterator; | 
| 14 |  | import java.util.List; | 
| 15 |  | import java.util.Map; | 
| 16 |  |  | 
| 17 |  | import org.dom4j.Comment; | 
| 18 |  | import org.dom4j.Document; | 
| 19 |  | import org.dom4j.DocumentType; | 
| 20 |  | import org.dom4j.Element; | 
| 21 |  | import org.dom4j.IllegalAddException; | 
| 22 |  | import org.dom4j.Node; | 
| 23 |  | import org.dom4j.ProcessingInstruction; | 
| 24 |  | import org.dom4j.QName; | 
| 25 |  | import org.dom4j.Text; | 
| 26 |  | import org.dom4j.Visitor; | 
| 27 |  | import org.dom4j.io.OutputFormat; | 
| 28 |  | import org.dom4j.io.XMLWriter; | 
| 29 |  |  | 
| 30 |  |  | 
| 31 |  |  | 
| 32 |  |  | 
| 33 |  |  | 
| 34 |  |  | 
| 35 |  |  | 
| 36 |  |  | 
| 37 |  |  | 
| 38 |  |  | 
| 39 |  | public abstract class AbstractDocument extends AbstractBranch implements | 
| 40 |  | Document { | 
| 41 |  |  | 
| 42 |  |  | 
| 43 |  | protected String encoding; | 
| 44 |  |  | 
| 45 | 6283 | public AbstractDocument() { | 
| 46 |  | } | 
| 47 |  |  | 
| 48 | 25 | public short getNodeType() { | 
| 49 | 25 | return DOCUMENT_NODE; | 
| 50 |  | } | 
| 51 |  |  | 
| 52 | 1 | public String getPath(Element context) { | 
| 53 | 1 | return "/"; | 
| 54 |  | } | 
| 55 |  |  | 
| 56 | 1 | public String getUniquePath(Element context) { | 
| 57 | 1 | return "/"; | 
| 58 |  | } | 
| 59 |  |  | 
| 60 | 6459 | public Document getDocument() { | 
| 61 | 6459 | return this; | 
| 62 |  | } | 
| 63 |  |  | 
| 64 | 0 | public String getXMLEncoding() { | 
| 65 | 0 | return null; | 
| 66 |  | } | 
| 67 |  |  | 
| 68 | 0 | public String getStringValue() { | 
| 69 | 0 | Element root = getRootElement(); | 
| 70 |  |  | 
| 71 | 0 | return (root != null) ? root.getStringValue() : ""; | 
| 72 |  | } | 
| 73 |  |  | 
| 74 | 58 | public String asXML() { | 
| 75 | 58 | OutputFormat format = new OutputFormat(); | 
| 76 | 58 | format.setEncoding(encoding); | 
| 77 |  |  | 
| 78 | 58 | try { | 
| 79 | 58 | StringWriter out = new StringWriter(); | 
| 80 | 58 | XMLWriter writer = new XMLWriter(out, format); | 
| 81 | 58 | writer.write(this); | 
| 82 | 58 | writer.flush(); | 
| 83 |  |  | 
| 84 | 58 | return out.toString(); | 
| 85 |  | } catch (IOException e) { | 
| 86 | 0 | throw new RuntimeException("IOException while generating textual " | 
| 87 |  | + "representation: " + e.getMessage()); | 
| 88 |  | } | 
| 89 |  | } | 
| 90 |  |  | 
| 91 | 0 | public void write(Writer out) throws IOException { | 
| 92 | 0 | OutputFormat format = new OutputFormat(); | 
| 93 | 0 | format.setEncoding(encoding); | 
| 94 |  |  | 
| 95 | 0 | XMLWriter writer = new XMLWriter(out, format); | 
| 96 | 0 | writer.write(this); | 
| 97 |  | } | 
| 98 |  |  | 
| 99 |  |  | 
| 100 |  |  | 
| 101 |  |  | 
| 102 |  |  | 
| 103 |  |  | 
| 104 |  |  | 
| 105 |  |  | 
| 106 |  |  | 
| 107 |  |  | 
| 108 | 0 | public void accept(Visitor visitor) { | 
| 109 | 0 | visitor.visit(this); | 
| 110 |  |  | 
| 111 | 0 | DocumentType docType = getDocType(); | 
| 112 |  |  | 
| 113 | 0 | if (docType != null) { | 
| 114 | 0 | visitor.visit(docType); | 
| 115 |  | } | 
| 116 |  |  | 
| 117 |  |  | 
| 118 | 0 | List content = content(); | 
| 119 |  |  | 
| 120 | 0 | if (content != null) { | 
| 121 | 0 | for (Iterator iter = content.iterator(); iter.hasNext();) { | 
| 122 | 0 | Object object = iter.next(); | 
| 123 |  |  | 
| 124 | 0 | if (object instanceof String) { | 
| 125 | 0 | Text text = getDocumentFactory() | 
| 126 |  | .createText((String) object); | 
| 127 | 0 | visitor.visit(text); | 
| 128 |  | } else { | 
| 129 | 0 | Node node = (Node) object; | 
| 130 | 0 | node.accept(visitor); | 
| 131 |  | } | 
| 132 |  | } | 
| 133 |  | } | 
| 134 |  | } | 
| 135 |  |  | 
| 136 | 159 | public String toString() { | 
| 137 | 159 | return super.toString() + " [Document: name " + getName() + "]"; | 
| 138 |  | } | 
| 139 |  |  | 
| 140 | 110 | public void normalize() { | 
| 141 | 110 | Element element = getRootElement(); | 
| 142 |  |  | 
| 143 | 110 | if (element != null) { | 
| 144 | 110 | element.normalize(); | 
| 145 |  | } | 
| 146 |  | } | 
| 147 |  |  | 
| 148 | 24 | public Document addComment(String comment) { | 
| 149 | 24 | Comment node = getDocumentFactory().createComment(comment); | 
| 150 | 24 | add(node); | 
| 151 |  |  | 
| 152 | 24 | return this; | 
| 153 |  | } | 
| 154 |  |  | 
| 155 | 25 | public Document addProcessingInstruction(String target, String data) { | 
| 156 | 25 | ProcessingInstruction node = getDocumentFactory() | 
| 157 |  | .createProcessingInstruction(target, data); | 
| 158 | 25 | add(node); | 
| 159 |  |  | 
| 160 | 25 | return this; | 
| 161 |  | } | 
| 162 |  |  | 
| 163 | 0 | public Document addProcessingInstruction(String target, Map data) { | 
| 164 | 0 | ProcessingInstruction node = getDocumentFactory() | 
| 165 |  | .createProcessingInstruction(target, data); | 
| 166 | 0 | add(node); | 
| 167 |  |  | 
| 168 | 0 | return this; | 
| 169 |  | } | 
| 170 |  |  | 
| 171 | 434 | public Element addElement(String name) { | 
| 172 | 434 | Element element = getDocumentFactory().createElement(name); | 
| 173 | 434 | add(element); | 
| 174 |  |  | 
| 175 | 434 | return element; | 
| 176 |  | } | 
| 177 |  |  | 
| 178 | 2 | public Element addElement(String qualifiedName, String namespaceURI) { | 
| 179 | 2 | Element element = getDocumentFactory().createElement(qualifiedName, | 
| 180 |  | namespaceURI); | 
| 181 | 2 | add(element); | 
| 182 |  |  | 
| 183 | 2 | return element; | 
| 184 |  | } | 
| 185 |  |  | 
| 186 | 5835 | public Element addElement(QName qName) { | 
| 187 | 5835 | Element element = getDocumentFactory().createElement(qName); | 
| 188 | 5835 | add(element); | 
| 189 |  |  | 
| 190 | 5835 | return element; | 
| 191 |  | } | 
| 192 |  |  | 
| 193 | 4 | public void setRootElement(Element rootElement) { | 
| 194 | 4 | clearContent(); | 
| 195 |  |  | 
| 196 | 4 | if (rootElement != null) { | 
| 197 | 4 | super.add(rootElement); | 
| 198 | 4 | rootElementAdded(rootElement); | 
| 199 |  | } | 
| 200 |  | } | 
| 201 |  |  | 
| 202 | 6288 | public void add(Element element) { | 
| 203 | 6288 | checkAddElementAllowed(element); | 
| 204 | 6287 | super.add(element); | 
| 205 | 6287 | rootElementAdded(element); | 
| 206 |  | } | 
| 207 |  |  | 
| 208 | 2 | public boolean remove(Element element) { | 
| 209 | 2 | boolean answer = super.remove(element); | 
| 210 | 2 | Element root = getRootElement(); | 
| 211 |  |  | 
| 212 | 2 | if ((root != null) && answer) { | 
| 213 | 0 | setRootElement(null); | 
| 214 |  | } | 
| 215 |  |  | 
| 216 | 2 | element.setDocument(null); | 
| 217 |  |  | 
| 218 | 2 | return answer; | 
| 219 |  | } | 
| 220 |  |  | 
| 221 | 0 | public Node asXPathResult(Element parent) { | 
| 222 | 0 | return this; | 
| 223 |  | } | 
| 224 |  |  | 
| 225 | 6341 | protected void childAdded(Node node) { | 
| 226 | 6341 | if (node != null) { | 
| 227 | 6341 | node.setDocument(this); | 
| 228 |  | } | 
| 229 |  | } | 
| 230 |  |  | 
| 231 | 4 | protected void childRemoved(Node node) { | 
| 232 | 4 | if (node != null) { | 
| 233 | 4 | node.setDocument(null); | 
| 234 |  | } | 
| 235 |  | } | 
| 236 |  |  | 
| 237 | 6288 | protected void checkAddElementAllowed(Element element) { | 
| 238 | 6288 | Element root = getRootElement(); | 
| 239 |  |  | 
| 240 | 6288 | if (root != null) { | 
| 241 | 1 | throw new IllegalAddException(this, element, | 
| 242 |  | "Cannot add another element to this " | 
| 243 |  | + "Document as it already has a root " | 
| 244 |  | + "element of: " + root.getQualifiedName()); | 
| 245 |  | } | 
| 246 |  | } | 
| 247 |  |  | 
| 248 |  |  | 
| 249 |  |  | 
| 250 |  |  | 
| 251 |  |  | 
| 252 |  |  | 
| 253 |  |  | 
| 254 |  | protected abstract void rootElementAdded(Element rootElement); | 
| 255 |  |  | 
| 256 | 11346 | public void setXMLEncoding(String enc) { | 
| 257 | 11346 | this.encoding = enc; | 
| 258 |  | } | 
| 259 |  | } | 
| 260 |  |  | 
| 261 |  |  | 
| 262 |  |  | 
| 263 |  |  | 
| 264 |  |  | 
| 265 |  |  | 
| 266 |  |  | 
| 267 |  |  | 
| 268 |  |  | 
| 269 |  |  | 
| 270 |  |  | 
| 271 |  |  | 
| 272 |  |  | 
| 273 |  |  | 
| 274 |  |  | 
| 275 |  |  | 
| 276 |  |  | 
| 277 |  |  | 
| 278 |  |  | 
| 279 |  |  | 
| 280 |  |  | 
| 281 |  |  | 
| 282 |  |  | 
| 283 |  |  | 
| 284 |  |  | 
| 285 |  |  | 
| 286 |  |  | 
| 287 |  |  | 
| 288 |  |  | 
| 289 |  |  | 
| 290 |  |  | 
| 291 |  |  | 
| 292 |  |  | 
| 293 |  |  | 
| 294 |  |  | 
| 295 |  |  | 
| 296 |  |  |