1 18 package org.apache.batik.dom; 19 20 import java.io.Serializable ; 21 22 import org.apache.batik.dom.events.EventSupport; 23 import org.apache.batik.dom.events.NodeEventTarget; 24 import org.apache.batik.dom.util.DOMUtilities; 25 import org.apache.batik.dom.util.XMLSupport; 26 import org.w3c.dom.DOMException ; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.NamedNodeMap ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.NodeList ; 31 import org.w3c.dom.events.DocumentEvent ; 32 import org.w3c.dom.events.Event ; 33 import org.w3c.dom.events.EventException ; 34 import org.w3c.dom.events.EventListener ; 35 import org.w3c.dom.events.MutationEvent ; 36 37 43 public abstract class AbstractNode 44 implements ExtendedNode, 45 Serializable { 46 47 50 protected final static NodeList EMPTY_NODE_LIST = new NodeList () { 51 public Node item(int i) { return null; } 52 public int getLength() { return 0; } 53 }; 54 55 58 protected AbstractDocument ownerDocument; 59 60 63 protected transient EventSupport eventSupport; 64 65 69 public void setNodeName(String v) { 70 } 71 72 75 public void setOwnerDocument(Document doc) { 76 ownerDocument = (AbstractDocument)doc; 77 } 78 79 83 public void setSpecified(boolean v) { 84 throw createDOMException(DOMException.INVALID_STATE_ERR, 85 "node.type", 86 new Object [] { new Integer (getNodeType()), 87 getNodeName()}); 88 } 89 90 94 public String getNodeValue() throws DOMException { 95 return null; 96 } 97 98 102 public void setNodeValue(String nodeValue) throws DOMException { 103 } 104 105 109 public Node getParentNode() { 110 return null; 111 } 112 113 117 public void setParentNode(Node v) { 118 throw createDOMException(DOMException.HIERARCHY_REQUEST_ERR, 119 "parent.not.allowed", 120 new Object [] { new Integer (getNodeType()), 121 getNodeName() }); 122 } 123 124 128 public NodeList getChildNodes() { 129 return EMPTY_NODE_LIST; 130 } 131 132 136 public Node getFirstChild() { 137 return null; 138 } 139 140 144 public Node getLastChild() { 145 return null; 146 } 147 148 152 public void setPreviousSibling(Node n) { 153 throw createDOMException(DOMException.HIERARCHY_REQUEST_ERR, 154 "sibling.not.allowed", 155 new Object [] { new Integer (getNodeType()), 156 getNodeName() }); 157 } 158 159 163 public Node getPreviousSibling() { 164 return null; 165 } 166 167 171 public void setNextSibling(Node n) { 172 throw createDOMException(DOMException.HIERARCHY_REQUEST_ERR, 173 "sibling.not.allowed", 174 new Object [] { new Integer (getNodeType()), 175 getNodeName() }); 176 } 177 178 182 public Node getNextSibling() { 183 return null; 184 } 185 186 190 public boolean hasAttributes() { 191 return false; 192 } 193 194 198 public NamedNodeMap getAttributes() { 199 return null; 200 } 201 202 206 public Document getOwnerDocument() { 207 return ownerDocument; 208 } 209 210 214 public String getNamespaceURI() { 215 return null; 216 } 217 218 223 public Node insertBefore(Node newChild, Node refChild) 224 throws DOMException { 225 throw createDOMException(DOMException.HIERARCHY_REQUEST_ERR, 226 "children.not.allowed", 227 new Object [] { new Integer (getNodeType()), 228 getNodeName() }); 229 } 230 231 236 public Node replaceChild(Node newChild, Node oldChild) 237 throws DOMException { 238 throw createDOMException(DOMException.HIERARCHY_REQUEST_ERR, 239 "children.not.allowed", 240 new Object [] { new Integer (getNodeType()), 241 getNodeName()}); 242 } 243 244 248 public Node removeChild(Node oldChild) throws DOMException { 249 throw createDOMException(DOMException.HIERARCHY_REQUEST_ERR, 250 "children.not.allowed", 251 new Object [] { new Integer (getNodeType()), 252 getNodeName() }); 253 } 254 255 259 public Node appendChild(Node newChild) throws DOMException { 260 throw createDOMException(DOMException.HIERARCHY_REQUEST_ERR, 261 "children.not.allowed", 262 new Object [] { new Integer (getNodeType()), 263 getNodeName() }); 264 } 265 266 270 public boolean hasChildNodes() { 271 return false; 272 } 273 274 277 public Node cloneNode(boolean deep) { 278 return (deep) ? deepCopyInto(newNode()) : copyInto(newNode()); 279 } 280 281 285 public void normalize() { 286 } 287 288 292 public boolean isSupported(String feature, String version) { 293 return getCurrentDocument().getImplementation().hasFeature(feature, 294 version); 295 } 296 297 300 public String getPrefix() { 301 return (getNamespaceURI() == null) 302 ? null 303 : DOMUtilities.getPrefix(getNodeName()); 304 } 305 306 309 public void setPrefix(String prefix) throws DOMException { 310 if (isReadonly()) { 311 throw createDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, 312 "readonly.node", 313 new Object [] { new Integer (getNodeType()), 314 getNodeName() }); 315 } 316 String uri = getNamespaceURI(); 317 if (uri == null) { 318 throw createDOMException(DOMException.NAMESPACE_ERR, 319 "namespace", 320 new Object [] { new Integer (getNodeType()), 321 getNodeName() }); 322 } 323 324 String name = getLocalName(); 325 if (prefix == null) { 326 setNodeName(name); 327 } 328 if (!prefix.equals("") && !DOMUtilities.isValidName(prefix)) { 329 throw createDOMException(DOMException.INVALID_CHARACTER_ERR, 330 "prefix", 331 new Object [] { new Integer (getNodeType()), 332 getNodeName(), 333 prefix }); 334 } 335 if (!DOMUtilities.isValidPrefix(prefix)) { 336 throw createDOMException(DOMException.NAMESPACE_ERR, 337 "prefix", 338 new Object [] { new Integer (getNodeType()), 339 getNodeName(), 340 prefix }); 341 } 342 if ((prefix.equals("xml") && 343 !XMLSupport.XML_NAMESPACE_URI.equals(uri)) || 344 (prefix.equals("xmlns") && 345 !XMLSupport.XMLNS_NAMESPACE_URI.equals(uri))) { 346 throw createDOMException(DOMException.NAMESPACE_ERR, 347 "namespace.uri", 348 new Object [] { new Integer (getNodeType()), 349 getNodeName(), 350 uri }); 351 } 352 setNodeName(prefix + ":" + name); 353 } 354 355 358 public String getLocalName() { 359 return (getNamespaceURI() == null) 360 ? null 361 : DOMUtilities.getLocalName(getNodeName()); 362 } 363 364 367 public DOMException createDOMException(short type, 368 String key, 369 Object [] args) { 370 try { 371 return new DOMException 372 (type, getCurrentDocument().formatMessage(key, args)); 373 } catch (Exception e) { 374 return new DOMException (type, key); 375 } 376 } 377 378 380 385 public void addEventListener(String type, 386 EventListener listener, 387 boolean useCapture) { 388 if (eventSupport == null) { 389 eventSupport = new EventSupport(); 390 AbstractDocument doc = getCurrentDocument(); 391 doc.setEventsEnabled(true); 392 } 393 eventSupport.addEventListener(type, listener, useCapture); 394 } 395 396 401 public void removeEventListener(String type, 402 EventListener listener, 403 boolean useCapture) { 404 if (eventSupport != null) { 405 eventSupport.removeEventListener(type, listener, useCapture); 406 } 407 } 408 409 413 public NodeEventTarget getParentNodeEventTarget() { 414 return (NodeEventTarget)getParentNode(); 415 } 416 417 421 public boolean dispatchEvent(Event evt) throws EventException { 422 return EventSupport.dispatchEvent(this, evt); 423 } 424 425 428 public EventSupport getEventSupport() { 429 return eventSupport; 430 } 431 432 435 public void fireDOMNodeInsertedIntoDocumentEvent() { 436 AbstractDocument doc = getCurrentDocument(); 437 if (doc.getEventsEnabled()) { 438 DocumentEvent de = (DocumentEvent )doc; 439 MutationEvent ev = (MutationEvent )de.createEvent("MutationEvents"); 440 ev.initMutationEvent("DOMNodeInsertedIntoDocument", 441 true, false, null, null, null, null, MutationEvent.ADDITION); 448 dispatchEvent(ev); 449 } 450 } 451 452 455 public void fireDOMNodeRemovedFromDocumentEvent() { 456 AbstractDocument doc = getCurrentDocument(); 457 if (doc.getEventsEnabled()) { 458 DocumentEvent de = (DocumentEvent )doc; 459 MutationEvent ev = (MutationEvent )de.createEvent("MutationEvents"); 460 ev.initMutationEvent("DOMNodeRemovedFromDocument", 461 true, false, null, null, null, null, MutationEvent.REMOVAL); 468 dispatchEvent(ev); 469 } 470 } 471 472 475 protected void fireDOMCharacterDataModifiedEvent(String oldv, 476 String newv) { 477 AbstractDocument doc = getCurrentDocument(); 478 if (doc.getEventsEnabled()) { 479 DocumentEvent de = (DocumentEvent )doc; 480 MutationEvent ev = (MutationEvent )de.createEvent("MutationEvents"); 481 ev.initMutationEvent("DOMCharacterDataModified", 482 true, false, null, oldv, newv, null, MutationEvent.MODIFICATION); 489 dispatchEvent(ev); 490 } 491 } 492 493 496 protected AbstractDocument getCurrentDocument() { 497 return ownerDocument; 498 } 499 500 503 protected abstract Node newNode(); 504 505 508 protected Node export(Node n, AbstractDocument d) { 509 AbstractNode p = (AbstractNode)n; 510 p.ownerDocument = d; 511 p.setReadonly(false); 512 return n; 513 } 514 515 518 protected Node deepExport(Node n, AbstractDocument d) { 519 AbstractNode p = (AbstractNode)n; 520 p.ownerDocument = d; 521 p.setReadonly(false); 522 return n; 523 } 524 525 529 protected Node copyInto(Node n) { 530 AbstractNode an = (AbstractNode)n; 531 an.ownerDocument = ownerDocument; 532 return n; 533 } 534 535 539 protected Node deepCopyInto(Node n) { 540 AbstractNode an = (AbstractNode)n; 541 an.ownerDocument = ownerDocument; 542 return n; 543 } 544 545 548 protected void checkChildType(Node n, boolean replace) { 549 throw createDOMException(DOMException.HIERARCHY_REQUEST_ERR, 550 "children.not.allowed", 551 new Object [] { new Integer (getNodeType()), 552 getNodeName() }); 553 } 554 } 555 | Popular Tags |