1 57 package org.enhydra.apache.html.dom; 58 59 60 import java.io.StringWriter ; 61 import java.lang.reflect.Constructor ; 62 import java.util.Hashtable ; 63 64 import org.enhydra.apache.xerces.dom.DocumentImpl; 65 import org.enhydra.apache.xerces.dom.NodeImpl; 66 import org.w3c.dom.Attr ; 67 import org.w3c.dom.DOMException ; 68 import org.w3c.dom.Element ; 69 import org.w3c.dom.Node ; 70 import org.w3c.dom.NodeList ; 71 import org.w3c.dom.html.HTMLCollection; 72 import org.w3c.dom.html.HTMLDocument; 73 import org.w3c.dom.html.HTMLElement; 74 import org.w3c.dom.html.HTMLTitleElement; 75 76 77 96 public class HTMLDocumentImpl 97 extends DocumentImpl 98 implements HTMLDocument 99 { 100 101 102 106 private HTMLCollectionImpl _anchors; 107 108 109 113 private HTMLCollectionImpl _forms; 114 115 116 120 private HTMLCollectionImpl _images; 121 122 123 127 private HTMLCollectionImpl _links; 128 129 130 134 private HTMLCollectionImpl _applets; 135 136 137 142 private StringWriter _writer; 143 144 145 154 private static Hashtable _elementTypesHTML; 155 156 157 163 private static final Class [] _elemClassSigHTML = 164 new Class [] { HTMLDocumentImpl.class, String .class }; 165 166 167 169 public HTMLDocumentImpl() 170 { 171 super(); 172 populateElementTypes(); 173 } 174 175 178 private Node getDirectChildElement(String name, 179 Node root) { 180 for (Node child = root.getFirstChild(); child != null; 181 child = child.getNextSibling()) { 182 if (child.getNodeName().equals(name)) { 183 return child; 184 } 185 } 186 return null; 187 } 188 189 190 public synchronized Element getDocumentElement() { 191 196 Element html = (Element )getDirectChildElement("HTML", this); 197 if (html == null) { 198 html = new HTMLHtmlElementImpl(this, "HTML"); 200 appendChild(html); 201 } 202 return html; 203 } 204 205 218 public synchronized HTMLElement getHead() { 219 224 Element html = getDocumentElement(); 226 HTMLElement head 227 = (HTMLElement)getDirectChildElement("HEAD", html); 228 if (head == null) { 229 head = new HTMLHeadElementImpl(this, "HEAD"); 231 html.insertBefore(head, html.getFirstChild()); 232 } 233 return head; 234 } 235 236 public synchronized String getTitle() { 237 242 HTMLTitleElement title 243 = (HTMLTitleElement)getDirectChildElement("TITLE", getHead()); 244 if (title == null) { 245 return ""; } else { 247 return title.getText(); 248 } 249 } 250 251 252 public synchronized void setTitle(String newTitle) { 253 257 HTMLElement head = getHead(); 258 HTMLTitleElement title 259 = (HTMLTitleElement)getDirectChildElement("TITLE", head); 260 if (title == null) { 261 title = new HTMLTitleElementImpl(this, "TITLE"); 262 } 263 title.setText(newTitle); 264 } 265 266 269 private HTMLElement findBody(Element html) { 270 HTMLElement body = (HTMLElement)getDirectChildElement("BODY", html); 271 if (body == null) { 272 body = (HTMLElement)getDirectChildElement("FRAMESET", html); 273 } 274 return body; 275 } 276 277 public synchronized HTMLElement getBody() { 278 283 Element html = getDocumentElement(); 285 HTMLElement body = findBody(html); 286 if (body == null) { 287 body = new HTMLBodyElementImpl(this, "BODY"); 289 html.appendChild(body); 290 } 291 return body; 292 } 293 294 295 public synchronized void setBody(HTMLElement newBody) { 296 301 Element html = getDocumentElement(); 303 HTMLElement body = findBody(html); 304 if (body == null) { 305 html.appendChild(newBody); 306 } else { 307 html.replaceChild(newBody, body); 308 } 309 } 310 311 312 public synchronized Element getElementById( String elementId ) 313 { 314 return getElementById( elementId, this ); 315 } 316 317 318 public NodeList getElementsByName( String elementName ) 319 { 320 return new NameNodeListImpl( this, elementName ); 321 } 322 323 324 public final NodeList getElementsByTagName( String tagName ) 325 { 326 return super.getElementsByTagName( tagName.toUpperCase() ); 327 } 328 329 330 public final NodeList getElementsByTagNameNS( String namespaceURI, 331 String localName ) 332 { 333 if ( namespaceURI != null && namespaceURI.length() > 0 ) 334 return super.getElementsByTagNameNS( namespaceURI, localName.toUpperCase() ); 335 else 336 return super.getElementsByTagName( localName.toUpperCase() ); 337 } 338 339 340 public Element createElementNS( String namespaceURI, String qualifiedName ) 341 { 342 if ( namespaceURI == null || namespaceURI.length() == 0 ) 343 return createElement( qualifiedName ); 344 else 345 return super.createElementNS( namespaceURI, qualifiedName ); 346 } 347 348 349 public Element createElement( String tagName ) 350 throws DOMException 351 { 352 Class elemClass; 353 Constructor cnst; 354 355 tagName = tagName.toUpperCase(); 359 elemClass = (Class ) _elementTypesHTML.get( tagName ); 360 if ( elemClass != null ) 361 { 362 try 366 { 367 cnst = elemClass.getConstructor( _elemClassSigHTML ); 368 return (Element ) cnst.newInstance( new Object [] { this, tagName } ); 369 } 370 catch ( Exception except ) 371 { 372 Throwable thrw; 373 374 if ( except instanceof java.lang.reflect.InvocationTargetException ) 375 thrw = ( (java.lang.reflect.InvocationTargetException ) except ).getTargetException(); 376 else 377 thrw = except; 378 381 throw new IllegalStateException ( "HTM15 Tag '" + tagName + "' associated with an Element class that failed to construct.\n" + tagName); 382 } 383 } 384 return new HTMLElementImpl( this, tagName ); 385 } 386 387 388 398 public Attr createAttribute( String name ) 399 throws DOMException 400 { 401 return super.createAttribute( name.toLowerCase() ); 402 } 403 404 405 public String getReferrer() 406 { 407 return null; 409 } 410 411 412 public String getDomain() 413 { 414 return null; 416 } 417 418 419 public String getURL() 420 { 421 return null; 423 } 424 425 426 public String getCookie() 427 { 428 return null; 430 } 431 432 433 public void setCookie( String cookie ) 434 { 435 } 437 438 439 public HTMLCollection getImages() 440 { 441 if ( _images == null ) 443 _images = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.IMAGE ); 444 return _images; 445 } 446 447 448 public HTMLCollection getApplets() 449 { 450 if ( _applets == null ) 452 _applets = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.APPLET ); 453 return _applets; 454 } 455 456 457 public HTMLCollection getLinks() 458 { 459 if ( _links == null ) 461 _links = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.LINK ); 462 return _links; 463 } 464 465 466 public HTMLCollection getForms() 467 { 468 if ( _forms == null ) 470 _forms = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.FORM ); 471 return _forms; 472 } 473 474 475 public HTMLCollection getAnchors() 476 { 477 if ( _anchors == null ) 479 _anchors = new HTMLCollectionImpl( getBody(), HTMLCollectionImpl.ANCHOR ); 480 return _anchors; 481 } 482 483 484 public void open() 485 { 486 if ( _writer == null ) 489 _writer = new StringWriter (); 490 } 491 492 493 public void close() 494 { 495 if ( _writer != null ) 497 { 498 _writer = null; 499 } 500 } 501 502 503 public void write( String text ) 504 { 505 if ( _writer != null ) 507 _writer.write( text ); 508 } 509 510 511 public void writeln( String text ) 512 { 513 if ( _writer != null ) 515 _writer.write( text + "\n" ); 516 } 517 518 519 public Node cloneNode( boolean deep ) 520 { 521 HTMLDocumentImpl clone; 522 NodeImpl node; 523 524 clone = new HTMLDocumentImpl(); 525 if ( deep ) { 526 node = (NodeImpl) getFirstChild(); 527 while ( node != null ) { 528 clone.appendChild( clone.importNode( node, true ) ); 529 node = (NodeImpl) node.getNextSibling(); 530 } 531 } 532 return clone; 533 } 534 535 536 543 private Element getElementById( String elementId, Node node ) 544 { 545 Node child; 546 Element result; 547 548 child = node.getFirstChild(); 549 while ( child != null ) 550 { 551 if ( child instanceof Element ) 552 { 553 if ( elementId.equals( ( (Element ) child ).getAttribute( "id" ) ) ) 554 return (Element ) child; 555 result = getElementById( elementId, child ); 556 if ( result != null ) 557 return result; 558 } 559 child = child.getNextSibling(); 560 } 561 return null; 562 } 563 564 565 570 private static void populateElementTypes() 571 { 572 582 if ( _elementTypesHTML != null ) 583 return; 584 _elementTypesHTML = new Hashtable ( 63 ); 585 populateElementType( "A", "HTMLAnchorElementImpl" ); 586 populateElementType( "APPLET", "HTMLAppletElementImpl" ); 587 populateElementType( "AREA", "HTMLAreaElementImpl" ); 588 populateElementType( "BASE", "HTMLBaseElementImpl" ); 589 populateElementType( "BASEFONT", "HTMLBaseFontElementImpl" ); 590 populateElementType( "BLOCKQUOTE", "HTMLQuoteElementImpl" ); 591 populateElementType( "BODY", "HTMLBodyElementImpl" ); 592 populateElementType( "BR", "HTMLBRElementImpl" ); 593 populateElementType( "BUTTON", "HTMLButtonElementImpl" ); 594 populateElementType( "DEL", "HTMLModElementImpl" ); 595 populateElementType( "DIR", "HTMLDirectoryElementImpl" ); 596 populateElementType( "DIV", "HTMLDivElementImpl" ); 597 populateElementType( "DL", "HTMLDListElementImpl" ); 598 populateElementType( "FIELDSET", "HTMLFieldSetElementImpl" ); 599 populateElementType( "FONT", "HTMLFontElementImpl" ); 600 populateElementType( "FORM", "HTMLFormElementImpl" ); 601 populateElementType( "FRAME","HTMLFrameElementImpl" ); 602 populateElementType( "FRAMESET", "HTMLFrameSetElementImpl" ); 603 populateElementType( "HEAD", "HTMLHeadElementImpl" ); 604 populateElementType( "H1", "HTMLHeadingElementImpl" ); 605 populateElementType( "H2", "HTMLHeadingElementImpl" ); 606 populateElementType( "H3", "HTMLHeadingElementImpl" ); 607 populateElementType( "H4", "HTMLHeadingElementImpl" ); 608 populateElementType( "H5", "HTMLHeadingElementImpl" ); 609 populateElementType( "H6", "HTMLHeadingElementImpl" ); 610 populateElementType( "HR", "HTMLHRElementImpl" ); 611 populateElementType( "HTML", "HTMLHtmlElementImpl" ); 612 populateElementType( "IFRAME", "HTMLIFrameElementImpl" ); 613 populateElementType( "IMG", "HTMLImageElementImpl" ); 614 populateElementType( "INPUT", "HTMLInputElementImpl" ); 615 populateElementType( "INS", "HTMLModElementImpl" ); 616 populateElementType( "ISINDEX", "HTMLIsIndexElementImpl" ); 617 populateElementType( "LABEL", "HTMLLabelElementImpl" ); 618 populateElementType( "LEGEND", "HTMLLegendElementImpl" ); 619 populateElementType( "LI", "HTMLLIElementImpl" ); 620 populateElementType( "LINK", "HTMLLinkElementImpl" ); 621 populateElementType( "MAP", "HTMLMapElementImpl" ); 622 populateElementType( "MENU", "HTMLMenuElementImpl" ); 623 populateElementType( "META", "HTMLMetaElementImpl" ); 624 populateElementType( "OBJECT", "HTMLObjectElementImpl" ); 625 populateElementType( "OL", "HTMLOListElementImpl" ); 626 populateElementType( "OPTGROUP", "HTMLOptGroupElementImpl" ); 627 populateElementType( "OPTION", "HTMLOptionElementImpl" ); 628 populateElementType( "P", "HTMLParagraphElementImpl" ); 629 populateElementType( "PARAM", "HTMLParamElementImpl" ); 630 populateElementType( "PRE", "HTMLPreElementImpl" ); 631 populateElementType( "Q", "HTMLQuoteElementImpl" ); 632 populateElementType( "SCRIPT", "HTMLScriptElementImpl" ); 633 populateElementType( "SELECT", "HTMLSelectElementImpl" ); 634 populateElementType( "STYLE", "HTMLStyleElementImpl" ); 635 populateElementType( "TABLE", "HTMLTableElementImpl" ); 636 populateElementType( "CAPTION", "HTMLTableCaptionElementImpl" ); 637 populateElementType( "TD", "HTMLTableCellElementImpl" ); 638 populateElementType( "TH", "HTMLTableCellElementImpl" ); 639 populateElementType( "COL", "HTMLTableColElementImpl" ); 640 populateElementType( "COLGROUP", "HTMLTableColElementImpl" ); 641 populateElementType( "TR", "HTMLTableRowElementImpl" ); 642 populateElementType( "TBODY", "HTMLTableSectionElementImpl" ); 643 populateElementType( "THEAD", "HTMLTableSectionElementImpl" ); 644 populateElementType( "TFOOT", "HTMLTableSectionElementImpl" ); 645 populateElementType( "TEXTAREA", "HTMLTextAreaElementImpl" ); 646 populateElementType( "TITLE", "HTMLTitleElementImpl" ); 647 populateElementType( "UL", "HTMLUListElementImpl" ); 648 } 649 650 651 private static void populateElementType( String tagName, String className ) 652 { 653 try { 654 _elementTypesHTML.put( tagName, Class.forName( "org.enhydra.apache.html.dom." + className ) ); 655 } catch ( ClassNotFoundException except ) { 656 new RuntimeException ( "HTM019 OpenXML Error: Could not find class " + className + " implementing HTML element " + tagName 657 + "\n" + className + "\t" + tagName); 658 } 659 } 660 661 662 } 663 664 | Popular Tags |