1 57 58 package org.enhydra.apache.xerces.dom; 59 60 import java.io.Serializable ; 61 import java.util.Enumeration ; 62 import java.util.Vector ; 63 64 import org.w3c.dom.DOMException ; 65 import org.w3c.dom.NamedNodeMap ; 66 import org.w3c.dom.Node ; 67 68 93 public class NamedNodeMapImpl 94 implements NamedNodeMap , Serializable { 95 96 100 101 static final long serialVersionUID = -7039242451046758020L; 102 103 107 protected short flags; 108 109 protected final static short READONLY = 0x1<<0; 110 protected final static short CHANGED = 0x1<<1; 111 protected final static short HASDEFAULTS = 0x1<<2; 112 113 114 protected Vector nodes; 115 116 protected NodeImpl ownerNode; 118 122 123 protected NamedNodeMapImpl(NodeImpl ownerNode) { 124 this.ownerNode = ownerNode; 125 } 126 127 131 137 public int getLength() { 138 return (nodes != null) ? nodes.size() : 0; 139 } 140 141 155 public Node item(int index) { 156 return (nodes != null && index < nodes.size()) ? 157 (Node )(nodes.elementAt(index)) : null; 158 } 159 160 167 public Node getNamedItem(String name) { 168 169 int i = findNamePoint(name,0); 170 return (i < 0) ? null : (Node )(nodes.elementAt(i)); 171 172 } 174 185 public Node getNamedItemNS(String namespaceURI, String localName) { 186 187 int i = findNamePoint(namespaceURI, localName); 188 return (i < 0) ? null : (Node )(nodes.elementAt(i)); 189 190 } 192 209 public Node setNamedItem(Node arg) 210 throws DOMException { 211 212 if (isReadOnly()) { 213 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 214 "DOM001 Modification not allowed"); 215 } 216 if (arg.getOwnerDocument() != ownerNode.ownerDocument()) { 217 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, 218 "DOM005 Wrong document"); 219 } 220 221 int i = findNamePoint(arg.getNodeName(),0); 222 NodeImpl previous = null; 223 if (i >= 0) { 224 previous = (NodeImpl) nodes.elementAt(i); 225 nodes.setElementAt(arg,i); 226 } else { 227 i = -1 - i; if (null == nodes) { 229 nodes = new Vector (5, 10); 230 } 231 nodes.insertElementAt(arg, i); 232 } 233 return previous; 234 235 } 237 248 public Node setNamedItemNS(Node arg) 249 throws DOMException { 250 251 if (isReadOnly()) { 252 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 253 "DOM001 Modification not allowed"); 254 } 255 256 if(arg.getOwnerDocument() != ownerNode.ownerDocument()) { 257 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, 258 "DOM005 Wrong document"); 259 } 260 261 int i = findNamePoint(arg.getNamespaceURI(), arg.getLocalName()); 262 NodeImpl previous = null; 263 if (i >= 0) { 264 previous = (NodeImpl) nodes.elementAt(i); 265 nodes.setElementAt(arg,i); 266 } else { 267 i = findNamePoint(arg.getNodeName(),0); 270 if (i >=0) { 271 previous = (NodeImpl) nodes.elementAt(i); 272 nodes.insertElementAt(arg,i); 273 } else { 274 i = -1 - i; if (null == nodes) { 276 nodes = new Vector (5, 10); 277 } 278 nodes.insertElementAt(arg, i); 279 } 280 } 281 return previous; 282 283 } 285 290 291 public Node removeNamedItem(String name) 292 throws DOMException { 293 294 if (isReadOnly()) { 295 throw 296 new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 297 "DOM001 Modification not allowed"); 298 } 299 int i = findNamePoint(name,0); 300 if (i < 0) { 301 throw new DOMException (DOMException.NOT_FOUND_ERR, 302 "DOM008 Not found"); 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 throw 331 new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 332 "DOM001 Modification not allowed"); 333 } 334 int i = findNamePoint(namespaceURI, name); 335 if (i < 0) { 336 throw new DOMException (DOMException.NOT_FOUND_ERR, 337 "DOM008 Not found"); 338 } 339 340 NodeImpl n = (NodeImpl)nodes.elementAt(i); 341 nodes.removeElementAt(i); 342 343 return n; 344 345 } 347 351 355 356 public NamedNodeMapImpl cloneMap(NodeImpl ownerNode) { 357 NamedNodeMapImpl newmap = new NamedNodeMapImpl(ownerNode); 358 newmap.cloneContent(this); 359 return newmap; 360 } 361 362 protected void cloneContent(NamedNodeMapImpl srcmap) { 363 if (srcmap.nodes != null) { 364 nodes = new Vector (srcmap.nodes.size()); 365 for (int i = 0; i < srcmap.nodes.size(); ++i) { 366 NodeImpl n = (NodeImpl) srcmap.nodes.elementAt(i); 367 NodeImpl clone = (NodeImpl) n.cloneNode(true); 368 clone.isSpecified(n.isSpecified()); 369 nodes.insertElementAt(clone, i); 370 } 371 } 372 } 374 378 389 void setReadOnly(boolean readOnly, boolean deep) { 390 391 isReadOnly(readOnly); 392 if(deep && nodes != null) { 393 Enumeration e=nodes.elements(); 394 while(e.hasMoreElements()) { 395 ((NodeImpl)e.nextElement()).setReadOnly(readOnly,deep); 396 } 397 } 398 399 } 401 405 boolean getReadOnly() { 406 return isReadOnly(); 407 } 409 410 414 418 void setOwnerDocument(CoreDocumentImpl doc) { 419 if (nodes != null) { 420 for (int i = 0; i < nodes.size(); i++) { 421 ((NodeImpl)item(i)).setOwnerDocument(doc); 422 } 423 } 424 } 425 426 final boolean isReadOnly() { 427 return (flags & READONLY) != 0; 428 } 429 430 final void isReadOnly(boolean value) { 431 flags = (short) (value ? flags | READONLY : flags & ~READONLY); 432 } 433 434 final boolean changed() { 435 return (flags & CHANGED) != 0; 436 } 437 438 final void changed(boolean value) { 439 flags = (short) (value ? flags | CHANGED : flags & ~CHANGED); 440 } 441 442 final boolean hasDefaults() { 443 return (flags & HASDEFAULTS) != 0; 444 } 445 446 final void hasDefaults(boolean value) { 447 flags = (short) (value ? flags | HASDEFAULTS : flags & ~HASDEFAULTS); 448 } 449 450 454 466 protected int findNamePoint(String name, int start) { 467 468 int i = 0; 470 if(nodes != null) { 471 int first = start; 472 int last = nodes.size() - 1; 473 474 while (first <= last) { 475 i = (first + last) / 2; 476 int test = name.compareTo(((Node )(nodes.elementAt(i))).getNodeName()); 477 if(test == 0) { 478 return i; } 480 else if (test < 0) { 481 last = i - 1; 482 } 483 else { 484 first = i + 1; 485 } 486 } 487 488 if (first > i) { 489 i = first; 490 } 491 } 492 493 return -1 - i; 495 } 497 498 500 protected int findNamePoint(String namespaceURI, String name) { 501 502 if (nodes == null) return -1; 503 if (name == null) return -1; 504 505 for (int i = 0; i < nodes.size(); i++) { 513 NodeImpl a = (NodeImpl)nodes.elementAt(i); 514 String aNamespaceURI = a.getNamespaceURI(); 515 String aLocalName = a.getLocalName(); 516 if (namespaceURI == null) { 517 if (aNamespaceURI == null 518 && 519 (name.equals(aLocalName) 520 || 521 (aLocalName == null && name.equals(a.getNodeName())))) 522 return i; 523 } else { 524 if (namespaceURI.equals(aNamespaceURI) 525 && 526 name.equals(aLocalName)) 527 return i; 528 } 529 } 530 return -1; 531 } 532 533 534 } | Popular Tags |