1 57 package org.enhydra.xml.xhtml.dom.xerces; 58 59 63 import java.io.StringWriter ; 64 import java.lang.reflect.Constructor ; 65 import java.util.HashMap ; 66 67 import org.enhydra.apache.html.dom.HTMLAnchorElementImpl; 68 import org.enhydra.apache.xerces.dom.DocumentImpl; 69 import org.enhydra.apache.xerces.dom.NodeImpl; 70 import org.enhydra.xml.io.DocumentInfo; 71 import org.enhydra.xml.xmlc.XMLCError; 72 import org.enhydra.xml.xmlc.dom.HTMLDomFactoryMethods; 73 import org.w3c.dom.DOMException ; 74 import org.w3c.dom.DOMImplementation ; 75 import org.w3c.dom.DocumentType ; 76 import org.w3c.dom.Element ; 77 import org.w3c.dom.Node ; 78 import org.w3c.dom.NodeList ; 79 import org.w3c.dom.html.HTMLCollection; 80 import org.w3c.dom.html.HTMLDocument; 81 import org.w3c.dom.html.HTMLElement; 82 import org.w3c.dom.html.HTMLTitleElement; 83 84 92 public class XHTMLDocumentBase extends DocumentImpl implements HTMLDocument, DocumentInfo { 93 97 private XHTMLCollectionImpl fAnchors; 98 99 103 private XHTMLCollectionImpl fForms; 104 105 109 private XHTMLCollectionImpl fImages; 110 111 115 private XHTMLCollectionImpl fLinks; 116 117 118 122 private XHTMLCollectionImpl fApplets; 123 124 129 private StringWriter fWriter; 130 131 140 private static HashMap fElementTypes; 141 142 148 private static final Class [] fElemConstructorSig = new Class [] { 149 XHTMLDocumentBase.class, String .class, String .class 150 }; 151 152 157 public XHTMLDocumentBase(HashMap elementTypes) { 158 super(); 159 fElementTypes = elementTypes; 160 } 161 162 165 public XHTMLDocumentBase(DocumentType doctype, 166 HashMap elementTypes) { 167 super(doctype); 168 fElementTypes = elementTypes; 169 } 170 171 174 private Node getDirectChildElement(String name, Node root) { 175 for (Node child = root.getFirstChild(); child != null; 176 child = child.getNextSibling()) { 177 if ((child instanceof Element ) 178 && child.getNodeName().equals(name)) { 179 return child; 180 } 181 } 182 183 return null; 184 } 185 186 189 public synchronized Element getDocumentElement() { 190 Element html = (Element )getDirectChildElement("html", this); 191 if (html == null) { 192 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, 193 "<html> document element not found"); 194 } 195 196 return html; 197 } 198 199 208 public synchronized HTMLElement getHead() { 209 210 Element html = getDocumentElement(); 216 HTMLElement head = (HTMLElement)getDirectChildElement("head", html); 217 218 if (head == null) { 219 head = new XHTMLHeadElementImpl(this, html.getNamespaceURI(), 221 "head"); 222 html.insertBefore(head, html.getFirstChild()); 223 } 224 225 return head; 226 } 227 228 231 public synchronized String getTitle() { 232 HTMLTitleElement title = 237 (HTMLTitleElement)getDirectChildElement("title", getHead()); 238 239 if (title == null) { 240 return ""; } else { 242 return title.getText(); 243 } 244 } 245 246 249 public synchronized void setTitle(String newTitle) { 250 HTMLElement head = getHead(); 254 HTMLTitleElement title = 255 (HTMLTitleElement)getDirectChildElement("title", head); 256 257 if (title == null) { 258 title = new XHTMLTitleElementImpl(this, head.getNamespaceURI(), 259 "title"); 260 } 261 title.setText(newTitle); 262 } 263 264 267 private HTMLElement findBody(Element html) { 268 HTMLElement body = (HTMLElement)getDirectChildElement("body", html); 269 if (body == null) { 270 body = (HTMLElement)getDirectChildElement("frameset", html); 271 } 272 273 return body; 274 } 275 276 279 public synchronized HTMLElement getBody() { 280 Element html = getDocumentElement(); 286 HTMLElement body = findBody(html); 287 288 if (body == null) { 289 body = new XHTMLBodyElementImpl(this, html.getNamespaceURI(), 291 "body"); 292 html.appendChild(body); 293 } 294 295 return body; 296 } 297 298 301 public synchronized void setBody(HTMLElement newBody) { 302 Element html = getDocumentElement(); 308 HTMLElement body = findBody(html); 309 310 if (body == null) { 311 html.appendChild(newBody); 312 } else { 313 html.replaceChild(newBody, body); 314 } 315 } 316 317 320 public synchronized Element getElementById(String elementId) { 321 return getElementById(elementId, this); 322 } 323 324 327 public NodeList getElementsByName(String elementName) { 328 return new NameNodeListImpl(this, elementName); 329 } 330 331 334 public final NodeList getElementsByTagName(String tagName) { 335 return super.getElementsByTagName(tagName); 336 } 337 338 341 public final NodeList getElementsByTagNameNS(String namespaceURI, 342 String localName) { 343 if (namespaceURI != null && namespaceURI.length() > 0) { 344 return super.getElementsByTagNameNS(namespaceURI, 345 localName); 346 } else { 347 return super.getElementsByTagName(localName); 348 } 349 } 350 351 354 public Element createElementNS(String namespaceURI, 355 String qualifiedName) { 356 int index = qualifiedName.indexOf(':'); 358 String tagName; 359 if (index < 0) { 360 tagName = qualifiedName; 361 } else { 362 tagName = qualifiedName.substring(index+1); 363 } 364 Class elemClass = (Class )fElementTypes.get(tagName); 365 if (elemClass != null) { 366 try { 367 Constructor cnst = elemClass.getConstructor(fElemConstructorSig); 368 return (Element )cnst.newInstance(new Object [] {this, namespaceURI, qualifiedName}); 369 } catch (Exception except) { 370 throw new XMLCError("failed to construct element for \"" 372 + qualifiedName + "\"", except); 373 } 374 } else { 375 return new XHTMLElementImpl(this, namespaceURI, qualifiedName); 376 } 377 } 378 379 382 public Element createElement(String tagName) throws DOMException { 383 return createElementNS(null, tagName); 384 } 385 386 389 public String getReferrer() { 390 return null; 392 } 393 394 397 public String getDomain() { 398 return null; 400 } 401 402 405 public String getURL() { 406 return null; 408 } 409 410 413 public String getCookie() { 414 return null; 416 } 417 418 421 public void setCookie(String cookie) { 422 } 424 425 428 public HTMLCollection getImages() { 429 if (fImages == null) { 431 fImages = new XHTMLCollectionImpl(getBody(), 432 XHTMLCollectionImpl.IMAGE); 433 } 434 435 return fImages; 436 } 437 438 441 public HTMLCollection getApplets() { 442 if (fApplets == null) { 444 fApplets = new XHTMLCollectionImpl(getBody(), 445 XHTMLCollectionImpl.APPLET); 446 } 447 448 return fApplets; 449 } 450 451 454 public HTMLCollection getLinks() { 455 if (fLinks == null) { 457 fLinks = new XHTMLCollectionImpl(getBody(), 458 XHTMLCollectionImpl.LINK); 459 } 460 461 return fLinks; 462 } 463 464 467 public HTMLCollection getForms() { 468 if (fForms == null) { 470 fForms = new XHTMLCollectionImpl(getBody(), 471 XHTMLCollectionImpl.FORM); 472 } 473 474 return fForms; 475 } 476 477 480 public HTMLCollection getAnchors() { 481 if (fAnchors == null) { 483 fAnchors = new XHTMLCollectionImpl(getBody(), 484 XHTMLCollectionImpl.ANCHOR); 485 } 486 487 return fAnchors; 488 } 489 490 493 public void open() { 494 if (fWriter == null) { 497 fWriter = new StringWriter (); 498 } 499 } 500 501 504 public void close() { 505 if (fWriter != null) { 507 fWriter = null; 508 } 509 } 510 511 514 public void write(String text) { 515 if (fWriter != null) { 517 fWriter.write(text); 518 } 519 } 520 521 524 public void writeln(String text) { 525 if (fWriter != null) { 527 fWriter.write(text + "\n"); 528 } 529 } 530 531 534 public Node cloneNode(boolean deep) { 535 XHTMLDocumentImpl clone; 536 NodeImpl node; 537 538 clone = new XHTMLDocumentImpl(); 539 if (deep) { 540 node = (NodeImpl) getFirstChild(); 541 542 while (node != null) { 543 clone.appendChild(clone.importNode(node, true)); 544 node = (NodeImpl) node.getNextSibling(); 545 } 546 } 547 548 return clone; 549 } 550 551 555 public DOMImplementation getImplementation() { 556 return XHTMLDOMImplementationImpl.getDOMImplementation(); 557 } 558 559 566 private Element getElementById(String elementId, Node node) { 567 Node child; 568 Element result; 569 570 child = node.getFirstChild(); 571 572 while (child != null) { 573 if (child instanceof Element ) { 574 if (elementId.equals(((Element ) child).getAttribute("id"))) { 575 return (Element ) child; 576 } 577 result = getElementById(elementId, child); 578 if (result != null) { 579 return result; 580 } 581 } 582 child = child.getNextSibling(); 583 } 584 585 return null; 586 } 587 public boolean isURLAttribute(Element element, String attrName) 588 { 589 return HTMLDomFactoryMethods.isURLAttribute(element, attrName); 590 } 591 592 } 593 | Popular Tags |