1 /* 2 * Copyright (C) 2007 Esmertec AG. 3 * Copyright (C) 2007 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.mms.dom; 19 20 import java.util.NoSuchElementException; 21 import java.util.Vector; 22 23 import org.w3c.dom.DOMException; 24 import org.w3c.dom.Document; 25 import org.w3c.dom.NamedNodeMap; 26 import org.w3c.dom.Node; 27 import org.w3c.dom.NodeList; 28 import org.w3c.dom.events.Event; 29 import org.w3c.dom.events.EventException; 30 import org.w3c.dom.events.EventListener; 31 import org.w3c.dom.events.EventTarget; 32 33 import com.android.mms.dom.events.EventTargetImpl; 34 35 public abstract class NodeImpl implements Node, EventTarget { 36 private Node mParentNode; 37 private final Vector<Node> mChildNodes = new Vector<Node>(); 38 DocumentImpl mOwnerDocument; 39 private final EventTarget mEventTarget = new EventTargetImpl(this); 40 41 /* 42 * Internal methods 43 */ 44 NodeImpl(DocumentImpl owner)45 protected NodeImpl(DocumentImpl owner) { 46 mOwnerDocument = owner; 47 } 48 49 /* 50 * Node Interface Methods 51 */ 52 appendChild(Node newChild)53 public Node appendChild(Node newChild) throws DOMException { 54 ((NodeImpl)newChild).setParentNode(this); 55 mChildNodes.remove(newChild); 56 mChildNodes.add(newChild); 57 return newChild; 58 } 59 cloneNode(boolean deep)60 public Node cloneNode(boolean deep) { 61 // TODO Auto-generated method stub 62 return null; 63 } 64 getAttributes()65 public NamedNodeMap getAttributes() { 66 // Default. Override in Element. 67 return null; 68 } 69 getChildNodes()70 public NodeList getChildNodes() { 71 return new NodeListImpl(this, null, false); 72 } 73 getFirstChild()74 public Node getFirstChild() { 75 Node firstChild = null; 76 try { 77 firstChild = mChildNodes.firstElement(); 78 } 79 catch (NoSuchElementException e) { 80 // Ignore and return null 81 } 82 return firstChild; 83 } 84 getLastChild()85 public Node getLastChild() { 86 Node lastChild = null; 87 try { 88 lastChild = mChildNodes.lastElement(); 89 } 90 catch (NoSuchElementException e) { 91 // Ignore and return null 92 } 93 return lastChild; 94 } 95 getLocalName()96 public String getLocalName() { 97 // TODO Auto-generated method stub 98 return null; 99 } 100 getNamespaceURI()101 public String getNamespaceURI() { 102 // TODO Auto-generated method stub 103 return null; 104 } 105 getNextSibling()106 public Node getNextSibling() { 107 if ((mParentNode != null) && (this != mParentNode.getLastChild())) { 108 Vector<Node> siblings = ((NodeImpl)mParentNode).mChildNodes; 109 int indexOfThis = siblings.indexOf(this); 110 return siblings.elementAt(indexOfThis + 1); 111 } 112 return null; 113 } 114 getNodeName()115 public abstract String getNodeName(); 116 getNodeType()117 public abstract short getNodeType(); 118 getNodeValue()119 public String getNodeValue() throws DOMException { 120 // Default behaviour. Override if required. 121 return null; 122 } 123 getOwnerDocument()124 public Document getOwnerDocument() { 125 return mOwnerDocument; 126 } 127 getParentNode()128 public Node getParentNode() { 129 return mParentNode; 130 } 131 getPrefix()132 public String getPrefix() { 133 // TODO Auto-generated method stub 134 return null; 135 } 136 getPreviousSibling()137 public Node getPreviousSibling() { 138 if ((mParentNode != null) && (this != mParentNode.getFirstChild())) { 139 Vector<Node> siblings = ((NodeImpl)mParentNode).mChildNodes; 140 int indexOfThis = siblings.indexOf(this); 141 return siblings.elementAt(indexOfThis - 1); 142 } 143 return null; 144 } 145 hasAttributes()146 public boolean hasAttributes() { 147 // Default. Override in Element. 148 return false; 149 } 150 hasChildNodes()151 public boolean hasChildNodes() { 152 return !(mChildNodes.isEmpty()); 153 } 154 insertBefore(Node newChild, Node refChild)155 public Node insertBefore(Node newChild, Node refChild) throws DOMException { 156 // TODO Auto-generated method stub 157 return null; 158 } 159 isSupported(String feature, String version)160 public boolean isSupported(String feature, String version) { 161 // TODO Auto-generated method stub 162 return false; 163 } 164 normalize()165 public void normalize() { 166 // TODO Auto-generated method stub 167 } 168 removeChild(Node oldChild)169 public Node removeChild(Node oldChild) throws DOMException { 170 if (mChildNodes.contains(oldChild)) { 171 mChildNodes.remove(oldChild); 172 ((NodeImpl)oldChild).setParentNode(null); 173 } else { 174 throw new DOMException(DOMException.NOT_FOUND_ERR, "Child does not exist"); 175 } 176 return null; 177 } 178 replaceChild(Node newChild, Node oldChild)179 public Node replaceChild(Node newChild, Node oldChild) throws DOMException { 180 if (mChildNodes.contains(oldChild)) { 181 // Try to remove the new child if available 182 try { 183 mChildNodes.remove(newChild); 184 } catch (DOMException e) { 185 // Ignore exception 186 } 187 mChildNodes.setElementAt(newChild, mChildNodes.indexOf(oldChild)); 188 ((NodeImpl)newChild).setParentNode(this); 189 ((NodeImpl)oldChild).setParentNode(null); 190 } else { 191 throw new DOMException(DOMException.NOT_FOUND_ERR, "Old child does not exist"); 192 } 193 return oldChild; 194 } 195 setNodeValue(String nodeValue)196 public void setNodeValue(String nodeValue) throws DOMException { 197 // Default behaviour. Override if required. 198 } 199 setPrefix(String prefix)200 public void setPrefix(String prefix) throws DOMException { 201 // TODO Auto-generated method stub 202 } 203 setParentNode(Node parentNode)204 private void setParentNode(Node parentNode) { 205 mParentNode = parentNode; 206 } 207 208 /* 209 * EventTarget Interface 210 */ 211 addEventListener(String type, EventListener listener, boolean useCapture)212 public void addEventListener(String type, EventListener listener, boolean useCapture) { 213 mEventTarget.addEventListener(type, listener, useCapture); 214 } 215 removeEventListener(String type, EventListener listener, boolean useCapture)216 public void removeEventListener(String type, EventListener listener, boolean useCapture) { 217 mEventTarget.removeEventListener(type, listener, useCapture); 218 } 219 dispatchEvent(Event evt)220 public boolean dispatchEvent(Event evt) throws EventException { 221 return mEventTarget.dispatchEvent(evt); 222 } 223 } 224