1 57 58 package com.sun.org.apache.xerces.internal.dom; 59 60 import java.io.Serializable ; 61 import java.util.Vector ; 62 63 import org.w3c.dom.DOMException ; 64 import org.w3c.dom.NamedNodeMap ; 65 import org.w3c.dom.Node ; 66 67 92 public class NamedNodeMapImpl 93 implements NamedNodeMap , Serializable { 94 95 99 100 static final long serialVersionUID = -7039242451046758020L; 101 102 106 protected short flags; 107 108 protected final static short READONLY = 0x1<<0; 109 protected final static short CHANGED = 0x1<<1; 110 protected final static short HASDEFAULTS = 0x1<<2; 111 112 113 protected Vector nodes; 114 115 protected NodeImpl ownerNode; 117 121 122 protected NamedNodeMapImpl(NodeImpl ownerNode) { 123 this.ownerNode = ownerNode; 124 } 125 126 130 136 public int getLength() { 137 return (nodes != null) ? nodes.size() : 0; 138 } 139 140 154 public Node item(int index) { 155 return (nodes != null && index < nodes.size()) ? 156 (Node )(nodes.elementAt(index)) : null; 157 } 158 159 166 public Node getNamedItem(String name) { 167 168 int i = findNamePoint(name,0); 169 return (i < 0) ? null : (Node )(nodes.elementAt(i)); 170 171 } 173 184 public Node getNamedItemNS(String namespaceURI, String localName) { 185 186 int i = findNamePoint(namespaceURI, localName); 187 return (i < 0) ? null : (Node )(nodes.elementAt(i)); 188 189 } 191 208 public Node setNamedItem(Node arg) 209 throws DOMException { 210 211 if (isReadOnly()) { 212 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 213 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 214 } 215 if (arg.getOwnerDocument() != ownerNode.ownerDocument()) { 216 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); 217 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 218 } 219 220 int i = findNamePoint(arg.getNodeName(),0); 221 NodeImpl previous = null; 222 if (i >= 0) { 223 previous = (NodeImpl) nodes.elementAt(i); 224 nodes.setElementAt(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 return previous; 233 234 } 236 247 public Node setNamedItemNS(Node arg) 248 throws DOMException { 249 250 if (isReadOnly()) { 251 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 252 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 253 } 254 255 if(arg.getOwnerDocument() != ownerNode.ownerDocument()) { 256 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); 257 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 258 } 259 260 int i = findNamePoint(arg.getNamespaceURI(), arg.getLocalName()); 261 NodeImpl previous = null; 262 if (i >= 0) { 263 previous = (NodeImpl) nodes.elementAt(i); 264 nodes.setElementAt(arg,i); 265 } else { 266 i = findNamePoint(arg.getNodeName(),0); 269 if (i >=0) { 270 previous = (NodeImpl) nodes.elementAt(i); 271 nodes.insertElementAt(arg,i); 272 } else { 273 i = -1 - i; if (null == nodes) { 275 nodes = new Vector (5, 10); 276 } 277 nodes.insertElementAt(arg, i); 278 } 279 } 280 return previous; 281 282 } 284 289 290 public Node removeNamedItem(String name) 291 throws DOMException { 292 293 if (isReadOnly()) { 294 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 295 throw 296 new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 297 msg); 298 } 299 int i = findNamePoint(name,0); 300 if (i < 0) { 301 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 302 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 303 } 304 305 NodeImpl n = (NodeImpl)nodes.elementAt(i); 306 nodes.removeElementAt(i); 307 308 return n; 309 310 } 312 326 public Node removeNamedItemNS(String namespaceURI, String name) 327 throws DOMException { 328 329 if (isReadOnly()) { 330 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 331 throw 332 new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 333 msg); 334 } 335 int i = findNamePoint(namespaceURI, name); 336 if (i < 0) { 337 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 338 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 339 } 340 341 NodeImpl n = (NodeImpl)nodes.elementAt(i); 342 nodes.removeElementAt(i); 343 344 return n; 345 346 } 348 352 356 357 public NamedNodeMapImpl cloneMap(NodeImpl ownerNode) { 358 NamedNodeMapImpl newmap = new NamedNodeMapImpl(ownerNode); 359 newmap.cloneContent(this); 360 return newmap; 361 } 362 363 protected void cloneContent(NamedNodeMapImpl srcmap) { 364 Vector srcnodes = srcmap.nodes; 365 if (srcnodes != null) { 366 int size = srcnodes.size(); 367 if (size != 0) { 368 if (nodes == null) { 369 nodes = new Vector (size); 370 } 371 nodes.setSize(size); 372 for (int i = 0; i < size; ++i) { 373 NodeImpl n = (NodeImpl) srcmap.nodes.elementAt(i); 374 NodeImpl clone = (NodeImpl) n.cloneNode(true); 375 clone.isSpecified(n.isSpecified()); 376 nodes.setElementAt(clone, i); 377 } 378 } 379 } 380 } 382 386 397 void setReadOnly(boolean readOnly, boolean deep) { 398 isReadOnly(readOnly); 399 if (deep && nodes != null) { 400 for (int i = nodes.size() - 1; i >= 0; i--) { 401 ((NodeImpl) nodes.elementAt(i)).setReadOnly(readOnly,deep); 402 } 403 } 404 } 406 410 boolean getReadOnly() { 411 return isReadOnly(); 412 } 414 415 419 423 void setOwnerDocument(CoreDocumentImpl doc) { 424 if (nodes != null) { 425 for (int i = 0; i < nodes.size(); i++) { 426 ((NodeImpl)item(i)).setOwnerDocument(doc); 427 } 428 } 429 } 430 431 final boolean isReadOnly() { 432 return (flags & READONLY) != 0; 433 } 434 435 final void isReadOnly(boolean value) { 436 flags = (short) (value ? flags | READONLY : flags & ~READONLY); 437 } 438 439 final boolean changed() { 440 return (flags & CHANGED) != 0; 441 } 442 443 final void changed(boolean value) { 444 flags = (short) (value ? flags | CHANGED : flags & ~CHANGED); 445 } 446 447 final boolean hasDefaults() { 448 return (flags & HASDEFAULTS) != 0; 449 } 450 451 final void hasDefaults(boolean value) { 452 flags = (short) (value ? flags | HASDEFAULTS : flags & ~HASDEFAULTS); 453 } 454 455 459 471 protected int findNamePoint(String name, int start) { 472 473 int i = 0; 475 if (nodes != null) { 476 int first = start; 477 int last = nodes.size() - 1; 478 479 while (first <= last) { 480 i = (first + last) / 2; 481 int test = name.compareTo(((Node )(nodes.elementAt(i))).getNodeName()); 482 if (test == 0) { 483 return i; } 485 else if (test < 0) { 486 last = i - 1; 487 } 488 else { 489 first = i + 1; 490 } 491 } 492 493 if (first > i) { 494 i = first; 495 } 496 } 497 498 return -1 - i; 500 } 502 503 505 protected int findNamePoint(String namespaceURI, String name) { 506 507 if (nodes == null) return -1; 508 if (name == null) return -1; 509 510 for (int i = 0; i < nodes.size(); i++) { 518 NodeImpl a = (NodeImpl)nodes.elementAt(i); 519 String aNamespaceURI = a.getNamespaceURI(); 520 String aLocalName = a.getLocalName(); 521 if (namespaceURI == null) { 522 if (aNamespaceURI == null 523 && 524 (name.equals(aLocalName) 525 || 526 (aLocalName == null && name.equals(a.getNodeName())))) 527 return i; 528 } else { 529 if (namespaceURI.equals(aNamespaceURI) 530 && 531 name.equals(aLocalName)) 532 return i; 533 } 534 } 535 return -1; 536 } 537 538 protected boolean precedes(Node a, Node b) { 541 542 if (nodes != null) { 543 for (int i = 0; i < nodes.size(); i++) { 544 Node n = (Node )nodes.elementAt(i); 545 if (n==a) return true; 546 if (n==b) return false; 547 } 548 } 549 550 return false; 551 } 552 553 554 557 protected void removeItem(int index) { 558 if (nodes != null && index < nodes.size()){ 559 nodes.removeElementAt(index); 560 } 561 } 562 563 564 protected Object getItem (int index){ 565 if (nodes !=null) { 566 return nodes.elementAt(index); 567 } 568 return null; 569 } 570 571 protected int addItem (Node arg) { 572 int i = findNamePoint(arg.getNamespaceURI(), arg.getLocalName()); 573 NodeImpl previous = null; 574 if (i >= 0) { 575 previous = (NodeImpl) nodes.elementAt(i); 576 nodes.setElementAt(arg,i); 577 } else { 578 i = findNamePoint(arg.getNodeName(),0); 581 if (i >=0) { 582 previous = (NodeImpl) nodes.elementAt(i); 583 nodes.insertElementAt(arg,i); 584 } else { 585 i = -1 - i; if (null == nodes) { 587 nodes = new Vector (5, 10); 588 } 589 nodes.insertElementAt(arg, i); 590 } 591 } 592 return i; 593 } 594 595 601 protected Vector cloneMap(Vector list){ 602 if (list == null) { 603 list = new Vector (5, 10); 604 } 605 list.setSize(0); 606 if (nodes != null) { 607 for (int i=0; i<nodes.size(); i++) { 608 list.insertElementAt(nodes.elementAt(i), i); 609 } 610 } 611 612 return list; 613 } 614 615 protected int getNamedItemIndex(String namespaceURI, String localName) { 616 return findNamePoint(namespaceURI, localName); 617 } 618 619 622 public void removeAll (){ 623 if (nodes != null) { 624 nodes.removeAllElements(); 625 } 626 } 627 628 629 } | Popular Tags |