khtml Library API Documentation

dom_doc.cpp

00001 /*
00002  * This file is part of the DOM implementation for KDE.
00003  *
00004  * (C) 1999 Lars Knoll (knoll@kde.org)
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public License
00017  * along with this library; see the file COPYING.LIB.  If not, write to
00018  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019  * Boston, MA 02111-1307, USA.
00020  *
00021  */
00022 
00023 #include "dom/dom_exception.h"
00024 #include "dom/dom_xml.h"
00025 #include "dom/dom2_range.h"
00026 #include "dom/dom2_events.h"
00027 #include "dom/dom2_views.h"
00028 #include "dom/dom2_traversal.h"
00029 #include "dom/html_document.h"
00030 #include "html/html_documentimpl.h"
00031 
00032 #include "xml/dom_docimpl.h"
00033 #include "xml/dom_elementimpl.h"
00034 
00035 #include <kdebug.h>
00036 
00037 namespace DOM {
00038 
00039 DOMImplementation::DOMImplementation()
00040 {
00041     impl = 0;
00042 }
00043 
00044 DOMImplementation::DOMImplementation(const DOMImplementation &other)
00045 {
00046     impl = other.impl;
00047     if (impl) impl->ref();
00048 }
00049 
00050 DOMImplementation::DOMImplementation(DOMImplementationImpl *i)
00051 {
00052     impl = i;
00053     if (impl) impl->ref();
00054 }
00055 
00056 DOMImplementation &DOMImplementation::operator = (const DOMImplementation &other)
00057 {
00058     if ( impl != other.impl ) {
00059         if (impl) impl->deref();
00060         impl = other.impl;
00061         if (impl) impl->ref();
00062     }
00063     return *this;
00064 }
00065 
00066 DOMImplementation::~DOMImplementation()
00067 {
00068     if (impl) impl->deref();
00069 }
00070 
00071 bool DOMImplementation::hasFeature( const DOMString &feature, const DOMString &version )
00072 {
00073     if (!impl)
00074     return false; // ### enable throw DOMException(DOMException::NOT_FOUND_ERR);
00075 
00076     return impl->hasFeature(feature,version);
00077 }
00078 
00079 DocumentType DOMImplementation::createDocumentType ( const DOMString &qualifiedName,
00080                                                      const DOMString &publicId,
00081                                                      const DOMString &systemId )
00082 {
00083     if (!impl)
00084     throw DOMException(DOMException::NOT_FOUND_ERR);
00085 
00086     int exceptioncode = 0;
00087     DocumentTypeImpl *r = impl->createDocumentType(qualifiedName, publicId, systemId, exceptioncode);
00088     if ( exceptioncode )
00089         throw DOMException( exceptioncode );
00090     return r;
00091 }
00092 
00093 Document DOMImplementation::createDocument ( const DOMString &namespaceURI,
00094                                              const DOMString &qualifiedName,
00095                                              const DocumentType &doctype )
00096 {
00097     if (!impl)
00098     throw DOMException(DOMException::NOT_FOUND_ERR);
00099 
00100     int exceptioncode = 0;
00101     DocumentImpl *r = impl->createDocument(namespaceURI, qualifiedName, doctype, exceptioncode );
00102     if ( exceptioncode )
00103         throw DOMException( exceptioncode );
00104     return r;
00105 }
00106 
00107 HTMLDocument DOMImplementation::createHTMLDocument( const DOMString& title )
00108 {
00109     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
00110 
00111     HTMLDocumentImpl* r = impl->createHTMLDocument( 0 /* ### create a view otherwise it doesn't work */);
00112 
00113     r->open();
00114 
00115     r->write(QString::fromLatin1("<HTML><HEAD><TITLE>") + title.string() +
00116              QString::fromLatin1("</TITLE></HEAD>"));
00117 
00118     return r;
00119 }
00120 
00121 DOMImplementation DOMImplementation::getInterface(const DOMString &feature) const
00122 {
00123     if (!impl)
00124         throw DOMException(DOMException::NOT_FOUND_ERR);
00125 
00126     return impl->getInterface(feature);
00127 }
00128 
00129 CSSStyleSheet DOMImplementation::createCSSStyleSheet(const DOMString &title, const DOMString &media)
00130 {
00131     if (!impl)
00132         throw DOMException(DOMException::NOT_FOUND_ERR);
00133 
00134     int exceptioncode = 0;
00135     CSSStyleSheetImpl *r = impl->createCSSStyleSheet(title.implementation(), media.implementation(),
00136                                                      exceptioncode);
00137     if ( exceptioncode )
00138         throw DOMException( exceptioncode );
00139     return r;
00140 }
00141 
00142 DOMImplementationImpl *DOMImplementation::handle() const
00143 {
00144     return impl;
00145 }
00146 
00147 bool DOMImplementation::isNull() const
00148 {
00149     return (impl == 0);
00150 }
00151 
00152 // ----------------------------------------------------------------------------
00153 
00154 Document::Document()
00155     : Node()
00156 {
00157     // we always want an implementation
00158     impl = DOMImplementationImpl::instance()->createDocument();
00159     impl->ref();
00160 }
00161 
00162 Document::Document(bool create)
00163     : Node()
00164 {
00165     if(create)
00166     {
00167     impl = DOMImplementationImpl::instance()->createDocument();
00168     impl->ref();
00169     }
00170     else
00171     impl = 0;
00172 //    kdDebug(6090) << "Document::Document(bool)" << endl;
00173 }
00174 
00175 Document::Document(const Document &other) : Node(other)
00176 {
00177 //    kdDebug(6090) << "Document::Document(Document &)" << endl;
00178 }
00179 
00180 Document::Document(DocumentImpl *i) : Node(i)
00181 {
00182 //    kdDebug(6090) << "Document::Document(DocumentImpl)" << endl;
00183 }
00184 
00185 Document &Document::operator = (const Node &other)
00186 {
00187     NodeImpl* ohandle = other.handle();
00188     if ( impl != ohandle ) {
00189         if (!ohandle || ohandle->nodeType() != DOCUMENT_NODE) {
00190         if ( impl ) impl->deref();
00191             impl = 0;
00192     } else {
00193             Node::operator =(other);
00194     }
00195     }
00196     return *this;
00197 }
00198 
00199 Document &Document::operator = (const Document &other)
00200 {
00201     Node::operator =(other);
00202     return *this;
00203 }
00204 
00205 Document::~Document()
00206 {
00207 //    kdDebug(6090) << "Document::~Document\n" << endl;
00208 }
00209 
00210 DocumentType Document::doctype() const
00211 {
00212     if (impl) return ((DocumentImpl *)impl)->doctype();
00213     return 0;
00214 }
00215 
00216 DOMImplementation Document::implementation() const
00217 {
00218     if (impl) return ((DocumentImpl *)impl)->implementation();
00219     return 0;
00220 }
00221 
00222 Element Document::documentElement() const
00223 {
00224     if (impl) return ((DocumentImpl *)impl)->documentElement();
00225     return 0;
00226 }
00227 
00228 Element Document::createElement( const DOMString &tagName )
00229 {
00230     if (!impl)
00231         throw DOMException(DOMException::NOT_FOUND_ERR);
00232 
00233     int exceptioncode = 0;
00234     ElementImpl* r = ((DocumentImpl *)impl)->createElement(tagName, &exceptioncode);
00235     if ( exceptioncode )
00236         throw DOMException( exceptioncode );
00237     return r;
00238 }
00239 
00240 Element Document::createElementNS( const DOMString &namespaceURI, const DOMString &qualifiedName )
00241 {
00242     if (!impl)
00243         throw DOMException(DOMException::NOT_FOUND_ERR);
00244 
00245     int exceptioncode = 0;
00246     ElementImpl* r = ((DocumentImpl *)impl)->createElementNS(namespaceURI,qualifiedName, &exceptioncode);
00247     if ( exceptioncode )
00248         throw DOMException( exceptioncode );
00249     return r;
00250 }
00251 
00252 DocumentFragment Document::createDocumentFragment(  )
00253 {
00254     if (impl) return ((DocumentImpl *)impl)->createDocumentFragment();
00255     return 0;
00256 }
00257 
00258 Text Document::createTextNode( const DOMString &data )
00259 {
00260     if (impl) return ((DocumentImpl *)impl)->createTextNode( data.implementation() );
00261     return 0;
00262 }
00263 
00264 Comment Document::createComment( const DOMString &data )
00265 {
00266     if (impl) return ((DocumentImpl *)impl)->createComment( data.implementation() );
00267     return 0;
00268 }
00269 
00270 CDATASection Document::createCDATASection( const DOMString &data )
00271 {
00272     // ### DOM1 spec says raise exception if html documents - what about XHTML documents?
00273     if (impl) return ((DocumentImpl *)impl)->createCDATASection( data.implementation() );
00274     return 0;
00275 }
00276 
00277 ProcessingInstruction Document::createProcessingInstruction( const DOMString &target, const DOMString &data )
00278 {
00279     if (impl) return ((DocumentImpl *)impl)->createProcessingInstruction( target, data.implementation() );
00280     return 0;
00281 }
00282 
00283 Attr Document::createAttribute( const DOMString &name )
00284 {
00285     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
00286     if (name.isNull()) throw DOMException(DOMException::NOT_FOUND_ERR);
00287     int exceptioncode = 0;
00288     AttrImpl* a = impl->getDocument()->createAttribute(name, &exceptioncode);
00289     if ( exceptioncode )
00290         throw DOMException( exceptioncode );
00291     return a;
00292 }
00293 
00294 Attr Document::createAttributeNS( const DOMString &namespaceURI, const DOMString &qualifiedName )
00295 {
00296     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
00297     if (qualifiedName.isNull()) throw DOMException(DOMException::NAMESPACE_ERR);
00298     int exceptioncode = 0;
00299     AttrImpl* a = impl->getDocument()->createAttributeNS(namespaceURI, qualifiedName, &exceptioncode);
00300     if ( exceptioncode )
00301         throw DOMException( exceptioncode );
00302     return a;
00303 }
00304 
00305 EntityReference Document::createEntityReference( const DOMString &name )
00306 {
00307     if (impl) return ((DocumentImpl *)impl)->createEntityReference( name );
00308     return 0;
00309 }
00310 
00311 Element Document::getElementById( const DOMString &elementId ) const
00312 {
00313     if(impl) return ((DocumentImpl *)impl)->getElementById( elementId );
00314     return 0;
00315 }
00316 
00317 NodeList Document::getElementsByTagName( const DOMString &tagName )
00318 {
00319     if (!impl) return 0;
00320     NodeImpl::Id id;
00321     if ( tagName == "*" )
00322         id = 0;
00323     else
00324         id = impl->getDocument()->getId(NodeImpl::ElementId, tagName.implementation(), false, true);
00325     return new TagNodeListImpl( impl, id );
00326 }
00327 
00328 NodeList Document::getElementsByTagNameNS( const DOMString &namespaceURI, const DOMString &localName )
00329 {
00330     if (!impl) return 0;
00331     return new TagNodeListImpl( impl, namespaceURI, localName );
00332 }
00333 
00334 Node Document::importNode( const Node & importedNode, bool deep )
00335 {
00336     if (!impl)
00337     throw DOMException(DOMException::INVALID_STATE_ERR);
00338 
00339     int exceptioncode = 0;
00340     NodeImpl *r = static_cast<DocumentImpl*>(impl)->importNode(importedNode.handle(), deep, exceptioncode);
00341     if (exceptioncode)
00342     throw DOMException(exceptioncode);
00343     return r;
00344 }
00345 
00346 bool Document::isHTMLDocument() const
00347 {
00348     if (impl) return ((DocumentImpl *)impl)->isHTMLDocument();
00349     return 0;
00350 }
00351 
00352 Range Document::createRange()
00353 {
00354     if (impl) return ((DocumentImpl *)impl)->createRange();
00355     return 0;
00356 }
00357 
00358 NodeIterator Document::createNodeIterator(Node root, unsigned long whatToShow,
00359                                     NodeFilter filter, bool entityReferenceExpansion)
00360 {
00361     if (!impl)
00362     throw DOMException(DOMException::INVALID_STATE_ERR);
00363 
00364     int exceptioncode = 0;
00365     NodeIteratorImpl *r = static_cast<DocumentImpl*>(impl)->createNodeIterator(root.handle(),
00366               whatToShow,filter,entityReferenceExpansion,exceptioncode);
00367     if (exceptioncode)
00368     throw DOMException(exceptioncode);
00369     return r;
00370 }
00371 
00372 TreeWalker Document::createTreeWalker(Node root, unsigned long whatToShow, NodeFilter filter,
00373                                 bool entityReferenceExpansion)
00374 {
00375     if (impl) return ((DocumentImpl *)impl)->createTreeWalker(root,whatToShow,filter,entityReferenceExpansion);
00376     return 0;
00377 }
00378 
00379 Event Document::createEvent(const DOMString &eventType)
00380 {
00381     if (!impl)
00382     throw DOMException(DOMException::INVALID_STATE_ERR);
00383 
00384     int exceptioncode = 0;
00385     EventImpl *r = ((DocumentImpl *)impl)->createEvent(eventType,exceptioncode);
00386     if (exceptioncode)
00387     throw DOMException(exceptioncode);
00388     return r;
00389 }
00390 
00391 AbstractView Document::defaultView() const
00392 {
00393     if (!impl)
00394     throw DOMException(DOMException::INVALID_STATE_ERR);
00395 
00396     return static_cast<DocumentImpl*>(impl)->defaultView();
00397 }
00398 
00399 StyleSheetList Document::styleSheets() const
00400 {
00401     if (!impl)
00402     throw DOMException(DOMException::INVALID_STATE_ERR);
00403 
00404     return static_cast<DocumentImpl*>(impl)->styleSheets();
00405 }
00406 
00407 DOMString Document::preferredStylesheetSet()
00408 {
00409     if (!impl)
00410         throw DOMException(DOMException::INVALID_STATE_ERR);
00411 
00412     return static_cast<DocumentImpl*>(impl)->preferredStylesheetSet();
00413 }
00414 
00415 DOMString Document::selectedStylesheetSet()
00416 {
00417     if (!impl)
00418         throw DOMException(DOMException::INVALID_STATE_ERR);
00419 
00420     return static_cast<DocumentImpl*>(impl)->selectedStylesheetSet();
00421 }
00422 
00423 void Document::setSelectedStylesheetSet(const DOMString& s)
00424 {
00425     if (!impl)
00426         throw DOMException(DOMException::INVALID_STATE_ERR);
00427 
00428     static_cast<DocumentImpl*>(impl)->setSelectedStylesheetSet(s);
00429 }
00430 
00431 
00432 KHTMLView *Document::view() const
00433 {
00434     if (!impl) return 0;
00435 
00436     return static_cast<DocumentImpl*>(impl)->view();
00437 }
00438 
00439 CSSStyleDeclaration Document::getOverrideStyle(const Element &elt, const DOMString &pseudoElt)
00440 {
00441     if (!impl)
00442     throw DOMException(DOMException::INVALID_STATE_ERR);
00443 
00444     int exceptioncode = 0;
00445     CSSStyleDeclarationImpl *r = ((DocumentImpl *)impl)->getOverrideStyle(static_cast<ElementImpl*>(elt.handle()),pseudoElt.implementation());
00446     if (exceptioncode)
00447     throw DOMException(exceptioncode);
00448     return r;
00449 }
00450 
00451 bool Document::async() const
00452 {
00453     if (!impl)
00454         throw DOMException(DOMException::INVALID_STATE_ERR);
00455 
00456     return static_cast<DocumentImpl*>( impl )->async(  );
00457 }
00458 
00459 void Document::setAsync( bool b )
00460 {
00461     if (!impl)
00462         throw DOMException(DOMException::INVALID_STATE_ERR);
00463 
00464     static_cast<DocumentImpl*>( impl )->setAsync( b );
00465 }
00466 
00467 void Document::abort()
00468 {
00469     if (!impl)
00470         throw DOMException(DOMException::INVALID_STATE_ERR);
00471 
00472 
00473     static_cast<DocumentImpl*>( impl )->abort(  );
00474 }
00475 
00476 void Document::load( const DOMString &uri )
00477 {
00478     if (!impl)
00479         throw DOMException(DOMException::INVALID_STATE_ERR);
00480 
00481     static_cast<DocumentImpl*>( impl )->load( uri );
00482 }
00483 
00484 void Document::loadXML( const DOMString &source )
00485 {
00486     if (!impl)
00487         throw DOMException(DOMException::INVALID_STATE_ERR);
00488 
00489 
00490     static_cast<DocumentImpl*>( impl )->loadXML( source );
00491 }
00492 
00493 bool Document::designMode() const {
00494     if (!impl)
00495         throw DOMException(DOMException::INVALID_STATE_ERR);
00496 
00497     return static_cast<DocumentImpl*>( impl )->designMode();
00498 }
00499 
00500 void Document::setDesignMode(bool enable) {
00501     if (!impl)
00502         throw DOMException(DOMException::INVALID_STATE_ERR);
00503 
00504     static_cast<DocumentImpl*>( impl )->setDesignMode( enable );
00505 }
00506 
00507 DOMString Document::completeURL(const DOMString& url)
00508 {
00509     if ( !impl ) return url;
00510     return static_cast<DocumentImpl*>( impl )->completeURL( url.string() );
00511 }
00512 
00513 void Document::updateRendering()
00514 {
00515     if ( !impl ) return;
00516     static_cast<DocumentImpl*>( impl )->updateRendering(  );
00517 }
00518 
00519 // ----------------------------------------------------------------------------
00520 
00521 DocumentFragment::DocumentFragment() : Node()
00522 {
00523 }
00524 
00525 DocumentFragment::DocumentFragment(const DocumentFragment &other) : Node(other)
00526 {
00527 }
00528 
00529 DocumentFragment &DocumentFragment::operator = (const Node &other)
00530 {
00531     NodeImpl* ohandle = other.handle();
00532     if ( impl != ohandle ) {
00533         if (!ohandle || ohandle->nodeType() != DOCUMENT_FRAGMENT_NODE) {
00534             if ( impl ) impl->deref();
00535             impl = 0;
00536         } else {
00537             Node::operator =(other);
00538         }
00539     }
00540     return *this;
00541 }
00542 
00543 DocumentFragment &DocumentFragment::operator = (const DocumentFragment &other)
00544 {
00545     Node::operator =(other);
00546     return *this;
00547 }
00548 
00549 DocumentFragment::~DocumentFragment()
00550 {
00551 }
00552 
00553 DocumentFragment::DocumentFragment(DocumentFragmentImpl *i) : Node(i)
00554 {
00555 }
00556 
00557 // ----------------------------------------------------------------------------
00558 
00559 DocumentType::DocumentType()
00560     : Node()
00561 {
00562 }
00563 
00564 DocumentType::DocumentType(const DocumentType &other)
00565     : Node(other)
00566 {
00567 }
00568 
00569 DocumentType::DocumentType(DocumentTypeImpl *impl) : Node(impl)
00570 {
00571 }
00572 
00573 DocumentType &DocumentType::operator = (const Node &other)
00574 {
00575     NodeImpl* ohandle = other.handle();
00576     if ( impl != ohandle ) {
00577         if (!ohandle || ohandle->nodeType() != DOCUMENT_TYPE_NODE) {
00578         if ( impl ) impl->deref();
00579             impl = 0;
00580     } else {
00581             Node::operator =(other);
00582     }
00583     }
00584     return *this;
00585 }
00586 
00587 DocumentType &DocumentType::operator = (const DocumentType &other)
00588 {
00589     Node::operator =(other);
00590     return *this;
00591 }
00592 
00593 DocumentType::~DocumentType()
00594 {
00595 }
00596 
00597 DOMString DocumentType::name() const
00598 {
00599     if (!impl)
00600     return DOMString(); // ### enable throw DOMException(DOMException::NOT_FOUND_ERR);
00601 
00602     return static_cast<DocumentTypeImpl*>(impl)->name();
00603 }
00604 
00605 NamedNodeMap DocumentType::entities() const
00606 {
00607     if (!impl)
00608     return 0; // ### enable throw DOMException(DOMException::NOT_FOUND_ERR);
00609 
00610     return static_cast<DocumentTypeImpl*>(impl)->entities();
00611 }
00612 
00613 NamedNodeMap DocumentType::notations() const
00614 {
00615     if (!impl)
00616     return 0; // ### enable throw DOMException(DOMException::NOT_FOUND_ERR);
00617 
00618     return static_cast<DocumentTypeImpl*>(impl)->notations();
00619 }
00620 
00621 DOMString DocumentType::publicId() const
00622 {
00623     if (!impl)
00624     throw DOMException(DOMException::NOT_FOUND_ERR);
00625 
00626     return static_cast<DocumentTypeImpl*>(impl)->publicId();
00627 }
00628 
00629 DOMString DocumentType::systemId() const
00630 {
00631     if (!impl)
00632     throw DOMException(DOMException::NOT_FOUND_ERR);
00633 
00634     return static_cast<DocumentTypeImpl*>(impl)->systemId();
00635 }
00636 
00637 DOMString DocumentType::internalSubset() const
00638 {
00639     if (!impl)
00640     throw DOMException(DOMException::NOT_FOUND_ERR);
00641 
00642     return static_cast<DocumentTypeImpl*>(impl)->internalSubset();
00643 }
00644 
00645 } // namespace
KDE Logo
This file is part of the documentation for khtml Library Version 3.2.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Feb 4 12:37:16 2004 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2003