1 23 79 package org.enhydra.xml.lazydom.html; 80 81 import java.io.StringWriter ; 82 import java.lang.reflect.Constructor ; 83 import java.util.HashMap ; 84 85 import org.enhydra.xml.dom.DOMAccess; 86 import org.enhydra.xml.lazydom.LazyDocument; 87 import org.enhydra.xml.lazydom.LazyElement; 88 import org.enhydra.xml.lazydom.LazyNode; 89 import org.enhydra.xml.lazydom.TemplateDOM; 90 import org.w3c.dom.Attr ; 91 import org.w3c.dom.DOMException ; 92 import org.w3c.dom.DOMImplementation ; 93 import org.w3c.dom.Document ; 94 import org.w3c.dom.Element ; 95 import org.w3c.dom.Node ; 96 import org.w3c.dom.NodeList ; 97 import org.w3c.dom.html.HTMLCollection; 98 import org.w3c.dom.html.HTMLDocument; 99 import org.w3c.dom.html.HTMLElement; 100 import org.w3c.dom.html.HTMLTitleElement; 101 102 109 110 111 130 public class LazyHTMLDocument 131 extends LazyDocument 132 implements HTMLDocument 133 { 134 135 136 140 private HTMLCollectionImpl _anchors; 141 142 143 147 private HTMLCollectionImpl _forms; 148 149 150 154 private HTMLCollectionImpl _images; 155 156 157 161 private HTMLCollectionImpl _links; 162 163 164 168 private HTMLCollectionImpl _applets; 169 170 171 176 private StringWriter _writer; 177 178 179 189 private static HashMap _elementConstHTML; 190 private static HashMap _tmpElementConstHTML; 191 192 193 199 private static final Class [] _elemClassSigHTML = 200 new Class [] { LazyHTMLDocument.class, LazyElement.class, String .class }; 201 202 203 206 public DOMImplementation getImplementation() { 207 return LazyHTMLDOMImplementation.getDOMImplementation(); 208 } 209 210 211 214 private Node getDirectChildElement(String name, 215 Node root) { 216 for (Node child = root.getFirstChild(); child != null; 217 child = child.getNextSibling()) { 218 if (child.getNodeName().equals(name)) { 219 return child; 220 } 221 } 222 return null; 223 } 224 225 226 public synchronized Element getDocumentElement() { 227 232 Element html = (Element)getDirectChildElement("HTML", this); 233 if (html == null) { 234 html = new HTMLHtmlElementImpl(this, null, "HTML"); 236 appendChild(html); 237 } 238 return html; 239 } 240 241 254 public synchronized HTMLElement getHead() { 255 260 Element html = getDocumentElement(); 262 HTMLElement head 263 = (HTMLElement)getDirectChildElement("HEAD", html); 264 if (head == null) { 265 head = new HTMLHeadElementImpl(this, null, "HEAD"); 267 html.insertBefore(head, html.getFirstChild()); 268 } 269 return head; 270 } 271 272 public synchronized String getTitle() { 273 278 HTMLTitleElement title 279 = (HTMLTitleElement)getDirectChildElement("TITLE", getHead()); 280 if (title == null) { 281 return ""; } else { 283 return title.getText(); 284 } 285 } 286 287 288 public synchronized void setTitle(String newTitle) { 289 293 HTMLElement head = getHead(); 294 HTMLTitleElement title 295 = (HTMLTitleElement)getDirectChildElement("TITLE", head); 296 if (title == null) { 297 title = new HTMLTitleElementImpl(this, null, "TITLE"); 298 } 299 title.setText(newTitle); 300 } 301 302 305 private HTMLElement findBody(Element html) { 306 HTMLElement body = (HTMLElement)getDirectChildElement("BODY", html); 307 if (body == null) { 308 body = (HTMLElement)getDirectChildElement("FRAMESET", html); 309 } 310 return body; 311 } 312 313 public synchronized HTMLElement getBody() { 314 319 Element html = getDocumentElement(); 321 HTMLElement body = findBody(html); 322 if (body == null) { 323 body = new HTMLBodyElementImpl(this, null, "BODY"); 325 html.appendChild(body); 326 } 327 return body; 328 } 329 330 331 public synchronized void setBody(HTMLElement newBody) { 332 337 Element html = getDocumentElement(); 339 HTMLElement body = findBody(html); 340 if (body == null) { 341 html.appendChild(newBody); 342 } else { 343 html.replaceChild(newBody, body); 344 } 345 } 346 347 348 public synchronized Element getElementById( String elementId ) 349 { 350 return getElementById( elementId, this ); 351 } 352 353 354 public NodeList getElementsByName( String elementName ) 355 { 356 return new NameNodeListImpl( this, elementName ); 357 } 358 359 360 public final NodeList getElementsByTagName( String tagName ) 361 { 362 return super.getElementsByTagName( tagName.toUpperCase() ); 363 } 364 365 366 public final NodeList getElementsByTagNameNS( String namespaceURI, 367 String localName ) 368 { 369 if ( namespaceURI != null && namespaceURI.length() > 0 ) 370 return super.getElementsByTagNameNS( namespaceURI, localName.toUpperCase() ); 371 else 372 return super.getElementsByTagName( localName.toUpperCase() ); 373 } 374 375 376 public Element createElementNS( String namespaceURI, String qualifiedName ) 377 { 378 if ( namespaceURI == null || namespaceURI.length() == 0 ) 379 return createElement( qualifiedName ); 380 else 381 return super.createElementNS( namespaceURI, qualifiedName ); 382 } 383 384 385 public Element createElement( LazyElement template, String tagName ) 386 throws DOMException 387 { 388 Constructor cnst; 389 390 tagName = tagName.toUpperCase(); 394 cnst = (Constructor ) _elementConstHTML.get( tagName ); 395 if ( cnst != null ) 396 { 397 try 401 { 402 return (Element) cnst.newInstance( new Object [] { this, template, tagName } ); 403 } 404 catch ( Exception except ) 405 { 406 Throwable thrw; 407 408 if ( except instanceof java.lang.reflect.InvocationTargetException ) 409 thrw = ( (java.lang.reflect.InvocationTargetException ) except ).getTargetException(); 410 else 411 thrw = except; 412 413 throw new IllegalStateException ( "HTM15 Tag '" + tagName + "' associated with an Element class that failed to construct.\n" + tagName); 414 } 415 } 416 return new LazyHTMLElement( this, template, tagName ); 417 } 418 419 420 430 public Attr createAttribute( String name ) 431 throws DOMException 432 { 433 return super.createAttribute( name.toLowerCase() ); 434 } 435 436 437 public String getReferrer() 438 { 439 return null; 441 } 442 443 444 public String getDomain() 445 { 446 return null; 448 } 449 450 451 public String getURL() 452 { 453 return null; 455 } 456 457 458 public String getCookie() 459 { 460 return null; 462 } 463 464 465 public void setCookie( String cookie ) 466 { 467 } 469 470 471 public HTMLCollection getImages() 472 { 473 if ( _images == null ) 475 _images = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.IMAGE ); 476 return _images; 477 } 478 479 480 public HTMLCollection getApplets() 481 { 482 if ( _applets == null ) 484 _applets = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.APPLET ); 485 return _applets; 486 } 487 488 489 public HTMLCollection getLinks() 490 { 491 if ( _links == null ) 493 _links = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.LINK ); 494 return _links; 495 } 496 497 498 public HTMLCollection getForms() 499 { 500 if ( _forms == null ) 502 _forms = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.FORM ); 503 return _forms; 504 } 505 506 507 public HTMLCollection getAnchors() 508 { 509 if ( _anchors == null ) 511 _anchors = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.ANCHOR ); 512 return _anchors; 513 } 514 515 516 public void open() 517 { 518 if ( _writer == null ) 521 _writer = new StringWriter (); 522 } 523 524 525 public void close() 526 { 527 if ( _writer != null ) 529 { 530 _writer = null; 531 } 532 } 533 534 535 public void write( String text ) 536 { 537 if ( _writer != null ) 539 _writer.write( text ); 540 } 541 542 543 public void writeln( String text ) 544 { 545 if ( _writer != null ) 547 _writer.write( text + "\n" ); 548 } 549 550 551 public Node cloneNode( boolean deep ) 552 { 553 LazyHTMLDocument clone; 554 LazyNode node; 555 556 clone = new LazyHTMLDocument(); 557 if ( deep ) { 558 node = (LazyNode) getFirstChild(); 559 while ( node != null ) { 560 clone.appendChild( clone.importNode( node, true ) ); 561 node = (LazyNode) node.getNextSibling(); 562 } 563 } 564 return clone; 565 } 566 567 568 575 private Element getElementById(String elementId, 576 Node node){ 577 Node child; 578 Element element; 579 580 child = DOMAccess.accessFirstChild(this, node); 581 while (child != null) { 582 if (child instanceof Element) { 583 element = (Element)child; 584 Attr attr = DOMAccess.accessAttribute(this, (Element)child, null, "id"); 585 if ((attr != null) 586 && elementId.equals(DOMAccess.accessAttributeValue(this, attr))) { 587 return DOMAccess.getExpandedElement(this, element); 588 } 589 element = getElementById(elementId, child); 591 if (element != null) { 592 return DOMAccess.getExpandedElement(this, element); 593 } 594 } 595 child = DOMAccess.accessNextSibling(this, child); 596 } 597 return null; 598 } 599 600 601 607 private static void populateElementTypes() 608 { 609 619 if ( _elementConstHTML != null ) 620 return; 621 _tmpElementConstHTML = new HashMap ( 63 ); 622 populateElementType( "A", "HTMLAnchorElementImpl" ); 623 populateElementType( "APPLET", "HTMLAppletElementImpl" ); 624 populateElementType( "AREA", "HTMLAreaElementImpl" ); 625 populateElementType( "BASE", "HTMLBaseElementImpl" ); 626 populateElementType( "BASEFONT", "HTMLBaseFontElementImpl" ); 627 populateElementType( "BLOCKQUOTE", "HTMLQuoteElementImpl" ); 628 populateElementType( "BODY", "HTMLBodyElementImpl" ); 629 populateElementType( "BR", "HTMLBRElementImpl" ); 630 populateElementType( "BUTTON", "HTMLButtonElementImpl" ); 631 populateElementType( "DEL", "HTMLModElementImpl" ); 632 populateElementType( "DIR", "HTMLDirectoryElementImpl" ); 633 populateElementType( "DIV", "HTMLDivElementImpl" ); 634 populateElementType( "DL", "HTMLDListElementImpl" ); 635 populateElementType( "FIELDSET", "HTMLFieldSetElementImpl" ); 636 populateElementType( "FONT", "HTMLFontElementImpl" ); 637 populateElementType( "FORM", "HTMLFormElementImpl" ); 638 populateElementType( "FRAME","HTMLFrameElementImpl" ); 639 populateElementType( "FRAMESET", "HTMLFrameSetElementImpl" ); 640 populateElementType( "HEAD", "HTMLHeadElementImpl" ); 641 populateElementType( "H1", "HTMLHeadingElementImpl" ); 642 populateElementType( "H2", "HTMLHeadingElementImpl" ); 643 populateElementType( "H3", "HTMLHeadingElementImpl" ); 644 populateElementType( "H4", "HTMLHeadingElementImpl" ); 645 populateElementType( "H5", "HTMLHeadingElementImpl" ); 646 populateElementType( "H6", "HTMLHeadingElementImpl" ); 647 populateElementType( "HR", "HTMLHRElementImpl" ); 648 populateElementType( "HTML", "HTMLHtmlElementImpl" ); 649 populateElementType( "IFRAME", "HTMLIFrameElementImpl" ); 650 populateElementType( "IMG", "HTMLImageElementImpl" ); 651 populateElementType( "INPUT", "HTMLInputElementImpl" ); 652 populateElementType( "INS", "HTMLModElementImpl" ); 653 populateElementType( "ISINDEX", "HTMLIsIndexElementImpl" ); 654 populateElementType( "LABEL", "HTMLLabelElementImpl" ); 655 populateElementType( "LEGEND", "HTMLLegendElementImpl" ); 656 populateElementType( "LI", "HTMLLIElementImpl" ); 657 populateElementType( "LINK", "HTMLLinkElementImpl" ); 658 populateElementType( "MAP", "HTMLMapElementImpl" ); 659 populateElementType( "MENU", "HTMLMenuElementImpl" ); 660 populateElementType( "META", "HTMLMetaElementImpl" ); 661 populateElementType( "OBJECT", "HTMLObjectElementImpl" ); 662 populateElementType( "OL", "HTMLOListElementImpl" ); 663 populateElementType( "OPTGROUP", "HTMLOptGroupElementImpl" ); 664 populateElementType( "OPTION", "HTMLOptionElementImpl" ); 665 populateElementType( "P", "HTMLParagraphElementImpl" ); 666 populateElementType( "PARAM", "HTMLParamElementImpl" ); 667 populateElementType( "PRE", "HTMLPreElementImpl" ); 668 populateElementType( "Q", "HTMLQuoteElementImpl" ); 669 populateElementType( "SCRIPT", "HTMLScriptElementImpl" ); 670 populateElementType( "SELECT", "HTMLSelectElementImpl" ); 671 populateElementType( "STYLE", "HTMLStyleElementImpl" ); 672 populateElementType( "TABLE", "HTMLTableElementImpl" ); 673 populateElementType( "CAPTION", "HTMLTableCaptionElementImpl" ); 674 populateElementType( "TD", "HTMLTableCellElementImpl" ); 675 populateElementType( "TH", "HTMLTableCellElementImpl" ); 676 populateElementType( "COL", "HTMLTableColElementImpl" ); 677 populateElementType( "COLGROUP", "HTMLTableColElementImpl" ); 678 populateElementType( "TR", "HTMLTableRowElementImpl" ); 679 populateElementType( "TBODY", "HTMLTableSectionElementImpl" ); 680 populateElementType( "THEAD", "HTMLTableSectionElementImpl" ); 681 populateElementType( "TFOOT", "HTMLTableSectionElementImpl" ); 682 populateElementType( "TEXTAREA", "HTMLTextAreaElementImpl" ); 683 populateElementType( "TITLE", "HTMLTitleElementImpl" ); 684 populateElementType( "UL", "HTMLUListElementImpl" ); 685 _elementConstHTML = _tmpElementConstHTML; 686 } 687 688 689 private static void populateElementType(String tagName, String className ) 690 { 691 try { 692 Class cl = Class.forName( "org.enhydra.xml.lazydom.html." + className ); 693 _tmpElementConstHTML.put( tagName, cl.getConstructor( _elemClassSigHTML )); 694 } catch ( ClassNotFoundException except ) { 695 new RuntimeException ( "HTM019 OpenXML Error: Could not find class " + className + " implementing HTML element " + tagName 696 + "\n" + className + "\t" + tagName); 697 } catch ( NoSuchMethodException except ) { 698 new RuntimeException ( "HTM019 OpenXML Error: Could not find constructor for class " + className + " implementing HTML element " + tagName 699 + "\n" + className + "\t" + tagName); 700 } 701 } 702 703 706 public LazyHTMLDocument() 707 { 708 super(); 709 populateElementTypes(); 710 } 711 712 715 public LazyHTMLDocument(TemplateDOM templateDOM) { 716 super(null, templateDOM); 717 populateElementTypes(); 718 } 719 720 723 public Element createElement(String tagName) throws DOMException { 724 return createElement(null, tagName); 725 } 726 727 730 public LazyElement createElement(int nodeId) throws DOMException { 731 LazyElement template = (LazyElement)getTemplateNode(nodeId); 732 return (LazyElement)createElement(template, template.getNodeName()); 733 } 734 } 735 736 | Popular Tags |