1 21 24 package org.lobobrowser.html.domimpl; 25 26 import java.util.*; 27 28 import org.lobobrowser.util.*; 29 import org.w3c.dom.Attr ; 30 import org.w3c.dom.DOMException ; 31 import org.w3c.dom.Element ; 32 import org.w3c.dom.NamedNodeMap ; 33 import org.w3c.dom.Node ; 34 import org.w3c.dom.NodeList ; 35 import org.w3c.dom.TypeInfo ; 36 import org.w3c.dom.Text ; 37 import org.w3c.dom.Comment ; 38 39 public class ElementImpl extends NodeImpl implements Element { 40 private final String name; 41 42 public ElementImpl(String name) { 43 super(); 44 this.name = name; 45 } 46 47 protected Map attributes; 48 49 54 public NamedNodeMap getAttributes() { 55 synchronized(this) { 56 Map attrs = this.attributes; 57 if (attrs == null) { 58 attrs = new HashMap(); 59 this.attributes = attrs; 60 } 61 return new NamedNodeMapImpl(this, this.attributes); 62 } 63 } 64 65 public boolean hasAttributes() { 66 synchronized(this) { 67 Map attrs = this.attributes; 68 return attrs == null ? false : !attrs.isEmpty(); 69 } 70 } 71 72 public boolean equalAttributes(Node arg) { 73 if (arg instanceof ElementImpl) { 74 synchronized(this) { 75 Map attrs1 = this.attributes; 76 if (attrs1 == null) { 77 attrs1 = Collections.EMPTY_MAP; 78 } 79 Map attrs2 = ((ElementImpl) arg).attributes; 80 if (attrs2 == null) { 81 attrs2 = Collections.EMPTY_MAP; 82 } 83 return Objects.equals(attrs1, attrs2); 84 } 85 } else { 86 return false; 87 } 88 } 89 90 private String id; 91 92 public String getId() { 93 return this.id; 94 } 95 96 public void setId(String id) { 97 this.setAttribute("id", id); 98 } 99 100 102 public String getTitle() { 103 return this.getAttribute("title"); 104 } 105 106 public void setTitle(String title) { 107 this.setAttribute("title", title); 108 } 109 110 public String getLang() { 111 return this.getAttribute("lang"); 112 } 113 114 public void setLang(String lang) { 115 this.setAttribute("lang", lang); 116 } 117 118 public String getDir() { 119 return this.getAttribute("dir"); 120 } 121 122 public void setDir(String dir) { 123 this.setAttribute("dir", dir); 124 } 125 126 public final String getAttribute(String name) { 127 String normalName = this.normalizeAttributeName(name); 128 synchronized(this) { 129 Map attributes = this.attributes; 130 return attributes == null ? null : (String ) attributes 131 .get(normalName); 132 } 133 } 134 135 private Attr getAttr(String normalName, String value) { 136 return new AttrImpl(normalName, value, true, this, "id" 138 .equals(normalName)); 139 } 140 141 public Attr getAttributeNode(String name) { 142 String normalName = this.normalizeAttributeName(name); 143 synchronized(this) { 144 Map attributes = this.attributes; 145 String value = attributes == null ? null : (String ) attributes 146 .get(normalName); 147 return value == null ? null : this.getAttr(normalName, value); 148 } 149 } 150 151 public Attr getAttributeNodeNS(String namespaceURI, String localName) 152 throws DOMException { 153 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, 154 "Namespaces not supported"); 155 } 156 157 public String getAttributeNS(String namespaceURI, String localName) 158 throws DOMException { 159 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, 160 "Namespaces not supported"); 161 } 162 163 protected static boolean isTagName(Node node, String name) { 164 return node.getNodeName().equalsIgnoreCase(name); 165 } 166 167 public NodeList getElementsByTagName(String name) { 168 boolean matchesAll = "*".equals(name); 169 List descendents = new LinkedList(); 170 synchronized(this.treeLock) { 171 ArrayList nl = this.nodeList; 172 if(nl != null) { 173 Iterator i = nl.iterator(); 174 while (i.hasNext()) { 175 Object child = i.next(); 176 if (child instanceof Element ) { 177 Element childElement = (Element ) child; 178 if (matchesAll || isTagName(childElement, name)) { 179 descendents.add(child); 180 } 181 NodeList sublist = childElement.getElementsByTagName(name); 182 int length = sublist.getLength(); 183 for (int idx = 0; idx < length; idx++) { 184 descendents.add(sublist.item(idx)); 185 } 186 } 187 } 188 } 189 } 190 return new NodeListImpl(descendents); 191 } 192 193 public NodeList getElementsByTagNameNS(String namespaceURI, String localName) 194 throws DOMException { 195 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, 196 "Namespaces not supported"); 197 } 198 199 public TypeInfo getSchemaTypeInfo() { 200 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, 201 "Namespaces not supported"); 202 } 203 204 public String getTagName() { 205 return this.getNodeName(); 206 } 207 208 public boolean hasAttribute(String name) { 209 String normalName = this.normalizeAttributeName(name); 210 synchronized(this) { 211 Map attributes = this.attributes; 212 return attributes == null ? false : attributes 213 .containsKey(normalName); 214 } 215 } 216 217 public boolean hasAttributeNS(String namespaceURI, String localName) 218 throws DOMException { 219 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, 220 "Namespaces not supported"); 221 } 222 223 public void removeAttribute(String name) throws DOMException { 224 String normalName = this.normalizeAttributeName(name); 225 synchronized(this) { 226 Map attributes = this.attributes; 227 if (attributes == null) { 228 return; 229 } 230 attributes.remove(normalName); 231 } 232 } 233 234 public Attr removeAttributeNode(Attr oldAttr) throws DOMException { 235 String normalName = this.normalizeAttributeName(oldAttr.getName()); 236 synchronized(this) { 237 Map attributes = this.attributes; 238 if (attributes == null) { 239 return null; 240 } 241 String oldValue = (String ) attributes.remove(normalName); 242 return oldValue == null ? null : this.getAttr(normalName, oldValue); 244 } 245 } 246 247 public void removeAttributeNS(String namespaceURI, String localName) 248 throws DOMException { 249 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, 250 "Namespaces not supported"); 251 } 252 253 protected void assignAttributeField(String normalName, String value) { 254 boolean isName = false; 258 if ("id".equals(normalName) || (isName = "name".equals(normalName))) { 259 String oldId = this.id; 263 this.id = value; 264 HTMLDocumentImpl document = (HTMLDocumentImpl) this.document; 265 if (document != null) { 266 if (oldId != null) { 267 document.removeElementById(oldId); 268 } 269 document.setElementById(value, this); 270 if(isName) { 271 String oldName = this.getAttribute("name"); 272 if(oldName != null) { 273 document.removeNamedItem(oldName); 274 } 275 document.setNamedItem(value, this); 276 } 277 } 278 } 279 } 280 281 protected final String normalizeAttributeName(String name) { 282 return name.toLowerCase(); 283 } 284 285 public void setAttribute(String name, String value) throws DOMException { 286 String normalName = this.normalizeAttributeName(name); 287 synchronized(this) { 288 Map attribs = this.attributes; 289 if (attribs == null) { 290 attribs = new HashMap(2); 291 this.attributes = attribs; 292 } 293 attribs.put(normalName, value); 294 } 295 this.assignAttributeField(normalName, value); 296 } 297 298 302 public void setAttributeImpl(String name, String value) throws DOMException { 303 String normalName = this.normalizeAttributeName(name); 304 Map attribs = this.attributes; 305 if (attribs == null) { 306 attribs = new HashMap(2); 307 this.attributes = attribs; 308 } 309 this.assignAttributeField(normalName, value); 310 attribs.put(normalName, value); 311 } 312 313 public Attr setAttributeNode(Attr newAttr) throws DOMException { 314 String normalName = this.normalizeAttributeName(newAttr.getName()); 315 String value = newAttr.getValue(); 316 synchronized(this) { 317 if (this.attributes == null) { 318 this.attributes = new HashMap(); 319 } 320 this.attributes.put(normalName, value); 321 } 323 this.assignAttributeField(normalName, value); 324 return newAttr; 325 } 326 327 public Attr setAttributeNodeNS(Attr newAttr) throws DOMException { 328 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, 329 "Namespaces not supported"); 330 } 331 332 public void setAttributeNS(String namespaceURI, String qualifiedName, 333 String value) throws DOMException { 334 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, 335 "Namespaces not supported"); 336 } 337 338 public void setIdAttribute(String name, boolean isId) throws DOMException { 339 String normalName = this.normalizeAttributeName(name); 340 if (!"id".equals(normalName)) { 341 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, 342 "IdAttribute can't be anything other than ID"); 343 } 344 } 345 346 public void setIdAttributeNode(Attr idAttr, boolean isId) 347 throws DOMException { 348 String normalName = this.normalizeAttributeName(idAttr.getName()); 349 if (!"id".equals(normalName)) { 350 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, 351 "IdAttribute can't be anything other than ID"); 352 } 353 } 354 355 public void setIdAttributeNS(String namespaceURI, String localName, 356 boolean isId) throws DOMException { 357 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, 358 "Namespaces not supported"); 359 } 360 361 366 public String getLocalName() { 367 return this.getNodeName(); 368 } 369 370 375 public String getNodeName() { 376 return this.name; 377 } 378 379 384 public short getNodeType() { 385 return Node.ELEMENT_NODE; 386 } 387 388 393 public String getNodeValue() throws DOMException { 394 return null; 395 } 396 397 402 public void setNodeValue(String nodeValue) throws DOMException { 403 } 405 406 412 protected String getRawInnerText(boolean includeComment) { 413 synchronized(this.treeLock) { 414 ArrayList nl = this.nodeList; 415 if(nl != null) { 416 Iterator i = nl.iterator(); 417 StringBuffer sb = null; 418 while (i.hasNext()) { 419 Object node = i.next(); 420 if (node instanceof Text ) { 421 Text tn = (Text ) node; 422 String txt = tn.getNodeValue(); 423 if (!"".equals(txt)) { 424 if (sb == null) { 425 sb = new StringBuffer (); 426 } 427 sb.append(txt); 428 } 429 } else if (node instanceof ElementImpl) { 430 ElementImpl en = (ElementImpl) node; 431 String txt = en.getRawInnerText(includeComment); 432 if (!"".equals(txt)) { 433 if (sb == null) { 434 sb = new StringBuffer (); 435 } 436 sb.append(txt); 437 } 438 } else if (includeComment && node instanceof Comment ) { 439 Comment cn = (Comment ) node; 440 String txt = cn.getNodeValue(); 441 if (!"".equals(txt)) { 442 if (sb == null) { 443 sb = new StringBuffer (); 444 } 445 sb.append(txt); 446 } 447 } 448 } 449 return sb == null ? "" : sb.toString(); 450 } 451 else { 452 return ""; 453 } 454 } 455 } 456 457 public String toString() { 458 StringBuffer sb = new StringBuffer (); 459 sb.append(this.getNodeName()); 460 sb.append(" ["); 461 NamedNodeMap attribs = this.getAttributes(); 462 int length = attribs.getLength(); 463 for (int i = 0; i < length; i++) { 464 Attr attr = (Attr ) attribs.item(i); 465 sb.append(attr.getNodeName()); 466 sb.append('='); 467 sb.append(attr.getNodeValue()); 468 if (i + 1 < length) { 469 sb.append(','); 470 } 471 } 472 sb.append("]"); 473 return sb.toString(); 474 } 475 476 480 public String getInnerText() { 481 StringBuffer buffer = new StringBuffer (); 482 synchronized(this.treeLock) { 483 this.appendInnerTextImpl(buffer); 484 } 485 return buffer.toString(); 486 } 487 488 public void setInnerText(String newText) { 489 org.w3c.dom.Document document = this.document; 490 if(document == null) { 491 this.warn("setInnerText(): Element " + this + " does not belong to a document."); 492 return; 493 } 494 synchronized(this.treeLock) { 495 ArrayList nl = this.nodeList; 496 if (nl != null) { 497 nl.clear(); 498 } 499 } 500 Node textNode = document.createTextNode(newText); 502 this.appendChild(textNode); 503 } 504 505 protected void appendInnerTextImpl(StringBuffer buffer) { 506 ArrayList nl = this.nodeList; 507 if (nl == null) { 508 return; 509 } 510 int size = nl.size(); 511 if (size == 0) { 512 return; 513 } 514 for (int i = 0; i < size; i++) { 515 Node child = (Node ) nl.get(i); 516 if (child instanceof ElementImpl) { 517 ((ElementImpl) child).appendInnerTextImpl(buffer); 518 } 519 if(child instanceof Comment ) { 520 } 522 else if (child instanceof Text ) { 523 buffer.append(((Text ) child).getTextContent()); 524 } 525 } 526 } 527 528 protected Node createSimilarNode() { 529 HTMLDocumentImpl doc = (HTMLDocumentImpl) this.document; 530 return doc == null ? null : doc.createElement(this.getTagName()); 531 } 532 } 533 | Popular Tags |