1 16 17 package org.apache.xerces.dom; 18 19 import java.io.Serializable ; 20 import java.util.Vector ; 21 22 import org.w3c.dom.DOMException ; 23 import org.w3c.dom.NamedNodeMap ; 24 import org.w3c.dom.Node ; 25 26 53 public class NamedNodeMapImpl 54 implements NamedNodeMap , Serializable { 55 56 60 61 static final long serialVersionUID = -7039242451046758020L; 62 63 67 protected short flags; 68 69 protected final static short READONLY = 0x1<<0; 70 protected final static short CHANGED = 0x1<<1; 71 protected final static short HASDEFAULTS = 0x1<<2; 72 73 74 protected Vector nodes; 75 76 protected NodeImpl ownerNode; 78 82 83 protected NamedNodeMapImpl(NodeImpl ownerNode) { 84 this.ownerNode = ownerNode; 85 } 86 87 91 97 public int getLength() { 98 return (nodes != null) ? nodes.size() : 0; 99 } 100 101 115 public Node item(int index) { 116 return (nodes != null && index < nodes.size()) ? 117 (Node )(nodes.elementAt(index)) : null; 118 } 119 120 127 public Node getNamedItem(String name) { 128 129 int i = findNamePoint(name,0); 130 return (i < 0) ? null : (Node )(nodes.elementAt(i)); 131 132 } 134 145 public Node getNamedItemNS(String namespaceURI, String localName) { 146 147 int i = findNamePoint(namespaceURI, localName); 148 return (i < 0) ? null : (Node )(nodes.elementAt(i)); 149 150 } 152 169 public Node setNamedItem(Node arg) 170 throws DOMException { 171 172 CoreDocumentImpl ownerDocument = ownerNode.ownerDocument(); 173 if (ownerDocument.errorChecking) { 174 if (isReadOnly()) { 175 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 176 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 177 } 178 if (arg.getOwnerDocument() != ownerDocument) { 179 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); 180 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 181 } 182 } 183 184 int i = findNamePoint(arg.getNodeName(),0); 185 NodeImpl previous = null; 186 if (i >= 0) { 187 previous = (NodeImpl) nodes.elementAt(i); 188 nodes.setElementAt(arg,i); 189 } else { 190 i = -1 - i; if (null == nodes) { 192 nodes = new Vector (5, 10); 193 } 194 nodes.insertElementAt(arg, i); 195 } 196 return previous; 197 198 } 200 211 public Node setNamedItemNS(Node arg) 212 throws DOMException { 213 214 CoreDocumentImpl ownerDocument = ownerNode.ownerDocument(); 215 if (ownerDocument.errorChecking) { 216 if (isReadOnly()) { 217 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 218 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 219 } 220 221 if(arg.getOwnerDocument() != ownerDocument) { 222 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); 223 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 224 } 225 } 226 227 int i = findNamePoint(arg.getNamespaceURI(), arg.getLocalName()); 228 NodeImpl previous = null; 229 if (i >= 0) { 230 previous = (NodeImpl) nodes.elementAt(i); 231 nodes.setElementAt(arg,i); 232 } else { 233 i = findNamePoint(arg.getNodeName(),0); 236 if (i >= 0) { 237 previous = (NodeImpl) nodes.elementAt(i); 238 nodes.insertElementAt(arg,i); 239 } else { 240 i = -1 - i; if (null == nodes) { 242 nodes = new Vector (5, 10); 243 } 244 nodes.insertElementAt(arg, i); 245 } 246 } 247 return previous; 248 249 } 251 256 257 public Node removeNamedItem(String name) 258 throws DOMException { 259 260 if (isReadOnly()) { 261 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 262 throw 263 new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 264 msg); 265 } 266 int i = findNamePoint(name,0); 267 if (i < 0) { 268 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 269 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 270 } 271 272 NodeImpl n = (NodeImpl)nodes.elementAt(i); 273 nodes.removeElementAt(i); 274 275 return n; 276 277 } 279 293 public Node removeNamedItemNS(String namespaceURI, String name) 294 throws DOMException { 295 296 if (isReadOnly()) { 297 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 298 throw 299 new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 300 msg); 301 } 302 int i = findNamePoint(namespaceURI, name); 303 if (i < 0) { 304 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 305 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 306 } 307 308 NodeImpl n = (NodeImpl)nodes.elementAt(i); 309 nodes.removeElementAt(i); 310 311 return n; 312 313 } 315 319 323 324 public NamedNodeMapImpl cloneMap(NodeImpl ownerNode) { 325 NamedNodeMapImpl newmap = new NamedNodeMapImpl(ownerNode); 326 newmap.cloneContent(this); 327 return newmap; 328 } 329 330 protected void cloneContent(NamedNodeMapImpl srcmap) { 331 Vector srcnodes = srcmap.nodes; 332 if (srcnodes != null) { 333 int size = srcnodes.size(); 334 if (size != 0) { 335 if (nodes == null) { 336 nodes = new Vector (size); 337 } 338 nodes.setSize(size); 339 for (int i = 0; i < size; ++i) { 340 NodeImpl n = (NodeImpl) srcmap.nodes.elementAt(i); 341 NodeImpl clone = (NodeImpl) n.cloneNode(true); 342 clone.isSpecified(n.isSpecified()); 343 nodes.setElementAt(clone, i); 344 } 345 } 346 } 347 } 349 353 364 void setReadOnly(boolean readOnly, boolean deep) { 365 isReadOnly(readOnly); 366 if (deep && nodes != null) { 367 for (int i = nodes.size() - 1; i >= 0; i--) { 368 ((NodeImpl) nodes.elementAt(i)).setReadOnly(readOnly,deep); 369 } 370 } 371 } 373 377 boolean getReadOnly() { 378 return isReadOnly(); 379 } 381 382 386 390 void setOwnerDocument(CoreDocumentImpl doc) { 391 if (nodes != null) { 392 for (int i = 0; i < nodes.size(); i++) { 393 ((NodeImpl)item(i)).setOwnerDocument(doc); 394 } 395 } 396 } 397 398 final boolean isReadOnly() { 399 return (flags & READONLY) != 0; 400 } 401 402 final void isReadOnly(boolean value) { 403 flags = (short) (value ? flags | READONLY : flags & ~READONLY); 404 } 405 406 final boolean changed() { 407 return (flags & CHANGED) != 0; 408 } 409 410 final void changed(boolean value) { 411 flags = (short) (value ? flags | CHANGED : flags & ~CHANGED); 412 } 413 414 final boolean hasDefaults() { 415 return (flags & HASDEFAULTS) != 0; 416 } 417 418 final void hasDefaults(boolean value) { 419 flags = (short) (value ? flags | HASDEFAULTS : flags & ~HASDEFAULTS); 420 } 421 422 426 438 protected int findNamePoint(String name, int start) { 439 440 int i = 0; 442 if (nodes != null) { 443 int first = start; 444 int last = nodes.size() - 1; 445 446 while (first <= last) { 447 i = (first + last) / 2; 448 int test = name.compareTo(((Node )(nodes.elementAt(i))).getNodeName()); 449 if (test == 0) { 450 return i; } 452 else if (test < 0) { 453 last = i - 1; 454 } 455 else { 456 first = i + 1; 457 } 458 } 459 460 if (first > i) { 461 i = first; 462 } 463 } 464 465 return -1 - i; 467 } 469 470 472 protected int findNamePoint(String namespaceURI, String name) { 473 474 if (nodes == null) return -1; 475 if (name == null) return -1; 476 477 for (int i = 0; i < nodes.size(); i++) { 485 NodeImpl a = (NodeImpl)nodes.elementAt(i); 486 String aNamespaceURI = a.getNamespaceURI(); 487 String aLocalName = a.getLocalName(); 488 if (namespaceURI == null) { 489 if (aNamespaceURI == null 490 && 491 (name.equals(aLocalName) 492 || 493 (aLocalName == null && name.equals(a.getNodeName())))) 494 return i; 495 } else { 496 if (namespaceURI.equals(aNamespaceURI) 497 && 498 name.equals(aLocalName)) 499 return i; 500 } 501 } 502 return -1; 503 } 504 505 protected boolean precedes(Node a, Node b) { 508 509 if (nodes != null) { 510 for (int i = 0; i < nodes.size(); i++) { 511 Node n = (Node )nodes.elementAt(i); 512 if (n==a) return true; 513 if (n==b) return false; 514 } 515 } 516 517 return false; 518 } 519 520 521 524 protected void removeItem(int index) { 525 if (nodes != null && index < nodes.size()){ 526 nodes.removeElementAt(index); 527 } 528 } 529 530 531 protected Object getItem (int index){ 532 if (nodes !=null) { 533 return nodes.elementAt(index); 534 } 535 return null; 536 } 537 538 protected int addItem (Node arg) { 539 int i = findNamePoint(arg.getNamespaceURI(), arg.getLocalName()); 540 if (i >= 0) { 541 nodes.setElementAt(arg,i); 542 } 543 else { 544 i = findNamePoint(arg.getNodeName(),0); 547 if (i >= 0) { 548 nodes.insertElementAt(arg,i); 549 } 550 else { 551 i = -1 - i; if (null == nodes) { 553 nodes = new Vector (5, 10); 554 } 555 nodes.insertElementAt(arg, i); 556 } 557 } 558 return i; 559 } 560 561 567 protected Vector cloneMap(Vector list){ 568 if (list == null) { 569 list = new Vector (5, 10); 570 } 571 list.setSize(0); 572 if (nodes != null) { 573 for (int i=0; i<nodes.size(); i++) { 574 list.insertElementAt(nodes.elementAt(i), i); 575 } 576 } 577 578 return list; 579 } 580 581 protected int getNamedItemIndex(String namespaceURI, String localName) { 582 return findNamePoint(namespaceURI, localName); 583 } 584 585 588 public void removeAll (){ 589 if (nodes != null) { 590 nodes.removeAllElements(); 591 } 592 } 593 594 595 } | Popular Tags |