1 28 29 package com.caucho.xml; 30 31 import org.w3c.dom.Attr ; 32 import org.w3c.dom.DOMException ; 33 import org.w3c.dom.NamedNodeMap ; 34 import org.w3c.dom.Node ; 35 36 public abstract class QAttributedNode extends QNode { 37 QAttr _firstAttribute; 38 39 42 public NamedNodeMap getAttributes() 43 { 44 return new QAttributeMap(this); 45 } 46 47 50 public boolean hasAttributes() 51 { 52 return _firstAttribute != null; 53 } 54 55 58 public Attr getFirstAttribute() 59 { 60 return _firstAttribute; 61 } 62 63 66 public String getAttribute(String name) 67 { 68 for (QAbstractNode attr = _firstAttribute; 69 attr != null; 70 attr = attr._next) { 71 if (name.equals(attr.getNodeName())) 72 return attr.getNodeValue(); 73 } 74 75 return ""; 76 } 77 78 81 public String getAttributeNS(String namespaceURI, String local) 82 { 83 for (QAbstractNode attr = _firstAttribute; 84 attr != null; 85 attr = attr._next) { 86 String attrURI = attr.getNamespaceURI(); 87 88 if (attr.getLocalName().equals(local) && 89 (attrURI == namespaceURI || 90 attrURI != null && attrURI.equals(namespaceURI))) 91 return attr.getNodeValue(); 92 } 93 94 return ""; 95 } 96 97 public boolean hasAttribute(String name) 98 { 99 for (QAbstractNode attr = _firstAttribute; 100 attr != null; 101 attr = attr._next) { 102 if (attr.getNodeName().equals(name)) 103 return true; 104 } 105 106 return false; 107 } 108 109 public boolean hasAttributeNS(String uri, String local) 110 { 111 for (QAbstractNode attr = _firstAttribute; 112 attr != null; 113 attr = attr._next) { 114 String attrURI = attr.getNamespaceURI(); 115 116 if (attr.getLocalName().equals(local) && 117 (attrURI == uri || attrURI != null && attrURI.equals(uri))) 118 return true; 119 } 120 121 return false; 122 } 123 124 127 public Attr getAttributeNode(String name) 128 { 129 for (QAbstractNode attr = _firstAttribute; 130 attr != null; 131 attr = attr._next) { 132 if (attr.getNodeName().equals(name)) 133 return (Attr ) attr; 134 } 135 136 return null; 137 } 138 139 public Attr getAttributeNodeNS(String uri, String local) 140 { 141 for (QAbstractNode attr = _firstAttribute; 142 attr != null; 143 attr = attr._next) { 144 String attrURI = attr.getNamespaceURI(); 145 146 if (attr.getLocalName().equals(local) && 147 (attrURI == uri || 148 attrURI != null && attrURI.equals(uri))) 149 return (Attr ) attr; 150 } 151 152 return null; 153 } 154 155 public void setAttribute(String name, String value) 156 throws DOMException 157 { 158 if (! isNameValid(name)) 159 throw new QDOMException(DOMException.INVALID_CHARACTER_ERR, 160 "illegal attribute `" + name + "'"); 161 162 setAttributeNode(_owner.createAttribute(name, value)); 163 } 164 165 public void setAttributeNS(String uri, String local, String value) 166 { 167 Attr attr = _owner.createAttributeNS(uri, local); 168 attr.setNodeValue(value); 169 170 setAttributeNodeNS(attr); 171 } 172 173 void setAttribute(QName name, String value) 174 throws DOMException 175 { 176 setAttributeNode(_owner.createAttribute(name, value)); 177 } 178 179 182 public void setIdAttribute(String name, boolean isId) 183 throws DOMException 184 { 185 } 186 187 190 public void setIdAttributeNS(String namespaceURI, String localName, 191 boolean isId) 192 throws DOMException 193 { 194 } 195 196 199 public void setIdAttributeNode(Attr attr, boolean isId) 200 throws DOMException 201 { 202 } 203 204 207 public Attr setAttributeNode(Attr attr) 208 throws DOMException 209 { 210 QAttr qAttr = (QAttr) attr; 211 212 if (qAttr._owner == null) 213 qAttr._owner = _owner; 214 else if (qAttr._owner != _owner) 215 throw new QDOMException(DOMException.WRONG_DOCUMENT_ERR, 216 "attribute from wrong document"); 217 218 if (qAttr._parent != null) 219 throw new QDOMException(DOMException.INUSE_ATTRIBUTE_ERR, 220 "attribute `" + attr.getNodeName() + 221 "' is in use"); 222 223 qAttr._parent = this; 224 225 QAttr old = unlink(attr.getNodeName()); 227 228 QAttr ptr = _firstAttribute; 229 230 if (ptr == null) { 231 _firstAttribute = qAttr; 232 } 233 else { 234 for (; ptr._next != null; ptr = (QAttr) ptr._next) { 235 } 236 237 ptr._next = qAttr; 238 } 239 240 return old; 241 } 242 243 public Attr setAttributeNodeNS(Attr attr) 244 throws DOMException 245 { 246 QAttr qAttr = (QAttr) attr; 247 248 if (qAttr._owner != _owner) 249 throw new QDOMException(DOMException.WRONG_DOCUMENT_ERR, 250 "attribute from wrong document"); 251 252 if (qAttr._parent != null) 253 throw new QDOMException(DOMException.INUSE_ATTRIBUTE_ERR, 254 "attribute `" + attr.getNodeName() + 255 "' is in use"); 256 257 QAttr old = unlink(qAttr.getNamespaceURI(), qAttr.getLocalName()); 259 260 qAttr._parent = this; 261 262 qAttr._next = _firstAttribute; 263 _firstAttribute = qAttr; 264 265 return old; 266 } 267 268 271 public void removeAttribute(String name) 272 { 273 if (! isNameValid(name)) 274 throw new QDOMException(DOMException.INVALID_CHARACTER_ERR, 275 "illegal attribute `" + name + "'"); 276 277 unlink(name); 278 } 279 280 283 public void removeAttributeNS(String uri, String name) 284 { 285 unlink(uri, name); 286 } 287 288 291 public Attr removeAttributeNode(Attr attr) 292 { 293 return unlink(attr.getNodeName()); 294 } 295 296 299 public Attr removeAttributeNodeNS(Attr attr) 300 { 301 return unlink(attr.getNamespaceURI(), attr.getLocalName()); 302 } 303 304 307 QAttr unlink(String name) 308 { 309 QAttr prev = null; 310 QAttr ptr; 311 312 for (ptr = _firstAttribute; 313 ptr != null && ! ptr.getNodeName().equals(name); 314 ptr = (QAttr) ptr._next) { 315 prev = ptr; 316 } 317 318 if (ptr == null) 319 return null; 320 321 if (prev == null) 322 _firstAttribute = (QAttr) ptr._next; 323 else 324 prev._next = ptr._next; 325 326 ptr._next = null; 327 328 return ptr; 329 } 330 331 334 public QAttr unlink(String uri, String local) 335 { 336 if (local == null || uri == null) 337 return null; 338 339 QAttr prev = null; 340 QAttr ptr; 341 342 for (ptr = (QAttr) _firstAttribute; 343 ptr != null && (! local.equals(ptr.getLocalName()) || 344 ! uri.equals(ptr.getNamespaceURI())); 345 ptr = (QAttr) ptr._next) { 346 prev = ptr; 347 } 348 349 if (ptr == null) 350 return null; 351 352 if (prev == null) 353 _firstAttribute = (QAttr) ptr._next; 354 else 355 prev._next = ptr._next; 356 357 ptr._next = null; 358 359 return ptr; 360 } 361 362 static class QAttributeMap implements NamedNodeMap { 363 QAttributedNode _elt; 364 int _i; 365 QAttr _attr; 366 367 QAttributeMap(QAttributedNode elt) 368 { 369 _elt = elt; 370 } 371 372 public Node getNamedItem(String name) 373 { 374 return _elt.getAttributeNode(name); 375 } 376 377 public Node getNamedItemNS(String uri, String localName) 378 { 379 return _elt.getAttributeNodeNS(uri, localName); 380 } 381 382 public Node setNamedItem(Node arg) throws DOMException 383 { 384 return _elt.setAttributeNode((Attr ) arg); 385 } 386 387 public Node setNamedItemNS(Node arg) 388 { 389 return _elt.setAttributeNodeNS((Attr ) arg); 390 } 391 392 public Node removeNamedItem(String name) throws DOMException 393 { 394 return _elt.unlink(name); 395 } 396 397 public Node removeNamedItemNS(String uri, String localName) 398 { 399 return _elt.getAttributeNodeNS(uri, localName); 400 } 401 402 public Node item(int index) 403 { 404 QAbstractNode attr = _elt._firstAttribute; 405 406 while (index > 0 && attr != null) { 407 attr = attr._next; 408 index--; 409 } 410 411 return attr; 412 } 413 414 public int getLength() 415 { 416 int length = 0; 417 418 for (QAbstractNode attr = _elt._firstAttribute; 419 attr != null; 420 attr = attr._next) 421 length++; 422 423 return length; 424 } 425 } 426 } 427 | Popular Tags |