1 57 package com.sun.org.apache.html.internal.dom; 58 59 60 import java.io.StringWriter ; 61 import java.lang.reflect.Constructor ; 62 import java.util.Hashtable ; 63 import java.util.Locale ; 64 65 import com.sun.org.apache.xerces.internal.dom.DocumentImpl; 66 import com.sun.org.apache.xerces.internal.dom.NodeImpl; 67 import org.w3c.dom.Attr ; 68 import org.w3c.dom.DOMException ; 69 import org.w3c.dom.Element ; 70 import org.w3c.dom.Node ; 71 import org.w3c.dom.NodeList ; 72 import org.w3c.dom.html.HTMLBodyElement; 73 import org.w3c.dom.html.HTMLCollection; 74 import org.w3c.dom.html.HTMLDocument; 75 import org.w3c.dom.html.HTMLElement; 76 import org.w3c.dom.html.HTMLFrameSetElement; 77 import org.w3c.dom.html.HTMLHeadElement; 78 import org.w3c.dom.html.HTMLHtmlElement; 79 import org.w3c.dom.html.HTMLTitleElement; 80 81 82 101 public class HTMLDocumentImpl 102 extends DocumentImpl 103 implements HTMLDocument 104 { 105 106 107 111 private HTMLCollectionImpl _anchors; 112 113 114 118 private HTMLCollectionImpl _forms; 119 120 121 125 private HTMLCollectionImpl _images; 126 127 128 132 private HTMLCollectionImpl _links; 133 134 135 139 private HTMLCollectionImpl _applets; 140 141 142 147 private StringWriter _writer; 148 149 150 159 private static Hashtable _elementTypesHTML; 160 161 162 168 private static final Class [] _elemClassSigHTML = 169 new Class [] { HTMLDocumentImpl.class, String .class }; 170 171 172 174 public HTMLDocumentImpl() 175 { 176 super(); 177 populateElementTypes(); 178 } 179 180 181 public synchronized Element getDocumentElement() 182 { 183 Node html; 184 Node child; 185 Node next; 186 187 html = getFirstChild(); 192 while ( html != null ) 193 { 194 if ( html instanceof HTMLHtmlElement ) 195 { 196 219 return (HTMLElement) html; 220 } 221 html = html.getNextSibling(); 222 } 223 224 html = new HTMLHtmlElementImpl( this, "HTML" ); 228 child = getFirstChild(); 229 while ( child != null ) 230 { 231 next = child.getNextSibling(); 232 html.appendChild( child ); 233 child = next; 234 } 235 appendChild( html ); 236 return (HTMLElement) html; 237 } 238 239 240 253 public synchronized HTMLElement getHead() 254 { 255 Node head; 256 Node html; 257 Node child; 258 Node next; 259 260 html = getDocumentElement(); 264 synchronized ( html ) 265 { 266 head = html.getFirstChild(); 267 while ( head != null && ! ( head instanceof HTMLHeadElement ) ) 268 head = head.getNextSibling(); 269 if ( head != null ) 272 { 273 synchronized ( head ) 274 { 275 child = html.getFirstChild(); 276 while ( child != null && child != head ) 277 { 278 next = child.getNextSibling(); 279 head.insertBefore( child, head.getFirstChild() ); 280 child = next; 281 } 282 } 283 return (HTMLElement) head; 284 } 285 286 head = new HTMLHeadElementImpl( this, "HEAD" ); 289 html.insertBefore( head, html.getFirstChild() ); 290 } 291 return (HTMLElement) head; 292 } 293 294 295 public synchronized String getTitle() 296 { 297 HTMLElement head; 298 NodeList list; 299 Node title; 300 301 head = getHead(); 305 title = head.getElementsByTagName( "TITLE" ).item( 0 ); 306 list = head.getElementsByTagName( "TITLE" ); 307 if ( list.getLength() > 0 ) { 308 title = list.item( 0 ); 309 return ( (HTMLTitleElement) title ).getText(); 310 } 311 return ""; 313 } 314 315 316 public synchronized void setTitle( String newTitle ) 317 { 318 HTMLElement head; 319 NodeList list; 320 Node title; 321 322 head = getHead(); 326 list = head.getElementsByTagName( "TITLE" ); 327 if ( list.getLength() > 0 ) { 328 title = list.item( 0 ); 329 if ( title.getParentNode() != head ) 330 head.appendChild( title ); 331 ( (HTMLTitleElement) title ).setText( newTitle ); 332 } 333 else 334 { 335 title = new HTMLTitleElementImpl( this, "TITLE" ); 338 ( (HTMLTitleElement) title ).setText( newTitle ); 339 head.appendChild( title ); 340 } 341 } 342 343 344 public synchronized HTMLElement getBody() 345 { 346 Node html; 347 Node head; 348 Node body; 349 Node child; 350 Node next; 351 352 html = getDocumentElement(); 356 head = getHead(); 357 synchronized ( html ) 358 { 359 body = head.getNextSibling(); 360 while ( body != null && ! ( body instanceof HTMLBodyElement ) 361 && ! ( body instanceof HTMLFrameSetElement ) ) 362 body = body.getNextSibling(); 363 364 if ( body != null ) 367 { 368 synchronized ( body ) 369 { 370 child = head.getNextSibling(); 371 while ( child != null && child != body ) 372 { 373 next = child.getNextSibling(); 374 body.insertBefore( child, body.getFirstChild() ); 375 child = next; 376 } 377 } 378 return (HTMLElement) body; 379 } 380 381 body = new HTMLBodyElementImpl( this, "BODY" ); 384 html.appendChild( body ); 385 } 386 return (HTMLElement) body; 387 } 388 389 390 public synchronized void setBody( HTMLElement newBody ) 391 { 392 Node html; 393 Node body; 394 Node head; 395 Node child; 396 NodeList list; 397 398 synchronized ( newBody ) 399 { 400 html = getDocumentElement(); 404 head = getHead(); 405 synchronized ( html ) 406 { 407 list = this.getElementsByTagName( "BODY" ); 408 if ( list.getLength() > 0 ) { 409 body = list.item( 0 ); 413 synchronized ( body ) 414 { 415 child = head; 416 while ( child != null ) 417 { 418 if ( child instanceof Element ) 419 { 420 if ( child != body ) 421 html.insertBefore( newBody, child ); 422 else 423 html.replaceChild( newBody, body ); 424 return; 425 } 426 child = child.getNextSibling(); 427 } 428 html.appendChild( newBody ); 429 } 430 return; 431 } 432 html.appendChild( newBody ); 435 } 436 } 437 } 438 439 440 public synchronized Element getElementById( String elementId ) 441 { 442 return getElementById( elementId, this ); 443 } 444 445 446 public NodeList getElementsByName( String elementName ) 447 { 448 return new NameNodeListImpl( this, elementName ); 449 } 450 451 452 public final NodeList getElementsByTagName( String tagName ) 453 { 454 return super.getElementsByTagName( tagName.toUpperCase(Locale.ENGLISH) ); 455 } 456 457 458 public final NodeList getElementsByTagNameNS( String namespaceURI, 459 String localName ) 460 { 461 if ( namespaceURI != null && namespaceURI.length() > 0 ) 462 return super.getElementsByTagNameNS( namespaceURI, localName.toUpperCase(Locale.ENGLISH) ); 463 else 464 return super.getElementsByTagName( localName.toUpperCase(Locale.ENGLISH) ); 465 } 466 467 468 481 public Element createElementNS(String namespaceURI, String qualifiedName, 482 String localpart) 483 throws DOMException 484 { 485 return createElementNS(namespaceURI, qualifiedName); 486 } 487 488 public Element createElementNS( String namespaceURI, String qualifiedName ) 489 { 490 if ( namespaceURI == null || namespaceURI.length() == 0 ) 491 return createElement( qualifiedName ); 492 else { 493 return super.createElementNS( namespaceURI, qualifiedName ); 494 } 495 } 496 497 498 public Element createElement( String tagName ) 499 throws DOMException 500 { 501 Class elemClass; 502 Constructor cnst; 503 504 tagName = tagName.toUpperCase(Locale.ENGLISH); 508 elemClass = (Class ) _elementTypesHTML.get( tagName ); 509 if ( elemClass != null ) 510 { 511 try 515 { 516 cnst = elemClass.getConstructor( _elemClassSigHTML ); 517 return (Element ) cnst.newInstance( new Object [] { this, tagName } ); 518 } 519 catch ( Exception except ) 520 { 521 Throwable thrw; 522 523 if ( except instanceof java.lang.reflect.InvocationTargetException ) 524 thrw = ( (java.lang.reflect.InvocationTargetException ) except ).getTargetException(); 525 else 526 thrw = except; 527 530 throw new IllegalStateException ( "HTM15 Tag '" + tagName + "' associated with an Element class that failed to construct.\n" + tagName); 531 } 532 } 533 return new HTMLElementImpl( this, tagName ); 534 } 535 536 537 547 public Attr createAttribute( String name ) 548 throws DOMException 549 { 550 return super.createAttribute( name.toLowerCase(Locale.ENGLISH) ); 551 } 552 553 554 public String getReferrer() 555 { 556 return null; 558 } 559 560 561 public String getDomain() 562 { 563 return null; 565 } 566 567 568 public String getURL() 569 { 570 return null; 572 } 573 574 575 public String getCookie() 576 { 577 return null; 579 } 580 581 582 public void setCookie( String cookie ) 583 { 584 } 586 587 588 public HTMLCollection getImages() 589 { 590 if ( _images == null ) 592 _images = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.IMAGE ); 593 return _images; 594 } 595 596 597 public HTMLCollection getApplets() 598 { 599 if ( _applets == null ) 601 _applets = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.APPLET ); 602 return _applets; 603 } 604 605 606 public HTMLCollection getLinks() 607 { 608 if ( _links == null ) 610 _links = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.LINK ); 611 return _links; 612 } 613 614 615 public HTMLCollection getForms() 616 { 617 if ( _forms == null ) 619 _forms = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.FORM ); 620 return _forms; 621 } 622 623 624 public HTMLCollection getAnchors() 625 { 626 if ( _anchors == null ) 628 _anchors = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.ANCHOR ); 629 return _anchors; 630 } 631 632 633 public void open() 634 { 635 if ( _writer == null ) 638 _writer = new StringWriter (); 639 } 640 641 642 public void close() 643 { 644 if ( _writer != null ) 646 { 647 _writer = null; 648 } 649 } 650 651 652 public void write( String text ) 653 { 654 if ( _writer != null ) 656 _writer.write( text ); 657 } 658 659 660 public void writeln( String text ) 661 { 662 if ( _writer != null ) 664 _writer.write( text + "\n" ); 665 } 666 667 668 public Node cloneNode( boolean deep ) 669 { 670 HTMLDocumentImpl clone; 671 NodeImpl node; 672 673 clone = new HTMLDocumentImpl(); 674 if ( deep ) { 675 node = (NodeImpl) getFirstChild(); 676 while ( node != null ) { 677 clone.appendChild( clone.importNode( node, true ) ); 678 node = (NodeImpl) node.getNextSibling(); 679 } 680 } 681 return clone; 682 } 683 684 685 692 private Element getElementById( String elementId, Node node ) 693 { 694 Node child; 695 Element result; 696 697 child = node.getFirstChild(); 698 while ( child != null ) 699 { 700 if ( child instanceof Element ) 701 { 702 if ( elementId.equals( ( (Element ) child ).getAttribute( "id" ) ) ) 703 return (Element ) child; 704 result = getElementById( elementId, child ); 705 if ( result != null ) 706 return result; 707 } 708 child = child.getNextSibling(); 709 } 710 return null; 711 } 712 713 714 719 private synchronized static void populateElementTypes() 720 { 721 731 if ( _elementTypesHTML != null ) 732 return; 733 _elementTypesHTML = new Hashtable ( 63 ); 734 populateElementType( "A", "HTMLAnchorElementImpl" ); 735 populateElementType( "APPLET", "HTMLAppletElementImpl" ); 736 populateElementType( "AREA", "HTMLAreaElementImpl" ); 737 populateElementType( "BASE", "HTMLBaseElementImpl" ); 738 populateElementType( "BASEFONT", "HTMLBaseFontElementImpl" ); 739 populateElementType( "BLOCKQUOTE", "HTMLQuoteElementImpl" ); 740 populateElementType( "BODY", "HTMLBodyElementImpl" ); 741 populateElementType( "BR", "HTMLBRElementImpl" ); 742 populateElementType( "BUTTON", "HTMLButtonElementImpl" ); 743 populateElementType( "DEL", "HTMLModElementImpl" ); 744 populateElementType( "DIR", "HTMLDirectoryElementImpl" ); 745 populateElementType( "DIV", "HTMLDivElementImpl" ); 746 populateElementType( "DL", "HTMLDListElementImpl" ); 747 populateElementType( "FIELDSET", "HTMLFieldSetElementImpl" ); 748 populateElementType( "FONT", "HTMLFontElementImpl" ); 749 populateElementType( "FORM", "HTMLFormElementImpl" ); 750 populateElementType( "FRAME","HTMLFrameElementImpl" ); 751 populateElementType( "FRAMESET", "HTMLFrameSetElementImpl" ); 752 populateElementType( "HEAD", "HTMLHeadElementImpl" ); 753 populateElementType( "H1", "HTMLHeadingElementImpl" ); 754 populateElementType( "H2", "HTMLHeadingElementImpl" ); 755 populateElementType( "H3", "HTMLHeadingElementImpl" ); 756 populateElementType( "H4", "HTMLHeadingElementImpl" ); 757 populateElementType( "H5", "HTMLHeadingElementImpl" ); 758 populateElementType( "H6", "HTMLHeadingElementImpl" ); 759 populateElementType( "HR", "HTMLHRElementImpl" ); 760 populateElementType( "HTML", "HTMLHtmlElementImpl" ); 761 populateElementType( "IFRAME", "HTMLIFrameElementImpl" ); 762 populateElementType( "IMG", "HTMLImageElementImpl" ); 763 populateElementType( "INPUT", "HTMLInputElementImpl" ); 764 populateElementType( "INS", "HTMLModElementImpl" ); 765 populateElementType( "ISINDEX", "HTMLIsIndexElementImpl" ); 766 populateElementType( "LABEL", "HTMLLabelElementImpl" ); 767 populateElementType( "LEGEND", "HTMLLegendElementImpl" ); 768 populateElementType( "LI", "HTMLLIElementImpl" ); 769 populateElementType( "LINK", "HTMLLinkElementImpl" ); 770 populateElementType( "MAP", "HTMLMapElementImpl" ); 771 populateElementType( "MENU", "HTMLMenuElementImpl" ); 772 populateElementType( "META", "HTMLMetaElementImpl" ); 773 populateElementType( "OBJECT", "HTMLObjectElementImpl" ); 774 populateElementType( "OL", "HTMLOListElementImpl" ); 775 populateElementType( "OPTGROUP", "HTMLOptGroupElementImpl" ); 776 populateElementType( "OPTION", "HTMLOptionElementImpl" ); 777 populateElementType( "P", "HTMLParagraphElementImpl" ); 778 populateElementType( "PARAM", "HTMLParamElementImpl" ); 779 populateElementType( "PRE", "HTMLPreElementImpl" ); 780 populateElementType( "Q", "HTMLQuoteElementImpl" ); 781 populateElementType( "SCRIPT", "HTMLScriptElementImpl" ); 782 populateElementType( "SELECT", "HTMLSelectElementImpl" ); 783 populateElementType( "STYLE", "HTMLStyleElementImpl" ); 784 populateElementType( "TABLE", "HTMLTableElementImpl" ); 785 populateElementType( "CAPTION", "HTMLTableCaptionElementImpl" ); 786 populateElementType( "TD", "HTMLTableCellElementImpl" ); 787 populateElementType( "TH", "HTMLTableCellElementImpl" ); 788 populateElementType( "COL", "HTMLTableColElementImpl" ); 789 populateElementType( "COLGROUP", "HTMLTableColElementImpl" ); 790 populateElementType( "TR", "HTMLTableRowElementImpl" ); 791 populateElementType( "TBODY", "HTMLTableSectionElementImpl" ); 792 populateElementType( "THEAD", "HTMLTableSectionElementImpl" ); 793 populateElementType( "TFOOT", "HTMLTableSectionElementImpl" ); 794 populateElementType( "TEXTAREA", "HTMLTextAreaElementImpl" ); 795 populateElementType( "TITLE", "HTMLTitleElementImpl" ); 796 populateElementType( "UL", "HTMLUListElementImpl" ); 797 } 798 799 800 private static void populateElementType( String tagName, String className ) 801 { 802 try { 803 _elementTypesHTML.put( tagName, 804 ObjectFactory.findProviderClass("com.sun.org.apache.html.internal.dom." + className, 805 HTMLDocumentImpl.class.getClassLoader(), true) ); 806 } catch ( Exception except ) { 807 new RuntimeException ( "HTM019 OpenXML Error: Could not find or execute class " + className + " implementing HTML element " + tagName 808 + "\n" + className + "\t" + tagName); 809 } 810 } 811 812 813 } 814 815 | Popular Tags |