00001
00024 #include "dom/dom_exception.h"
00025 #include "dom/dom_xml.h"
00026 #include "dom/dom2_range.h"
00027 #include "dom/dom2_events.h"
00028 #include "dom/dom2_views.h"
00029 #include "dom/dom2_traversal.h"
00030 #include "dom/html_document.h"
00031 #include "html/html_documentimpl.h"
00032
00033 #include "xml/dom_docimpl.h"
00034 #include "xml/dom_elementimpl.h"
00035
00036 #include <kdebug.h>
00037
00038 using namespace DOM;
00039
00040 DOMImplementation::DOMImplementation()
00041 {
00042 impl = 0;
00043 }
00044
00045 DOMImplementation::DOMImplementation(const DOMImplementation &other)
00046 {
00047 impl = other.impl;
00048 if (impl) impl->ref();
00049 }
00050
00051 DOMImplementation::DOMImplementation(DOMImplementationImpl *i)
00052 {
00053 impl = i;
00054 if (impl) impl->ref();
00055 }
00056
00057 DOMImplementation &DOMImplementation::operator = (const DOMImplementation &other)
00058 {
00059 if ( impl != other.impl ) {
00060 if (impl) impl->deref();
00061 impl = other.impl;
00062 if (impl) impl->ref();
00063 }
00064 return *this;
00065 }
00066
00067 DOMImplementation::~DOMImplementation()
00068 {
00069 if (impl) impl->deref();
00070 }
00071
00072 bool DOMImplementation::hasFeature( const DOMString &feature, const DOMString &version )
00073 {
00074 if (!impl)
00075 return false;
00076
00077 return impl->hasFeature(feature,version);
00078 }
00079
00080 DocumentType DOMImplementation::createDocumentType ( const DOMString &qualifiedName,
00081 const DOMString &publicId,
00082 const DOMString &systemId )
00083 {
00084 if (!impl)
00085 throw DOMException(DOMException::NOT_FOUND_ERR);
00086
00087 int exceptioncode = 0;
00088 DocumentTypeImpl *r = impl->createDocumentType(qualifiedName, publicId, systemId, exceptioncode);
00089 if ( exceptioncode )
00090 throw DOMException( exceptioncode );
00091 return r;
00092 }
00093
00094 Document DOMImplementation::createDocument ( const DOMString &namespaceURI,
00095 const DOMString &qualifiedName,
00096 const DocumentType &doctype )
00097 {
00098 if (!impl)
00099 throw DOMException(DOMException::NOT_FOUND_ERR);
00100
00101 int exceptioncode = 0;
00102 DocumentImpl *r = impl->createDocument(namespaceURI, qualifiedName, doctype, exceptioncode );
00103 if ( exceptioncode )
00104 throw DOMException( exceptioncode );
00105 return r;
00106 }
00107
00108 HTMLDocument DOMImplementation::createHTMLDocument( const DOMString& title )
00109 {
00110 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
00111
00112 HTMLDocumentImpl* r = impl->createHTMLDocument( 0 );
00113
00114 r->open();
00115
00116 r->write(QString::fromLatin1("<HTML><HEAD><TITLE>") + title.string() +
00117 QString::fromLatin1("</TITLE></HEAD>"));
00118
00119 return r;
00120 }
00121
00122 DOMImplementation DOMImplementation::getInterface(const DOMString &feature) const
00123 {
00124 if (!impl)
00125 throw DOMException(DOMException::NOT_FOUND_ERR);
00126
00127 return impl->getInterface(feature);
00128 }
00129
00130 CSSStyleSheet DOMImplementation::createCSSStyleSheet(const DOMString &title, const DOMString &media)
00131 {
00132 if (!impl)
00133 throw DOMException(DOMException::NOT_FOUND_ERR);
00134
00135 int exceptioncode = 0;
00136 CSSStyleSheetImpl *r = impl->createCSSStyleSheet(title.implementation(), media.implementation(),
00137 exceptioncode);
00138 if ( exceptioncode )
00139 throw DOMException( exceptioncode );
00140 return r;
00141 }
00142
00143 DOMImplementationImpl *DOMImplementation::handle() const
00144 {
00145 return impl;
00146 }
00147
00148 bool DOMImplementation::isNull() const
00149 {
00150 return (impl == 0);
00151 }
00152
00153
00154
00155 Document::Document()
00156 : Node()
00157 {
00158
00159 impl = DOMImplementationImpl::instance()->createDocument();
00160 impl->ref();
00161 }
00162
00163 Document::Document(bool create)
00164 : Node()
00165 {
00166 if(create)
00167 {
00168 impl = DOMImplementationImpl::instance()->createDocument();
00169 impl->ref();
00170 }
00171 else
00172 impl = 0;
00173
00174 }
00175
00176 Document::Document(const Document &other) : Node(other)
00177 {
00178
00179 }
00180
00181 Document::Document(DocumentImpl *i) : Node(i)
00182 {
00183
00184 }
00185
00186 Document &Document::operator = (const Node &other)
00187 {
00188 NodeImpl* ohandle = other.handle();
00189 if ( impl != ohandle ) {
00190 if (!ohandle || ohandle->nodeType() != DOCUMENT_NODE) {
00191 if ( impl ) impl->deref();
00192 impl = 0;
00193 } else {
00194 Node::operator =(other);
00195 }
00196 }
00197 return *this;
00198 }
00199
00200 Document &Document::operator = (const Document &other)
00201 {
00202 Node::operator =(other);
00203 return *this;
00204 }
00205
00206 Document::~Document()
00207 {
00208
00209 }
00210
00211 DocumentType Document::doctype() const
00212 {
00213 if (impl) return ((DocumentImpl *)impl)->doctype();
00214 return 0;
00215 }
00216
00217 DOMImplementation Document::implementation() const
00218 {
00219 if (impl) return ((DocumentImpl *)impl)->implementation();
00220 return 0;
00221 }
00222
00223 Element Document::documentElement() const
00224 {
00225 if (impl) return ((DocumentImpl *)impl)->documentElement();
00226 return 0;
00227 }
00228
00229 Element Document::createElement( const DOMString &tagName )
00230 {
00231 if (!impl)
00232 return 0;
00233 int exceptioncode = 0;
00234 Element el = ((DocumentImpl *)impl)->createElement(tagName, &exceptioncode);
00235 if (exceptioncode)
00236 throw DOMException(exceptioncode);
00237 return el;
00238 }
00239
00240 Element Document::createElementNS( const DOMString &namespaceURI, const DOMString &qualifiedName )
00241 {
00242 if (!impl)
00243 return 0;
00244 int exceptioncode = 0;
00245 Element el = ((DocumentImpl *)impl)->createElementNS(namespaceURI, qualifiedName, &exceptioncode);
00246 if (exceptioncode)
00247 throw DOMException(exceptioncode);
00248 return el;
00249 }
00250
00251 DocumentFragment Document::createDocumentFragment( )
00252 {
00253 if (impl) return ((DocumentImpl *)impl)->createDocumentFragment();
00254 return 0;
00255 }
00256
00257 Text Document::createTextNode( const DOMString &data )
00258 {
00259 if (impl) return ((DocumentImpl *)impl)->createTextNode( data.implementation() );
00260 return 0;
00261 }
00262
00263 Comment Document::createComment( const DOMString &data )
00264 {
00265 if (impl) return ((DocumentImpl *)impl)->createComment( data.implementation() );
00266 return 0;
00267 }
00268
00269 CDATASection Document::createCDATASection( const DOMString &data )
00270 {
00271
00272 if (impl) return ((DocumentImpl *)impl)->createCDATASection( data.implementation() );
00273 return 0;
00274 }
00275
00276 ProcessingInstruction Document::createProcessingInstruction( const DOMString &target, const DOMString &data )
00277 {
00278 if (impl) return ((DocumentImpl *)impl)->createProcessingInstruction( target, data.implementation() );
00279 return 0;
00280 }
00281
00282 Attr Document::createAttribute( const DOMString &name )
00283 {
00284 return createAttributeNS(DOMString(), name);
00285 }
00286
00287 Attr Document::createAttributeNS( const DOMString &namespaceURI, const DOMString &qualifiedName )
00288 {
00289 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
00290 if (qualifiedName.isNull()) throw DOMException(DOMException::NAMESPACE_ERR);
00291
00292 DOMString localName(qualifiedName.copy());
00293 DOMString prefix;
00294 int colonpos;
00295 if ((colonpos = qualifiedName.find(':')) >= 0) {
00296 prefix = qualifiedName.copy();
00297 prefix.truncate(colonpos);
00298 localName.remove(0, colonpos+1);
00299 }
00300
00301 int exceptioncode = 0;
00302 NodeImpl::Id id = static_cast<DocumentImpl*>(impl)->attrId(namespaceURI.implementation(), localName.implementation(), false , &exceptioncode);
00303 if (exceptioncode)
00304 throw DOMException(exceptioncode);
00305
00306 Attr r = static_cast<DocumentImpl*>(impl)->createAttribute(id);
00307 if (r.handle() && prefix.implementation())
00308 r.handle()->setPrefix(prefix.implementation(), exceptioncode);
00309 if (exceptioncode)
00310 throw DOMException(exceptioncode);
00311 return r;
00312 }
00313
00314 EntityReference Document::createEntityReference( const DOMString &name )
00315 {
00316 if (impl) return ((DocumentImpl *)impl)->createEntityReference( name );
00317 return 0;
00318 }
00319
00320 Element Document::getElementById( const DOMString &elementId ) const
00321 {
00322 if(impl) return ((DocumentImpl *)impl)->getElementById( elementId );
00323 return 0;
00324 }
00325
00326 NodeList Document::getElementsByTagName( const DOMString &tagName )
00327 {
00328 if (!impl) return 0;
00329 return static_cast<DocumentImpl*>(impl)->
00330 getElementsByTagNameNS(0, tagName.implementation());
00331 }
00332
00333 NodeList Document::getElementsByTagNameNS( const DOMString &namespaceURI, const DOMString &localName )
00334 {
00335 if (!impl) return 0;
00336 return static_cast<DocumentImpl*>(impl)->
00337 getElementsByTagNameNS(namespaceURI.implementation(), localName.implementation());
00338 }
00339
00340 Node Document::importNode( const Node & importedNode, bool deep )
00341 {
00342 if (!impl)
00343 throw DOMException(DOMException::INVALID_STATE_ERR);
00344
00345 int exceptioncode = 0;
00346 NodeImpl *r = static_cast<DocumentImpl*>(impl)->importNode(importedNode.handle(), deep, exceptioncode);
00347 if (exceptioncode)
00348 throw DOMException(exceptioncode);
00349 return r;
00350 }
00351
00352 bool Document::isHTMLDocument() const
00353 {
00354 if (impl) return ((DocumentImpl *)impl)->isHTMLDocument();
00355 return 0;
00356 }
00357
00358 Range Document::createRange()
00359 {
00360 if (impl) return ((DocumentImpl *)impl)->createRange();
00361 return 0;
00362 }
00363
00364 NodeIterator Document::createNodeIterator(Node root, unsigned long whatToShow,
00365 NodeFilter filter, bool entityReferenceExpansion)
00366 {
00367 if (!impl)
00368 throw DOMException(DOMException::INVALID_STATE_ERR);
00369
00370 int exceptioncode = 0;
00371 NodeIteratorImpl *r = static_cast<DocumentImpl*>(impl)->createNodeIterator(root.handle(),
00372 whatToShow,filter,entityReferenceExpansion,exceptioncode);
00373 if (exceptioncode)
00374 throw DOMException(exceptioncode);
00375 return r;
00376 }
00377
00378 TreeWalker Document::createTreeWalker(Node root, unsigned long whatToShow, NodeFilter filter,
00379 bool entityReferenceExpansion)
00380 {
00381 if (impl) return ((DocumentImpl *)impl)->createTreeWalker(root,whatToShow,filter,entityReferenceExpansion);
00382 return 0;
00383 }
00384
00385 Event Document::createEvent(const DOMString &eventType)
00386 {
00387 if (!impl)
00388 throw DOMException(DOMException::INVALID_STATE_ERR);
00389
00390 int exceptioncode = 0;
00391 EventImpl *r = ((DocumentImpl *)impl)->createEvent(eventType,exceptioncode);
00392 if (exceptioncode)
00393 throw DOMException(exceptioncode);
00394 return r;
00395 }
00396
00397 AbstractView Document::defaultView() const
00398 {
00399 if (!impl)
00400 throw DOMException(DOMException::INVALID_STATE_ERR);
00401
00402 return ((DocumentImpl *)impl)->defaultView();
00403 }
00404
00405 StyleSheetList Document::styleSheets() const
00406 {
00407 if (!impl)
00408 throw DOMException(DOMException::INVALID_STATE_ERR);
00409
00410 return ((DocumentImpl *)impl)->styleSheets();
00411 }
00412
00413
00414 KHTMLView *Document::view() const
00415 {
00416 if (!impl) return 0;
00417
00418 return static_cast<DocumentImpl*>(impl)->view();
00419 }
00420
00421 DOMString Document::completeURL(const DOMString& url)
00422 {
00423 if ( !impl ) return url;
00424 return static_cast<DocumentImpl*>( impl )->completeURL( url.string() );
00425 }
00426
00427 CSSStyleDeclaration Document::getOverrideStyle(const Element &elt, const DOMString &pseudoElt)
00428 {
00429 if (!impl)
00430 throw DOMException(DOMException::INVALID_STATE_ERR);
00431
00432 int exceptioncode = 0;
00433 CSSStyleDeclarationImpl *r = ((DocumentImpl *)impl)->getOverrideStyle(static_cast<ElementImpl*>(elt.handle()),pseudoElt.implementation());
00434 if (exceptioncode)
00435 throw DOMException(exceptioncode);
00436 return r;
00437 }
00438
00439
00440
00441 DocumentFragment::DocumentFragment() : Node()
00442 {
00443 }
00444
00445 DocumentFragment::DocumentFragment(const DocumentFragment &other) : Node(other)
00446 {
00447 }
00448
00449 DocumentFragment &DocumentFragment::operator = (const Node &other)
00450 {
00451 NodeImpl* ohandle = other.handle();
00452 if ( impl != ohandle ) {
00453 if (!ohandle || ohandle->nodeType() != DOCUMENT_FRAGMENT_NODE) {
00454 if ( impl ) impl->deref();
00455 impl = 0;
00456 } else {
00457 Node::operator =(other);
00458 }
00459 }
00460 return *this;
00461 }
00462
00463 DocumentFragment &DocumentFragment::operator = (const DocumentFragment &other)
00464 {
00465 Node::operator =(other);
00466 return *this;
00467 }
00468
00469 DocumentFragment::~DocumentFragment()
00470 {
00471 }
00472
00473 DocumentFragment::DocumentFragment(DocumentFragmentImpl *i) : Node(i)
00474 {
00475 }
00476
00477
00478
00479 DocumentType::DocumentType()
00480 : Node()
00481 {
00482 }
00483
00484 DocumentType::DocumentType(const DocumentType &other)
00485 : Node(other)
00486 {
00487 }
00488
00489 DocumentType::DocumentType(DocumentTypeImpl *impl) : Node(impl)
00490 {
00491 }
00492
00493 DocumentType &DocumentType::operator = (const Node &other)
00494 {
00495 NodeImpl* ohandle = other.handle();
00496 if ( impl != ohandle ) {
00497 if (!ohandle || ohandle->nodeType() != DOCUMENT_TYPE_NODE) {
00498 if ( impl ) impl->deref();
00499 impl = 0;
00500 } else {
00501 Node::operator =(other);
00502 }
00503 }
00504 return *this;
00505 }
00506
00507 DocumentType &DocumentType::operator = (const DocumentType &other)
00508 {
00509 Node::operator =(other);
00510 return *this;
00511 }
00512
00513 DocumentType::~DocumentType()
00514 {
00515 }
00516
00517 DOMString DocumentType::name() const
00518 {
00519 if (!impl)
00520 return DOMString();
00521
00522 return static_cast<DocumentTypeImpl*>(impl)->name();
00523 }
00524
00525 NamedNodeMap DocumentType::entities() const
00526 {
00527 if (!impl)
00528 return 0;
00529
00530 return static_cast<DocumentTypeImpl*>(impl)->entities();
00531 }
00532
00533 NamedNodeMap DocumentType::notations() const
00534 {
00535 if (!impl)
00536 return 0;
00537
00538 return static_cast<DocumentTypeImpl*>(impl)->notations();
00539 }
00540
00541 DOMString DocumentType::publicId() const
00542 {
00543 if (!impl)
00544 throw DOMException(DOMException::NOT_FOUND_ERR);
00545
00546 return static_cast<DocumentTypeImpl*>(impl)->publicId();
00547 }
00548
00549 DOMString DocumentType::systemId() const
00550 {
00551 if (!impl)
00552 throw DOMException(DOMException::NOT_FOUND_ERR);
00553
00554 return static_cast<DocumentTypeImpl*>(impl)->systemId();
00555 }
00556
00557 DOMString DocumentType::internalSubset() const
00558 {
00559 if (!impl)
00560 throw DOMException(DOMException::NOT_FOUND_ERR);
00561
00562 return static_cast<DocumentTypeImpl*>(impl)->internalSubset();
00563 }
00564