1 9 10 package org.ozoneDB.xml.dom4j.o3impl; 11 12 import org.dom4j.*; 13 import org.xml.sax.EntityResolver ; 14 15 import java.util.Collections ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 25 public class DefaultDocument extends AbstractDocument { 26 27 protected static final List EMPTY_LIST = Collections.EMPTY_LIST; 28 protected static final Iterator EMPTY_ITERATOR = EMPTY_LIST.iterator(); 29 30 31 private String name; 32 33 34 private Element rootElement; 35 36 37 private List content; 38 39 40 private DocumentType docType; 41 42 43 private transient EntityResolver entityResolver; 44 45 public DefaultDocument() { 46 } 47 48 public DefaultDocument(String name) { 49 this.name = name; 50 } 51 52 public DefaultDocument(Element rootElement) { 53 this.rootElement = rootElement; 54 } 55 56 public DefaultDocument(DocumentType docType) { 57 this.docType = docType; 58 } 59 60 public DefaultDocument(Element rootElement, DocumentType docType) { 61 this.rootElement = rootElement; 62 this.docType = docType; 63 } 64 65 public DefaultDocument(String name, Element rootElement, DocumentType docType) { 66 this.name = name; 67 this.rootElement = rootElement; 68 this.docType = docType; 69 } 70 71 public String getName() { 72 return name; 73 } 74 75 public void setName(String name) { 76 this.name = name; 77 } 78 79 80 public Element getRootElement() { 81 return rootElement; 82 } 83 84 public DocumentType getDocType() { 85 return docType; 86 } 87 88 public void setDocType(DocumentType docType) { 89 this.docType = docType; 90 } 91 92 public Document addDocType(String name, String publicId, String systemId) { 93 setDocType(getNodeFactory().createDocType(name, publicId, systemId)); 94 return this; 95 } 96 97 public EntityResolver getEntityResolver() { 98 return entityResolver; 99 } 100 101 public void setEntityResolver(EntityResolver entityResolver) { 102 this.entityResolver = entityResolver; 103 } 104 105 public Object clone() { 106 DefaultDocument document = (DefaultDocument) super.clone(); 107 document.rootElement = null; 108 document.content = null; 109 document.appendContent(this); 110 return document; 111 } 112 113 public List processingInstructions() { 114 List source = contentList(); 115 List answer = createResultList(); 116 int size = source.size(); 117 for (int i = 0; i < size; i++) { 118 Object object = source.get(i); 119 if (object instanceof ProcessingInstruction) { 120 answer.add(object); 121 } 122 } 123 return answer; 124 } 125 126 public List processingInstructions(String target) { 127 List source = contentList(); 128 List answer = createResultList(); 129 int size = source.size(); 130 for (int i = 0; i < size; i++) { 131 Object object = source.get(i); 132 if (object instanceof ProcessingInstruction) { 133 ProcessingInstruction pi = (ProcessingInstruction) object; 134 if (target.equals(pi.getName())) { 135 answer.add(pi); 136 } 137 } 138 } 139 return answer; 140 } 141 142 public ProcessingInstruction processingInstruction(String target) { 143 List source = contentList(); 144 int size = source.size(); 145 for (int i = 0; i < size; i++) { 146 Object object = source.get(i); 147 if (object instanceof ProcessingInstruction) { 148 ProcessingInstruction pi = (ProcessingInstruction) object; 149 if (target.equals(pi.getName())) { 150 return pi; 151 } 152 } 153 } 154 return null; 155 } 156 157 public boolean removeProcessingInstruction(String target) { 158 List source = contentList(); 159 for (Iterator iter = source.iterator(); iter.hasNext();) { 160 Object object = iter.next(); 161 if (object instanceof ProcessingInstruction) { 162 ProcessingInstruction pi = (ProcessingInstruction) object; 163 if (target.equals(pi.getName())) { 164 iter.remove(); 165 return true; 166 } 167 } 168 } 169 return false; 170 } 171 172 173 public void setContent(List content) { 174 rootElement = null; 175 contentRemoved(); 176 if (content instanceof ContentListFacade) { 177 content = ((ContentListFacade) content).getBackingList(); 178 } 179 if (content == null) { 180 this.content = null; 181 } else { 182 int size = content.size(); 183 List newContent = createContentList(size); 184 for (int i = 0; i < size; i++) { 185 Object object = content.get(i); 186 if (object instanceof Node) { 187 Node node = (Node) object; 188 Document doc = node.getDocument(); 189 if (doc != null && doc != this) { 190 node = (Node) node.clone(); 191 } 192 if (node instanceof Element) { 193 if (rootElement == null) { 194 rootElement = (Element) node; 195 } else { 196 throw new IllegalAddException("A document may only contain one Element: " + content); 197 } 198 } 199 newContent.add(node); 200 childAdded(node); 201 } 202 } 203 this.content = newContent; 204 } 205 } 206 207 public void clearContent() { 208 contentRemoved(); 209 content = null; 210 rootElement = null; 211 } 212 213 214 protected List contentList() { 217 if (content == null) { 218 content = createContentList(); 219 if (rootElement != null) { 220 content.add(rootElement); 221 } 222 } 223 return content; 224 } 225 226 227 protected void addNode(Node node) { 228 if (node != null) { 229 Document document = node.getDocument(); 230 if (document != null && document != this) { 231 String message = "The Node already has an existing document: " + document; 233 throw new IllegalAddException(this, node, message); 234 } 235 contentList().add(node); 236 childAdded(node); 237 } 238 } 239 240 protected void addNode(int index, Node node) { 241 if (node != null) { 242 Document document = node.getDocument(); 243 if (document != null && document != this) { 244 String message = "The Node already has an existing document: " + document; 246 throw new IllegalAddException(this, node, message); 247 } 248 contentList().add(index, node); 249 childAdded(node); 250 } 251 } 252 253 protected boolean removeNode(Node node) { 254 if (node == rootElement) { 255 rootElement = null; 256 } 257 if (contentList().remove(node)) { 258 childRemoved(node); 259 return true; 260 } 261 return false; 262 } 263 264 protected void rootElementAdded(Element element) { 265 this.rootElement = element; 266 element.setDocument(this); 267 } 268 269 } 270 271 272 316 | Popular Tags |