1 57 58 package org.xquark.xpath.datamodel.xerces.dom; 59 60 import java.util.Vector ; 61 62 import org.w3c.dom.DOMException ; 63 import org.w3c.dom.Node ; 64 import org.w3c.dom.events.MutationEvent ; 65 import org.xquark.xpath.datamodel.xerces.dom.events.MutationEventImpl; 66 67 77 public class AttributeMap extends NamedNodeMapImpl { 78 79 final static boolean DEBUG = false; 80 81 85 86 protected AttributeMap(ElementImpl ownerNode, NamedNodeMapImpl defaults) { 87 super(ownerNode); 88 if (defaults != null) { 89 cloneContent(defaults); 91 if (nodes != null) { 92 hasDefaults(true); 93 } 94 } 95 } 96 97 106 public Node setNamedItem(Node arg) 107 throws DOMException { 108 109 if (isReadOnly()) { 110 throw 111 new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 112 "DOM001 Modification not allowed"); 113 } 114 if(arg.getOwnerDocument() != ownerNode.ownerDocument()) { 115 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, 116 "DOM005 Wrong document"); 117 } 118 if (arg.getNodeType() != Node.ATTRIBUTE_NODE) { 119 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, 120 "DOM006 Hierarchy request error"); 121 } 122 123 NodeImpl argn = (NodeImpl)arg; 124 125 if (argn.isOwned()) { 126 throw new DOMException (DOMException.INUSE_ATTRIBUTE_ERR, 127 "DOM009 Attribute already in use"); 128 } 129 130 argn.ownerNode = ownerNode; 132 argn.isOwned(true); 133 134 int i = findNamePoint(arg.getNodeName(),0); 135 NodeImpl previous = null; 136 if (i >= 0) { 137 previous = (NodeImpl) nodes.elementAt(i); 138 nodes.setElementAt(arg,i); 139 previous.ownerNode = ownerNode.ownerDocument(); 140 previous.isOwned(false); 141 previous.isSpecified(true); 143 } else { 144 i = -1 - i; if (null == nodes) { 146 nodes = new Vector (5, 10); 147 } 148 nodes.insertElementAt(arg, i); 149 } 150 151 if (NodeImpl.MUTATIONEVENTS && 152 ownerNode.ownerDocument().mutationEvents) { 153 ownerNode.dispatchAggregateEvents( 155 (AttrImpl)arg, 156 previous==null ? null : previous.getNodeValue(), 157 previous==null ? 158 MutationEvent.ADDITION : MutationEvent.MODIFICATION 159 ); 160 } 161 162 if (!argn.isNormalized()) { 165 ownerNode.isNormalized(false); 166 } 167 return previous; 168 169 } 171 178 public Node setNamedItemNS(Node arg) 179 throws DOMException { 180 181 if (isReadOnly()) { 182 throw 183 new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 184 "DOM001 Modification not allowed"); 185 } 186 187 if(arg.getOwnerDocument() != ownerNode.ownerDocument()) { 188 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, 189 "DOM005 Wrong document"); 190 } 191 192 if (arg.getNodeType() != Node.ATTRIBUTE_NODE) { 193 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, 194 "DOM006 Hierarchy request error"); 195 } 196 NodeImpl argn = (NodeImpl)arg; 197 if (argn.isOwned()) { 198 throw new DOMException (DOMException.INUSE_ATTRIBUTE_ERR, 199 "DOM009 Attribute already in use"); 200 } 201 202 argn.ownerNode = ownerNode; 204 argn.isOwned(true); 205 206 int i = findNamePoint(argn.getNamespaceURI(), argn.getLocalName()); 207 NodeImpl previous = null; 208 if (i >= 0) { 209 previous = (NodeImpl) nodes.elementAt(i); 210 nodes.setElementAt(arg,i); 211 previous.ownerNode = ownerNode.ownerDocument(); 212 previous.isOwned(false); 213 previous.isSpecified(true); 215 } else { 216 i = findNamePoint(arg.getNodeName(),0); 219 if (i >=0) { 220 previous = (NodeImpl) nodes.elementAt(i); 221 nodes.insertElementAt(arg,i); 222 } else { 223 i = -1 - i; if (null == nodes) { 225 nodes = new Vector (5, 10); 226 } 227 nodes.insertElementAt(arg, i); 228 } 229 } 230 232 if (NodeImpl.MUTATIONEVENTS 235 && ownerNode.ownerDocument().mutationEvents) 236 { 237 ownerNode.dispatchAggregateEvents( 239 (AttrImpl)arg, 240 previous==null ? null : previous.getNodeValue(), 241 previous==null ? 242 MutationEvent.ADDITION : MutationEvent.MODIFICATION 243 ); 244 } 245 246 if (!argn.isNormalized()) { 249 ownerNode.isNormalized(false); 250 } 251 return previous; 252 253 } 255 267 268 public Node removeNamedItem(String name) 269 throws DOMException { 270 return internalRemoveNamedItem(name, true); 271 } 272 273 277 Node safeRemoveNamedItem(String name) { 278 return internalRemoveNamedItem(name, false); 279 } 280 281 285 final protected Node internalRemoveNamedItem(String name, boolean raiseEx){ 286 if (isReadOnly()) { 287 throw 288 new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 289 "DOM001 Modification not allowed"); 290 } 291 int i = findNamePoint(name,0); 292 if (i < 0) { 293 if (raiseEx) { 294 throw new DOMException (DOMException.NOT_FOUND_ERR, 295 "DOM008 Not found"); 296 } else { 297 return null; 298 } 299 } 300 301 LCount lc=null; 302 String oldvalue=""; 303 AttrImpl enclosingAttribute=null; 304 if (NodeImpl.MUTATIONEVENTS 305 && ownerNode.ownerDocument().mutationEvents) 306 { 307 lc=LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED); 309 if(lc.captures+lc.bubbles+lc.defaults>0) 310 { 311 enclosingAttribute=(AttrImpl)(nodes.elementAt(i)); 312 oldvalue=enclosingAttribute.getNodeValue(); 313 } 314 } 316 NodeImpl n = (NodeImpl)nodes.elementAt(i); 317 if (hasDefaults()) { 319 NamedNodeMapImpl defaults = 320 ((ElementImpl) ownerNode).getDefaultAttributes(); 321 Node d; 322 if (defaults != null && (d = defaults.getNamedItem(name)) != null 323 && findNamePoint(name, i+1) < 0) { 324 325 NodeImpl clone = (NodeImpl)d.cloneNode(true); 326 clone.ownerNode = ownerNode; 327 clone.isOwned(true); 328 clone.isSpecified(false); 329 nodes.setElementAt(clone, i); 330 } else { 331 nodes.removeElementAt(i); 332 } 333 } else { 334 nodes.removeElementAt(i); 335 } 336 337 339 n.ownerNode = ownerNode.ownerDocument(); 341 n.isOwned(false); 342 n.isSpecified(true); 344 345 if(NodeImpl.MUTATIONEVENTS && ownerNode.ownerDocument().mutationEvents) 349 { 350 if(lc.captures+lc.bubbles+lc.defaults>0) { 353 MutationEventImpl me= new MutationEventImpl(); 354 me.initMutationEvent(MutationEventImpl.DOM_ATTR_MODIFIED, 355 true, false, 356 null, n.getNodeValue(), 357 null, name, MutationEvent.REMOVAL); 358 ownerNode.dispatchEvent(me); 359 } 360 361 ownerNode.dispatchAggregateEvents(null,null,(short)0); 365 } 366 367 return n; 368 369 } 371 387 public Node removeNamedItemNS(String namespaceURI, String name) 388 throws DOMException { 389 return internalRemoveNamedItemNS(namespaceURI, name, true); 390 } 391 392 396 Node safeRemoveNamedItemNS(String namespaceURI, String name) { 397 return internalRemoveNamedItemNS(namespaceURI, name, false); 398 } 399 400 405 final protected Node internalRemoveNamedItemNS(String namespaceURI, 406 String name, 407 boolean raiseEx) { 408 if (isReadOnly()) { 409 throw 410 new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 411 "DOM001 Modification not allowed"); 412 } 413 int i = findNamePoint(namespaceURI, name); 414 if (i < 0) { 415 if (raiseEx) { 416 throw new DOMException (DOMException.NOT_FOUND_ERR, 417 "DOM008 Not found"); 418 } else { 419 return null; 420 } 421 } 422 423 LCount lc=null; 424 String oldvalue=""; 425 AttrImpl enclosingAttribute=null; 426 if (NodeImpl.MUTATIONEVENTS 427 && ownerNode.ownerDocument().mutationEvents) 428 { 429 lc=LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED); 431 if(lc.captures+lc.bubbles+lc.defaults>0) 432 { 433 enclosingAttribute=(AttrImpl)(nodes.elementAt(i)); 434 oldvalue=enclosingAttribute.getNodeValue(); 435 } 436 } 438 NodeImpl n = (NodeImpl)nodes.elementAt(i); 439 String nodeName = n.getNodeName(); 441 if (hasDefaults()) { 442 NamedNodeMapImpl defaults = 443 ((ElementImpl) ownerNode).getDefaultAttributes(); 444 Node d; 445 if (defaults != null 446 && (d = defaults.getNamedItem(nodeName)) != null) 447 { 448 int j = findNamePoint(nodeName,0); 449 if (j>=0 && findNamePoint(nodeName, j+1) < 0) { 450 NodeImpl clone = (NodeImpl)d.cloneNode(true); 451 clone.ownerNode = ownerNode; 452 clone.isOwned(true); 453 clone.isSpecified(false); 454 nodes.setElementAt(clone, i); 455 } else { 456 nodes.removeElementAt(i); 457 } 458 } else { 459 nodes.removeElementAt(i); 460 } 461 } else { 462 nodes.removeElementAt(i); 463 } 464 465 467 n.ownerNode = ownerNode.ownerDocument(); 470 n.isOwned(false); 471 n.isSpecified(true); 473 474 if(NodeImpl.MUTATIONEVENTS && ownerNode.ownerDocument().mutationEvents) 478 { 479 if(lc.captures+lc.bubbles+lc.defaults>0) { 482 MutationEventImpl me= new MutationEventImpl(); 483 me.initMutationEvent(MutationEventImpl.DOM_ATTR_MODIFIED, 484 true, false, null, n.getNodeValue(), 485 null, name, MutationEvent.REMOVAL); 486 ownerNode.dispatchEvent(me); 487 } 488 489 ownerNode.dispatchAggregateEvents(null,null,(short)0); 493 } 494 return n; 495 496 } 498 502 506 507 public NamedNodeMapImpl cloneMap(NodeImpl ownerNode) { 508 AttributeMap newmap = 509 new AttributeMap((ElementImpl) ownerNode, null); 510 newmap.hasDefaults(hasDefaults()); 511 newmap.cloneContent(this); 512 return newmap; 513 } 515 518 protected void cloneContent(NamedNodeMapImpl srcmap) { 519 if (srcmap.nodes != null) { 520 if (nodes == null) { 521 nodes = new Vector (srcmap.nodes.size()); 522 } 523 else { 524 nodes.setSize(srcmap.nodes.size()); 525 } 526 for (int i = 0; i < srcmap.nodes.size(); ++i) { 527 NodeImpl n = (NodeImpl) srcmap.nodes.elementAt(i); 528 NodeImpl clone = (NodeImpl) n.cloneNode(true); 529 clone.isSpecified(n.isSpecified()); 530 nodes.insertElementAt(clone, i); 531 clone.ownerNode = ownerNode; 532 clone.isOwned(true); 533 } 534 } 535 } 537 538 542 protected void reconcileDefaults(NamedNodeMapImpl defaults) { 543 544 int nsize = (nodes != null) ? nodes.size() : 0; 546 for (int i = nsize - 1; i >= 0; i--) { 547 AttrImpl attr = (AttrImpl) nodes.elementAt(i); 548 if (!attr.isSpecified()) { 549 attr.ownerNode = ownerNode.ownerDocument(); 551 attr.isOwned(false); 552 attr.isSpecified(true); 554 nodes.removeElementAt(i); 555 } 556 } 557 if (defaults == null) { 559 return; 560 } 561 if (nodes == null || nodes.size() == 0) { 562 cloneContent(defaults); 563 } 564 else { 565 int dsize = defaults.nodes.size(); 566 for (int n = 0; n < dsize; n++) { 567 AttrImpl d = (AttrImpl) defaults.nodes.elementAt(n); 568 int i = findNamePoint(d.getNodeName(), 0); 569 if (i < 0) { 570 NodeImpl clone = (NodeImpl) d.cloneNode(true); 571 clone.ownerNode = ownerNode; 572 clone.isOwned(true); 573 clone.isSpecified(false); 574 nodes.setElementAt(clone, i); 575 } 576 } 577 } 578 579 } 581 } | Popular Tags |