1 16 package com.google.gwt.xml.client.impl; 17 18 import com.google.gwt.core.client.JavaScriptException; 19 import com.google.gwt.core.client.JavaScriptObject; 20 import com.google.gwt.xml.client.DOMException; 21 import com.google.gwt.xml.client.Document; 22 import com.google.gwt.xml.client.NamedNodeMap; 23 import com.google.gwt.xml.client.Node; 24 import com.google.gwt.xml.client.NodeList; 25 26 29 class NodeImpl extends DOMItem implements Node { 30 31 37 static Node build(JavaScriptObject node) { 38 if (node == null) { 39 return null; 40 } 41 short nodeType = XMLParserImpl.getNodeType(node); 42 switch (nodeType) { 43 case Node.ATTRIBUTE_NODE: 44 return new AttrImpl(node); 45 case Node.CDATA_SECTION_NODE: 46 return new CDATASectionImpl(node); 47 case Node.COMMENT_NODE: 48 return new CommentImpl(node); 49 case Node.DOCUMENT_FRAGMENT_NODE: 50 return new DocumentFragmentImpl(node); 51 case Node.DOCUMENT_NODE: 52 return new DocumentImpl(node); 53 case Node.ELEMENT_NODE: 54 return new ElementImpl(node); 55 case Node.PROCESSING_INSTRUCTION_NODE: 56 return new ProcessingInstructionImpl(node); 57 case Node.TEXT_NODE: 58 return new TextImpl(node); 59 default: 60 return new NodeImpl(node); 61 } 62 } 63 64 69 protected NodeImpl(JavaScriptObject jso) { 70 super(jso); 71 } 72 73 77 public Node appendChild(Node newChild) { 78 try { 79 final JavaScriptObject newChildJs = ((DOMItem) newChild).getJsObject(); 80 final JavaScriptObject appendChildResults = XMLParserImpl.appendChild( 81 this.getJsObject(), newChildJs); 82 return NodeImpl.build(appendChildResults); 83 } catch (JavaScriptException e) { 84 throw new DOMNodeException(DOMException.INVALID_MODIFICATION_ERR, e, this); 85 } 86 } 87 88 92 public Node cloneNode(boolean deep) { 93 return NodeImpl.build(XMLParserImpl.cloneNode(this.getJsObject(), deep)); 94 } 95 96 public NamedNodeMap getAttributes() { 97 return new NamedNodeMapImpl(XMLParserImpl.getAttributes(this.getJsObject())); 98 } 99 100 public NodeList getChildNodes() { 101 return new NodeListImpl(XMLParserImpl.getChildNodes(this.getJsObject())); 102 } 103 104 public Node getFirstChild() { 105 return getChildNodes().item(0); 106 } 107 108 public Node getLastChild() { 109 return getChildNodes().item(getChildNodes().getLength() - 1); 110 } 111 112 116 public String getNamespaceURI() { 117 return XMLParserImpl.getNamespaceURI(this.getJsObject()); 118 } 119 120 public Node getNextSibling() { 121 return NodeImpl.build(XMLParserImpl.getNextSibling(this.getJsObject())); 122 } 123 124 public String getNodeName() { 125 return XMLParserImpl.getNodeName(this.getJsObject()); 126 } 127 128 public short getNodeType() { 129 return XMLParserImpl.getNodeType(this.getJsObject()); 130 } 131 132 public String getNodeValue() { 133 return XMLParserImpl.getNodeValue(this.getJsObject()); 134 } 135 136 public Document getOwnerDocument() { 137 return (Document) NodeImpl.build(XMLParserImpl.getOwnerDocument(this 138 .getJsObject())); 139 } 140 141 public Node getParentNode() { 142 return NodeImpl.build(XMLParserImpl.getParentNode(this.getJsObject())); 143 } 144 145 149 public String getPrefix() { 150 return XMLParserImpl.getPrefix(this.getJsObject()); 151 } 152 153 public Node getPreviousSibling() { 154 return NodeImpl.build(XMLParserImpl.getPreviousSibling(this.getJsObject())); 155 } 156 157 161 public boolean hasAttributes() { 162 return XMLParserImpl.hasAttributes(this.getJsObject()); 163 } 164 165 169 public boolean hasChildNodes() { 170 return XMLParserImpl.hasChildNodes(this.getJsObject()); 171 } 172 173 177 public Node insertBefore(Node newChild, Node refChild) { 178 try { 179 final JavaScriptObject newChildJs = ((DOMItem) newChild).getJsObject(); 180 final JavaScriptObject refChildJs; 181 if (refChild != null) { 182 refChildJs = ((DOMItem) refChild).getJsObject(); 183 } else { 184 refChildJs = null; 185 } 186 final JavaScriptObject insertBeforeResults = XMLParserImpl.insertBefore( 187 this.getJsObject(), newChildJs, refChildJs); 188 return NodeImpl.build(insertBeforeResults); 189 } catch (JavaScriptException e) { 190 throw new DOMNodeException(DOMException.INVALID_MODIFICATION_ERR, e, this); 191 } 192 } 193 194 198 public void normalize() { 199 XMLParserImpl.normalize(this.getJsObject()); 200 } 201 202 206 public Node removeChild(Node oldChild) { 207 try { 208 final JavaScriptObject oldChildJs = ((DOMItem) oldChild).getJsObject(); 209 final JavaScriptObject removeChildResults = XMLParserImpl.removeChild( 210 this.getJsObject(), oldChildJs); 211 return NodeImpl.build(removeChildResults); 212 } catch (JavaScriptException e) { 213 throw new DOMNodeException(DOMException.INVALID_MODIFICATION_ERR, e, this); 214 } 215 } 216 217 221 public Node replaceChild(Node newChild, Node oldChild) { 222 try { 223 final JavaScriptObject newChildJs = ((DOMItem) newChild).getJsObject(); 224 final JavaScriptObject oldChildJs = ((DOMItem) oldChild).getJsObject(); 225 final JavaScriptObject replaceChildResults = XMLParserImpl.replaceChild( 226 this.getJsObject(), newChildJs, oldChildJs); 227 return NodeImpl.build(replaceChildResults); 228 } catch (JavaScriptException e) { 229 throw new DOMNodeException(DOMException.INVALID_MODIFICATION_ERR, e, this); 230 } 231 } 232 233 237 public void setNodeValue(String nodeValue) { 238 try { 239 XMLParserImpl.setNodeValue(this.getJsObject(), nodeValue); 240 } catch (JavaScriptException e) { 241 throw new DOMNodeException(DOMException.INVALID_MODIFICATION_ERR, e, this); 242 } 243 } 244 } 245 | Popular Tags |