1 9 10 package org.ozoneDB.xml.dom4j.o3impl; 11 12 import org.dom4j.*; 13 import org.dom4j.io.OutputFormat; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.StringTokenizer ; 19 20 26 public abstract class AbstractBranch extends AbstractNode implements Branch { 27 28 29 protected static final OutputFormat outputFormat = new OutputFormat(); 30 31 protected static final int DEFAULT_CONTENT_LIST_SIZE = 5; 32 33 34 public AbstractBranch() { 35 } 36 37 38 public boolean isReadOnly() { 39 return false; 40 } 41 42 public boolean hasContent() { 43 return nodeCount() > 0; 44 } 45 46 public List content() { 47 List backingList = contentList(); 48 return new ContentListFacade(this, backingList); 49 } 50 51 public String getText() { 52 List content = contentList(); 53 if (content != null) { 54 int size = content.size(); 55 if (size >= 1) { 56 Object first = content.get(0); 57 String firstText = getContentAsText(first); 58 if (size == 1) { 59 return firstText; 61 } else { 62 StringBuffer buffer = new StringBuffer (firstText); 63 for (int i = 1; i < size; i++) { 64 Object node = content.get(i); 65 buffer.append(getContentAsText(node)); 66 } 67 return buffer.toString(); 68 } 69 } 70 } 71 return ""; 72 } 73 74 77 protected String getContentAsText(Object content) { 78 if (content instanceof Node) { 79 Node node = (Node) content; 80 switch (node.getNodeType()) { 81 case CDATA_SECTION_NODE: 82 case ENTITY_REFERENCE_NODE: 84 case TEXT_NODE: 85 return node.getText(); 86 } 87 } else if (content instanceof String ) { 88 return (String ) content; 89 } 90 return ""; 91 } 92 93 95 protected String getContentAsStringValue(Object content) { 96 if (content instanceof Node) { 97 Node node = (Node) content; 98 switch (node.getNodeType()) { 99 case CDATA_SECTION_NODE: 100 case ENTITY_REFERENCE_NODE: 102 case TEXT_NODE: 103 case ELEMENT_NODE: 104 return node.getStringValue(); 105 } 106 } else if (content instanceof String ) { 107 return (String ) content; 108 } 109 return ""; 110 } 111 112 113 public String getTextTrim() { 114 String text = getText(); 115 116 StringBuffer textContent = new StringBuffer (); 117 StringTokenizer tokenizer = new StringTokenizer (text); 118 while (tokenizer.hasMoreTokens()) { 119 String str = tokenizer.nextToken(); 120 textContent.append(str); 121 if (tokenizer.hasMoreTokens()) { 122 textContent.append(" "); } 124 } 125 126 return textContent.toString(); 127 } 128 129 public void setProcessingInstructions(List listOfPIs) { 130 for (Iterator iter = listOfPIs.iterator(); iter.hasNext();) { 131 ProcessingInstruction pi = (ProcessingInstruction) iter.next(); 132 addNode(pi); 133 } 134 } 135 136 public Element addElement(String name) { 137 Element node = getNodeFactory().createElement(name); 138 add(node); 139 return node; 140 } 141 142 public Element addElement(String qualifiedName, String namespaceURI) { 143 Element node = getNodeFactory().createElement(qualifiedName, namespaceURI); 144 add(node); 145 return node; 146 } 147 148 public Element addElement(QName qname) { 149 Element node = getNodeFactory().createElement(qname); 150 add(node); 151 return node; 152 } 153 154 public Element addElement(String name, String prefix, String uri) { 155 Namespace namespace = getNodeFactory().createNamespace(prefix, uri); 156 QName qName = getNodeFactory().createQName(name, namespace); 157 return addElement(qName); 158 } 159 160 162 public void add(Node node) { 163 switch (node.getNodeType()) { 164 case ELEMENT_NODE: 165 add((Element) node); 166 break; 167 case COMMENT_NODE: 168 add((Comment) node); 169 break; 170 case PROCESSING_INSTRUCTION_NODE: 171 add((ProcessingInstruction) node); 172 break; 173 default: 174 invalidNodeTypeAddException(node); 175 } 176 } 177 178 public boolean remove(Node node) { 179 switch (node.getNodeType()) { 180 case ELEMENT_NODE: 181 return remove((Element) node); 182 case COMMENT_NODE: 183 return remove((Comment) node); 184 case PROCESSING_INSTRUCTION_NODE: 185 return remove((ProcessingInstruction) node); 186 default: 187 invalidNodeTypeAddException(node); 188 return false; 189 } 190 } 191 192 194 public void add(Comment comment) { 195 addNode(comment); 196 } 197 198 public void add(Element element) { 199 addNode(element); 200 } 201 202 public void add(ProcessingInstruction pi) { 203 addNode(pi); 204 } 205 206 public boolean remove(Comment comment) { 207 return removeNode(comment); 208 } 209 210 public boolean remove(Element element) { 211 return removeNode(element); 212 } 213 214 public boolean remove(ProcessingInstruction pi) { 215 return removeNode(pi); 216 } 217 218 219 public Element elementByID(String elementID) { 220 for (int i = 0, size = nodeCount(); i < size; i++) { 221 Node node = node(i); 222 if (node instanceof Element) { 223 Element element = (Element) node; 224 String id = elementID(element); 225 if (id != null && id.equals(elementID)) { 226 return element; 227 } else { 228 element = element.elementByID(elementID); 229 if (element != null) { 230 return element; 231 } 232 } 233 } 234 } 235 return null; 236 } 237 238 public void appendContent(Branch branch) { 239 for (int i = 0, size = branch.nodeCount(); i < size; i++) { 240 Node node = branch.node(i); 241 add((Node) node.clone()); 242 } 243 } 244 245 246 public Node node(int index) { 247 Object object = contentList().get(index); 248 if (object instanceof Node) { 249 return (Node) object; 250 } 251 if (object instanceof String ) { 252 return getNodeFactory().createText(object.toString()); 253 } 254 return null; 255 } 256 257 public int nodeCount() { 258 return contentList().size(); 259 } 260 261 public int indexOf(Node node) { 262 return contentList().indexOf(node); 263 } 264 265 public Iterator nodeIterator() { 266 return contentList().iterator(); 267 } 268 269 270 272 274 protected String elementID(Element element) { 275 return element.attributeValue("ID"); 278 } 279 280 281 protected abstract List contentList(); 282 283 286 protected List createContentList() { 287 return new ArrayList (DEFAULT_CONTENT_LIST_SIZE); 288 } 289 290 293 protected List createContentList(int size) { 294 return new ArrayList (size); 295 } 296 297 298 301 protected BackedList createResultList() { 302 return new BackedList(this, contentList()); 303 } 304 305 308 protected List createSingleResultList(Object result) { 309 BackedList list = new BackedList(this, contentList(), 1); 310 list.addLocal(result); 311 return list; 312 } 313 314 317 protected List createEmptyList() { 318 return new BackedList(this, contentList(), 0); 319 } 320 321 322 protected abstract void addNode(Node node); 323 324 protected abstract void addNode(int index, Node node); 325 326 protected abstract boolean removeNode(Node node); 327 328 329 333 protected abstract void childAdded(Node node); 334 335 339 protected abstract void childRemoved(Node node); 340 341 345 protected void contentRemoved() { 346 List content = contentList(); 347 for (int i = 0, size = content.size(); i < size; i++) { 348 Object object = content.get(i); 349 if (object instanceof Node) { 350 childRemoved((Node) object); 351 } 352 } 353 } 354 355 358 protected void invalidNodeTypeAddException(Node node) { 359 throw new IllegalAddException("Invalid node type. Cannot add node: " + node + " to this branch: " + this); 360 } 361 362 363 } 364 365 366 410 | Popular Tags |