1 23 24 package org.enhydra.xml.dom; 25 26 import org.enhydra.xml.lazydom.LazyAttr; 27 import org.enhydra.xml.lazydom.LazyDocument; 28 import org.enhydra.xml.lazydom.LazyElement; 29 import org.enhydra.xml.lazydom.LazyNode; 30 import org.enhydra.xml.lazydom.LazyParent; 31 import org.w3c.dom.Attr ; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.DocumentType ; 34 import org.w3c.dom.Element ; 35 import org.w3c.dom.Node ; 36 37 41 44 55 public final class DOMAccess { 56 59 private Document fDocument; 60 61 68 public DOMAccess(Document document) { 69 fDocument = document; 70 } 71 72 77 public static DocumentType accessDocumentType(Document document) { 78 if (document instanceof LazyDocument) { 79 LazyDocument lazyDoc = (LazyDocument)document; 80 if (lazyDoc.isTemplateNode() || lazyDoc.isDocTypeExpanded()) { 81 return lazyDoc.getDoctype(); 82 } else { 83 return lazyDoc.getTemplateDocument().getDoctype(); 84 } 85 } else { 86 return document.getDoctype(); 87 } 88 } 89 90 94 public DocumentType accessDocumentType() { 95 return accessDocumentType(fDocument); 96 } 97 98 102 private static Node accessTemplateFirstChild(LazyDocument lazyDoc, 103 LazyParent lazyParent) { 104 LazyNode templateChild = (LazyNode)lazyParent.getFirstChild(); 105 if (templateChild == null) { 106 return null; } else if (lazyDoc.isTemplateNode()) { 108 return templateChild; } else { 110 LazyNode instanceChild = lazyDoc.getExpandedNode(templateChild.getNodeId()); 111 if (instanceChild != null) { 112 return instanceChild; 113 } else { 114 return templateChild; 115 } 116 } 117 } 118 119 126 public static Node accessFirstChild(Document document, 127 Node parent) { 128 if (parent instanceof LazyParent) { 129 LazyParent lazyParent = (LazyParent)parent; 130 if (lazyParent.isTemplateNode()) { 131 return accessTemplateFirstChild((LazyDocument)document, 133 lazyParent); 134 } else if (lazyParent.areChildrenExpanded()) { 135 return lazyParent.getFirstChild(); 137 } else { 138 return lazyParent.getTemplateNode().getFirstChild(); 140 } 141 } else { 142 return parent.getFirstChild(); 144 } 145 } 146 147 153 public Node accessFirstChild(Node parent) { 154 return accessFirstChild(fDocument, parent); 155 } 156 157 161 private static Node accessTemplateNextSibling(LazyDocument lazyDoc, 162 LazyNode templateNode) { 163 LazyNode templateSibling = (LazyNode)templateNode.getNextSibling(); 164 if (templateSibling == null) { 165 return null; } else { 167 LazyNode instanceSibling = lazyDoc.getExpandedNode(templateSibling.getNodeId()); 168 if (instanceSibling != null) { 169 return instanceSibling; 170 } else { 171 return templateSibling; 172 } 173 } 174 } 175 176 180 private static Node accessInstanceNextSibling(LazyDocument lazyDoc, 181 LazyNode instanceNode) { 182 if (instanceNode instanceof LazyParent) { 183 if (((LazyParent)instanceNode).isParentExpanded()) { 187 return instanceNode.getNextSibling(); 188 } else { 189 LazyNode templateNode = instanceNode.getTemplateNode(); 191 LazyNode templateSibling = (LazyNode)instanceNode.getNextSibling(); 192 if (templateSibling != null) { 193 LazyNode instanceSibling = lazyDoc.getExpandedNode(templateSibling.getNodeId()); 194 if (instanceSibling != null) { 195 return instanceSibling; 196 } else { 197 return templateSibling; 198 } 199 } else { 200 return null; } 202 } 203 } else { 204 return instanceNode.getNextSibling(); 206 } 207 } 208 209 216 public static Node accessNextSibling(Document document, 217 Node node) { 218 if (node instanceof LazyNode) { 219 LazyNode lazyNode = (LazyNode)node; 220 if (lazyNode.isTemplateNode()) { 221 return accessTemplateNextSibling((LazyDocument)document, 223 lazyNode); 224 } else { 225 return accessInstanceNextSibling((LazyDocument)document, 226 lazyNode); 227 } 228 } else { 229 return node.getNextSibling(); 231 } 232 } 233 234 240 public Node accessNextSibling(Node node) { 241 return accessNextSibling(fDocument, node); 242 } 243 244 250 public static Element accessDocumentElement(Document document) { 251 if (document instanceof LazyDocument) { 252 Node child = accessFirstChild(document, document); 253 while (child != null) { 254 if (child instanceof Element) { 255 break; 256 } 257 child = accessNextSibling(document, child); 258 } 259 return (Element)child; 260 } else { 261 return document.getDocumentElement(); 262 } 263 } 264 265 270 public Element accessDocumentElement() { 271 return accessDocumentElement(fDocument); 272 } 273 274 284 public static Attr accessAttribute(Document document, 285 Element element, 286 String namespaceURI, 287 String name) { 288 Element accessElement; 291 if (element instanceof LazyElement) { 292 LazyElement lazyElement = (LazyElement)element; 293 if (lazyElement.areAttributesExpanded()) { 294 accessElement = lazyElement; 296 } else { 297 accessElement = lazyElement.getTemplateElement(); 299 } 300 } else { 301 accessElement = element; 303 } 304 305 if (namespaceURI != null) { 307 return accessElement.getAttributeNodeNS(namespaceURI, name); 308 } else { 309 return accessElement.getAttributeNode(name); 310 } 311 } 312 313 322 public Attr accessAttribute(Element element, 323 String namespaceURI, 324 String name) { 325 return accessAttribute(fDocument, element, namespaceURI, name); 326 } 327 328 335 public static String accessAttributeValue(Document document, 336 Attr attr) { 337 if (attr instanceof LazyAttr) { 338 LazyAttr lazyAttr = (LazyAttr)attr; 339 if (lazyAttr.areChildrenExpanded()) { 340 return lazyAttr.getValue(); 342 } else { 343 return ((LazyAttr)lazyAttr.getTemplateNode()).getValue(); 345 } 346 } else { 347 return attr.getValue(); 349 } 350 } 351 352 358 public String accessAttributeValue(Attr attr) { 359 return accessAttributeValue(fDocument, attr); 360 } 361 362 370 public static Node getExpandedNode(Document document, 371 Node node) { 372 if ((node instanceof LazyNode) && ((LazyNode)node).isTemplateNode()) { 373 return (Node)((LazyDocument)document).getNodeById(((LazyNode)node).getNodeId()); 374 } else { 375 return node; 376 } 377 } 378 379 386 public Node getExpandedNode(Node node) { 387 return getExpandedNode(fDocument, node); 388 } 389 390 399 public static Element getExpandedElement(Document document, 400 Element element) { 401 if ((element instanceof LazyElement) && ((LazyElement)element).isTemplateNode()) { 402 return (Element)((LazyDocument)document).getNodeById(((LazyElement)element).getNodeId()); 403 } else { 404 return element; 405 } 406 } 407 408 416 public Element getExpandedElement(Element element) { 417 return getExpandedElement(fDocument, element); 418 } 419 420 } 421 | Popular Tags |