1 16 package org.apache.axis2.saaj; 17 18 import org.apache.axis2.om.*; 19 import org.apache.axis2.util.Dom2OmUtils; 20 import org.w3c.dom.DOMException ; 21 import org.w3c.dom.Document ; 22 import org.w3c.dom.NamedNodeMap ; 23 import org.w3c.dom.NodeList ; 24 25 import javax.xml.soap.Node ; 26 import javax.xml.soap.SOAPElement ; 27 import javax.xml.soap.SOAPException ; 28 import java.util.Iterator ; 29 30 36 public class NodeImpl implements Node { 37 38 41 protected org.apache.axis2.om.OMNode omNode; 42 45 protected org.w3c.dom.Document document; 46 47 49 53 public NodeImpl(){ 54 55 } 56 57 62 public NodeImpl(OMNode node){ 63 this.omNode = node; 64 } 65 70 public NodeImpl(OMAttribute attrib){ 71 } 76 77 82 public NodeImpl(OMNamespace ns){ 83 } 88 89 93 97 98 public OMNode getOMNode(){ 99 return omNode; 100 } 101 102 107 public String getValue() { 108 109 if(omNode.getType() == OMNode.TEXT_NODE) 110 return ((OMText)omNode).getText(); 111 else if(omNode.getType() == OMNode.ELEMENT_NODE) 112 return new NodeImpl(((OMElement)omNode).getFirstChild()).getValue(); 113 return null; 114 } 115 116 122 public void setParentElement(SOAPElement parent) throws SOAPException { 123 124 OMElement omParent = ((SOAPElementImpl)parent).getOMElement(); 125 omNode.setParent(omParent); 126 } 127 128 132 public SOAPElement getParentElement() { 133 134 OMElement omParent = (OMElement)omNode.getParent(); 135 return new SOAPElementImpl(omParent); 136 } 137 138 142 public void detachNode() { 143 144 omNode.detach(); 145 } 146 147 151 public void recycleNode() { 152 155 } 156 157 163 public void setValue(String value) { 164 165 if(omNode.getType() == OMNode.TEXT_NODE){ 166 OMElement parent = (OMElement)omNode.getParent(); 167 ((OMText)omNode).discard(); 168 omNode = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createText(parent, value); 169 } else if(omNode.getType() == OMNode.ELEMENT_NODE){ 170 OMNode firstChild = ((OMElement)omNode).getFirstChild(); 171 if(firstChild == null ){ 172 firstChild = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createText((OMElement)omNode, value); 173 } 174 else if(firstChild.getType() == OMNode.TEXT_NODE){ 175 ((OMText)firstChild).discard(); 176 firstChild = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createText((OMElement)omNode, value); 177 } 178 } else{ 179 throw new IllegalStateException (); 180 } 181 } 182 183 187 public short getNodeType() { 188 189 return (short)omNode.getType(); 190 } 191 192 196 public void normalize() { 197 200 } 201 202 206 public boolean hasAttributes() { 207 if(omNode instanceof OMElement){ 208 Iterator iter = ((OMElement)omNode).getAttributes(); 209 return (iter.hasNext() ? true : false); 210 } 211 return false; 212 } 213 214 218 public boolean hasChildNodes() { 219 if(omNode instanceof OMElement){ 220 Iterator iter = ((OMElement)omNode).getChildren(); 221 return (iter.hasNext() ? true : false); 222 } 223 return false; 224 } 225 226 230 public String getLocalName() { 231 if(omNode.getType() == ELEMENT_NODE || omNode.getType() 232 == ATTRIBUTE_NODE) 233 return ((OMElement)omNode).getLocalName(); 234 return null; 237 } 238 239 243 public String getNamespaceURI() { 244 245 return ((OMElement)omNode).getNamespace().getName(); 246 } 247 248 252 public String getNodeName() { 253 254 if(omNode.getType() == OMNode.ELEMENT_NODE ) 255 return ((OMElement)omNode).getLocalName(); 256 else if(omNode.getType() == OMNode.COMMENT_NODE) 257 return "#comment"; 258 else if(omNode.getType() == OMNode.CDATA_SECTION_NODE) 259 return "#cdata-section"; 260 else if(omNode.getType() == OMNode.TEXT_NODE) 261 return "#text"; 262 return null; 265 } 266 267 271 public String getNodeValue() throws DOMException { 272 if(omNode.getType() == OMNode.TEXT_NODE) 274 return ((OMText)omNode).getText(); 275 else 277 return null; 278 } 279 280 284 public String getPrefix() { 285 if(omNode.getType() == OMNode.ELEMENT_NODE) 286 return ((OMElement)omNode).getNamespace().getPrefix(); 287 return null; 288 } 289 290 294 public void setNodeValue(String value) throws DOMException { 295 296 if(omNode.getType() == OMNode.TEXT_NODE){ 297 OMElement parent = (OMElement)omNode.getParent(); 298 ((OMText)omNode).discard(); 299 omNode = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createText(parent, value); 300 } 301 } 302 303 307 public void setPrefix(String prefix) throws DOMException { 308 if(omNode.getType() == OMNode.ELEMENT_NODE ){ 310 OMNamespace ns = ((OMElement)omNode).getNamespace(); 311 String uri = ns.getName(); 312 OMNamespace newNs = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMNamespace(uri, prefix); 313 ((OMElement)omNode).setNamespace(newNs); 314 } 315 316 } 317 318 322 public void setOwnerDocument(Document doc){ 323 this.document = doc; 325 } 326 327 331 public Document getOwnerDocument() { 332 return document; 334 } 335 336 340 public NamedNodeMap getAttributes() { 341 Iterator iter = ((OMElement)omNode).getAttributes(); 345 346 return null; 347 } 348 349 353 public org.w3c.dom.Node getFirstChild() { 354 OMNode child = ((OMElement)omNode).getFirstChild(); 356 return new NodeImpl(child); 357 } 358 359 363 public org.w3c.dom.Node getLastChild() { 364 365 Iterator children = ((OMElement)omNode).getChildren(); 366 Object child = null; 367 while(children.hasNext()){ 368 child = children.next(); 369 } 370 if(child instanceof OMNode){ 371 return new NodeImpl((OMNode)child); 372 } 373 return null; 374 } 375 376 379 public org.w3c.dom.Node getNextSibling() { 380 381 OMNode sibling = omNode.getNextSibling(); 382 return new NodeImpl(sibling); 383 } 384 385 386 public org.w3c.dom.Node getParentNode() { 387 388 OMElement parent = (OMElement)omNode.getParent(); 389 return new NodeImpl(parent); 390 } 391 392 395 public org.w3c.dom.Node getPreviousSibling() { 396 397 OMNode prevSibling = omNode.getPreviousSibling(); 398 return new NodeImpl(prevSibling); 399 } 400 401 404 public org.w3c.dom.Node cloneNode(boolean deep) { 405 return null; 407 } 408 409 412 public NodeList getChildNodes() { 413 Iterator iter = ((OMElement)omNode).getChildren(); 414 NodeListImpl list = new NodeListImpl(); 415 while(iter.hasNext()){ 416 OMNode omChild = (OMNode)iter.next(); 417 Node child = new NodeImpl(omChild); 418 list.addNode(child); 419 } 420 return list; 421 } 422 423 426 public boolean isSupported(String arg0, String arg1) { 427 return false; 429 } 430 431 434 public org.w3c.dom.Node appendChild(org.w3c.dom.Node node) 435 throws DOMException { 436 437 OMNode child = Dom2OmUtils.toOM(node); 438 if(omNode.getType() == OMNode.ELEMENT_NODE) 439 ((OMElement)omNode).addChild(child); 440 return null; 441 } 442 443 446 public org.w3c.dom.Node removeChild(org.w3c.dom.Node oldChild) 447 throws DOMException { 448 OMNode child = Dom2OmUtils.toOM(oldChild); 450 if(omNode.getType() == OMNode.ELEMENT_NODE){ 451 Iterator iter = ((OMElement)omNode).getChildren(); 452 while(iter.hasNext()){ 453 Object nextChild = iter.next(); 454 if(nextChild instanceof OMNode && nextChild.equals(child)){ 455 ((OMElement)nextChild).discard(); 456 return oldChild; 457 } 458 } 459 } 460 461 return null; 462 } 463 464 467 public org.w3c.dom.Node insertBefore(org.w3c.dom.Node arg0, 468 org.w3c.dom.Node arg1) throws DOMException { 469 470 return null; 471 } 472 473 476 public org.w3c.dom.Node replaceChild(org.w3c.dom.Node newChild, 477 org.w3c.dom.Node refChild) throws DOMException { 478 OMNode newOmChild = Dom2OmUtils.toOM(newChild); 479 OMNode refOmChild = Dom2OmUtils.toOM(refChild); 480 if(omNode.getType() == OMNode.ELEMENT_NODE){ 481 Iterator iter = ((OMElement)omNode).getChildren(); 482 while(iter.hasNext()){ 483 Object nextChild = iter.next(); 484 if(nextChild instanceof OMNode && nextChild.equals(refOmChild)){ 485 486 } 487 } 488 } 489 return null; 490 } 491 492 public boolean equals(Object o){ 493 if(o instanceof NodeImpl){ 494 if(this.omNode.equals(((NodeImpl)o).omNode)) 495 return true; 496 } 497 return false; 498 } 499 500 } 501 | Popular Tags |