1 57 58 package com.sun.org.apache.xerces.internal.dom; 59 60 import java.util.Vector ; 61 62 import org.w3c.dom.DOMException ; 63 import org.w3c.dom.Node ; 64 65 66 80 public class AttributeMap extends NamedNodeMapImpl { 81 82 83 static final long serialVersionUID = 8872606282138665383L; 84 85 89 90 protected AttributeMap(ElementImpl ownerNode, NamedNodeMapImpl defaults) { 91 super(ownerNode); 92 if (defaults != null) { 93 cloneContent(defaults); 95 if (nodes != null) { 96 hasDefaults(true); 97 } 98 } 99 } 100 101 110 public Node setNamedItem(Node arg) 111 throws DOMException { 112 113 if (isReadOnly()) { 114 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 115 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 116 } 117 if(arg.getOwnerDocument() != ownerNode.ownerDocument()) { 118 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); 119 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 120 } 121 if (arg.getNodeType() != Node.ATTRIBUTE_NODE) { 122 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null); 123 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, msg); 124 } 125 126 AttrImpl argn = (AttrImpl)arg; 127 128 if (argn.isOwned()){ 129 if (argn.getOwnerElement() != ownerNode) { 130 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INUSE_ATTRIBUTE_ERR", null); 131 throw new DOMException (DOMException.INUSE_ATTRIBUTE_ERR, msg); 132 } 133 return arg; 135 } 136 137 argn.ownerNode = ownerNode; 139 argn.isOwned(true); 140 141 int i = findNamePoint(arg.getNodeName(),0); 142 AttrImpl previous = null; 143 if (i >= 0) { 144 previous = (AttrImpl) nodes.elementAt(i); 145 nodes.setElementAt(arg,i); 146 previous.ownerNode = ownerNode.ownerDocument(); 147 previous.isOwned(false); 148 previous.isSpecified(true); 150 } else { 151 i = -1 - i; if (null == nodes) { 153 nodes = new Vector (5, 10); 154 } 155 nodes.insertElementAt(arg, i); 156 } 157 158 ownerNode.ownerDocument().setAttrNode(argn, previous); 160 161 if (!argn.isNormalized()) { 164 ownerNode.isNormalized(false); 165 } 166 return previous; 167 168 } 170 177 public Node setNamedItemNS(Node arg) 178 throws DOMException { 179 180 if (isReadOnly()) { 181 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 182 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 183 } 184 185 if(arg.getOwnerDocument() != ownerNode.ownerDocument()) { 186 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); 187 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 188 } 189 190 if (arg.getNodeType() != Node.ATTRIBUTE_NODE) { 191 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null); 192 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, msg); 193 } 194 AttrImpl argn = (AttrImpl)arg; 195 196 if (argn.isOwned()){ 197 if (argn.getOwnerElement() != ownerNode) { 198 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INUSE_ATTRIBUTE_ERR", null); 199 throw new DOMException (DOMException.INUSE_ATTRIBUTE_ERR, msg); 200 } 201 return arg; 203 } 204 205 argn.ownerNode = ownerNode; 207 argn.isOwned(true); 208 209 int i = findNamePoint(argn.getNamespaceURI(), argn.getLocalName()); 210 AttrImpl previous = null; 211 if (i >= 0) { 212 previous = (AttrImpl) nodes.elementAt(i); 213 nodes.setElementAt(arg,i); 214 previous.ownerNode = ownerNode.ownerDocument(); 215 previous.isOwned(false); 216 previous.isSpecified(true); 218 } else { 219 i = findNamePoint(arg.getNodeName(),0); 222 if (i >=0) { 223 previous = (AttrImpl) nodes.elementAt(i); 224 nodes.insertElementAt(arg,i); 225 } else { 226 i = -1 - i; if (null == nodes) { 228 nodes = new Vector (5, 10); 229 } 230 nodes.insertElementAt(arg, i); 231 } 232 } 233 235 ownerNode.ownerDocument().setAttrNode(argn, previous); 237 238 if (!argn.isNormalized()) { 241 ownerNode.isNormalized(false); 242 } 243 return previous; 244 245 } 247 259 260 public Node removeNamedItem(String name) 261 throws DOMException { 262 return internalRemoveNamedItem(name, true); 263 } 264 265 269 Node safeRemoveNamedItem(String name) { 270 return internalRemoveNamedItem(name, false); 271 } 272 273 274 286 protected Node removeItem(Node item, boolean addDefault) 287 throws DOMException { 288 289 int index = -1; 290 if (nodes != null) { 291 for (int i = 0; i < nodes.size(); i++) { 292 if (nodes.elementAt(i) == item) { 293 index = i; 294 break; 295 } 296 } 297 } 298 if (index < 0) { 299 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 300 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 301 } 302 303 return remove((AttrImpl)item, index, addDefault); 304 } 305 306 310 final protected Node internalRemoveNamedItem(String name, boolean raiseEx){ 311 if (isReadOnly()) { 312 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 313 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 314 } 315 int i = findNamePoint(name,0); 316 if (i < 0) { 317 if (raiseEx) { 318 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 319 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 320 } else { 321 return null; 322 } 323 } 324 325 return remove((AttrImpl)nodes.elementAt(i), i, true); 326 327 } 329 private final Node remove(AttrImpl attr, int index, 330 boolean addDefault) { 331 332 CoreDocumentImpl ownerDocument = ownerNode.ownerDocument(); 333 String name = attr.getNodeName(); 334 if (attr.isIdAttribute()) { 335 ownerDocument.removeIdentifier(attr.getValue()); 336 } 337 338 if (hasDefaults() && addDefault) { 339 NamedNodeMapImpl defaults = 341 ((ElementImpl) ownerNode).getDefaultAttributes(); 342 343 Node d; 344 if (defaults != null && 345 (d = defaults.getNamedItem(name)) != null && 346 findNamePoint(name, index+1) < 0) { 347 NodeImpl clone = (NodeImpl)d.cloneNode(true); 348 if (d.getLocalName() !=null){ 349 ((AttrNSImpl)clone).namespaceURI = attr.getNamespaceURI(); 354 } 355 clone.ownerNode = ownerNode; 356 clone.isOwned(true); 357 clone.isSpecified(false); 358 359 nodes.setElementAt(clone, index); 360 if (attr.isIdAttribute()) { 361 ownerDocument.putIdentifier(clone.getNodeValue(), 362 (ElementImpl)ownerNode); 363 } 364 } else { 365 nodes.removeElementAt(index); 366 } 367 } else { 368 nodes.removeElementAt(index); 369 } 370 371 373 attr.ownerNode = ownerDocument; 375 attr.isOwned(false); 376 377 attr.isSpecified(true); 380 attr.isIdAttribute(false); 381 382 ownerDocument.removedAttrNode(attr, ownerNode, name); 384 385 return attr; 386 } 387 388 404 public Node removeNamedItemNS(String namespaceURI, String name) 405 throws DOMException { 406 return internalRemoveNamedItemNS(namespaceURI, name, true); 407 } 408 409 413 Node safeRemoveNamedItemNS(String namespaceURI, String name) { 414 return internalRemoveNamedItemNS(namespaceURI, name, false); 415 } 416 417 422 final protected Node internalRemoveNamedItemNS(String namespaceURI, 423 String name, 424 boolean raiseEx) { 425 if (isReadOnly()) { 426 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 427 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 428 } 429 int i = findNamePoint(namespaceURI, name); 430 if (i < 0) { 431 if (raiseEx) { 432 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 433 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 434 } else { 435 return null; 436 } 437 } 438 439 AttrImpl n = (AttrImpl)nodes.elementAt(i); 440 CoreDocumentImpl ownerDocument = ownerNode.ownerDocument(); 441 442 if (n.isIdAttribute()) { 443 ownerDocument.removeIdentifier(n.getValue()); 444 } 445 String nodeName = n.getNodeName(); 447 if (hasDefaults()) { 448 NamedNodeMapImpl defaults = ((ElementImpl) ownerNode).getDefaultAttributes(); 449 Node d; 450 if (defaults != null 451 && (d = defaults.getNamedItem(nodeName)) != null) 452 { 453 int j = findNamePoint(nodeName,0); 454 if (j>=0 && findNamePoint(nodeName, j+1) < 0) { 455 NodeImpl clone = (NodeImpl)d.cloneNode(true); 456 clone.ownerNode = ownerNode; 457 if (d.getLocalName() != null) { 458 ((AttrNSImpl)clone).namespaceURI = namespaceURI; 463 } 464 clone.isOwned(true); 465 clone.isSpecified(false); 466 nodes.setElementAt(clone, i); 467 if (clone.isIdAttribute()) { 468 ownerDocument.putIdentifier(clone.getNodeValue(), 469 (ElementImpl)ownerNode); 470 } 471 } else { 472 nodes.removeElementAt(i); 473 } 474 } else { 475 nodes.removeElementAt(i); 476 } 477 } else { 478 nodes.removeElementAt(i); 479 } 480 481 483 n.ownerNode = ownerDocument; 485 n.isOwned(false); 486 n.isSpecified(true); 489 n.isIdAttribute(false); 491 492 ownerDocument.removedAttrNode(n, ownerNode, name); 494 495 return n; 496 497 } 499 503 507 508 public NamedNodeMapImpl cloneMap(NodeImpl ownerNode) { 509 AttributeMap newmap = 510 new AttributeMap((ElementImpl) ownerNode, null); 511 newmap.hasDefaults(hasDefaults()); 512 newmap.cloneContent(this); 513 return newmap; 514 } 516 519 protected void cloneContent(NamedNodeMapImpl srcmap) { 520 Vector srcnodes = srcmap.nodes; 521 if (srcnodes != null) { 522 int size = srcnodes.size(); 523 if (size != 0) { 524 if (nodes == null) { 525 nodes = new Vector (size); 526 } 527 nodes.setSize(size); 528 for (int i = 0; i < size; ++i) { 529 NodeImpl n = (NodeImpl) srcnodes.elementAt(i); 530 NodeImpl clone = (NodeImpl) n.cloneNode(true); 531 clone.isSpecified(n.isSpecified()); 532 nodes.setElementAt(clone, i); 533 clone.ownerNode = ownerNode; 534 clone.isOwned(true); 535 } 536 } 537 } 538 } 540 541 544 void moveSpecifiedAttributes(AttributeMap srcmap) { 545 int nsize = (srcmap.nodes != null) ? srcmap.nodes.size() : 0; 546 for (int i = nsize - 1; i >= 0; i--) { 547 AttrImpl attr = (AttrImpl) srcmap.nodes.elementAt(i); 548 if (attr.isSpecified()) { 549 srcmap.remove(attr, i, false); 550 if (attr.getLocalName() != null) { 551 setNamedItem(attr); 552 } 553 else { 554 setNamedItemNS(attr); 555 } 556 } 557 } 558 } 560 561 565 protected void reconcileDefaults(NamedNodeMapImpl defaults) { 566 567 int nsize = (nodes != null) ? nodes.size() : 0; 569 for (int i = nsize - 1; i >= 0; i--) { 570 AttrImpl attr = (AttrImpl) nodes.elementAt(i); 571 if (!attr.isSpecified()) { 572 remove(attr, i, false); 573 } 574 } 575 if (defaults == null) { 577 return; 578 } 579 if (nodes == null || nodes.size() == 0) { 580 cloneContent(defaults); 581 } 582 else { 583 int dsize = defaults.nodes.size(); 584 for (int n = 0; n < dsize; n++) { 585 AttrImpl d = (AttrImpl) defaults.nodes.elementAt(n); 586 int i = findNamePoint(d.getNodeName(), 0); 587 if (i < 0) { 588 i = -1 - i; 589 NodeImpl clone = (NodeImpl) d.cloneNode(true); 590 clone.ownerNode = ownerNode; 591 clone.isOwned(true); 592 clone.isSpecified(false); 593 nodes.insertElementAt(clone, i); 594 } 595 } 596 } 597 598 } 600 } | Popular Tags |