1 16 17 package org.apache.xerces.dom; 18 19 import java.util.Vector ; 20 21 import org.w3c.dom.DOMException ; 22 import org.w3c.dom.Node ; 23 24 25 41 public class AttributeMap extends NamedNodeMapImpl { 42 43 44 static final long serialVersionUID = 8872606282138665383L; 45 46 50 51 protected AttributeMap(ElementImpl ownerNode, NamedNodeMapImpl defaults) { 52 super(ownerNode); 53 if (defaults != null) { 54 cloneContent(defaults); 56 if (nodes != null) { 57 hasDefaults(true); 58 } 59 } 60 } 61 62 71 public Node setNamedItem(Node arg) 72 throws DOMException { 73 74 boolean errCheck = ownerNode.ownerDocument().errorChecking; 75 if (errCheck) { 76 if (isReadOnly()) { 77 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 78 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 79 } 80 if (arg.getOwnerDocument() != ownerNode.ownerDocument()) { 81 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); 82 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 83 } 84 if (arg.getNodeType() != Node.ATTRIBUTE_NODE) { 85 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null); 86 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, msg); 87 } 88 } 89 AttrImpl argn = (AttrImpl)arg; 90 91 if (argn.isOwned()){ 92 if (errCheck && argn.getOwnerElement() != ownerNode) { 93 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INUSE_ATTRIBUTE_ERR", null); 94 throw new DOMException (DOMException.INUSE_ATTRIBUTE_ERR, msg); 95 } 96 return arg; 98 } 99 100 101 argn.ownerNode = ownerNode; 103 argn.isOwned(true); 104 105 int i = findNamePoint(arg.getNodeName(),0); 106 AttrImpl previous = null; 107 if (i >= 0) { 108 previous = (AttrImpl) nodes.elementAt(i); 109 nodes.setElementAt(arg,i); 110 previous.ownerNode = ownerNode.ownerDocument(); 111 previous.isOwned(false); 112 previous.isSpecified(true); 114 } else { 115 i = -1 - i; if (null == nodes) { 117 nodes = new Vector (5, 10); 118 } 119 nodes.insertElementAt(arg, i); 120 } 121 122 ownerNode.ownerDocument().setAttrNode(argn, previous); 124 125 if (!argn.isNormalized()) { 128 ownerNode.isNormalized(false); 129 } 130 return previous; 131 132 } 134 141 public Node setNamedItemNS(Node arg) 142 throws DOMException { 143 144 boolean errCheck = ownerNode.ownerDocument().errorChecking; 145 if (errCheck) { 146 if (isReadOnly()) { 147 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 148 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 149 } 150 if(arg.getOwnerDocument() != ownerNode.ownerDocument()) { 151 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); 152 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 153 } 154 if (arg.getNodeType() != Node.ATTRIBUTE_NODE) { 155 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null); 156 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, msg); 157 } 158 } 159 AttrImpl argn = (AttrImpl)arg; 160 161 if (argn.isOwned()){ 162 if (errCheck && argn.getOwnerElement() != ownerNode) { 163 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INUSE_ATTRIBUTE_ERR", null); 164 throw new DOMException (DOMException.INUSE_ATTRIBUTE_ERR, msg); 165 } 166 return arg; 168 } 169 170 argn.ownerNode = ownerNode; 172 argn.isOwned(true); 173 174 int i = findNamePoint(argn.getNamespaceURI(), argn.getLocalName()); 175 AttrImpl previous = null; 176 if (i >= 0) { 177 previous = (AttrImpl) nodes.elementAt(i); 178 nodes.setElementAt(arg,i); 179 previous.ownerNode = ownerNode.ownerDocument(); 180 previous.isOwned(false); 181 previous.isSpecified(true); 183 } else { 184 i = findNamePoint(arg.getNodeName(),0); 187 if (i >=0) { 188 previous = (AttrImpl) nodes.elementAt(i); 189 nodes.insertElementAt(arg,i); 190 } else { 191 i = -1 - i; if (null == nodes) { 193 nodes = new Vector (5, 10); 194 } 195 nodes.insertElementAt(arg, i); 196 } 197 } 198 200 ownerNode.ownerDocument().setAttrNode(argn, previous); 202 203 if (!argn.isNormalized()) { 206 ownerNode.isNormalized(false); 207 } 208 return previous; 209 210 } 212 224 225 public Node removeNamedItem(String name) 226 throws DOMException { 227 return internalRemoveNamedItem(name, true); 228 } 229 230 234 Node safeRemoveNamedItem(String name) { 235 return internalRemoveNamedItem(name, false); 236 } 237 238 239 251 protected Node removeItem(Node item, boolean addDefault) 252 throws DOMException { 253 254 int index = -1; 255 if (nodes != null) { 256 for (int i = 0; i < nodes.size(); i++) { 257 if (nodes.elementAt(i) == item) { 258 index = i; 259 break; 260 } 261 } 262 } 263 if (index < 0) { 264 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 265 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 266 } 267 268 return remove((AttrImpl)item, index, addDefault); 269 } 270 271 275 final protected Node internalRemoveNamedItem(String name, boolean raiseEx){ 276 if (isReadOnly()) { 277 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 278 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 279 } 280 int i = findNamePoint(name,0); 281 if (i < 0) { 282 if (raiseEx) { 283 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 284 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 285 } else { 286 return null; 287 } 288 } 289 290 return remove((AttrImpl)nodes.elementAt(i), i, true); 291 292 } 294 private final Node remove(AttrImpl attr, int index, 295 boolean addDefault) { 296 297 CoreDocumentImpl ownerDocument = ownerNode.ownerDocument(); 298 String name = attr.getNodeName(); 299 if (attr.isIdAttribute()) { 300 ownerDocument.removeIdentifier(attr.getValue()); 301 } 302 303 if (hasDefaults() && addDefault) { 304 NamedNodeMapImpl defaults = 306 ((ElementImpl) ownerNode).getDefaultAttributes(); 307 308 Node d; 309 if (defaults != null && 310 (d = defaults.getNamedItem(name)) != null && 311 findNamePoint(name, index+1) < 0) { 312 NodeImpl clone = (NodeImpl)d.cloneNode(true); 313 if (d.getLocalName() !=null){ 314 ((AttrNSImpl)clone).namespaceURI = attr.getNamespaceURI(); 319 } 320 clone.ownerNode = ownerNode; 321 clone.isOwned(true); 322 clone.isSpecified(false); 323 324 nodes.setElementAt(clone, index); 325 if (attr.isIdAttribute()) { 326 ownerDocument.putIdentifier(clone.getNodeValue(), 327 (ElementImpl)ownerNode); 328 } 329 } else { 330 nodes.removeElementAt(index); 331 } 332 } else { 333 nodes.removeElementAt(index); 334 } 335 336 338 attr.ownerNode = ownerDocument; 340 attr.isOwned(false); 341 342 attr.isSpecified(true); 345 attr.isIdAttribute(false); 346 347 ownerDocument.removedAttrNode(attr, ownerNode, name); 349 350 return attr; 351 } 352 353 369 public Node removeNamedItemNS(String namespaceURI, String name) 370 throws DOMException { 371 return internalRemoveNamedItemNS(namespaceURI, name, true); 372 } 373 374 378 Node safeRemoveNamedItemNS(String namespaceURI, String name) { 379 return internalRemoveNamedItemNS(namespaceURI, name, false); 380 } 381 382 387 final protected Node internalRemoveNamedItemNS(String namespaceURI, 388 String name, 389 boolean raiseEx) { 390 391 CoreDocumentImpl ownerDocument = ownerNode.ownerDocument(); 392 if (ownerDocument.errorChecking && isReadOnly()) { 393 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 394 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 395 } 396 int i = findNamePoint(namespaceURI, name); 397 if (i < 0) { 398 if (raiseEx) { 399 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 400 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 401 } else { 402 return null; 403 } 404 } 405 406 AttrImpl n = (AttrImpl)nodes.elementAt(i); 407 408 if (n.isIdAttribute()) { 409 ownerDocument.removeIdentifier(n.getValue()); 410 } 411 String nodeName = n.getNodeName(); 413 if (hasDefaults()) { 414 NamedNodeMapImpl defaults = ((ElementImpl) ownerNode).getDefaultAttributes(); 415 Node d; 416 if (defaults != null 417 && (d = defaults.getNamedItem(nodeName)) != null) 418 { 419 int j = findNamePoint(nodeName,0); 420 if (j>=0 && findNamePoint(nodeName, j+1) < 0) { 421 NodeImpl clone = (NodeImpl)d.cloneNode(true); 422 clone.ownerNode = ownerNode; 423 if (d.getLocalName() != null) { 424 ((AttrNSImpl)clone).namespaceURI = namespaceURI; 429 } 430 clone.isOwned(true); 431 clone.isSpecified(false); 432 nodes.setElementAt(clone, i); 433 if (clone.isIdAttribute()) { 434 ownerDocument.putIdentifier(clone.getNodeValue(), 435 (ElementImpl)ownerNode); 436 } 437 } else { 438 nodes.removeElementAt(i); 439 } 440 } else { 441 nodes.removeElementAt(i); 442 } 443 } else { 444 nodes.removeElementAt(i); 445 } 446 447 449 n.ownerNode = ownerDocument; 451 n.isOwned(false); 452 n.isSpecified(true); 455 n.isIdAttribute(false); 457 458 ownerDocument.removedAttrNode(n, ownerNode, name); 460 461 return n; 462 463 } 465 469 473 474 public NamedNodeMapImpl cloneMap(NodeImpl ownerNode) { 475 AttributeMap newmap = 476 new AttributeMap((ElementImpl) ownerNode, null); 477 newmap.hasDefaults(hasDefaults()); 478 newmap.cloneContent(this); 479 return newmap; 480 } 482 485 protected void cloneContent(NamedNodeMapImpl srcmap) { 486 Vector srcnodes = srcmap.nodes; 487 if (srcnodes != null) { 488 int size = srcnodes.size(); 489 if (size != 0) { 490 if (nodes == null) { 491 nodes = new Vector (size); 492 } 493 nodes.setSize(size); 494 for (int i = 0; i < size; ++i) { 495 NodeImpl n = (NodeImpl) srcnodes.elementAt(i); 496 NodeImpl clone = (NodeImpl) n.cloneNode(true); 497 clone.isSpecified(n.isSpecified()); 498 nodes.setElementAt(clone, i); 499 clone.ownerNode = ownerNode; 500 clone.isOwned(true); 501 } 502 } 503 } 504 } 506 507 510 void moveSpecifiedAttributes(AttributeMap srcmap) { 511 int nsize = (srcmap.nodes != null) ? srcmap.nodes.size() : 0; 512 for (int i = nsize - 1; i >= 0; i--) { 513 AttrImpl attr = (AttrImpl) srcmap.nodes.elementAt(i); 514 if (attr.isSpecified()) { 515 srcmap.remove(attr, i, false); 516 if (attr.getLocalName() != null) { 517 setNamedItem(attr); 518 } 519 else { 520 setNamedItemNS(attr); 521 } 522 } 523 } 524 } 526 527 531 protected void reconcileDefaults(NamedNodeMapImpl defaults) { 532 533 int nsize = (nodes != null) ? nodes.size() : 0; 535 for (int i = nsize - 1; i >= 0; i--) { 536 AttrImpl attr = (AttrImpl) nodes.elementAt(i); 537 if (!attr.isSpecified()) { 538 remove(attr, i, false); 539 } 540 } 541 if (defaults == null) { 543 return; 544 } 545 if (nodes == null || nodes.size() == 0) { 546 cloneContent(defaults); 547 } 548 else { 549 int dsize = defaults.nodes.size(); 550 for (int n = 0; n < dsize; n++) { 551 AttrImpl d = (AttrImpl) defaults.nodes.elementAt(n); 552 int i = findNamePoint(d.getNodeName(), 0); 553 if (i < 0) { 554 i = -1 - i; 555 NodeImpl clone = (NodeImpl) d.cloneNode(true); 556 clone.ownerNode = ownerNode; 557 clone.isOwned(true); 558 clone.isSpecified(false); 559 nodes.insertElementAt(clone, i); 560 } 561 } 562 } 563 564 } 566 } | Popular Tags |