1 16 package org.apache.html.dom; 17 18 import java.io.StringWriter ; 19 import java.lang.reflect.Constructor ; 20 import java.util.Hashtable ; 21 import java.util.Locale ; 22 23 import org.apache.xerces.dom.DocumentImpl; 24 import org.apache.xerces.dom.NodeImpl; 25 import org.w3c.dom.Attr ; 26 import org.w3c.dom.DOMException ; 27 import org.w3c.dom.Element ; 28 import org.w3c.dom.Node ; 29 import org.w3c.dom.NodeList ; 30 import org.w3c.dom.html.HTMLBodyElement; 31 import org.w3c.dom.html.HTMLCollection; 32 import org.w3c.dom.html.HTMLDocument; 33 import org.w3c.dom.html.HTMLElement; 34 import org.w3c.dom.html.HTMLFrameSetElement; 35 import org.w3c.dom.html.HTMLHeadElement; 36 import org.w3c.dom.html.HTMLHtmlElement; 37 import org.w3c.dom.html.HTMLTitleElement; 38 39 59 public class HTMLDocumentImpl 60 extends DocumentImpl 61 implements HTMLDocument 62 { 63 64 private static final long serialVersionUID = 3258132457579427892L; 65 66 70 private HTMLCollectionImpl _anchors; 71 72 73 77 private HTMLCollectionImpl _forms; 78 79 80 84 private HTMLCollectionImpl _images; 85 86 87 91 private HTMLCollectionImpl _links; 92 93 94 98 private HTMLCollectionImpl _applets; 99 100 101 106 private StringWriter _writer; 107 108 109 118 private static Hashtable _elementTypesHTML; 119 120 121 127 private static final Class [] _elemClassSigHTML = 128 new Class [] { HTMLDocumentImpl.class, String .class }; 129 130 131 133 public HTMLDocumentImpl() 134 { 135 super(); 136 populateElementTypes(); 137 } 138 139 140 public synchronized Element getDocumentElement() 141 { 142 Node html; 143 Node child; 144 Node next; 145 146 html = getFirstChild(); 151 while ( html != null ) 152 { 153 if ( html instanceof HTMLHtmlElement ) 154 { 155 178 return (HTMLElement) html; 179 } 180 html = html.getNextSibling(); 181 } 182 183 html = new HTMLHtmlElementImpl( this, "HTML" ); 187 child = getFirstChild(); 188 while ( child != null ) 189 { 190 next = child.getNextSibling(); 191 html.appendChild( child ); 192 child = next; 193 } 194 appendChild( html ); 195 return (HTMLElement) html; 196 } 197 198 199 212 public synchronized HTMLElement getHead() 213 { 214 Node head; 215 Node html; 216 Node child; 217 Node next; 218 219 html = getDocumentElement(); 223 synchronized ( html ) 224 { 225 head = html.getFirstChild(); 226 while ( head != null && ! ( head instanceof HTMLHeadElement ) ) 227 head = head.getNextSibling(); 228 if ( head != null ) 231 { 232 synchronized ( head ) 233 { 234 child = html.getFirstChild(); 235 while ( child != null && child != head ) 236 { 237 next = child.getNextSibling(); 238 head.insertBefore( child, head.getFirstChild() ); 239 child = next; 240 } 241 } 242 return (HTMLElement) head; 243 } 244 245 head = new HTMLHeadElementImpl( this, "HEAD" ); 248 html.insertBefore( head, html.getFirstChild() ); 249 } 250 return (HTMLElement) head; 251 } 252 253 254 public synchronized String getTitle() 255 { 256 HTMLElement head; 257 NodeList list; 258 Node title; 259 260 head = getHead(); 264 title = head.getElementsByTagName( "TITLE" ).item( 0 ); 265 list = head.getElementsByTagName( "TITLE" ); 266 if ( list.getLength() > 0 ) { 267 title = list.item( 0 ); 268 return ( (HTMLTitleElement) title ).getText(); 269 } 270 return ""; 272 } 273 274 275 public synchronized void setTitle( String newTitle ) 276 { 277 HTMLElement head; 278 NodeList list; 279 Node title; 280 281 head = getHead(); 285 list = head.getElementsByTagName( "TITLE" ); 286 if ( list.getLength() > 0 ) { 287 title = list.item( 0 ); 288 if ( title.getParentNode() != head ) 289 head.appendChild( title ); 290 ( (HTMLTitleElement) title ).setText( newTitle ); 291 } 292 else 293 { 294 title = new HTMLTitleElementImpl( this, "TITLE" ); 297 ( (HTMLTitleElement) title ).setText( newTitle ); 298 head.appendChild( title ); 299 } 300 } 301 302 303 public synchronized HTMLElement getBody() 304 { 305 Node html; 306 Node head; 307 Node body; 308 Node child; 309 Node next; 310 311 html = getDocumentElement(); 315 head = getHead(); 316 synchronized ( html ) 317 { 318 body = head.getNextSibling(); 319 while ( body != null && ! ( body instanceof HTMLBodyElement ) 320 && ! ( body instanceof HTMLFrameSetElement ) ) 321 body = body.getNextSibling(); 322 323 if ( body != null ) 326 { 327 synchronized ( body ) 328 { 329 child = head.getNextSibling(); 330 while ( child != null && child != body ) 331 { 332 next = child.getNextSibling(); 333 body.insertBefore( child, body.getFirstChild() ); 334 child = next; 335 } 336 } 337 return (HTMLElement) body; 338 } 339 340 body = new HTMLBodyElementImpl( this, "BODY" ); 343 html.appendChild( body ); 344 } 345 return (HTMLElement) body; 346 } 347 348 349 public synchronized void setBody( HTMLElement newBody ) 350 { 351 Node html; 352 Node body; 353 Node head; 354 Node child; 355 NodeList list; 356 357 synchronized ( newBody ) 358 { 359 html = getDocumentElement(); 363 head = getHead(); 364 synchronized ( html ) 365 { 366 list = this.getElementsByTagName( "BODY" ); 367 if ( list.getLength() > 0 ) { 368 body = list.item( 0 ); 372 synchronized ( body ) 373 { 374 child = head; 375 while ( child != null ) 376 { 377 if ( child instanceof Element ) 378 { 379 if ( child != body ) 380 html.insertBefore( newBody, child ); 381 else 382 html.replaceChild( newBody, body ); 383 return; 384 } 385 child = child.getNextSibling(); 386 } 387 html.appendChild( newBody ); 388 } 389 return; 390 } 391 html.appendChild( newBody ); 394 } 395 } 396 } 397 398 399 public synchronized Element getElementById( String elementId ) 400 { 401 return getElementById( elementId, this ); 402 } 403 404 405 public NodeList getElementsByName( String elementName ) 406 { 407 return new NameNodeListImpl( this, elementName ); 408 } 409 410 411 public final NodeList getElementsByTagName( String tagName ) 412 { 413 return super.getElementsByTagName( tagName.toUpperCase(Locale.ENGLISH) ); 414 } 415 416 417 public final NodeList getElementsByTagNameNS( String namespaceURI, 418 String localName ) 419 { 420 if ( namespaceURI != null && namespaceURI.length() > 0 ) 421 return super.getElementsByTagNameNS( namespaceURI, localName.toUpperCase(Locale.ENGLISH) ); 422 else 423 return super.getElementsByTagName( localName.toUpperCase(Locale.ENGLISH) ); 424 } 425 426 427 440 public Element createElementNS(String namespaceURI, String qualifiedName, 441 String localpart) 442 throws DOMException 443 { 444 return createElementNS(namespaceURI, qualifiedName); 445 } 446 447 public Element createElementNS( String namespaceURI, String qualifiedName ) 448 { 449 if ( namespaceURI == null || namespaceURI.length() == 0 ) 450 return createElement( qualifiedName ); 451 else { 452 return super.createElementNS( namespaceURI, qualifiedName ); 453 } 454 } 455 456 457 public Element createElement( String tagName ) 458 throws DOMException 459 { 460 Class elemClass; 461 Constructor cnst; 462 463 tagName = tagName.toUpperCase(Locale.ENGLISH); 467 elemClass = (Class ) _elementTypesHTML.get( tagName ); 468 if ( elemClass != null ) 469 { 470 try 474 { 475 cnst = elemClass.getConstructor( _elemClassSigHTML ); 476 return (Element ) cnst.newInstance( new Object [] { this, tagName } ); 477 } 478 catch ( Exception except ) 479 { 480 Throwable thrw; 481 482 if ( except instanceof java.lang.reflect.InvocationTargetException ) 483 thrw = ( (java.lang.reflect.InvocationTargetException ) except ).getTargetException(); 484 else 485 thrw = except; 486 489 throw new IllegalStateException ( "HTM15 Tag '" + tagName + "' associated with an Element class that failed to construct.\n" + tagName); 490 } 491 } 492 return new HTMLElementImpl( this, tagName ); 493 } 494 495 496 506 public Attr createAttribute( String name ) 507 throws DOMException 508 { 509 return super.createAttribute( name.toLowerCase(Locale.ENGLISH) ); 510 } 511 512 513 public String getReferrer() 514 { 515 return null; 517 } 518 519 520 public String getDomain() 521 { 522 return null; 524 } 525 526 527 public String getURL() 528 { 529 return null; 531 } 532 533 534 public String getCookie() 535 { 536 return null; 538 } 539 540 541 public void setCookie( String cookie ) 542 { 543 } 545 546 547 public HTMLCollection getImages() 548 { 549 if ( _images == null ) 551 _images = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.IMAGE ); 552 return _images; 553 } 554 555 556 public HTMLCollection getApplets() 557 { 558 if ( _applets == null ) 560 _applets = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.APPLET ); 561 return _applets; 562 } 563 564 565 public HTMLCollection getLinks() 566 { 567 if ( _links == null ) 569 _links = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.LINK ); 570 return _links; 571 } 572 573 574 public HTMLCollection getForms() 575 { 576 if ( _forms == null ) 578 _forms = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.FORM ); 579 return _forms; 580 } 581 582 583 public HTMLCollection getAnchors() 584 { 585 if ( _anchors == null ) 587 _anchors = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.ANCHOR ); 588 return _anchors; 589 } 590 591 592 public void open() 593 { 594 if ( _writer == null ) 597 _writer = new StringWriter (); 598 } 599 600 601 public void close() 602 { 603 if ( _writer != null ) 605 { 606 _writer = null; 607 } 608 } 609 610 611 public void write( String text ) 612 { 613 if ( _writer != null ) 615 _writer.write( text ); 616 } 617 618 619 public void writeln( String text ) 620 { 621 if ( _writer != null ) 623 _writer.write( text + "\n" ); 624 } 625 626 627 public Node cloneNode( boolean deep ) 628 { 629 HTMLDocumentImpl clone; 630 NodeImpl node; 631 632 clone = new HTMLDocumentImpl(); 633 if ( deep ) { 634 node = (NodeImpl) getFirstChild(); 635 while ( node != null ) { 636 clone.appendChild( clone.importNode( node, true ) ); 637 node = (NodeImpl) node.getNextSibling(); 638 } 639 } 640 return clone; 641 } 642 643 644 651 private Element getElementById( String elementId, Node node ) 652 { 653 Node child; 654 Element result; 655 656 child = node.getFirstChild(); 657 while ( child != null ) 658 { 659 if ( child instanceof Element ) 660 { 661 if ( elementId.equals( ( (Element ) child ).getAttribute( "id" ) ) ) 662 return (Element ) child; 663 result = getElementById( elementId, child ); 664 if ( result != null ) 665 return result; 666 } 667 child = child.getNextSibling(); 668 } 669 return null; 670 } 671 672 673 678 private synchronized static void populateElementTypes() 679 { 680 690 if ( _elementTypesHTML != null ) 691 return; 692 _elementTypesHTML = new Hashtable ( 63 ); 693 populateElementType( "A", "HTMLAnchorElementImpl" ); 694 populateElementType( "APPLET", "HTMLAppletElementImpl" ); 695 populateElementType( "AREA", "HTMLAreaElementImpl" ); 696 populateElementType( "BASE", "HTMLBaseElementImpl" ); 697 populateElementType( "BASEFONT", "HTMLBaseFontElementImpl" ); 698 populateElementType( "BLOCKQUOTE", "HTMLQuoteElementImpl" ); 699 populateElementType( "BODY", "HTMLBodyElementImpl" ); 700 populateElementType( "BR", "HTMLBRElementImpl" ); 701 populateElementType( "BUTTON", "HTMLButtonElementImpl" ); 702 populateElementType( "DEL", "HTMLModElementImpl" ); 703 populateElementType( "DIR", "HTMLDirectoryElementImpl" ); 704 populateElementType( "DIV", "HTMLDivElementImpl" ); 705 populateElementType( "DL", "HTMLDListElementImpl" ); 706 populateElementType( "FIELDSET", "HTMLFieldSetElementImpl" ); 707 populateElementType( "FONT", "HTMLFontElementImpl" ); 708 populateElementType( "FORM", "HTMLFormElementImpl" ); 709 populateElementType( "FRAME","HTMLFrameElementImpl" ); 710 populateElementType( "FRAMESET", "HTMLFrameSetElementImpl" ); 711 populateElementType( "HEAD", "HTMLHeadElementImpl" ); 712 populateElementType( "H1", "HTMLHeadingElementImpl" ); 713 populateElementType( "H2", "HTMLHeadingElementImpl" ); 714 populateElementType( "H3", "HTMLHeadingElementImpl" ); 715 populateElementType( "H4", "HTMLHeadingElementImpl" ); 716 populateElementType( "H5", "HTMLHeadingElementImpl" ); 717 populateElementType( "H6", "HTMLHeadingElementImpl" ); 718 populateElementType( "HR", "HTMLHRElementImpl" ); 719 populateElementType( "HTML", "HTMLHtmlElementImpl" ); 720 populateElementType( "IFRAME", "HTMLIFrameElementImpl" ); 721 populateElementType( "IMG", "HTMLImageElementImpl" ); 722 populateElementType( "INPUT", "HTMLInputElementImpl" ); 723 populateElementType( "INS", "HTMLModElementImpl" ); 724 populateElementType( "ISINDEX", "HTMLIsIndexElementImpl" ); 725 populateElementType( "LABEL", "HTMLLabelElementImpl" ); 726 populateElementType( "LEGEND", "HTMLLegendElementImpl" ); 727 populateElementType( "LI", "HTMLLIElementImpl" ); 728 populateElementType( "LINK", "HTMLLinkElementImpl" ); 729 populateElementType( "MAP", "HTMLMapElementImpl" ); 730 populateElementType( "MENU", "HTMLMenuElementImpl" ); 731 populateElementType( "META", "HTMLMetaElementImpl" ); 732 populateElementType( "OBJECT", "HTMLObjectElementImpl" ); 733 populateElementType( "OL", "HTMLOListElementImpl" ); 734 populateElementType( "OPTGROUP", "HTMLOptGroupElementImpl" ); 735 populateElementType( "OPTION", "HTMLOptionElementImpl" ); 736 populateElementType( "P", "HTMLParagraphElementImpl" ); 737 populateElementType( "PARAM", "HTMLParamElementImpl" ); 738 populateElementType( "PRE", "HTMLPreElementImpl" ); 739 populateElementType( "Q", "HTMLQuoteElementImpl" ); 740 populateElementType( "SCRIPT", "HTMLScriptElementImpl" ); 741 populateElementType( "SELECT", "HTMLSelectElementImpl" ); 742 populateElementType( "STYLE", "HTMLStyleElementImpl" ); 743 populateElementType( "TABLE", "HTMLTableElementImpl" ); 744 populateElementType( "CAPTION", "HTMLTableCaptionElementImpl" ); 745 populateElementType( "TD", "HTMLTableCellElementImpl" ); 746 populateElementType( "TH", "HTMLTableCellElementImpl" ); 747 populateElementType( "COL", "HTMLTableColElementImpl" ); 748 populateElementType( "COLGROUP", "HTMLTableColElementImpl" ); 749 populateElementType( "TR", "HTMLTableRowElementImpl" ); 750 populateElementType( "TBODY", "HTMLTableSectionElementImpl" ); 751 populateElementType( "THEAD", "HTMLTableSectionElementImpl" ); 752 populateElementType( "TFOOT", "HTMLTableSectionElementImpl" ); 753 populateElementType( "TEXTAREA", "HTMLTextAreaElementImpl" ); 754 populateElementType( "TITLE", "HTMLTitleElementImpl" ); 755 populateElementType( "UL", "HTMLUListElementImpl" ); 756 } 757 758 759 private static void populateElementType( String tagName, String className ) 760 { 761 try { 762 _elementTypesHTML.put( tagName, 763 ObjectFactory.findProviderClass("org.apache.html.dom." + className, 764 HTMLDocumentImpl.class.getClassLoader(), true) ); 765 } catch ( Exception except ) { 766 new RuntimeException ( "HTM019 OpenXML Error: Could not find or execute class " + className + " implementing HTML element " + tagName 767 + "\n" + className + "\t" + tagName); 768 } 769 } 770 771 772 } 773 774 | Popular Tags |