1 57 58 package org.enhydra.apache.xerces.dom; 59 60 import java.util.Vector ; 61 62 import org.w3c.dom.DOMException ; 63 import org.w3c.dom.Node ; 64 65 66 79 public class AttributeMap extends NamedNodeMapImpl { 80 81 85 86 protected AttributeMap(ElementImpl ownerNode, NamedNodeMapImpl defaults) { 87 super(ownerNode); 88 if (defaults != null) { 89 cloneContent(defaults); 91 if (nodes != null) { 92 hasDefaults(true); 93 } 94 } 95 } 96 97 106 public Node setNamedItem(Node arg) 107 throws DOMException { 108 109 if (isReadOnly()) { 110 throw 111 new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 112 "DOM001 Modification not allowed"); 113 } 114 if(arg.getOwnerDocument() != ownerNode.ownerDocument()) { 115 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, 116 "DOM005 Wrong document"); 117 } 118 if (arg.getNodeType() != Node.ATTRIBUTE_NODE) { 119 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, 120 "DOM006 Hierarchy request error"); 121 } 122 123 AttrImpl argn = (AttrImpl)arg; 124 125 if (argn.isOwned()) { 126 throw new DOMException (DOMException.INUSE_ATTRIBUTE_ERR, 127 "DOM009 Attribute already in use"); 128 } 129 130 argn.ownerNode = ownerNode; 132 argn.isOwned(true); 133 134 int i = findNamePoint(arg.getNodeName(),0); 135 AttrImpl previous = null; 136 if (i >= 0) { 137 previous = (AttrImpl) nodes.elementAt(i); 138 nodes.setElementAt(arg,i); 139 previous.ownerNode = ownerNode.ownerDocument(); 140 previous.isOwned(false); 141 previous.isSpecified(true); 143 } else { 144 i = -1 - i; if (null == nodes) { 146 nodes = new Vector (5, 10); 147 } 148 nodes.insertElementAt(arg, i); 149 } 150 151 ownerNode.ownerDocument().setAttrNode(argn, previous); 153 154 if (!argn.isNormalized()) { 157 ownerNode.isNormalized(false); 158 } 159 return previous; 160 161 } 163 170 public Node setNamedItemNS(Node arg) 171 throws DOMException { 172 173 if (isReadOnly()) { 174 throw 175 new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 176 "DOM001 Modification not allowed"); 177 } 178 179 if(arg.getOwnerDocument() != ownerNode.ownerDocument()) { 180 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, 181 "DOM005 Wrong document"); 182 } 183 184 if (arg.getNodeType() != Node.ATTRIBUTE_NODE) { 185 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, 186 "DOM006 Hierarchy request error"); 187 } 188 AttrImpl argn = (AttrImpl)arg; 189 if (argn.isOwned()) { 190 throw new DOMException (DOMException.INUSE_ATTRIBUTE_ERR, 191 "DOM009 Attribute already in use"); 192 } 193 194 argn.ownerNode = ownerNode; 196 argn.isOwned(true); 197 198 int i = findNamePoint(argn.getNamespaceURI(), argn.getLocalName()); 199 AttrImpl previous = null; 200 if (i >= 0) { 201 previous = (AttrImpl) nodes.elementAt(i); 202 nodes.setElementAt(arg,i); 203 previous.ownerNode = ownerNode.ownerDocument(); 204 previous.isOwned(false); 205 previous.isSpecified(true); 207 } else { 208 i = findNamePoint(arg.getNodeName(),0); 211 if (i >=0) { 212 previous = (AttrImpl) nodes.elementAt(i); 213 nodes.insertElementAt(arg,i); 214 } else { 215 i = -1 - i; if (null == nodes) { 217 nodes = new Vector (5, 10); 218 } 219 nodes.insertElementAt(arg, i); 220 } 221 } 222 224 ownerNode.ownerDocument().setAttrNode(argn, previous); 226 227 if (!argn.isNormalized()) { 230 ownerNode.isNormalized(false); 231 } 232 return previous; 233 234 } 236 248 249 public Node removeNamedItem(String name) 250 throws DOMException { 251 return internalRemoveNamedItem(name, true); 252 } 253 254 258 Node safeRemoveNamedItem(String name) { 259 return internalRemoveNamedItem(name, false); 260 } 261 262 266 final protected Node internalRemoveNamedItem(String name, boolean raiseEx){ 267 if (isReadOnly()) { 268 throw 269 new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 270 "DOM001 Modification not allowed"); 271 } 272 int i = findNamePoint(name,0); 273 if (i < 0) { 274 if (raiseEx) { 275 throw new DOMException (DOMException.NOT_FOUND_ERR, 276 "DOM008 Not found"); 277 } else { 278 return null; 279 } 280 } 281 282 AttrImpl n = (AttrImpl)nodes.elementAt(i); 283 CoreDocumentImpl ownerDocument = ownerNode.ownerDocument(); 284 285 if (hasDefaults()) { 287 NamedNodeMapImpl defaults = 288 ((ElementImpl) ownerNode).getDefaultAttributes(); 289 Node d; 290 if (defaults != null && (d = defaults.getNamedItem(name)) != null 291 && findNamePoint(name, i+1) < 0) { 292 293 NodeImpl clone = (NodeImpl)d.cloneNode(true); 294 clone.ownerNode = ownerNode; 295 clone.isOwned(true); 296 clone.isSpecified(false); 297 nodes.setElementAt(clone, i); 298 } else { 299 nodes.removeElementAt(i); 300 } 301 } else { 302 nodes.removeElementAt(i); 303 } 304 305 307 n.ownerNode = ownerDocument; 309 n.isOwned(false); 310 n.isSpecified(true); 312 313 ownerDocument.removedAttrNode(n, ownerNode, name); 315 316 return n; 317 318 } 320 336 public Node removeNamedItemNS(String namespaceURI, String name) 337 throws DOMException { 338 return internalRemoveNamedItemNS(namespaceURI, name, true); 339 } 340 341 345 Node safeRemoveNamedItemNS(String namespaceURI, String name) { 346 return internalRemoveNamedItemNS(namespaceURI, name, false); 347 } 348 349 354 final protected Node internalRemoveNamedItemNS(String namespaceURI, 355 String name, 356 boolean raiseEx) { 357 if (isReadOnly()) { 358 throw 359 new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 360 "DOM001 Modification not allowed"); 361 } 362 int i = findNamePoint(namespaceURI, name); 363 if (i < 0) { 364 if (raiseEx) { 365 throw new DOMException (DOMException.NOT_FOUND_ERR, 366 "DOM008 Not found"); 367 } else { 368 return null; 369 } 370 } 371 372 AttrImpl n = (AttrImpl)nodes.elementAt(i); 373 CoreDocumentImpl ownerDocument = ownerNode.ownerDocument(); 374 375 String nodeName = n.getNodeName(); 377 if (hasDefaults()) { 378 NamedNodeMapImpl defaults = 379 ((ElementImpl) ownerNode).getDefaultAttributes(); 380 Node d; 381 if (defaults != null 382 && (d = defaults.getNamedItem(nodeName)) != null) 383 { 384 int j = findNamePoint(nodeName,0); 385 if (j>=0 && findNamePoint(nodeName, j+1) < 0) { 386 NodeImpl clone = (NodeImpl)d.cloneNode(true); 387 clone.ownerNode = ownerNode; 388 if (clone instanceof AttrNSImpl) { 393 ((AttrNSImpl)clone).namespaceURI = namespaceURI; 394 } 395 clone.isOwned(true); 396 clone.isSpecified(false); 397 nodes.setElementAt(clone, i); 398 } else { 399 nodes.removeElementAt(i); 400 } 401 } else { 402 nodes.removeElementAt(i); 403 } 404 } else { 405 nodes.removeElementAt(i); 406 } 407 408 410 n.ownerNode = ownerDocument; 412 n.isOwned(false); 413 n.isSpecified(true); 415 416 ownerDocument.removedAttrNode(n, ownerNode, name); 418 419 return n; 420 421 } 423 427 431 432 public NamedNodeMapImpl cloneMap(NodeImpl ownerNode) { 433 AttributeMap newmap = 434 new AttributeMap((ElementImpl) ownerNode, null); 435 newmap.hasDefaults(hasDefaults()); 436 newmap.cloneContent(this); 437 return newmap; 438 } 440 443 protected void cloneContent(NamedNodeMapImpl srcmap) { 444 if (srcmap.nodes != null) { 445 if (nodes == null) { 446 nodes = new Vector (srcmap.nodes.size()); 447 } 448 else { 449 nodes.setSize(srcmap.nodes.size()); 450 } 451 for (int i = 0; i < srcmap.nodes.size(); ++i) { 452 NodeImpl n = (NodeImpl) srcmap.nodes.elementAt(i); 453 NodeImpl clone = (NodeImpl) n.cloneNode(true); 454 clone.isSpecified(n.isSpecified()); 455 nodes.insertElementAt(clone, i); 456 clone.ownerNode = ownerNode; 457 clone.isOwned(true); 458 } 459 } 460 } 462 463 467 protected void reconcileDefaults(NamedNodeMapImpl defaults) { 468 469 int nsize = (nodes != null) ? nodes.size() : 0; 471 for (int i = nsize - 1; i >= 0; i--) { 472 AttrImpl attr = (AttrImpl) nodes.elementAt(i); 473 if (!attr.isSpecified()) { 474 attr.ownerNode = ownerNode.ownerDocument(); 476 attr.isOwned(false); 477 attr.isSpecified(true); 479 nodes.removeElementAt(i); 480 } 481 } 482 if (defaults == null) { 484 return; 485 } 486 if (nodes == null || nodes.size() == 0) { 487 cloneContent(defaults); 488 } 489 else { 490 int dsize = defaults.nodes.size(); 491 for (int n = 0; n < dsize; n++) { 492 AttrImpl d = (AttrImpl) defaults.nodes.elementAt(n); 493 int i = findNamePoint(d.getNodeName(), 0); 494 if (i < 0) { 495 NodeImpl clone = (NodeImpl) d.cloneNode(true); 496 clone.ownerNode = ownerNode; 497 clone.isOwned(true); 498 clone.isSpecified(false); 499 nodes.setElementAt(clone, i); 500 } 501 } 502 } 503 504 } 506 } | Popular Tags |